Browse Source

Now always copying GL data

Thomas Buck 9 years ago
parent
commit
5c9a47e4cd
12 changed files with 201 additions and 335 deletions
  1. 3
    4
      include/Mesh.h
  2. 0
    3
      include/Render.h
  3. 1
    2
      include/RoomMesh.h
  4. 3
    2
      include/Sprite.h
  5. 15
    16
      include/system/Shader.h
  6. 42
    54
      src/Camera.cpp
  7. 27
    32
      src/Mesh.cpp
  8. 9
    31
      src/Render.cpp
  9. 2
    10
      src/RoomData.cpp
  10. 6
    8
      src/RoomMesh.cpp
  11. 16
    21
      src/Sprite.cpp
  12. 77
    152
      src/system/Shader.cpp

+ 3
- 4
include/Mesh.h View File

@@ -45,14 +45,13 @@ class Mesh {
45 45
   private:
46 46
     std::vector<unsigned short> indicesBuff;
47 47
     std::vector<glm::vec3> verticesBuff;
48
+    std::vector<glm::vec2> uvsBuff;
48 49
     std::vector<unsigned int> texturesBuff;
49 50
 
50 51
     std::vector<unsigned short> indicesColorBuff;
51 52
     std::vector<glm::vec3> verticesColorBuff;
52
-    std::vector<unsigned int> colorsBuff;
53
-
54
-    ShaderBuffer indices, vertices, uvs;
55
-    ShaderBuffer indicesColor, verticesColor, colors;
53
+    std::vector<glm::vec3> colorsBuff;
54
+    std::vector<unsigned int> colorsIndexBuff;
56 55
 };
57 56
 
58 57
 #endif

+ 0
- 3
include/Render.h View File

