src/gl/gridprogram.cpp

Wed, 11 Mar 2020 17:19:28 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 11 Mar 2020 17:19:28 +0200
changeset 78
97c3ce5aa498
parent 72
7c27cda03747
child 90
e234edb5e613
permissions
-rw-r--r--

fixed signed vs unsigned nonsense in gl::Compiler::idFromColor

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;
72
7c27cda03747 stretch the grid quadrilateral less, hopefully this makes fragments small enough even on integrated
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
30 const mat4 stretch = mat4(vec4(1000, 0, 0, 0), vec4(0, 1000, 0, 0), vec4(0, 0, 1000, 0), vec4(0, 0, 0, 1));
64
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 55
diff changeset
31 uniform mat4 grid;
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 void main()
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 {
64
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 55
diff changeset
35 gl_Position = projection * view * model * grid * stretch * vec4(in_position, 0.0, 1.0);
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 ex_uv = in_position;
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
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 const char fragmentShaderSource[] = R"(
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 #version 330 core
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 out vec4 color;
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 smooth in vec2 ex_uv;
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
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
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 void main(void)
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 {
72
7c27cda03747 stretch the grid quadrilateral less, hopefully this makes fragments small enough even on integrated
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
50 float dx = fract(ex_uv.y / 0.001f);
7c27cda03747 stretch the grid quadrilateral less, hopefully this makes fragments small enough even on integrated
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
51 float dy = fract(ex_uv.x / 0.001f);
54
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 */
72
7c27cda03747 stretch the grid quadrilateral less, hopefully this makes fragments small enough even on integrated
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
59 d *= (1 + cos(ex_uv.y / 0.0001f * pi)) * 0.5f;
7c27cda03747 stretch the grid quadrilateral less, hopefully this makes fragments small enough even on integrated
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
60 d *= (1 + cos(ex_uv.x / 0.0001f * pi)) * 0.5f;
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
61 color = vec4(gridColor.xyz, gridColor.w * d);
53
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 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
66
64
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 55
diff changeset
67 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
68 {
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 55
diff changeset
69 this->setMatrix("grid", newGridMatrix);
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 55
diff changeset
70 }
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 55
diff changeset
71
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
72 void GridProgram::setGridColor(const QColor& newGridColor)
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
73 {
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
74 const glm::vec4 vec = gl::colorToVector4(newGridColor);
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
75 if (this->isInitialized)
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
76 {
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
77 this->program->bind();
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
78 this->program->setUniformVector("gridColor", vec);
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
79 this->program->release();
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
80 this->checkForGLErrors();
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 else
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
83 {
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
84 this->gridColor = vec;
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
85 }
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
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
88 const char* GridProgram::vertexShaderSource() const
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
89 {
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
90 return ::vertexShaderSource;
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
91 }
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
92
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
93 const char* GridProgram::fragmentShaderSource() const
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
94 {
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
95 return ::fragmentShaderSource;
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 void* GridProgram::vertexData() const
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
99 {
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
100 return ::data;
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
101 }
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
102
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
103 int GridProgram::vertexSize() const
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
104 {
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
105 return sizeof data[0];
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
106 }
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
107
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
108 int GridProgram::vertexCount() const
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
109 {
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
110 return countof(data);
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
111 }
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
112
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
113 void GridProgram::setupVertexArrays()
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
114 {
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
115 this->program->enableAttributeArray(0);
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
116 this->program->setAttributeBuffer(0, GL_FLOAT, 0, 2, 0);
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
117 this->program->setUniformVector("gridColor", this->gridColor);
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
118 }
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
119
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
120 GLenum GridProgram::drawMode() const
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
121 {
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
122 return GL_QUADS;
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
123 }

mercurial