Fri, 01 Jul 2022 16:46:43 +0300
Fix right click to delete not really working properly
Instead of removing the point that had been added, it would remove
the point that is being drawn, which would cause it to overwrite the
previous point using the new point, causing a bit of a delay
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 | |
291
42b4953dff85
Let's bring GLEW back after all
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
290
diff
changeset
|
20 | #include <GL/glew.h> |
100 | 21 | #include <QWidget> |
19 | 22 | #include <QColor> |
53 | 23 | #include <QOpenGLBuffer> |
24 | #include <QOpenGLShader> | |
25 | #include <QOpenGLShaderProgram> | |
26 | #include <QOpenGLVertexArrayObject> | |
27 | #include <glm/gtc/type_ptr.hpp> | |
264
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
28 | #include "src/basics.h" |
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
29 | #include "src/colors.h" |
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
30 | #include "src/model.h" |
19 | 31 | |
215
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
210
diff
changeset
|
32 | class RenderLayer |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
210
diff
changeset
|
33 | { |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
34 | protected: |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
35 | class PartRenderer* renderer; |
215
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
210
diff
changeset
|
36 | public: |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
37 | virtual ~RenderLayer(){} |
215
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
210
diff
changeset
|
38 | virtual void initializeGL(){} |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
210
diff
changeset
|
39 | virtual void paintGL(){} |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
210
diff
changeset
|
40 | virtual void overpaint(QPainter*){} |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
210
diff
changeset
|
41 | virtual void mvpMatrixChanged(const glm::mat4& mvpMatrix) = 0; |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
42 | virtual void mouseMoved(const QMouseEvent*){} |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
43 | virtual void mouseClick(const QMouseEvent*){} |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
245
diff
changeset
|
44 | void setRendererPointer(class PartRenderer* newRenderer) |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
45 | { |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
245
diff
changeset
|
46 | this->renderer = newRenderer; |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
47 | } |
215
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
210
diff
changeset
|
48 | }; |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
210
diff
changeset
|
49 | |
19 | 50 | namespace gl |
51 | { | |
53 | 52 | class ShaderProgram; |
53 | ||
54 | void buildShaders( | |
55 | QOpenGLShaderProgram* shaderProgram, | |
56 | const char* vertexShaderSource, | |
57 | const char* fragmentShaderSource); | |
58 | void checkForGLErrors(QWidget* parent); | |
55 | 59 | inline glm::vec3 colorToVector3(const QColor& color) |
60 | { | |
206
654661eab7f3
More refactor, merged main.h, basics.h and utility.h into one header file basics.h and removed plenty of unused code
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
61 | return {float_cast(color.redF()), float_cast(color.greenF()), float_cast(color.blueF())}; |
55 | 62 | } |
63 | inline glm::vec4 colorToVector4(const QColor& color) | |
64 | { | |
206
654661eab7f3
More refactor, merged main.h, basics.h and utility.h into one header file basics.h and removed plenty of unused code
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
65 | return {gl::colorToVector3(color), float_cast(color.alphaF())}; |
55 | 66 | } |
19 | 67 | } |
68 | ||
53 | 69 | class gl::ShaderProgram : public QOpenGLShaderProgram |
70 | { | |
71 | public: | |
72 | using QOpenGLShaderProgram::QOpenGLShaderProgram; | |
73 | // for some reason I cannot overload setUniformValue properly with this template thing | |
74 | template<typename Float, glm::qualifier Prec> | |
75 | void setUniformMatrix(const char* uniformName, const glm::mat<4, 4, Float, Prec>& value) | |
76 | { | |
77 | const float (*array)[4][4] = reinterpret_cast<const float(*)[4][4]>(glm::value_ptr(value)); | |
78 | this->setUniformValue(uniformName, *array); | |
79 | } | |
55 | 80 | template<typename Float, glm::qualifier Prec> |
81 | void setUniformVector(const char* uniformName, const glm::vec<4, Float, Prec>& value) | |
53 | 82 | { |
55 | 83 | this->setUniformValue(uniformName, value.x, value.y, value.z, value.w); |
53 | 84 | } |
85 | }; | |
86 | ||
19 | 87 | namespace gl |
88 | { | |
91 | 89 | // Different ways to render the scene |
19 | 90 | enum class RenderStyle |
91 | { | |
91 | 92 | // Normal rendering style |
19 | 93 | Normal, |
91 | 94 | // Use green colour for front faces and red colour for back faces |
19 | 95 | BfcRedGreen, |
91 | 96 | // Use a different colour for each object |
46 | 97 | RandomColors, |
91 | 98 | // Render so that the colour of an object has an one to one correspondence with its id |
119 | 99 | PickScene, |
100 | // Render a scene where vertices can be picked | |
101 | VertexPickScene, | |
19 | 102 | }; |
91 | 103 | |
104 | // Different ways to render fragments. | |
37 | 105 | // These are also defined in shaders |
106 | enum class FragmentStyle | |
107 | { | |
91 | 108 | // Use normal colours |
37 | 109 | Normal = 0, |
91 | 110 | // Use a distinctive green colour for BFC red/green view |
37 | 111 | BfcGreen = 1, |
91 | 112 | // Use a distinctive red colour for BFC red/green view |
37 | 113 | BfcRed = 2, |
91 | 114 | // Use a colour based on the object to distinguish objects |
37 | 115 | RandomColors = 3, |
91 | 116 | // Use a colour that codes the object's id |
46 | 117 | Id = 4, |
119 | 118 | // Render everything black |
119 | Black = 5, | |
37 | 120 | }; |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
121 | |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
122 | // User options for rendering |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
123 | struct RenderPreferences |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
124 | { |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
125 | gl::RenderStyle style = gl::RenderStyle::Normal; |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
126 | QColor mainColor{255, 255, 64}; |
40
30cb5e836736
added configurable background color
Teemu Piippo <teemu@hecknology.net>
parents:
39
diff
changeset
|
127 | QColor backgroundColor{48, 48, 48}; |
48
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
46
diff
changeset
|
128 | QColor selectedColor{32, 32, 255}; |
44
c6114b3af3a6
added configurable line thickness
Teemu Piippo <teemu@hecknology.net>
parents:
41
diff
changeset
|
129 | GLfloat lineThickness = 2.0f; |
45
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
130 | bool lineAntiAliasing = true; |
170
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
131 | bool drawAxes = true; |
231
a9bf6bab5ea2
Add wireframe button
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
132 | bool wireframe = false; |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
133 | }; |
19 | 134 | } |
218
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
217
diff
changeset
|
135 | |
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
217
diff
changeset
|
136 | Q_DECLARE_METATYPE(gl::RenderStyle) |
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
217
diff
changeset
|
137 | |
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
217
diff
changeset
|
138 | inline QDataStream &operator<<(QDataStream& stream, const gl::RenderStyle style) |
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
217
diff
changeset
|
139 | { |
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
217
diff
changeset
|
140 | return stream << enum_value_cast(style); |
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
217
diff
changeset
|
141 | } |
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
217
diff
changeset
|
142 | |
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
217
diff
changeset
|
143 | inline QDataStream &operator>>(QDataStream& stream, gl::RenderStyle& style) |
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
217
diff
changeset
|
144 | { |
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
217
diff
changeset
|
145 | return stream >> enum_value_cast(style); |
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
217
diff
changeset
|
146 | } |
245
a41ccc6924e3
improve text rendering
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
147 | |
290
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
148 | constexpr glm::vec3 idToColor(std::int32_t id) |
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
149 | { |
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
150 | const int r = (id & 0x0000ff) >> 0; |
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
151 | const int g = (id & 0x00ff00) >> 8; |
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
152 | const int b = (id & 0xff0000) >> 16; |
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
153 | return { |
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
154 | static_cast<float>(r) / 255.0f, |
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
155 | static_cast<float>(g) / 255.0f, |
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
156 | static_cast<float>(b) / 255.0f, |
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
157 | }; |
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
158 | } |
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
159 | |
245
a41ccc6924e3
improve text rendering
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
160 | #include <QPainter> |
a41ccc6924e3
improve text rendering
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
161 | #include <QPainterPath> |
a41ccc6924e3
improve text rendering
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
162 | inline void drawBorderedText(QPainter* painter, const QPointF& point, const QFont& font, const QString& text) |
a41ccc6924e3
improve text rendering
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
163 | { |
a41ccc6924e3
improve text rendering
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
164 | QPainterPath path; |
a41ccc6924e3
improve text rendering
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
165 | path.addText(point, font, text); |
a41ccc6924e3
improve text rendering
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
166 | painter->save(); |
a41ccc6924e3
improve text rendering
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
167 | painter->setBrush(Qt::white); |
a41ccc6924e3
improve text rendering
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
168 | painter->setPen({Qt::black, 0.1 * font.pointSizeF()}); |
a41ccc6924e3
improve text rendering
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
169 | painter->drawPath(path); |
a41ccc6924e3
improve text rendering
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
170 | painter->restore(); |
a41ccc6924e3
improve text rendering
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
171 | } |