@@ -33,9 +33,6 @@ class Render {
33 33
 
34 34
     static void screenShot(const char* filenameBase);
35 35
 
36
-    static void drawTexture(float x, float y, float w, float h, glm::vec4 color,
37
-                            unsigned int texture, TextureStorage s);
38
-
39 36
     static RenderMode getMode() { return mode; }
40 37
     static void setMode(RenderMode m) { mode = m; }
41 38
 

+ 1
- 2
include/RoomMesh.h View File

@@ -11,7 +11,6 @@
11 11
 #include <vector>
12 12
 
13 13
 #include "Mesh.h"
14
-#include "system/Shader.h"
15 14
 
16 15
 struct RoomVertexTR2 {
17 16
     int x, y, z; // Vertex coordinates, relative to x/zOffset
@@ -36,8 +35,8 @@ class RoomMesh {
36 35
   private:
37 36
     std::vector<unsigned short> indicesBuff;
38 37
     std::vector<glm::vec3> verticesBuff;
38
+    std::vector<glm::vec2> uvsBuff;
39 39
     std::vector<unsigned int> texturesBuff;
40
-    ShaderBuffer indices, vertices, uvs;
41 40
 };
42 41
 
43 42
 #endif

+ 3
- 2
include/Sprite.h View File

@@ -8,7 +8,7 @@
8 8
 #ifndef _SPRITE_H_
9 9
 #define _SPRITE_H_
10 10
 
11
-#include "system/Shader.h"
11
+#include <vector>
12 12
 
13 13
 class Sprite {
14 14
   public:
@@ -20,7 +20,8 @@ class Sprite {
20 20
 
21 21
   private:
22 22
     int texture;
23
-    ShaderBuffer vertices, uvs;
23
+    std::vector<glm::vec3> vertexBuff;
24
+    std::vector<glm::vec2> uvBuff;
24 25
     glm::vec4 uv2D;
25 26
 };
26 27
 

+ 15
- 16
include/system/Shader.h View File

@@ -75,34 +75,32 @@ class Shader {
75 75
 
76 76
     static void set2DState(bool on, bool depth = true);
77 77
 
78
-    static void drawGL(ShaderBuffer& vertices, ShaderBuffer& uvs, glm::vec4 color, unsigned int texture,
79
-                       TextureStorage store = TextureStorage::SYSTEM, gl::GLenum mode = gl::GL_TRIANGLES,
80
-                       ShaderTexture* target = nullptr, Shader& shader = textShader);
81
-
82
-    static void drawGL(ShaderBuffer& vertices, ShaderBuffer& uvs, unsigned int texture, glm::mat4 MVP,
83
-                       TextureStorage store = TextureStorage::GAME, ShaderTexture* target = nullptr,
78
+    static void drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec2>& uvs,
79
+                       glm::mat4 MVP, unsigned int texture, TextureStorage store,
80
+                       gl::GLenum mode = gl::GL_TRIANGLES, ShaderTexture* target = nullptr,
84 81
                        Shader& shader = textureShader);
85
-    static void drawGL(ShaderBuffer& vertices, ShaderBuffer& uvs, ShaderBuffer& indices,
86
-                       unsigned int texture, glm::mat4 MVP,
87
-                       TextureStorage store = TextureStorage::GAME, ShaderTexture* target = nullptr,
82
+    static void drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec2>& uvs,
83
+                       std::vector<unsigned short>& indices, glm::mat4 MVP,
84
+                       unsigned int texture, TextureStorage store,
85
+                       gl::GLenum mode = gl::GL_TRIANGLES, ShaderTexture* target = nullptr,
88 86
                        Shader& shader = textureShader);
89 87
 
90
-    static void drawGL(ShaderBuffer& vertices, ShaderBuffer& colors, glm::mat4 MVP,
88
+    static void drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& colors,
89
+                       glm::mat4 MVP, gl::GLenum mode = gl::GL_TRIANGLES,
90
+                       ShaderTexture* target = nullptr, Shader& shader = colorShader);
91
+    static void drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& colors,
92
+                       std::vector<unsigned short>& indices, glm::mat4 MVP,
91 93
                        gl::GLenum mode = gl::GL_TRIANGLES, ShaderTexture* target = nullptr,
92 94
                        Shader& shader = colorShader);
93
-    static void drawGL(ShaderBuffer& vertices, ShaderBuffer& colors, ShaderBuffer& indices,
94
-                       glm::mat4 MVP, gl::GLenum mode = gl::GL_TRIANGLES, ShaderTexture* target = nullptr,
95
-                       Shader& shader = colorShader);
96 95
 
97 96
     static std::string getVersion(bool linked);
98 97
 
99 98
   private:
100 99
     int programID;
101 100
     std::vector<unsigned int> uniforms;
101
+    ShaderBuffer vertexBuffer, otherBuffer, indexBuffer;
102 102
 
103
-    static Shader textShader;
104
-    static const char* textShaderVertex;
105
-    static const char* textShaderFragment;
103
+    static void bindProperBuffer(ShaderTexture* target);
106 104
 
107 105
     static Shader textureShader;
108 106
     static const char* textureShaderVertex;
@@ -113,6 +111,7 @@ class Shader {
113 111
     static const char* colorShaderFragment;
114 112
 
115 113
     static unsigned int vertexArrayID;
114
+    static bool lastBufferWasNotFramebuffer;
116 115
 };
117 116
 
118 117
 #endif

+ 42
- 54
src/Camera.cpp View File

@@ -271,11 +271,11 @@ static glm::vec3 frustumColors[6] = {
271 271
 };
272 272
 static glm::vec3 frustumVertices[8];
273 273
 
274
-static ShaderBuffer vertexBuffer;
275
-static ShaderBuffer colorBuffer;
276
-static ShaderBuffer indexBuffer;
277
-static ShaderBuffer vertexPointBuffer;
278
-static ShaderBuffer colorPointBuffer;
274
+static std::vector<glm::vec3> vertexBuffer;
275
+static std::vector<glm::vec3> colorBuffer;
276
+static std::vector<unsigned short> indexBuffer;
277
+static std::vector<glm::vec3> vertexPointBuffer;
278
+static std::vector<glm::vec3> colorPointBuffer;
279 279
 
280 280
 void Camera::calculateFrustumPlanes() {
281 281
     glm::mat4 combo = projection * view;
@@ -304,76 +304,64 @@ void Camera::calculateFrustumPlanes() {
304 304
     planes[NEAR].set(frustumVertices[NTL], frustumVertices[NTR], frustumVertices[NBR]);
305 305
     planes[FAR].set(frustumVertices[FTR], frustumVertices[FTL], frustumVertices[FBL]);
306 306
 
307
-    std::vector<glm::vec3> verts;
308
-
309 307
     // Near
310
-    verts.push_back(frustumVertices[NTL]);
311
-    verts.push_back(frustumVertices[NTR]);
312
-    verts.push_back(frustumVertices[NBR]);
313
-    verts.push_back(frustumVertices[NBL]);
308
+    vertexBuffer.push_back(frustumVertices[NTL]);
309
+    vertexBuffer.push_back(frustumVertices[NTR]);
310
+    vertexBuffer.push_back(frustumVertices[NBR]);
311
+    vertexBuffer.push_back(frustumVertices[NBL]);
314 312
 
315 313
     // Far
316
-    verts.push_back(frustumVertices[FTR]);
317
-    verts.push_back(frustumVertices[FTL]);
318
-    verts.push_back(frustumVertices[FBL]);
319
-    verts.push_back(frustumVertices[FBR]);
314
+    vertexBuffer.push_back(frustumVertices[FTR]);
315
+    vertexBuffer.push_back(frustumVertices[FTL]);
316
+    vertexBuffer.push_back(frustumVertices[FBL]);
317
+    vertexBuffer.push_back(frustumVertices[FBR]);
320 318
 
321 319
     // Top
322
-    verts.push_back(frustumVertices[NTR]);
323
-    verts.push_back(frustumVertices[NTL]);
324
-    verts.push_back(frustumVertices[FTL]);
325
-    verts.push_back(frustumVertices[FTR]);
320
+    vertexBuffer.push_back(frustumVertices[NTR]);
321
+    vertexBuffer.push_back(frustumVertices[NTL]);
322
+    vertexBuffer.push_back(frustumVertices[FTL]);
323
+    vertexBuffer.push_back(frustumVertices[FTR]);
326 324
 
327 325
     // Bottom
328
-    verts.push_back(frustumVertices[NBL]);
329
-    verts.push_back(frustumVertices[NBR]);
330
-    verts.push_back(frustumVertices[FBR]);
331
-    verts.push_back(frustumVertices[FBL]);
326
+    vertexBuffer.push_back(frustumVertices[NBL]);
327
+    vertexBuffer.push_back(frustumVertices[NBR]);
328
+    vertexBuffer.push_back(frustumVertices[FBR]);
329
+    vertexBuffer.push_back(frustumVertices[FBL]);
332 330
 
333 331
     // Left
334
-    verts.push_back(frustumVertices[NTL]);
335
-    verts.push_back(frustumVertices[NBL]);
336
-    verts.push_back(frustumVertices[FBL]);
337
-    verts.push_back(frustumVertices[FTL]);
332
+    vertexBuffer.push_back(frustumVertices[NTL]);
333
+    vertexBuffer.push_back(frustumVertices[NBL]);
334
+    vertexBuffer.push_back(frustumVertices[FBL]);
335
+    vertexBuffer.push_back(frustumVertices[FTL]);
338 336
 
339 337
     // Right
340
-    verts.push_back(frustumVertices[NBR]);
341
-    verts.push_back(frustumVertices[NTR]);
342
-    verts.push_back(frustumVertices[FTR]);
343
-    verts.push_back(frustumVertices[FBR]);
344
-
345
-    vertexBuffer.bufferData(verts);
346
-
347
-    verts.clear();
348
-    std::vector<glm::vec3> cols;
349
-
350
-    verts.push_back(getPosition());
351
-    cols.push_back(glm::vec3(1.0f, 1.0f, 1.0f));
338
+    vertexBuffer.push_back(frustumVertices[NBR]);
339
+    vertexBuffer.push_back(frustumVertices[NTR]);
340
+    vertexBuffer.push_back(frustumVertices[FTR]);
341
+    vertexBuffer.push_back(frustumVertices[FBR]);
352 342
 
353
-    vertexPointBuffer.bufferData(verts);
354
-    colorPointBuffer.bufferData(cols);
343
+    // Position indicator
344
+    vertexPointBuffer.push_back(getPosition());
345
+    colorPointBuffer.push_back(glm::vec3(1.0f, 1.0f, 1.0f));
355 346
 
356
-    if (colorBuffer.getSize() == 0) {
357
-        cols.clear();
347
+    // Lazy initialization of frustum plane color buffer
348
+    if (colorBuffer.size() == 0) {
358 349
         for (int i = 0; i < 6; i++) {
359 350
             for (int j = 0; j < 4; j++) {
360
-                cols.push_back(frustumColors[i]);
351
+                colorBuffer.push_back(frustumColors[i]);
361 352
             }
362 353
         }
363
-        colorBuffer.bufferData(cols);
364 354
     }
365 355
 
366
-    if (indexBuffer.getSize() == 0) {
367
-        std::vector<unsigned short> inds;
356
+    if (indexBuffer.size() == 0) {
368 357
         for (int i = 0; i < 6; i++) {
369
-            inds.push_back(4 * i);
370
-            inds.push_back((4 * i) + 1);
371
-            inds.push_back((4 * i) + 2);
372
-            inds.push_back((4 * i) + 3);
373
-            inds.push_back((4 * i) + 2);
374
-            inds.push_back(4 * i);
358
+            indexBuffer.push_back(4 * i);
359
+            indexBuffer.push_back((4 * i) + 1);
360
+            indexBuffer.push_back((4 * i) + 2);
361
+            indexBuffer.push_back((4 * i) + 3);
362
+            indexBuffer.push_back((4 * i) + 2);
363
+            indexBuffer.push_back(4 * i);
375 364
         }
376
-        indexBuffer.bufferData(inds);
377 365
     }
378 366
 }
379 367
 

+ 27
- 32
src/Mesh.cpp View File

@@ -18,36 +18,36 @@ Mesh::Mesh(const std::vector<glm::vec3>& vert,
18 18
            const std::vector<IndexedColoredRectangle>& coloredTri) {
19 19
     for (auto& t : rect) {
20 20
         indicesBuff.push_back(0);
21
-        verticesBuff.push_back(glm::vec3(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z));
22
-        verticesBuff.push_back(glm::vec3(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z));
23
-        verticesBuff.push_back(glm::vec3(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z));
24
-        verticesBuff.push_back(glm::vec3(vert.at(t.v4).x, vert.at(t.v4).y, vert.at(t.v4).z));
21
+        verticesBuff.emplace_back(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z);
22
+        verticesBuff.emplace_back(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z);
23
+        verticesBuff.emplace_back(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z);
24
+        verticesBuff.emplace_back(vert.at(t.v4).x, vert.at(t.v4).y, vert.at(t.v4).z);
25 25
         texturesBuff.push_back(t.texture);
26 26
     }
27 27
 
28 28
     for (auto& t : tri) {
29 29
         indicesBuff.push_back(1);
30
-        verticesBuff.push_back(glm::vec3(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z));
31
-        verticesBuff.push_back(glm::vec3(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z));
32
-        verticesBuff.push_back(glm::vec3(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z));
30
+        verticesBuff.emplace_back(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z);
31
+        verticesBuff.emplace_back(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z);
32
+        verticesBuff.emplace_back(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z);
33 33
         texturesBuff.push_back(t.texture);
34 34
     }
35 35
 
36 36
     for (auto& t : coloredRect) {
37 37
         indicesColorBuff.push_back(0);
38
-        verticesColorBuff.push_back(glm::vec3(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z));
39
-        verticesColorBuff.push_back(glm::vec3(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z));
40
-        verticesColorBuff.push_back(glm::vec3(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z));
41
-        verticesColorBuff.push_back(glm::vec3(vert.at(t.v4).x, vert.at(t.v4).y, vert.at(t.v4).z));
42
-        colorsBuff.push_back(t.index);
38
+        verticesColorBuff.emplace_back(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z);
39
+        verticesColorBuff.emplace_back(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z);
40
+        verticesColorBuff.emplace_back(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z);
41
+        verticesColorBuff.emplace_back(vert.at(t.v4).x, vert.at(t.v4).y, vert.at(t.v4).z);
42
+        colorsIndexBuff.push_back(t.index);
43 43
     }
44 44
 
45 45
     for (auto& t : coloredTri) {
46 46
         indicesColorBuff.push_back(1);
47
-        verticesColorBuff.push_back(glm::vec3(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z));
48
-        verticesColorBuff.push_back(glm::vec3(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z));
49
-        verticesColorBuff.push_back(glm::vec3(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z));
50
-        colorsBuff.push_back(t.index);
47
+        verticesColorBuff.emplace_back(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z);
48
+        verticesColorBuff.emplace_back(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z);
49
+        verticesColorBuff.emplace_back(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z);
50
+        colorsIndexBuff.push_back(t.index);
51 51
     }
52 52
 }
53 53
 
@@ -85,8 +85,8 @@ void Mesh::prepare() {
85 85
     orAssertEqual(vert.size(), uvBuff.size());
86 86
 
87 87
     indicesBuff = std::move(ind);
88
-    vertices.bufferData(vert);
89
-    uvs.bufferData(uvBuff);
88
+    verticesBuff = std::move(vert);
89
+    uvsBuff = std::move(uvBuff);
90 90
     texturesBuff = std::move(tex);
91 91
 
92 92
     std::vector<unsigned short> indCol;
@@ -103,7 +103,7 @@ void Mesh::prepare() {
103 103
                 v = 0;
104 104
             indCol.push_back(vertCol.size());
105 105
             vertCol.push_back(verticesColorBuff.at(vertIndex + v));
106
-            glm::vec4 c = TextureManager::getPalette(colorsBuff.at(i));
106
+            glm::vec4 c = TextureManager::getPalette(colorsIndexBuff.at(i));
107 107
             cols.push_back(glm::vec3(c.x, c.y, c.z));
108 108
         }
109 109
 
@@ -118,14 +118,9 @@ void Mesh::prepare() {
118 118
     orAssertEqual(indCol.size() % 3, 0);
119 119
     orAssertEqual(vertCol.size(), cols.size());
120 120
 
121
-    indicesColor.bufferData(indCol);
122
-    verticesColor.bufferData(vertCol);
123
-    colors.bufferData(cols);
124
-
125
-    verticesBuff.clear();
126
-    indicesColorBuff.clear();
127
-    verticesColorBuff.clear();
128
-    colorsBuff.clear();
121
+    indicesColorBuff = std::move(indCol);
122
+    verticesColorBuff = std::move(vertCol);
123
+    colorsBuff = std::move(cols);
129 124
 }
130 125
 
131 126
 void Mesh::display(glm::mat4 MVP, ShaderTexture* shaderTexture) {
@@ -140,10 +135,10 @@ void Mesh::display(glm::mat4 MVP, ShaderTexture* shaderTexture) {
140 135
                 indexPos++;
141 136
             }
142 137
 
143
-            std::vector<unsigned short> ind(indicesBuff.begin() + indexStart,
138
+            std::vector<unsigned short> indices(indicesBuff.begin() + indexStart,
144 139
                                             indicesBuff.begin() + indexPos);
145
-            indices.bufferData(ind);
146
-            Shader::drawGL(vertices, uvs, indices, texture, MVP, TextureStorage::GAME, shaderTexture);
140
+            Shader::drawGL(verticesBuff, uvsBuff, indices, MVP, texture,
141
+                           TextureStorage::GAME, gl::GL_TRIANGLES, shaderTexture);
147 142
 
148 143
             if (indexPos < indicesBuff.size()) {
149 144
                 indexStart = indexPos;
@@ -153,7 +148,7 @@ void Mesh::display(glm::mat4 MVP, ShaderTexture* shaderTexture) {
153 148
         }
154 149
     }
155 150
 
156
-    if (indicesColor.getSize() > 0)
157
-        Shader::drawGL(verticesColor, colors, indicesColor, MVP, gl::GL_TRIANGLES, shaderTexture);
151
+    if (indicesColorBuff.size() > 0)
152
+        Shader::drawGL(verticesColorBuff, colorsBuff, indicesColorBuff, MVP, gl::GL_TRIANGLES, shaderTexture);
158 153
 }
159 154
 

+ 9
- 31
src/Render.cpp View File

@@ -33,9 +33,15 @@ void Render::display() {
33 33
     gl::glClear(gl::GL_COLOR_BUFFER_BIT | gl::GL_DEPTH_BUFFER_BIT);
34 34
 
35 35
     if (mode == RenderMode::LoadScreen) {
36
-        glm::vec4 color(1.0f, 1.0f, 1.0f, 1.0f);
37
-        drawTexture(0.0f, 0.0f, Window::getSize().x, Window::getSize().y,
38
-                    color, TEXTURE_SPLASH, TextureStorage::SYSTEM);
36
+        ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f));
37
+        ImGui::SetNextWindowSize(ImVec2(Window::getSize().x, Window::getSize().y));
38
+        ImGui::Begin("SplashWindow", nullptr, ImGuiWindowFlags_NoTitleBar
39
+                     | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove
40
+                     | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse
41
+                     | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoSavedSettings);
42
+        auto bm = TextureManager::getBufferManager(TEXTURE_SPLASH, TextureStorage::SYSTEM);
43
+        ImGui::Image(bm, ImVec2(Window::getSize().x, Window::getSize().y));
44
+        ImGui::End();
39 45
         return;
40 46
     }
41 47
 
@@ -157,34 +163,6 @@ void Render::screenShot(const char* filenameBase) {
157 163
     delete [] buffer;
158 164
 }
159 165
 
160
-void Render::drawTexture(float x, float y, float w, float h, glm::vec4 color,
161
-                         unsigned int texture, TextureStorage s) {
162
-    std::vector<glm::vec2> vertices;
163
-    std::vector<glm::vec2> uvs;
164
-
165
-    vertices.push_back(glm::vec2(x, y + h));
166
-    vertices.push_back(glm::vec2(x + w, y + h));
167
-    vertices.push_back(glm::vec2(x, y));
168
-
169
-    vertices.push_back(glm::vec2(x + w, y));
170
-    vertices.push_back(glm::vec2(x, y));
171
-    vertices.push_back(glm::vec2(x + w, y + h));
172
-
173
-    uvs.push_back(glm::vec2(0.0f, 1.0f));
174
-    uvs.push_back(glm::vec2(1.0f, 1.0f));
175
-    uvs.push_back(glm::vec2(0.0f, 0.0f));
176
-
177
-    uvs.push_back(glm::vec2(1.0f, 0.0f));
178
-    uvs.push_back(glm::vec2(0.0f, 0.0f));
179
-    uvs.push_back(glm::vec2(1.0f, 1.0f));
180
-
181
-    static ShaderBuffer vert, uv;
182
-    vert.bufferData(vertices);
183
-    uv.bufferData(uvs);
184
-
185
-    Shader::drawGL(vert, uv, color, texture, s);
186
-}
187
-
188 166
 static const int modeStringCount = 4;
189 167
 static const char* modeStrings[modeStringCount] = {
190 168
     "Splash", "Texture", "Wireframe", "Solid"

+ 2
- 10
src/RoomData.cpp View File

@@ -41,11 +41,7 @@ void BoundingBox::display(glm::mat4 VP, glm::vec3 colorLine, glm::vec3 colorDot)
41 41
     inds.push_back(5);
42 42
     inds.push_back(2);
43 43
 
44
-    static ShaderBuffer vert, col, ind;
45
-    vert.bufferData(verts);
46
-    col.bufferData(cols);
47
-    ind.bufferData(inds);
48
-    Shader::drawGL(vert, col, ind, VP, gl::GL_LINE_STRIP);
44
+    Shader::drawGL(verts, cols, inds, VP, gl::GL_LINE_STRIP);
49 45
 
50 46
     cols.clear();
51 47
     inds.clear();
@@ -55,11 +51,7 @@ void BoundingBox::display(glm::mat4 VP, glm::vec3 colorLine, glm::vec3 colorDot)
55 51
         inds.push_back(i);
56 52
     }
57 53
 
58
-    static ShaderBuffer vert2, col2, ind2;
59
-    vert2.bufferData(verts);
60
-    col2.bufferData(cols);
61
-    ind2.bufferData(inds);
62
-    Shader::drawGL(vert2, col2, ind2, VP, gl::GL_POINTS);
54
+    Shader::drawGL(verts, cols, inds, VP, gl::GL_POINTS);
63 55
 }
64 56
 
65 57
 // ----------------------------------------------------------------------------

+ 6
- 8
src/RoomMesh.cpp View File

@@ -7,6 +7,7 @@
7 7
 
8 8
 #include "global.h"
9 9
 #include "TextureManager.h"
10
+#include "system/Shader.h"
10 11
 #include "RoomMesh.h"
11 12
 
12 13
 RoomMesh::RoomMesh(const std::vector<RoomVertexTR2>& vert,
@@ -33,7 +34,6 @@ RoomMesh::RoomMesh(const std::vector<RoomVertexTR2>& vert,
33 34
 void RoomMesh::prepare() {
34 35
     std::vector<unsigned short> ind;
35 36
     std::vector<glm::vec3> vert;
36
-    std::vector<glm::vec2> uvBuff;
37 37
     std::vector<unsigned int> tex;
38 38
 
39 39
     int vertIndex = 0;
@@ -47,7 +47,7 @@ void RoomMesh::prepare() {
47 47
                 v = 0;
48 48
             ind.push_back(vert.size());
49 49
             vert.push_back(verticesBuff.at(vertIndex + v));
50
-            uvBuff.push_back(TextureManager::getTile(texturesBuff.at(i)).getUV(v));
50
+            uvsBuff.push_back(TextureManager::getTile(texturesBuff.at(i)).getUV(v));
51 51
             tex.push_back(texture);
52 52
         }
53 53
 
@@ -61,11 +61,10 @@ void RoomMesh::prepare() {
61 61
 
62 62
     orAssertEqual(ind.size() % 3, 0);
63 63
     orAssertEqual(vert.size(), tex.size());
64
-    orAssertEqual(vert.size(), uvBuff.size());
64
+    orAssertEqual(vert.size(), uvsBuff.size());
65 65
 
66 66
     indicesBuff = std::move(ind);
67
-    vertices.bufferData(vert);
68
-    uvs.bufferData(uvBuff);
67
+    verticesBuff = std::move(vert);
69 68
     texturesBuff = std::move(tex);
70 69
 }
71 70
 
@@ -81,10 +80,9 @@ void RoomMesh::display(glm::mat4 MVP) {
81 80
                 indexPos++;
82 81
             }
83 82
 
84
-            std::vector<unsigned short> ind(indicesBuff.begin() + indexStart,
83
+            std::vector<unsigned short> indices(indicesBuff.begin() + indexStart,
85 84
                                             indicesBuff.begin() + indexPos);
86
-            indices.bufferData(ind);
87
-            Shader::drawGL(vertices, uvs, indices, texture, MVP);
85
+            Shader::drawGL(verticesBuff, uvsBuff, indices, MVP, texture, TextureStorage::GAME);
88 86
 
89 87
             if (indexPos < indicesBuff.size()) {
90 88
                 indexStart = indexPos;

+ 16
- 21
src/Sprite.cpp View File

@@ -23,30 +23,25 @@ Sprite::Sprite(int tile, int x, int y, int width, int height) : texture(tile) {
23 23
     int width2 = (int)(width * scale);
24 24
     int height2 = (int)(height * scale);
25 25
 
26
-    std::vector<glm::vec2> uv;
27
-    uv.emplace_back(float(x + texelOffset) / texelScale, float(y + height) / texelScale);
28
-    uv.emplace_back(float(x + texelOffset) / texelScale, float(y + texelOffset) / texelScale);
29
-    uv.emplace_back(float(x + width) / texelScale, float(y + texelOffset) / texelScale);
30
-    uv.emplace_back(float(x + width) / texelScale, float(y + height) / texelScale);
31
-    uv.emplace_back(uv.at(0));
32
-    uv.emplace_back(uv.at(2));
33
-
34
-    std::vector<glm::vec3> vert;
35
-    vert.emplace_back(float(-width2) / 2.0f, 0.0f, 0.0f);
36
-    vert.emplace_back(float(-width2) / 2.0f, float(-height2), 0.0f);
37
-    vert.emplace_back(float(width2) / 2.0f, float(-height2), 0.0f);
38
-    vert.emplace_back(float(width2) / 2.0f, 0.0f, 0.0f);
39
-    vert.emplace_back(vert.at(0));
40
-    vert.emplace_back(vert.at(2));
41
-
42
-    vertices.bufferData(vert);
43
-    uvs.bufferData(uv);
44
-
45
-    uv2D = glm::vec4(uv.at(0), uv.at(2));
26
+    uvBuff.emplace_back(float(x + texelOffset) / texelScale, float(y + height) / texelScale);
27
+    uvBuff.emplace_back(float(x + texelOffset) / texelScale, float(y + texelOffset) / texelScale);
28
+    uvBuff.emplace_back(float(x + width) / texelScale, float(y + texelOffset) / texelScale);
29
+    uvBuff.emplace_back(float(x + width) / texelScale, float(y + height) / texelScale);
30
+    uvBuff.emplace_back(uvBuff.at(0));
31
+    uvBuff.emplace_back(uvBuff.at(2));
32
+
33
+    vertexBuff.emplace_back(float(-width2) / 2.0f, 0.0f, 0.0f);
34
+    vertexBuff.emplace_back(float(-width2) / 2.0f, float(-height2), 0.0f);
35
+    vertexBuff.emplace_back(float(width2) / 2.0f, float(-height2), 0.0f);
36
+    vertexBuff.emplace_back(float(width2) / 2.0f, 0.0f, 0.0f);
37
+    vertexBuff.emplace_back(vertexBuff.at(0));
38
+    vertexBuff.emplace_back(vertexBuff.at(2));
39
+
40
+    uv2D = glm::vec4(uvBuff.at(0), uvBuff.at(2));
46 41
 }
47 42
 
48 43
 void Sprite::display(glm::mat4 MVP) {
49
-    Shader::drawGL(vertices, uvs, texture, MVP);
44
+    Shader::drawGL(vertexBuff, uvBuff, MVP, texture, TextureStorage::GAME);
50 45
 }
51 46
 
52 47
 // ----------------------------------------------------------------------------

+ 77
- 152
src/system/Shader.cpp View File

@@ -27,7 +27,7 @@ void ShaderBuffer::bufferData(int elem, int size, void* data) {
27 27
 
28 28
     boundSize = elem;
29 29
     gl::glBindBuffer(gl::GL_ARRAY_BUFFER, buffer);
30
-    gl::glBufferData(gl::GL_ARRAY_BUFFER, elem * size, data, gl::GL_STATIC_DRAW);
30
+    gl::glBufferData(gl::GL_ARRAY_BUFFER, elem * size, data, gl::GL_DYNAMIC_DRAW);
31 31
 }
32 32
 
33 33
 void ShaderBuffer::bindBuffer() {
@@ -234,10 +234,10 @@ std::string Shader::getVersion(bool linked) {
234 234
     }
235 235
 }
236 236
 
237
-Shader Shader::textShader;
238 237
 Shader Shader::textureShader;
239 238
 Shader Shader::colorShader;
240 239
 unsigned int Shader::vertexArrayID = 0;
240
+bool Shader::lastBufferWasNotFramebuffer = true;
241 241
 
242 242
 int Shader::initialize() {
243 243
     gl::glGenVertexArrays(1, &vertexArrayID);
@@ -254,26 +254,17 @@ int Shader::initialize() {
254 254
 
255 255
     gl::glPointSize(5.0f);
256 256
 
257
-    if (textShader.compile(textShaderVertex, textShaderFragment) < 0)
258
-        return -1;
259
-    if (textShader.addUniform("screen") < 0)
260
-        return -2;
261
-    if (textShader.addUniform("textureSampler") < 0)
262
-        return -3;
263
-    if (textShader.addUniform("colorVar") < 0)
264
-        return -4;
265
-
266 257
     if (textureShader.compile(textureShaderVertex, textureShaderFragment) < 0)
267
-        return -5;
258
+        return -1;
268 259
     if (textureShader.addUniform("MVP") < 0)
269
-        return -6;
260
+        return -2;
270 261
     if (textureShader.addUniform("textureSampler") < 0)
271
-        return -7;
262
+        return -3;
272 263
 
273 264
     if (colorShader.compile(colorShaderVertex, colorShaderFragment) < 0)
274
-        return -8;
265
+        return -4;
275 266
     if (colorShader.addUniform("MVP") < 0)
276
-        return -9;
267
+        return -5;
277 268
 
278 269
     return 0;
279 270
 }
@@ -294,183 +285,117 @@ void Shader::set2DState(bool on, bool depth) {
294 285
     }
295 286
 }
296 287
 
297
-void Shader::drawGL(ShaderBuffer& vertices, ShaderBuffer& uvs, glm::vec4 color,
298
-                    unsigned int texture, TextureStorage store, gl::GLenum mode,
299
-                    ShaderTexture* target, Shader& shader) {
300
-    orAssert(vertices.getSize() == uvs.getSize());
301
-    if (mode == gl::GL_TRIANGLES) {
302
-        orAssert((vertices.getSize() % 3) == 0);
303
-    }
304
-
305
-    if (target == nullptr) {
288
+void Shader::bindProperBuffer(ShaderTexture* target) {
289
+    if ((target == nullptr) && lastBufferWasNotFramebuffer) {
290
+        lastBufferWasNotFramebuffer = false;
306 291
         gl::glBindFramebuffer(gl::GL_FRAMEBUFFER, 0);
307 292
         gl::glViewport(0, 0, Window::getSize().x, Window::getSize().y);
308
-    } else {
293
+    } else if (target != nullptr) {
294
+        lastBufferWasNotFramebuffer = true;
309 295
         target->bind();
310 296
     }
311
-
312
-    shader.use();
313
-    shader.loadUniform(0, glm::vec2(Window::getSize()));
314
-    shader.loadUniform(1, texture, store);
315
-    shader.loadUniform(2, color);
316
-    vertices.bindBuffer(0, 2);
317
-    uvs.bindBuffer(1, 2);
318
-
319
-    set2DState(true);
320
-    gl::glDrawArrays(mode, 0, vertices.getSize());
321
-    set2DState(false);
322
-
323
-    vertices.unbind(0);
324
-    uvs.unbind(1);
325 297
 }
326 298
 
327
-void Shader::drawGL(ShaderBuffer& vertices, ShaderBuffer& uvs, unsigned int texture,
328
-                    glm::mat4 MVP, TextureStorage store, ShaderTexture* target,
329
-                    Shader& shader) {
330
-    orAssert(vertices.getSize() == uvs.getSize());
331
-    orAssert((vertices.getSize() % 3) == 0);
332
-
333
-    if (target == nullptr) {
334
-        gl::glBindFramebuffer(gl::GL_FRAMEBUFFER, 0);
335
-        gl::glViewport(0, 0, Window::getSize().x, Window::getSize().y);
336
-    } else {
337
-        target->bind();
338
-    }
299
+void Shader::drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec2>& uvs,
300
+                    glm::mat4 MVP, unsigned int texture, TextureStorage store,
301
+                    gl::GLenum mode, ShaderTexture* target, Shader& shader) {
302
+    orAssert(vertices.size() == uvs.size());
303
+    if (mode == gl::GL_TRIANGLES)
304
+        orAssert((vertices.size() % 3) == 0);
305
+    bindProperBuffer(target);
339 306
 
340 307
     shader.use();
341 308
     shader.loadUniform(0, MVP);
342 309
     shader.loadUniform(1, texture, store);
343
-    vertices.bindBuffer(0, 3);
344
-    uvs.bindBuffer(1, 2);
345
-    gl::glDrawArrays(gl::GL_TRIANGLES, 0, vertices.getSize());
346
-    vertices.unbind(0);
347
-    uvs.unbind(1);
348
-}
349 310
 
350
-void Shader::drawGL(ShaderBuffer& vertices, ShaderBuffer& uvs, ShaderBuffer& indices,
351
-                    unsigned int texture, glm::mat4 MVP, TextureStorage store,
352
-                    ShaderTexture* target, Shader& shader) {
353
-    orAssert(vertices.getSize() == uvs.getSize());
354
-    orAssert((indices.getSize() % 3) == 0);
311
+    shader.vertexBuffer.bufferData(vertices);
312
+    shader.otherBuffer.bufferData(uvs);
355 313
 
356
-    if (target == nullptr) {
357
-        gl::glBindFramebuffer(gl::GL_FRAMEBUFFER, 0);
358
-        gl::glViewport(0, 0, Window::getSize().x, Window::getSize().y);
359
-    } else {
360
-        target->bind();
314
+    shader.vertexBuffer.bindBuffer(0, 3);
315
+    shader.otherBuffer.bindBuffer(1, 2);
361 316
 
362
-        unsigned int sz = vertices.getSize();
363
-        glm::vec3* buffer = new glm::vec3[sz];
364
-        gl::glBindBuffer(gl::GL_ARRAY_BUFFER, vertices.getBuffer());
365
-        gl::glGetBufferSubData(gl::GL_ARRAY_BUFFER, 0, sz * sizeof(glm::vec3), buffer);
317
+    gl::glDrawArrays(mode, 0, shader.vertexBuffer.getSize());
366 318
 
367
-        Log::get(LOG_DEBUG) << "drawGL Vertex dump:" << Log::endl;
368
-        for (unsigned int i = 0; i < sz; i++) {
369
-            glm::vec4 tmp(buffer[i], 1.0f);
370
-            tmp = MVP * tmp;
371
-            glm::vec3 res(tmp.x, tmp.y, tmp.z);
372
-            Log::get(LOG_DEBUG) << buffer[i] << " -> " << res << Log::endl;
373
-        }
319
+    shader.vertexBuffer.unbind(0);
320
+    shader.otherBuffer.unbind(1);
321
+}
374 322
 
375
-        delete [] buffer;
376
-    }
323
+void Shader::drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec2>& uvs,
324
+                    std::vector<unsigned short>& indices, glm::mat4 MVP,
325
+                    unsigned int texture, TextureStorage store,
326
+                    gl::GLenum mode, ShaderTexture* target, Shader& shader) {
327
+    orAssert(vertices.size() == uvs.size());
328
+    if (mode == gl::GL_TRIANGLES)
329
+        orAssert((indices.size() % 3) == 0);
330
+    bindProperBuffer(target);
377 331
 
378 332
     shader.use();
379 333
     shader.loadUniform(0, MVP);
380 334
     shader.loadUniform(1, texture, store);
381
-    vertices.bindBuffer(0, 3);
382
-    uvs.bindBuffer(1, 2);
383
-    indices.bindBuffer();
384
-    gl::glDrawElements(gl::GL_TRIANGLES, indices.getSize(), gl::GL_UNSIGNED_SHORT, nullptr);
385
-    vertices.unbind(0);
386
-    uvs.unbind(1);
387
-}
388 335
 
389
-void Shader::drawGL(ShaderBuffer& vertices, ShaderBuffer& colors, glm::mat4 MVP,
390
-                    gl::GLenum mode, ShaderTexture* target, Shader& shader) {
391
-    orAssert(vertices.getSize() == colors.getSize());
392
-    if (mode == gl::GL_TRIANGLES) {
393
-        orAssert((vertices.getSize() % 3) == 0);
394
-    }
336
+    shader.vertexBuffer.bufferData(vertices);
337
+    shader.otherBuffer.bufferData(uvs);
338
+    shader.indexBuffer.bufferData(indices);
395 339
 
396
-    if (target == nullptr) {
397
-        gl::glBindFramebuffer(gl::GL_FRAMEBUFFER, 0);
398
-        gl::glViewport(0, 0, Window::getSize().x, Window::getSize().y);
399
-    } else {
400
-        target->bind();
401
-    }
340
+    shader.vertexBuffer.bindBuffer(0, 3);
341
+    shader.otherBuffer.bindBuffer(1, 2);
342
+    shader.indexBuffer.bindBuffer();
402 343
 
403
-    shader.use();
404
-    shader.loadUniform(0, MVP);
405
-    vertices.bindBuffer(0, 3);
406
-    colors.bindBuffer(1, 3);
407
-    gl::glDrawArrays(mode, 0, vertices.getSize());
408
-    vertices.unbind(0);
409
-    colors.unbind(1);
344
+    gl::glDrawElements(mode, shader.indexBuffer.getSize(), gl::GL_UNSIGNED_SHORT, nullptr);
345
+
346
+    shader.vertexBuffer.unbind(0);
347
+    shader.otherBuffer.unbind(1);
410 348
 }
411 349
 
412
-void Shader::drawGL(ShaderBuffer& vertices, ShaderBuffer& colors, ShaderBuffer& indices,
350
+void Shader::drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& colors,
413 351
                     glm::mat4 MVP, gl::GLenum mode, ShaderTexture* target, Shader& shader) {
414
-    orAssert(vertices.getSize() == colors.getSize());
415
-    if (mode == gl::GL_TRIANGLES) {
416
-        orAssert((indices.getSize() % 3) == 0);
417
-    }
418
-
419
-    if (target == nullptr) {
420
-        gl::glBindFramebuffer(gl::GL_FRAMEBUFFER, 0);
421
-        gl::glViewport(0, 0, Window::getSize().x, Window::getSize().y);
422
-    } else {
423
-        target->bind();
424
-    }
352
+    orAssert(vertices.size() == colors.size());
353
+    if (mode == gl::GL_TRIANGLES)
354
+        orAssert((vertices.size() % 3) == 0);
355
+    bindProperBuffer(target);
425 356
 
426 357
     shader.use();
427 358
     shader.loadUniform(0, MVP);
428
-    vertices.bindBuffer(0, 3);
429
-    colors.bindBuffer(1, 3);
430
-    indices.bindBuffer();
431
-    gl::glDrawElements(mode, indices.getSize(), gl::GL_UNSIGNED_SHORT, nullptr);
432
-    vertices.unbind(0);
433
-    colors.unbind(1);
434
-}
435
-
436
-// --------------------------------------
437
-// *INDENT-OFF*
438 359
 
439
-const char* Shader::textShaderVertex = R"!?!(
440
-#version 330 core
441
-
442
-layout(location = 0) in vec2 vertexPosition_screen;
443
-layout(location = 1) in vec2 vertexUV;
444
-
445
-out vec2 UV;
360
+    shader.vertexBuffer.bufferData(vertices);
361
+    shader.otherBuffer.bufferData(colors);
446 362
 
447
-uniform vec2 screen;
363
+    shader.vertexBuffer.bindBuffer(0, 3);
364
+    shader.otherBuffer.bindBuffer(1, 3);
448 365
 
449
-void main() {
450
-    vec2 halfScreen = screen / 2;
451
-    vec2 vertexPosition_homogenous = (vertexPosition_screen - halfScreen) / halfScreen;
366
+    gl::glDrawArrays(mode, 0, shader.vertexBuffer.getSize());
452 367
 
453
-    gl_Position = vec4(vertexPosition_homogenous.x, -vertexPosition_homogenous.y, 0, 1);
454
-    UV = vertexUV;
368
+    shader.vertexBuffer.unbind(0);
369
+    shader.otherBuffer.unbind(1);
455 370
 }
456
-)!?!";
457 371
 
458
-const char* Shader::textShaderFragment = R"!?!(
459
-#version 330 core
372
+void Shader::drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& colors,
373
+                    std::vector<unsigned short>& indices, glm::mat4 MVP,
374
+                    gl::GLenum mode, ShaderTexture* target, Shader& shader) {
375
+    orAssert(vertices.size() == colors.size());
376
+    if (mode == gl::GL_TRIANGLES)
377
+        orAssert((indices.size() % 3) == 0);
378
+    bindProperBuffer(target);
460 379
 
461
-in vec2 UV;
380
+    shader.use();
381
+    shader.loadUniform(0, MVP);
462 382
 
463
-layout(location = 0) out vec4 color;
383
+    shader.vertexBuffer.bufferData(vertices);
384
+    shader.otherBuffer.bufferData(colors);
385
+    shader.indexBuffer.bufferData(indices);
464 386
 
465
-uniform sampler2D textureSampler;
466
-uniform vec4 colorVar;
387
+    shader.vertexBuffer.bindBuffer(0, 3);
388
+    shader.otherBuffer.bindBuffer(1, 3);
389
+    shader.indexBuffer.bindBuffer();
467 390
 
468
-void main() {
469
-    color = texture(textureSampler, UV) * colorVar;
391
+    gl::glDrawElements(mode, shader.indexBuffer.getSize(), gl::GL_UNSIGNED_SHORT, nullptr);
392
+
393
+    shader.vertexBuffer.unbind(0);
394
+    shader.otherBuffer.unbind(1);
470 395
 }
471
-)!?!";
472 396
 
473 397
 // --------------------------------------
398
+// *INDENT-OFF*
474 399
 
475 400
 const char* Shader::textureShaderVertex = R"!?!(
476 401
 #version 330 core

Loading…
Cancel
Save