Bladeren bron

Optimized Mesh rendering

Thomas Buck 9 jaren geleden
bovenliggende
commit
4ebef759ab
4 gewijzigde bestanden met toevoegingen van 38 en 36 verwijderingen
  1. 7
    0
      include/system/Shader.h
  2. 10
    18
      src/Mesh.cpp
  3. 8
    16
      src/RoomMesh.cpp
  4. 13
    2
      src/system/Shader.cpp

+ 7
- 0
include/system/Shader.h Bestand weergeven

85
                        gl::GLenum mode = gl::GL_TRIANGLES, ShaderTexture* target = nullptr,
85
                        gl::GLenum mode = gl::GL_TRIANGLES, ShaderTexture* target = nullptr,
86
                        Shader& shader = textureShader);
86
                        Shader& shader = textureShader);
87
 
87
 
88
+    static void drawGLBuffer(std::vector<glm::vec3>& vertices, std::vector<glm::vec2>& uvs,
89
+                             Shader& shader = textureShader);
90
+    static void drawGLOnly(std::vector<unsigned short>& indices, glm::mat4 MVP,
91
+                           unsigned int texture, TextureStorage store,
92
+                           gl::GLenum mode = gl::GL_TRIANGLES, ShaderTexture* target = nullptr,
93
+                           Shader& shader = textureShader);
94
+
88
     static void drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& colors,
95
     static void drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& colors,
89
                        glm::mat4 MVP, gl::GLenum mode = gl::GL_TRIANGLES,
96
                        glm::mat4 MVP, gl::GLenum mode = gl::GL_TRIANGLES,
90
                        ShaderTexture* target = nullptr, Shader& shader = colorShader);
97
                        ShaderTexture* target = nullptr, Shader& shader = colorShader);

+ 10
- 18
src/Mesh.cpp Bestand weergeven

125
 
125
 
