Mon, 11 May 2020 12:18:59 +0300
object editing
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 | |
20 | #include <QColor> | |
53 | 21 | #include <QOpenGLBuffer> |
19 | 22 | #include <QOpenGLFunctions> |
53 | 23 | #include <QOpenGLShader> |
24 | #include <QOpenGLShaderProgram> | |
25 | #include <QOpenGLVertexArrayObject> | |
26 | #include <glm/gtc/type_ptr.hpp> | |
19 | 27 | #include "basics.h" |
28 | #include "colors.h" | |
29 | ||
30 | namespace gl | |
31 | { | |
32 | struct Polygon; | |
53 | 33 | class ShaderProgram; |
34 | ||
35 | void buildShaders( | |
36 | QOpenGLShaderProgram* shaderProgram, | |
37 | const char* vertexShaderSource, | |
38 | const char* fragmentShaderSource); | |
39 | void checkForGLErrors(QWidget* parent); | |
55 | 40 | inline glm::vec3 colorToVector3(const QColor& color) |
41 | { | |
42 | return {toFloat(color.redF()), toFloat(color.greenF()), toFloat(color.blueF())}; | |
43 | } | |
44 | inline glm::vec4 colorToVector4(const QColor& color) | |
45 | { | |
46 | return {gl::colorToVector3(color), toFloat(color.alphaF())}; | |
47 | } | |
19 | 48 | } |
49 | ||
53 | 50 | class gl::ShaderProgram : public QOpenGLShaderProgram |
51 | { | |
52 | public: | |
53 | using QOpenGLShaderProgram::QOpenGLShaderProgram; | |
54 | // for some reason I cannot overload setUniformValue properly with this template thing | |
55 | template<typename Float, glm::qualifier Prec> | |
56 | void setUniformMatrix(const char* uniformName, const glm::mat<4, 4, Float, Prec>& value) | |
57 | { | |
58 | const float (*array)[4][4] = reinterpret_cast<const float(*)[4][4]>(glm::value_ptr(value)); | |
59 | this->setUniformValue(uniformName, *array); | |
60 | } | |
55 | 61 | template<typename Float, glm::qualifier Prec> |
62 | void setUniformVector(const char* uniformName, const glm::vec<4, Float, Prec>& value) | |
53 | 63 | { |
55 | 64 | this->setUniformValue(uniformName, value.x, value.y, value.z, value.w); |
53 | 65 | } |
66 | }; | |
67 | ||
19 | 68 | struct gl::Polygon |
69 | { | |
70 | enum Type : qint8 | |
71 | { | |
72 | EdgeLine, | |
73 | Triangle, | |
74 | Quadrilateral, | |
75 | ConditionalEdge | |
80 | 76 | } type; |
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
77 | 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
|
78 | ldraw::Color color; |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
79 | ldraw::id_t id; |
19 | 80 | |
81 | /** | |
82 | * @return amount of vertices used for geometry | |
83 | */ | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
84 | inline unsigned int numPolygonVertices() const |
19 | 85 | { |
86 | if (type == Type::ConditionalEdge) | |
87 | return 2; | |
88 | else | |
89 | return numVertices(); | |
90 | } | |
91 | ||
92 | /** | |
93 | * @return amount of vertices | |
94 | */ | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
95 | inline unsigned int numVertices() const |
19 | 96 | { |
97 | switch (type) | |
98 | { | |
99 | case Type::EdgeLine: | |
100 | return 2; | |
101 | case Type::Triangle: | |
102 | return 3; | |
103 | case Type::ConditionalEdge: | |
104 | case Type::Quadrilateral: | |
105 | return 4; | |
106 | } | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
107 | return 0; |
19 | 108 | } |
109 | }; | |
110 | ||
111 | Q_DECLARE_METATYPE(gl::Polygon) | |
112 | ||
113 | namespace gl | |
114 | { | |
80 | 115 | constexpr Polygon::Type POLYGON_TYPES[] = |
116 | { | |
117 | Polygon::Type::EdgeLine, | |
118 | Polygon::Type::Triangle, | |
119 | Polygon::Type::Quadrilateral, | |
120 | Polygon::Type::ConditionalEdge | |
121 | }; | |
122 | ||
123 | constexpr int NUM_POLYGON_TYPES = countof(POLYGON_TYPES); | |
124 | ||
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
125 | inline Polygon edgeLine(const glm::vec3& v_1, const glm::vec3& v_2, ldraw::Color color, ldraw::id_t id) |
19 | 126 | { |
21 | 127 | return {Polygon::EdgeLine, {v_1, v_2}, color, id}; |
19 | 128 | } |
129 | ||
21 | 130 | inline Polygon triangle( |
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
131 | const glm::vec3& v_1, |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
132 | const glm::vec3& v_2, |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
133 | 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
|
134 | ldraw::Color color, |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
135 | ldraw::id_t id) |
19 | 136 | { |
21 | 137 | return {Polygon::Triangle, {v_1, v_2, v_3}, color, id}; |
19 | 138 | } |
139 | ||
140 | inline Polygon quadrilateral( | |
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
141 | const glm::vec3& v_1, |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
142 | const glm::vec3& v_2, |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
143 | const glm::vec3& v_3, |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
144 | 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
|
145 | ldraw::Color color, |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
146 | ldraw::id_t id) |
19 | 147 | { |
21 | 148 | return {Polygon::Quadrilateral, {v_1, v_2, v_3, v_4}, color, id}; |
19 | 149 | } |
150 | ||
151 | inline Polygon conditionalEdge( | |
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
152 | const glm::vec3& v_1, |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
153 | const glm::vec3& v_2, |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
154 | const glm::vec3& control_1, |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
155 | 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
|
156 | ldraw::Color color, |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
157 | ldraw::id_t id) |
19 | 158 | { |
21 | 159 | return {Polygon::ConditionalEdge, {v_1, v_2, control_1, control_2}, color, id}; |
19 | 160 | } |
161 | ||
162 | // Vbo names | |
26 | 163 | enum class ArrayClass : std::uint8_t |
19 | 164 | { |
165 | Lines, | |
166 | Triangles, | |
167 | Quads, | |
168 | ConditionalLines | |
169 | }; | |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
34
diff
changeset
|
170 | |
19 | 171 | enum class RenderStyle |
172 | { | |
173 | Normal, | |
174 | Wireframe, | |
175 | BfcRedGreen, | |
46 | 176 | RandomColors, |
177 | PickScene | |
19 | 178 | }; |
37 | 179 | |
180 | // These are also defined in shaders | |
181 | enum class FragmentStyle | |
182 | { | |
183 | Normal = 0, | |
184 | BfcGreen = 1, | |
185 | BfcRed = 2, | |
186 | RandomColors = 3, | |
46 | 187 | Id = 4, |
37 | 188 | }; |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
189 | |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
190 | // User options for rendering |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
191 | struct RenderPreferences |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
192 | { |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
193 | gl::RenderStyle style = gl::RenderStyle::Normal; |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
194 | QColor mainColor{255, 255, 64}; |
40
30cb5e836736
added configurable background color
Teemu Piippo <teemu@hecknology.net>
parents:
39
diff
changeset
|
195 | QColor backgroundColor{48, 48, 48}; |
48
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
46
diff
changeset
|
196 | QColor selectedColor{32, 32, 255}; |
44
c6114b3af3a6
added configurable line thickness
Teemu Piippo <teemu@hecknology.net>
parents:
41
diff
changeset
|
197 | GLfloat lineThickness = 2.0f; |
45
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
198 | bool lineAntiAliasing = true; |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
199 | }; |
19 | 200 | } |