Bladeren bron

Fixed BoundingBox rendering problems

Thomas Buck 9 jaren geleden
bovenliggende
commit
7f672f627f
5 gewijzigde bestanden met toevoegingen van 91 en 6 verwijderingen
  1. 4
    0
      .gitignore
  2. 2
    1
      include/BoundingBox.h
  3. 11
    0
      include/system/Shader.h
  4. 5
    5
      src/BoundingBox.cpp
  5. 69
    0
      src/system/Shader.cpp

+ 4
- 0
.gitignore Bestand weergeven

@@ -1,2 +1,6 @@
1 1
 .DS_Store
2
+.VolumeIcon.icns
3
+.fseventsd
4
+Icon

5
+
2 6
 build*

+ 2
- 1
include/BoundingBox.h Bestand weergeven

@@ -27,7 +27,8 @@ class BoundingBox {
27 27
   private:
28 28
     std::array<glm::vec3, 8> corner;
29 29
 
30
-    static std::vector<glm::vec3> vertices, colorsLine, colorsPoint;
30
+    static std::vector<glm::vec4> vertices;
31
+    static std::vector<glm::vec3> colorsLine, colorsPoint;
31 32
     static std::vector<unsigned short> indicesLine;
32 33
 };
33 34
 

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

@@ -100,6 +100,13 @@ class Shader {
100 100
                        gl::GLenum mode = gl::GL_TRIANGLES, ShaderTexture* target = nullptr,
101 101
                        Shader& shader = colorShader);
102 102
 
103
+    static void drawGL(std::vector<glm::vec4>& vertices, std::vector<glm::vec3>& colors,
104
+                       gl::GLenum mode = gl::GL_TRIANGLES, ShaderTexture* target = nullptr,
105
+                       Shader& shader = transformedColorShader);
106
+    static void drawGL(std::vector<glm::vec4>& vertices, std::vector<glm::vec3>& colors,
107
+                       std::vector<unsigned short>& indices, gl::GLenum mode = gl::GL_TRIANGLES,
108
+                       ShaderTexture* target = nullptr, Shader& shader = transformedColorShader);
109
+
103 110
     static std::string getVersion(bool linked);
104 111
 
105 112
   private:
@@ -117,6 +124,10 @@ class Shader {
117 124
     static const char* colorShaderVertex;
118 125
     static const char* colorShaderFragment;
119 126
 
127
+    static Shader transformedColorShader;
128
+    static const char* transformedColorShaderVertex;
129
+    static const char* transformedColorShaderFragment;
130
+
120 131
     static unsigned int vertexArrayID;
121 132
     static bool lastBufferWasNotFramebuffer;
122 133
 };

+ 5
- 5
src/BoundingBox.cpp Bestand weergeven

@@ -13,7 +13,8 @@
13 13
 
14 14
 #include <glbinding/gl/gl.h>
15 15
 
16
-std::vector<glm::vec3> BoundingBox::vertices, BoundingBox::colorsLine, BoundingBox::colorsPoint;
16
+std::vector<glm::vec4> BoundingBox::vertices;
17
+std::vector<glm::vec3> BoundingBox::colorsLine, BoundingBox::colorsPoint;
17 18
 std::vector<unsigned short> BoundingBox::indicesLine;
18 19
 
