src/gl/vertexprogram.cpp

Fri, 11 Mar 2022 20:19:26 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 11 Mar 2022 20:19:26 +0200
changeset 175
41628bdab71c
parent 172
50f055543ff6
child 196
6bcb284679d4
permissions
-rw-r--r--

add grid actions

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
172
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
34 std::vector<glm::vec3> sphere(const int d2)
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
35 {
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
36 std::vector<glm::vec3> result;
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
37 result.reserve(12 * d2 * d2);
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
38 for (int i = 0; i < d2; ++i)
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
39 {
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
40 const float alpha = i * math::pi / d2;
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
41 const float alpha_2 = (i + 1) * math::pi / d2;
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
42 for (int j = -d2; j < d2; ++j)
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
43 {
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
44 const float beta = j * math::pi / d2;
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
45 const float beta_2 = (j + 1) * math::pi / d2;
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
46 const float x1 = cos(beta) * sin(alpha);
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
47 const float x2 = cos(beta) * sin(alpha_2);
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
48 const float x3 = cos(beta_2) * sin(alpha_2);
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
49 const float x4 = cos(beta_2) * sin(alpha);
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
50 const float z1 = sin(beta) * sin(alpha);
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
51 const float z2 = sin(beta) * sin(alpha_2);
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
52 const float z3 = sin(beta_2) * sin(alpha_2);
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
53 const float z4 = sin(beta_2) * sin(alpha);
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
54 const float y1 = cos(alpha);
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
55 const float y2 = cos(alpha_2);
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
56 result.push_back({x1, y1, z1});
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
57 result.push_back({x2, y2, z2});
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
58 result.push_back({x3, y2, z3});
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
59 result.push_back({x1, y1, z1});
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
60 result.push_back({x3, y2, z3});
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
61 result.push_back({x4, y1, z4});
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
62 }
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
63 }
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
64 return result;
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
65 }
118
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 VertexProgram::VertexProgram(QObject *parent) :
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 AbstractBasicShaderProgram{parent}
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 {
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 const char *VertexProgram::vertexShaderSource() 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 ::vertexShaderSource;
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 const char *VertexProgram::fragmentShaderSource() const
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 return ::fragmentShaderSource;
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
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
82 const void *VertexProgram::vertexData() const
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83 {
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84 return this->data.data();
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
85 }
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 int VertexProgram::vertexSize() const
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
88 {
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
89 return sizeof(Vertex);
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
90 }
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 int VertexProgram::vertexCount() const
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
93 {
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
94 return this->data.size();
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95 }
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 void VertexProgram::setupVertexArrays()
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
98 {
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
99 for (int i : {0, 1})
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
100 {
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
101 this->program->enableAttributeArray(i);
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
102 }
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
103 const int stride = this->vertexSize();
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
104 this->program->setAttributeBuffer(0, GL_FLOAT, offsetof(Vertex, position), 3, stride);
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
105 this->program->setAttributeBuffer(1, GL_FLOAT, offsetof(Vertex, color), 3, stride);
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
106 }
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
107
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
108 GLenum VertexProgram::drawMode() const
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
109 {
119
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
110 return GL_TRIANGLES;
118
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
111 }
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
112
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
113 QOpenGLBuffer::UsagePattern VertexProgram::usagePattern() const
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
114 {
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
115 return QOpenGLBuffer::DynamicDraw;
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
116 }
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
117
119
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
118 void VertexProgram::setFragmentStyle(const FragmentStyle newFragmentStyle)
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
119 {
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
120 this->fragmentStyle = newFragmentStyle;
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
121 }
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
122
118
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
123 void VertexProgram::build(const Document *document)
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
124 {
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
125 constexpr glm::vec3 color = {0.0, 1.0, 1.0};
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
126 this->data.clear();
172
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
127 const std::vector<glm::vec3> sphere = ::sphere(8 / 2);
120
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
128 document->applyToVertices([&](const glm::vec3&, const VertexMap::VertexInfo& info)
118
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
129 {
172
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
130 reserveMore(this->data, sphere.size());
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
131 for (const glm::vec3& point : sphere)
119
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
132 {
172
50f055543ff6 Render vertices as spheres
Teemu Piippo <teemu@hecknology.net>
parents: 120
diff changeset
133 const glm::vec3 transformed = glm::scale(info.transform, glm::vec3{0.5, 0.5, 0.5}) * glm::vec4{point, 1};
120
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
134 this->data.push_back({transformed, color});
119
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
135 }
118
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
136 });
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
137 this->buffer.bind();
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
138 this->buffer.allocate(this->vertexData(), this->vertexCount() * this->vertexSize());
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
139 this->buffer.release();
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
140 }

mercurial