src/gl/vertexprogram.cpp

changeset 119
24275a4064f4
parent 118
8e1c9f18ae15
child 120
8c9fff699241
equal deleted inserted replaced
118:8e1c9f18ae15 119:24275a4064f4
1 #include "vertexprogram.h" 1 #include "vertexprogram.h"
2 #include "document.h" 2 #include "document.h"
3 3
4 static const char vertexShaderSource[] = R"( 4 static const char vertexShaderSource[] = R"(
5 #version 330 core 5 #version 330 core
6 6 const int FRAGSTYLE_Normal = 0;
7 const int FRAGSTYLE_Id = 1;
7 layout (location = 0) in vec3 in_position; 8 layout (location = 0) in vec3 in_position;
8 layout (location = 1) in vec3 in_color; 9 layout (location = 1) in vec3 in_color;
9 uniform mat4 view; 10 uniform mat4 view;
10 uniform mat4 projection; 11 uniform mat4 projection;
11 uniform mat4 model; 12 uniform mat4 model;
28 { 29 {
29 color = vec4(ex_color, 1); 30 color = vec4(ex_color, 1);
30 } 31 }
31 )"; 32 )";
32 33
33 static constexpr int VERTICES_PER_OBJECT = 16; 34 static constexpr glm::vec3 o = {0, 0, 0};
35 static constexpr glm::vec3 c1 = {1, -1, 1};
36 static constexpr glm::vec3 c2 = {1, -1, -1};
37 static constexpr glm::vec3 c3 = {-1, -1, -1};
38 static constexpr glm::vec3 c4 = {-1, -1, 1};
34 39
40 static constexpr glm::vec3 markerGeometry[] = {
41 o, c1, c2,
42 o, c2, c3,
43 o, c3, c4,
44 o, c4, c1,
45 };
35 46
36 VertexProgram::VertexProgram(QObject *parent) : 47 VertexProgram::VertexProgram(QObject *parent) :
37 AbstractBasicShaderProgram{parent} 48 AbstractBasicShaderProgram{parent}
38 { 49 {
39 } 50 }
74 this->program->setAttributeBuffer(1, GL_FLOAT, offsetof(Vertex, color), 3, stride); 85 this->program->setAttributeBuffer(1, GL_FLOAT, offsetof(Vertex, color), 3, stride);
75 } 86 }
76 87
77 GLenum VertexProgram::drawMode() const 88 GLenum VertexProgram::drawMode() const
78 { 89 {
79 return GL_LINES; 90 return GL_TRIANGLES;
80 } 91 }
81 92
82 QOpenGLBuffer::UsagePattern VertexProgram::usagePattern() const 93 QOpenGLBuffer::UsagePattern VertexProgram::usagePattern() const
83 { 94 {
84 return QOpenGLBuffer::DynamicDraw; 95 return QOpenGLBuffer::DynamicDraw;
96 }
97
98 void VertexProgram::setFragmentStyle(const FragmentStyle newFragmentStyle)
99 {
100 this->fragmentStyle = newFragmentStyle;
85 } 101 }
86 102
87 void VertexProgram::build(const Document *document) 103 void VertexProgram::build(const Document *document)
88 { 104 {
89 constexpr float size = 1; 105 constexpr float size = 1;
90 constexpr glm::vec3 color = {0.0, 1.0, 1.0}; 106 constexpr glm::vec3 color = {0.0, 1.0, 1.0};
91 this->data.clear(); 107 this->data.clear();
92 document->applyToVertices([&](const glm::vec3& vertex, const std::set<ldraw::id_t>&) 108 document->applyToVertices([&](const glm::vec3& vertex, const std::set<ldraw::id_t>&)
93 { 109 {
94 reserveMore(this->data, VERTICES_PER_OBJECT); 110 reserveMore(this->data, ::countof(::markerGeometry));
95 this->data.push_back({vertex, color}); 111 for (const glm::vec3& point : ::markerGeometry)
96 this->data.push_back({vertex + glm::vec3{size, -size, size}, color}); 112 {
97 this->data.push_back({vertex, color}); 113 this->data.push_back({vertex + point * size, color});
98 this->data.push_back({vertex + glm::vec3{size, -size, -size}, color}); 114 }
99 this->data.push_back({vertex, color});
100 this->data.push_back({vertex + glm::vec3{-size, -size, -size}, color});
101 this->data.push_back({vertex, color});
102 this->data.push_back({vertex + glm::vec3{-size, -size, size}, color});
103 this->data.push_back({vertex + glm::vec3{size, -size, size}, color});
104 this->data.push_back({vertex + glm::vec3{size, -size, -size}, color});
105 this->data.push_back({vertex + glm::vec3{size, -size, -size}, color});
106 this->data.push_back({vertex + glm::vec3{-size, -size, -size}, color});
107 this->data.push_back({vertex + glm::vec3{-size, -size, -size}, color});
108 this->data.push_back({vertex + glm::vec3{-size, -size, size}, color});
109 this->data.push_back({vertex + glm::vec3{-size, -size, size}, color});
110 this->data.push_back({vertex + glm::vec3{size, -size, size}, color});
111 }); 115 });
112 this->buffer.bind(); 116 this->buffer.bind();
113 this->buffer.allocate(this->vertexData(), this->vertexCount() * this->vertexSize()); 117 this->buffer.allocate(this->vertexData(), this->vertexCount() * this->vertexSize());
114 this->buffer.release(); 118 this->buffer.release();
115 } 119 }

mercurial