src/gl/gridprogram.cpp

Thu, 13 Feb 2020 12:51:27 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 13 Feb 2020 12:51:27 +0200
changeset 53
3af627f7a40f
child 54
a4055f67b9c7
permissions
-rw-r--r--

added grid program

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

mercurial