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