126
 void Mesh::display(glm::mat4 MVP, ShaderTexture* shaderTexture) {
126
 void Mesh::display(glm::mat4 MVP, ShaderTexture* shaderTexture) {
127
     if (indicesBuff.size() > 0) {
127
     if (indicesBuff.size() > 0) {
128
-        unsigned int indexStart = 0;
129
-        unsigned int indexPos = 1;
130
-        unsigned int texture = texturesBuff.at(indicesBuff.at(0));
131
-
132
-        while ((indexStart != indexPos) && (indexPos < indicesBuff.size())) {
133
-            while ((indexPos < indicesBuff.size())
134
-                   && (texturesBuff.at(indicesBuff.at(indexPos)) == texture)) {
135
-                indexPos++;
128
+        Shader::drawGLBuffer(verticesBuff, uvsBuff);
129
+
130
+        for (int i = 0; i < TextureManager::numTextures(TextureStorage::GAME); i++) {
131
+            std::vector<unsigned short> indices;
132
+            for (int n = 0; n < indicesBuff.size(); n++) {
133
+                if (texturesBuff.at(indicesBuff.at(n)) == i) {
134
+                    indices.push_back(indicesBuff.at(n));
135
+                }
136
             }
136
             }
137
 
137
 
138
-            std::vector<unsigned short> indices(indicesBuff.begin() + indexStart,
139
-                                            indicesBuff.begin() + indexPos);
140
-            Shader::drawGL(verticesBuff, uvsBuff, indices, MVP, texture,
141
-                           TextureStorage::GAME, gl::GL_TRIANGLES, shaderTexture);
142
-
143
-            if (indexPos < indicesBuff.size()) {
144
-                indexStart = indexPos;
145
-                indexPos += 1;
146
-                texture = texturesBuff.at(indicesBuff.at(indexStart));
147
-            }
138
+            Shader::drawGLOnly(indices, MVP, i, TextureStorage::GAME,
139
+                               gl::GL_TRIANGLES, shaderTexture);
148
         }
140
         }
149
     }
141
     }
150
 
142
 

+ 8
- 16
src/RoomMesh.cpp Bestand weergeven

70
 
70
 
71
 void RoomMesh::display(glm::mat4 MVP) {
71
 void RoomMesh::display(glm::mat4 MVP) {
72
     if (indicesBuff.size() > 0) {
72
     if (indicesBuff.size() > 0) {
73
-        unsigned int indexStart = 0;
74
-        unsigned int indexPos = 1;
75
-        unsigned int texture = texturesBuff.at(indicesBuff.at(0));
73
+        Shader::drawGLBuffer(verticesBuff, uvsBuff);
76
 
74
 
77
-        while ((indexStart != indexPos) && (indexPos < indicesBuff.size())) {
78
-            while ((indexPos < indicesBuff.size())
79
-                   && (texturesBuff.at(indicesBuff.at(indexPos)) == texture)) {
80
-                indexPos++;
75
+        for (int i = 0; i < TextureManager::numTextures(TextureStorage::GAME); i++) {
76
+            std::vector<unsigned short> indices;
77
+            for (int n = 0; n < indicesBuff.size(); n++) {
78
+                if (texturesBuff.at(indicesBuff.at(n)) == i) {
79
+                    indices.push_back(indicesBuff.at(n));
80
+                }
81
             }
81
             }
82
 
82
 
83
-            std::vector<unsigned short> indices(indicesBuff.begin() + indexStart,
84
-                                            indicesBuff.begin() + indexPos);
85
-            Shader::drawGL(verticesBuff, uvsBuff, indices, MVP, texture, TextureStorage::GAME);
86
-
87
-            if (indexPos < indicesBuff.size()) {
88
-                indexStart = indexPos;
89
-                indexPos += 1;
90
-                texture = texturesBuff.at(indicesBuff.at(indexStart));
91
-            }
83
+            Shader::drawGLOnly(indices, MVP, i, TextureStorage::GAME);
92
         }
84
         }
93
     }
85
     }
94
 }
86
 }

+ 13
- 2
src/system/Shader.cpp Bestand weergeven

324
                     std::vector<unsigned short>& indices, glm::mat4 MVP,
324
                     std::vector<unsigned short>& indices, glm::mat4 MVP,
325
                     unsigned int texture, TextureStorage store,
325
                     unsigned int texture, TextureStorage store,
326
                     gl::GLenum mode, ShaderTexture* target, Shader& shader) {
326
                     gl::GLenum mode, ShaderTexture* target, Shader& shader) {
327
+    drawGLBuffer(vertices, uvs);
328
+    drawGLOnly(indices, MVP, texture, store, mode, target, shader);
329
+}
330
+
331
+void Shader::drawGLBuffer(std::vector<glm::vec3>& vertices, std::vector<glm::vec2>& uvs, Shader& shader) {
327
     orAssert(vertices.size() == uvs.size());
332
     orAssert(vertices.size() == uvs.size());
333
+
334
+    shader.vertexBuffer.bufferData(vertices);
335
+    shader.otherBuffer.bufferData(uvs);
336
+}
337
+
338
+void Shader::drawGLOnly(std::vector<unsigned short>& indices, glm::mat4 MVP,
339
+                        unsigned int texture, TextureStorage store,
340
+                        gl::GLenum mode, ShaderTexture* target, Shader& shader) {
328
     if (mode == gl::GL_TRIANGLES)
341
     if (mode == gl::GL_TRIANGLES)
329
         orAssert((indices.size() % 3) == 0);
342
         orAssert((indices.size() % 3) == 0);
330
     bindProperBuffer(target);
343
     bindProperBuffer(target);
333
     shader.loadUniform(0, MVP);
346
     shader.loadUniform(0, MVP);
334
     shader.loadUniform(1, texture, store);
347
     shader.loadUniform(1, texture, store);
335
 
348
 
336
-    shader.vertexBuffer.bufferData(vertices);
337
-    shader.otherBuffer.bufferData(uvs);
338
     shader.indexBuffer.bufferData(indices);
349
     shader.indexBuffer.bufferData(indices);
339
 
350
 
340
     shader.vertexBuffer.bindBuffer(0, 3);
351
     shader.vertexBuffer.bindBuffer(0, 3);

Laden…
Annuleren
Opslaan