Sun, 29 Aug 2021 20:05:24 +0300
esc with draw mode now clears the polygon
118 | 1 | #include "vertexprogram.h" |
2 | #include "document.h" | |
3 | ||
4 | static const char vertexShaderSource[] = R"( | |
5 | #version 330 core | |
119 | 6 | const int FRAGSTYLE_Normal = 0; |
7 | const int FRAGSTYLE_Id = 1; | |
118 | 8 | layout (location = 0) in vec3 in_position; |
9 | layout (location = 1) in vec3 in_color; | |
10 | uniform mat4 view; | |
11 | uniform mat4 projection; | |
12 | uniform mat4 model; | |
13 | smooth out vec3 ex_color; | |
14 | ||
15 | void main() | |
16 | { | |
17 | gl_Position = projection * view * model * vec4(in_position, 1.0); | |
18 | ex_color = in_color; | |
19 | } | |
20 | )"; | |
21 | ||
22 | static const char fragmentShaderSource[] = R"( | |
23 | #version 330 core | |
24 | ||
25 | out vec4 color; | |
26 | smooth in vec3 ex_color; | |
27 | ||
28 | void main(void) | |
29 | { | |
30 | color = vec4(ex_color, 1); | |
31 | } | |
32 | )"; | |
33 | ||
119 | 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}; | |
118 | 39 | |
119 | 40 | static constexpr glm::vec3 markerGeometry[] = { |
41 | o, c1, c2, | |
42 | o, c2, c3, | |
43 | o, c3, c4, | |
44 | o, c4, c1, | |
45 | }; | |
118 | 46 | |
47 | VertexProgram::VertexProgram(QObject *parent) : | |
48 | AbstractBasicShaderProgram{parent} | |
49 | { | |
50 | } | |
51 | ||
52 | const char *VertexProgram::vertexShaderSource() const | |
53 | { | |
54 | return ::vertexShaderSource; | |
55 | } | |
56 | ||
57 | const char *VertexProgram::fragmentShaderSource() const | |
58 | { | |
59 | return ::fragmentShaderSource; | |
60 | } | |
61 | ||
62 | const void *VertexProgram::vertexData() const | |
63 | { | |
64 | return this->data.data(); | |
65 | } | |
66 | ||
67 | int VertexProgram::vertexSize() const | |
68 | { | |
69 | return sizeof(Vertex); | |
70 | } | |
71 | ||
72 | int VertexProgram::vertexCount() const | |
73 | { | |
74 | return this->data.size(); | |
75 | } | |
76 | ||
77 | void VertexProgram::setupVertexArrays() | |
78 | { | |
79 | for (int i : {0, 1}) | |
80 | { | |
81 | this->program->enableAttributeArray(i); | |
82 | } | |
83 | const int stride = this->vertexSize(); | |
84 | this->program->setAttributeBuffer(0, GL_FLOAT, offsetof(Vertex, position), 3, stride); | |
85 | this->program->setAttributeBuffer(1, GL_FLOAT, offsetof(Vertex, color), 3, stride); | |
86 | } | |
87 | ||
88 | GLenum VertexProgram::drawMode() const | |
89 | { | |
119 | 90 | return GL_TRIANGLES; |
118 | 91 | } |
92 | ||
93 | QOpenGLBuffer::UsagePattern VertexProgram::usagePattern() const | |
94 | { | |
95 | return QOpenGLBuffer::DynamicDraw; | |
96 | } | |
97 | ||
119 | 98 | void VertexProgram::setFragmentStyle(const FragmentStyle newFragmentStyle) |
99 | { | |
100 | this->fragmentStyle = newFragmentStyle; | |
101 | } | |
102 | ||
118 | 103 | void VertexProgram::build(const Document *document) |
104 | { | |
105 | constexpr glm::vec3 color = {0.0, 1.0, 1.0}; | |
106 | this->data.clear(); | |
120
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
107 | document->applyToVertices([&](const glm::vec3&, const VertexMap::VertexInfo& info) |
118 | 108 | { |
119 | 109 | reserveMore(this->data, ::countof(::markerGeometry)); |
110 | for (const glm::vec3& point : ::markerGeometry) | |
111 | { | |
120
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
112 | const glm::vec3 transformed = info.transform * glm::vec4{point, 1}; |
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
113 | this->data.push_back({transformed, color}); |
119 | 114 | } |
118 | 115 | }); |
116 | this->buffer.bind(); | |
117 | this->buffer.allocate(this->vertexData(), this->vertexCount() * this->vertexSize()); | |
118 | this->buffer.release(); | |
119 | } |