src/gl/gridprogram.cpp

Sat, 05 Mar 2022 13:59:54 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 05 Mar 2022 13:59:54 +0200
changeset 166
8857351912d0
parent 156
65b75beed7e0
child 167
c1ff4f107569
permissions
-rw-r--r--

Fix rendering of cursor coordinates on bright background

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;
64
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 55
diff changeset
30 uniform mat4 grid;
53
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 {
156
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
34 gl_Position = projection * view * model * grid * vec4(in_position, 0.0, 1.0);
53
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;
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
44 uniform vec4 gridColor;
53
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 {
156
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
48 float dx = fract(ex_uv.y);
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
49 float dy = fract(ex_uv.x);
54
a4055f67b9c7 made the grid look nicer
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
50 /* fade the grid towards extreme co-ordinates */
156
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
51 float d = (1.0f - 0.015 * max(abs(ex_uv.x), abs(ex_uv.y)));
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
52 color = vec4(gridColor.xyz, gridColor.w * d);
53
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
156
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
56 GridProgram::GridProgram(QObject *parent) :
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
57 AbstractBasicShaderProgram{parent}
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
58 {
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
59 this->gridData.reserve(8004);
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
60 for (int i = -1000; i < 1000; i += 1)
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
61 {
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
62 this->gridData.push_back({i, -1000});
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
63 this->gridData.push_back({i, 1000});
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
64 }
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
65 for (int i = -1000; i < 1000; i += 1)
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
66 {
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
67 this->gridData.push_back({-1000, i});
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
68 this->gridData.push_back({1000, i});
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
69 }
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
70 }
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71
64
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 55
diff changeset
72 void GridProgram::setGridMatrix(const glm::mat4& newGridMatrix)
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 55
diff changeset
73 {
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 55
diff changeset
74 this->setMatrix("grid", newGridMatrix);
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 55
diff changeset
75 }
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 55
diff changeset
76
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
77 void GridProgram::setGridColor(const QColor& newGridColor)
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
78 {
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
79 const glm::vec4 vec = gl::colorToVector4(newGridColor);
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
80 if (this->isInitialized)
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
81 {
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
82 this->program->bind();
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
83 this->program->setUniformVector("gridColor", vec);
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
84 this->program->release();
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
85 this->checkForGLErrors();
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
86 }
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
87 else
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
88 {
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
89 this->gridColor = vec;
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
90 }
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
91 }
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
92
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
93 const char* GridProgram::vertexShaderSource() const
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
94 {
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
95 return ::vertexShaderSource;
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
96 }
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
97
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
98 const char* GridProgram::fragmentShaderSource() const
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
99 {
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
100 return ::fragmentShaderSource;
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
101 }
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
102
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
103 const void* GridProgram::vertexData() const
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
104 {
156
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
105 return this->gridData.data();
53
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
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
108 int GridProgram::vertexSize() const
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
109 {
156
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
110 return sizeof this->gridData[0];
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
111 }
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
112
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
113 int GridProgram::vertexCount() const
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
114 {
156
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
115 return this->gridData.size();
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
116 }
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
117
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
118 void GridProgram::setupVertexArrays()
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
119 {
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
120 this->program->enableAttributeArray(0);
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
121 this->program->setAttributeBuffer(0, GL_FLOAT, 0, 2, 0);
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
122 this->program->setUniformVector("gridColor", this->gridColor);
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
123 }
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
124
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
125 GLenum GridProgram::drawMode() const
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
126 {
156
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
127 return GL_LINES;
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
128 }
118
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 90
diff changeset
129
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 90
diff changeset
130 QOpenGLBuffer::UsagePattern GridProgram::usagePattern() const
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 90
diff changeset
131 {
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 90
diff changeset
132 return QOpenGLBuffer::StaticDraw;
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 90
diff changeset
133 }

mercurial