Wed, 25 May 2022 18:33:38 +0300
fix up things and remove unnecessary code
19 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 - 2018 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 | #pragma once | |
100 | 20 | #include <QWidget> |
19 | 21 | #include <QColor> |
53 | 22 | #include <QOpenGLBuffer> |
19 | 23 | #include <QOpenGLFunctions> |
53 | 24 | #include <QOpenGLShader> |
25 | #include <QOpenGLShaderProgram> | |
26 | #include <QOpenGLVertexArrayObject> | |
27 | #include <glm/gtc/type_ptr.hpp> | |
19 | 28 | #include "basics.h" |
29 | #include "colors.h" | |
30 | ||
31 | namespace gl | |
32 | { | |
33 | struct Polygon; | |
53 | 34 | class ShaderProgram; |
35 | ||
36 | void buildShaders( | |
37 | QOpenGLShaderProgram* shaderProgram, | |
38 | const char* vertexShaderSource, | |
39 | const char* fragmentShaderSource); | |
40 | void checkForGLErrors(QWidget* parent); | |
55 | 41 | inline glm::vec3 colorToVector3(const QColor& color) |
42 | { | |
43 | return {toFloat(color.redF()), toFloat(color.greenF()), toFloat(color.blueF())}; | |
44 | } | |
45 | inline glm::vec4 colorToVector4(const QColor& color) | |
46 | { | |
47 | return {gl::colorToVector3(color), toFloat(color.alphaF())}; | |
48 | } | |
19 | 49 | } |
50 | ||
53 | 51 | class gl::ShaderProgram : public QOpenGLShaderProgram |
52 | { | |
53 | public: | |
54 | using QOpenGLShaderProgram::QOpenGLShaderProgram; | |
55 | // for some reason I cannot overload setUniformValue properly with this template thing | |
56 | template<typename Float, glm::qualifier Prec> | |
57 | void setUniformMatrix(const char* uniformName, const glm::mat<4, 4, Float, Prec>& value) | |
58 | { | |
59 | const float (*array)[4][4] = reinterpret_cast<const float(*)[4][4]>(glm::value_ptr(value)); | |
60 | this->setUniformValue(uniformName, *array); | |
61 | } | |
55 | 62 | template<typename Float, glm::qualifier Prec> |
63 | void setUniformVector(const char* uniformName, const glm::vec<4, Float, Prec>& value) | |
53 | 64 | { |
55 | 65 | this->setUniformValue(uniformName, value.x, value.y, value.z, value.w); |
53 | 66 | } |
67 | }; | |
68 | ||
19 | 69 | struct gl::Polygon |
70 | { | |
71 | enum Type : qint8 | |
72 | { | |
73 | EdgeLine, | |
74 | Triangle, | |
75 | Quadrilateral, | |
76 | ConditionalEdge | |
80 | 77 | } type; |
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
78 | glm::vec3 vertices[4]; |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
34
diff
changeset
|
79 | ldraw::Color color; |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
80 | ldraw::id_t id; |
19 | 81 | |
82 | /** | |
83 | * @return amount of vertices used for geometry | |
84 | */ | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
85 | inline unsigned int numPolygonVertices() const |
19 | 86 | { |
87 | if (type == Type::ConditionalEdge) | |
88 | return 2; | |
89 | else | |
90 | return numVertices(); | |
91 | } | |
92 | ||
93 | /** | |
94 | * @return amount of vertices | |
95 | */ | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
96 | inline unsigned int numVertices() const |
19 | 97 | { |
98 | switch (type) | |
99 | { | |
100 | case Type::EdgeLine: | |
101 | return 2; | |
102 | case Type::Triangle: | |
103 | return 3; | |
104 | case Type::ConditionalEdge: | |
105 | case Type::Quadrilateral: | |
106 | return 4; | |
107 | } | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
108 | return 0; |
19 | 109 | } |
110 | }; | |
111 | ||
112 | Q_DECLARE_METATYPE(gl::Polygon) | |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
170
diff
changeset
|
113 | extern QOpenGLFunctions glfunc; |
19 | 114 | |
115 | namespace gl | |
116 | { | |
80 | 117 | constexpr Polygon::Type POLYGON_TYPES[] = |
118 | { | |
119 | Polygon::Type::EdgeLine, | |
120 | Polygon::Type::Triangle, | |
121 | Polygon::Type::Quadrilateral, | |
122 | Polygon::Type::ConditionalEdge | |
123 | }; | |
124 | ||
125 | constexpr int NUM_POLYGON_TYPES = countof(POLYGON_TYPES); | |
126 | ||
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
127 | inline Polygon edgeLine(const glm::vec3& v_1, const glm::vec3& v_2, ldraw::Color color, ldraw::id_t id) |
19 | 128 | { |
21 | 129 | return {Polygon::EdgeLine, {v_1, v_2}, color, id}; |
19 | 130 | } |
131 | ||
21 | 132 | inline Polygon triangle( |
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
133 | const glm::vec3& v_1, |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
134 | const glm::vec3& v_2, |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
135 | const glm::vec3& v_3, |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
34
diff
changeset
|
136 | ldraw::Color color, |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
137 | ldraw::id_t id) |
19 | 138 | { |
21 | 139 | return {Polygon::Triangle, {v_1, v_2, v_3}, color, id}; |
19 | 140 | } |
141 | ||
142 | inline Polygon quadrilateral( | |
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
143 | const glm::vec3& v_1, |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
144 | const glm::vec3& v_2, |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
145 | const glm::vec3& v_3, |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
146 | const glm::vec3& v_4, |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
34
diff
changeset
|
147 | ldraw::Color color, |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
148 | ldraw::id_t id) |
19 | 149 | { |
21 | 150 | return {Polygon::Quadrilateral, {v_1, v_2, v_3, v_4}, color, id}; |
19 | 151 | } |
152 | ||
153 | inline Polygon conditionalEdge( | |
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
154 | const glm::vec3& v_1, |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
155 | const glm::vec3& v_2, |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
156 | const glm::vec3& control_1, |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
157 | const glm::vec3& control_2, |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
34
diff
changeset
|
158 | ldraw::Color color, |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
159 | ldraw::id_t id) |
19 | 160 | { |
21 | 161 | return {Polygon::ConditionalEdge, {v_1, v_2, control_1, control_2}, color, id}; |
19 | 162 | } |
163 | ||
164 | // Vbo names | |
26 | 165 | enum class ArrayClass : std::uint8_t |
19 | 166 | { |
167 | Lines, | |
168 | Triangles, | |
169 | Quads, | |
170 | ConditionalLines | |
171 | }; | |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
34
diff
changeset
|
172 | |
115 | 173 | constexpr ArrayClass ARRAY_CLASSES[] = { |
174 | ArrayClass::Lines, | |
175 | ArrayClass::Triangles, | |
176 | ArrayClass::Quads, | |
177 | ArrayClass::ConditionalLines, | |
178 | }; | |
26 | 179 | constexpr int NUM_ARRAY_CLASSES = countof(ARRAY_CLASSES); |
19 | 180 | |
91 | 181 | // Different ways to render the scene |
19 | 182 | enum class RenderStyle |
183 | { | |
91 | 184 | // Normal rendering style |
19 | 185 | Normal, |
91 | 186 | // Render all polygons as lines |
19 | 187 | Wireframe, |
91 | 188 | // Use green colour for front faces and red colour for back faces |
19 | 189 | BfcRedGreen, |
91 | 190 | // Use a different colour for each object |
46 | 191 | RandomColors, |
91 | 192 | // Render so that the colour of an object has an one to one correspondence with its id |
119 | 193 | PickScene, |
194 | // Render a scene where vertices can be picked | |
195 | VertexPickScene, | |
19 | 196 | }; |
91 | 197 | |
198 | // Different ways to render fragments. | |
37 | 199 | // These are also defined in shaders |
200 | enum class FragmentStyle | |
201 | { | |
91 | 202 | // Use normal colours |
37 | 203 | Normal = 0, |
91 | 204 | // Use a distinctive green colour for BFC red/green view |
37 | 205 | BfcGreen = 1, |
91 | 206 | // Use a distinctive red colour for BFC red/green view |
37 | 207 | BfcRed = 2, |
91 | 208 | // Use a colour based on the object to distinguish objects |
37 | 209 | RandomColors = 3, |
91 | 210 | // Use a colour that codes the object's id |
46 | 211 | Id = 4, |
119 | 212 | // Render everything black |
213 | Black = 5, | |
37 | 214 | }; |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
215 | |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
216 | // User options for rendering |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
217 | struct RenderPreferences |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
218 | { |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
219 | gl::RenderStyle style = gl::RenderStyle::Normal; |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
220 | QColor mainColor{255, 255, 64}; |
40
30cb5e836736
added configurable background color
Teemu Piippo <teemu@hecknology.net>
parents:
39
diff
changeset
|
221 | QColor backgroundColor{48, 48, 48}; |
48
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
46
diff
changeset
|
222 | QColor selectedColor{32, 32, 255}; |
44
c6114b3af3a6
added configurable line thickness
Teemu Piippo <teemu@hecknology.net>
parents:
41
diff
changeset
|
223 | GLfloat lineThickness = 2.0f; |
45
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
224 | bool lineAntiAliasing = true; |
170
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
225 | bool drawAxes = true; |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
226 | }; |
19 | 227 | } |