19 20
 BoundingBox::BoundingBox(glm::vec3 min, glm::vec3 max) {
@@ -39,8 +40,7 @@ void BoundingBox::display(glm::mat4 VP, glm::vec3 colorLine, glm::vec3 colorDot)
39 40
     auto startSize = vertices.size();
40 41
 
41 42
     for (auto& c : corner) {
42
-        glm::vec4 t = VP * glm::vec4(c, 1.0f);
43
-        vertices.emplace_back(glm::vec3(t) / t.w);
43
+        vertices.emplace_back(VP * glm::vec4(c, 1.0f));
44 44
         colorsLine.emplace_back(colorLine);
45 45
         colorsPoint.emplace_back(colorDot);
46 46
     }
@@ -73,8 +73,8 @@ void BoundingBox::display(glm::mat4 VP, glm::vec3 colorLine, glm::vec3 colorDot)
73 73
 
74 74
 void BoundingBox::display() {
75 75
     if (vertices.size() > 0) {
76
-        Shader::drawGL(vertices, colorsLine, indicesLine, glm::mat4(1.0f), gl::GL_LINES);
77
-        Shader::drawGL(vertices, colorsPoint, glm::mat4(1.0f), gl::GL_POINTS);
76
+        Shader::drawGL(vertices, colorsLine, indicesLine, gl::GL_LINES);
77
+        Shader::drawGL(vertices, colorsPoint, gl::GL_POINTS);
78 78
     }
79 79
 
80 80
     vertices.clear();

+ 69
- 0
src/system/Shader.cpp Bestand weergeven

@@ -246,6 +246,7 @@ std::string Shader::getVersion(bool linked) {
246 246
 
247 247
 Shader Shader::textureShader;
248 248
 Shader Shader::colorShader;
249
+Shader Shader::transformedColorShader;
249 250
 unsigned int Shader::vertexArrayID = 0;
250 251
 bool Shader::lastBufferWasNotFramebuffer = true;
251 252
 
@@ -276,6 +277,9 @@ int Shader::initialize() {
276 277
     if (colorShader.addUniform("MVP") < 0)
277 278
         return -5;
278 279
 
280
+    if (transformedColorShader.compile(transformedColorShaderVertex, transformedColorShaderFragment) < 0)
281
+        return -6;
282
+
279 283
     return 0;
280 284
 }
281 285
 
@@ -402,6 +406,43 @@ void Shader::drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& co
402 406
     shader.otherBuffer.unbind(1);
403 407
 }
404 408
 
409
+void Shader::drawGL(std::vector<glm::vec4>& vertices, std::vector<glm::vec3>& colors,
410
+                    gl::GLenum mode, ShaderTexture* target, Shader& shader) {
411
+    bindProperBuffer(target);
412
+
413
+    shader.use();
414
+    shader.vertexBuffer.bufferData(vertices);
415
+    shader.otherBuffer.bufferData(colors);
416
+
417
+    shader.vertexBuffer.bindBuffer(0, 4);
418
+    shader.otherBuffer.bindBuffer(1, 3);
419
+
420
+    gl::glDrawArrays(mode, 0, shader.vertexBuffer.getSize());
421
+
422
+    shader.vertexBuffer.unbind(0);
423
+    shader.otherBuffer.unbind(0);
424
+}
425
+
426
+void Shader::drawGL(std::vector<glm::vec4>& vertices, std::vector<glm::vec3>& colors,
427
+                    std::vector<unsigned short>& indices, gl::GLenum mode,
428
+                    ShaderTexture* target, Shader& shader) {
429
+    bindProperBuffer(target);
430
+
431
+    shader.use();
432
+    shader.vertexBuffer.bufferData(vertices);
433
+    shader.otherBuffer.bufferData(colors);
434
+    shader.indexBuffer.bufferData(indices);
435
+
436
+    shader.vertexBuffer.bindBuffer(0, 4);
437
+    shader.otherBuffer.bindBuffer(1, 3);
438
+    shader.indexBuffer.bindBuffer();
439
+
440
+    gl::glDrawElements(mode, shader.indexBuffer.getSize(), gl::GL_UNSIGNED_SHORT, nullptr);
441
+
442
+    shader.vertexBuffer.unbind(0);
443
+    shader.otherBuffer.unbind(0);
444
+}
445
+
405 446
 // --------------------------------------
406 447
 // *INDENT-OFF*
407 448
 
@@ -468,5 +509,33 @@ void main() {
468 509
 )!?!";
469 510
 
470 511
 // --------------------------------------
512
+
513
+const char* Shader::transformedColorShaderVertex = R"!?!(
514
+#version 330 core
515
+
516
+layout(location = 0) in vec4 vertexPosition;
517
+layout(location = 1) in vec3 vertexColor;
518
+
519
+out vec3 color;
520
+
521
+void main() {
522
+    gl_Position = vertexPosition;
523
+    color = vertexColor;
524
+}
525
+)!?!";
526
+
527
+const char* Shader::transformedColorShaderFragment = R"!?!(
528
+#version 330 core
529
+
530
+in vec3 color;
531
+
532
+layout(location = 0) out vec4 color_out;
533
+
534
+void main() {
535
+    color_out = vec4(color, 1);
536
+}
537
+)!?!";
538
+
539
+// --------------------------------------
471 540
 // *INDENT-ON*
472 541
 

Laden…
Annuleren
Opslaan