Browse Source

Optimized Mesh rendering

Thomas Buck 9 years ago
parent
commit
4ebef759ab
4 changed files with 38 additions and 36 deletions
  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 View File

@@ -85,6 +85,13 @@ class Shader {
85 85
                        gl::GLenum mode = gl::GL_TRIANGLES, ShaderTexture* target = nullptr,
86 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 95
     static void drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& colors,
89 96
                        glm::mat4 MVP, gl::GLenum mode = gl::GL_TRIANGLES,
90 97
                        ShaderTexture* target = nullptr, Shader& shader = colorShader);

+ 10
- 18
src/Mesh.cpp View File

@@ -125,26 +125,18 @@ void Mesh::prepare() {
125 125
 
126 126
 void Mesh::display(glm::mat4 MVP, ShaderTexture* shaderTexture) {
127 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 View File

@@ -70,25 +70,17 @@ void RoomMesh::prepare() {
70 70
 
71 71
 void RoomMesh::display(glm::mat4 MVP) {
72 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 View File

@@ -324,7 +324,20 @@ void Shader::drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec2>& uv
324 324
                     std::vector<unsigned short>& indices, glm::mat4 MVP,
325 325
                     unsigned int texture, TextureStorage store,
326 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 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 341
     if (mode == gl::GL_TRIANGLES)
329 342
         orAssert((indices.size() % 3) == 0);
330 343
     bindProperBuffer(target);
@@ -333,8 +346,6 @@ void Shader::drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec2>& uv
333 346
     shader.loadUniform(0, MVP);
334 347
     shader.loadUniform(1, texture, store);
335 348
 
336
-    shader.vertexBuffer.bufferData(vertices);
337
-    shader.otherBuffer.bufferData(uvs);
338 349
     shader.indexBuffer.bufferData(indices);
339 350
 
340 351
     shader.vertexBuffer.bindBuffer(0, 3);

Loading…
Cancel
Save