src/gl/axesprogram.cpp

Fri, 06 Mar 2020 16:08:53 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 06 Mar 2020 16:08:53 +0200
changeset 69
a36913fc552a
child 70
f21b800b02a4
permissions
-rw-r--r--

begin work on axes program

69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 /*
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 * Copyright (C) 2020 Teemu Piippo
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 *
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 * (at your option) any later version.
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 *
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 * GNU General Public License for more details.
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 *
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 */
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #include "gridprogram.h"
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 // Based on https://stackoverflow.com/q/30842755
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 const char vertexShaderSource[] = R"(
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 #version 330 core
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 layout (location = 0) in vec3 in_position;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 layout (location = 1) in vec3 in_color;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 uniform mat4 view;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 uniform mat4 projection;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 uniform mat4 model;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 smooth out vec3 ex_color;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 void main()
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 {
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 gl_Position = projection * view * model * vec4(in_position, 0.0, 1.0);
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 ex_uv = ex_color;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 )";
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 const char fragmentShaderSource[] = R"(
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 #version 330 core
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 out vec4 color;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 smooth in vec3 ex_color;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 void main(void)
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 {
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 color = vec4(ex_color, 1)
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 )";
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 namespace
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 {
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 struct AxesVertex
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 {
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 glm::vec3 position;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 glm::vec3 color;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 };
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 static AxesVertex data[] =
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61 {
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62 AxesVertex{{-10000, 0, 0}, {1, 0, 0}},
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 AxesVertex{{10000, 0, 0}, {1, 0, 0}},
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 AxesVertex{{0, -10000, 0}, {0, 1, 0}},
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 AxesVertex{{0, 10000, 0}, {0, 1, 0}},
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 AxesVertex{{0, 0, -10000}, {0, 0, 1}},
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 AxesVertex{{0, 0, 10000}, {0, 0, 1}},
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 };
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 AxesProgram::AxesProgram(QObject* parent) :
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 QObject{parent},
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 buffer{QOpenGLBuffer::VertexBuffer},
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 vertexShader{QOpenGLShader::Vertex},
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 fragmentShader{QOpenGLShader::Fragment}
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 {
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78 void AxesProgram::initialize()
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 {
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
80 if (not isInitialized)
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
81 {
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
82 this->initializeOpenGLFunctions();
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83 this->isInitialized = true;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84 this->program.emplace(this);
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
85 gl::buildShaders(&*this->program, ::vertexShaderSource, ::fragmentShaderSource);
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
86 this->program->bind();
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
87 this->buffer.create();
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
88 this->buffer.bind();
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
89 this->buffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
90 this->buffer.allocate(data, countof(data) * sizeof data[0]);
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
91 this->vertexArrayObject.create();
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
92 this->vertexArrayObject.bind();
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
93 this->program->enableAttributeArray(0);
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
94 this->program->setAttributeBuffer(0, GL_FLOAT, 0, 2, 0);
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95 this->program->setUniformVector("gridColor", this->gridColor);
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
96 this->vertexArrayObject.release();
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
97 this->buffer.release();
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
98 this->program->release();
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
99 this->checkForGLErrors();
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
100 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
101 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
102
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
103 void AxesProgram::setViewMatrix(const glm::mat4& newViewMatrix)
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
104 {
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
105 this->setMatrix("view", newViewMatrix);
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
106 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
107
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
108 void AxesProgram::setProjectionMatrix(const glm::mat4& newProjectionMatrix)
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
109 {
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
110 this->setMatrix("projection", newProjectionMatrix);
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
111 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
112
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
113 void AxesProgram::setModelMatrix(const glm::mat4& newModelMatrix)
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
114 {
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
115 this->setMatrix("model", newModelMatrix);
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
116 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
117
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
118 void AxesProgram::setGridMatrix(const glm::mat4& newGridMatrix)
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
119 {
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
120 this->setMatrix("grid", newGridMatrix);
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
121 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
122
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
123 void AxesProgram::setMatrix(const char* name, const glm::mat4& matrix)
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
124 {
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
125 Q_ASSERT(this->isInitialized);
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
126 this->program->bind();
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
127 this->program->setUniformMatrix(name, matrix);
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
128 this->program->release();
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
129 this->checkForGLErrors();
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
130 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
131
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
132 void GridProgram::setGridColor(const QColor& newGridColor)
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
133 {
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
134 const glm::vec4 vec = gl::colorToVector4(newGridColor);
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
135 if (this->isInitialized)
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
136 {
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
137 this->program->bind();
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
138 this->program->setUniformVector("gridColor", vec);
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
139 this->program->release();
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
140 this->checkForGLErrors();
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
141 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
142 else
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
143 {
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
144 this->gridColor = vec;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
145 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
146 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
147
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
148 void GridProgram::draw()
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
149 {
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
150 this->program->bind();
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
151 this->vertexArrayObject.bind();
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
152 glDrawArrays(GL_QUADS, 0, countof(data));
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
153 this->vertexArrayObject.release();
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
154 this->program->release();
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
155 this->checkForGLErrors();
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
156 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
157
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
158 void GridProgram::teardown()
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
159 {
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
160 this->vertexArrayObject.destroy();
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
161 this->buffer.destroy();
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
162 this->program.reset();
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
163 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
164
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
165 void GridProgram::checkForGLErrors()
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
166 {
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
167 gl::checkForGLErrors(qobject_cast<QWidget*>(this->parent()));
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
168 }

mercurial