src/gl/vertexprogram.cpp

Fri, 27 Aug 2021 14:38:56 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 27 Aug 2021 14:38:56 +0300
changeset 123
e3fe3617b631
parent 120
8c9fff699241
child 172
50f055543ff6
permissions
-rw-r--r--

refactor

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

mercurial