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