Mon, 04 Jul 2022 15:37:22 +0300
Only enable the text editor when having a model open
24 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 - 2020 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 | ||
291
42b4953dff85
Let's bring GLEW back after all
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
290
diff
changeset
|
19 | #include <GL/glew.h> |
28 | 20 | #include <glm/ext/matrix_transform.hpp> |
21 | #include <glm/ext/matrix_clip_space.hpp> | |
238
b8ad4c12d937
Fix pick scene rendering: we cannot use glReadPixels when using multisampling, so we need to render the pick scene to a separate frame buffer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
237
diff
changeset
|
22 | #include <QOpenGLFramebufferObject> |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
23 | #include <QPainter> |
100 | 24 | #include <GL/glu.h> |
17 | 25 | #include <QMouseEvent> |
26 | 26 | #include <QMessageBox> |
47 | 27 | #include <QAbstractButton> |
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/geometry.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/model.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/settings.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
|
31 | #include "src/gl/partrenderer.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
|
32 | #include "src/gl/compiler.h" |
17 | 33 | |
120
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
34 | static constexpr double MIN_ZOOM = -3.0; |
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
35 | static constexpr double MAX_ZOOM = 3.0; |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
250
diff
changeset
|
36 | |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
34
diff
changeset
|
37 | PartRenderer::PartRenderer( |
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
34
diff
changeset
|
38 | Model* model, |
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
34
diff
changeset
|
39 | DocumentManager* documents, |
205 | 40 | const ColorTable& colorTable, |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
34
diff
changeset
|
41 | QWidget* parent) : |
21 | 42 | QOpenGLWidget{parent}, |
43 | model{model}, | |
44 | documents{documents}, | |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
45 | colorTable{colorTable} |
17 | 46 | { |
47 | this->setMouseTracking(true); | |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
48 | this->setFocusPolicy(Qt::WheelFocus); |
234
87ee9824210b
Readd axis labels, fix antialiasing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
232
diff
changeset
|
49 | QSurfaceFormat surfaceFormat; |
87ee9824210b
Readd axis labels, fix antialiasing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
232
diff
changeset
|
50 | surfaceFormat.setSamples(8); |
87ee9824210b
Readd axis labels, fix antialiasing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
232
diff
changeset
|
51 | this->setFormat(surfaceFormat); |
329
6d75fa09cc0c
fix some issues like subfile references not showing up properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
313
diff
changeset
|
52 | const auto setNeedBuild = [&]{this->needBuild = true;}; |
6d75fa09cc0c
fix some issues like subfile references not showing up properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
313
diff
changeset
|
53 | connect(model, &Model::rowsInserted, setNeedBuild); |
6d75fa09cc0c
fix some issues like subfile references not showing up properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
313
diff
changeset
|
54 | connect(model, &Model::rowsRemoved, setNeedBuild); |
6d75fa09cc0c
fix some issues like subfile references not showing up properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
313
diff
changeset
|
55 | connect(model, &Model::dataChanged, setNeedBuild); |
6d75fa09cc0c
fix some issues like subfile references not showing up properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
313
diff
changeset
|
56 | connect(model, &Model::modelReset, setNeedBuild); |
215
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
57 | const auto updateLayerMvpMatrix = [this]{ |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
58 | const glm::mat4 newMvpMatrix = this->projectionMatrix * this->viewMatrix * this->modelMatrix; |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
59 | for (RenderLayer* layer : this->activeRenderLayers) { |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
60 | layer->mvpMatrixChanged(newMvpMatrix); |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
61 | } |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
62 | for (RenderLayer* layer : this->inactiveRenderLayers) { |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
63 | layer->mvpMatrixChanged(newMvpMatrix); |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
64 | } |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
65 | }; |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
66 | connect(this, &PartRenderer::modelMatrixChanged, updateLayerMvpMatrix); |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
67 | connect(this, &PartRenderer::viewMatrixChanged, updateLayerMvpMatrix); |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
68 | connect(this, &PartRenderer::projectionMatrixChanged, updateLayerMvpMatrix); |
17 | 69 | } |
70 | ||
26 | 71 | PartRenderer::~PartRenderer() |
72 | { | |
73 | } | |
74 | ||
17 | 75 | void PartRenderer::initializeGL() |
76 | { | |
291
42b4953dff85
Let's bring GLEW back after all
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
290
diff
changeset
|
77 | glewInit(); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
78 | gl::initializeModelShaders(&this->shaders); |
215
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
79 | for (RenderLayer* layer : this->activeRenderLayers) { |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
80 | layer->initializeGL(); |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
81 | } |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
82 | for (RenderLayer* layer : this->inactiveRenderLayers) { |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
83 | layer->initializeGL(); |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
84 | } |
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
85 | connect(this->model, &Model::dataChanged, this, &PartRenderer::build); |
17 | 86 | this->initialized = true; |
32 | 87 | this->modelQuaternion = glm::angleAxis(glm::radians(30.0f), glm::vec3{-1, 0, 0}); |
88 | this->modelQuaternion *= glm::angleAxis(glm::radians(225.0f), glm::vec3{-0, 1, 0}); | |
55 | 89 | this->updateModelMatrix(); |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
90 | this->updateViewMatrix(); |
30 | 91 | this->update(); |
17 | 92 | } |
93 | ||
94 | void PartRenderer::resizeGL(int width, int height) | |
95 | { | |
57
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
96 | this->viewportVector = {0, 0, width, height}; |
28 | 97 | glViewport(0, 0, width, height); |
98 | this->projectionMatrix = glm::perspective( | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
99 | glm::radians(45.0f), |
28 | 100 | static_cast<float>(width) / static_cast<float>(height), |
101 | 0.1f, | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
102 | 10000.f); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
103 | gl::setShaderUniformMatrix(&this->shaders, "projectionMatrix", this->projectionMatrix); |
112 | 104 | Q_EMIT projectionMatrixChanged(this->projectionMatrix); |
17 | 105 | } |
106 | ||
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
107 | static constexpr GLenum getGlTypeForArrayClass(const gl::ArrayClass vboClass) |
21 | 108 | { |
109 | switch (vboClass) | |
110 | { | |
26 | 111 | case gl::ArrayClass::Lines: |
112 | case gl::ArrayClass::ConditionalLines: | |
21 | 113 | return GL_LINES; |
26 | 114 | case gl::ArrayClass::Triangles: |
21 | 115 | return GL_TRIANGLES; |
26 | 116 | case gl::ArrayClass::Quads: |
21 | 117 | return GL_QUADS; |
118 | } | |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
119 | throw std::runtime_error{"bad value for vboClass"}; |
21 | 120 | } |
121 | ||
17 | 122 | void PartRenderer::paintGL() |
123 | { | |
47 | 124 | glEnable(GL_DEPTH_TEST); |
125 | glShadeModel(GL_SMOOTH); | |
126 | this->renderScene(); | |
215
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
127 | for (RenderLayer* layer : this->activeRenderLayers) { |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
128 | layer->paintGL(); |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
129 | } |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
130 | QPainter painter{this}; |
234
87ee9824210b
Readd axis labels, fix antialiasing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
232
diff
changeset
|
131 | painter.setRenderHint(QPainter::Antialiasing); |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
132 | for (RenderLayer* layer : this->activeRenderLayers) { |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
133 | layer->overpaint(&painter); |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
134 | } |
47 | 135 | } |
136 | ||
137 | void PartRenderer::renderScene() | |
138 | { | |
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
139 | if (this->needBuild) |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
140 | { |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
141 | gl::build(&this->shaders, this->model, this->colorTable, this->documents, this->renderPreferences); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
142 | this->boundingBox = gl::boundingBoxForModel(this->model, this->documents); |
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
143 | this->needBuild = false; |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
144 | } |
53 | 145 | this->checkForGLErrors(); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
146 | if (true |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
147 | and this->renderPreferences.lineAntiAliasing |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
148 | and this->renderPreferences.style != gl::RenderStyle::PickScene |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
149 | ) { |
45
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
150 | glEnable(GL_LINE_SMOOTH); |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
151 | glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
152 | } |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
153 | else { |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
154 | glDisable(GL_LINE_SMOOTH); |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
155 | } |
46 | 156 | if (this->renderPreferences.style != gl::RenderStyle::PickScene) |
157 | { | |
47 | 158 | const QColor& backgroundColor = this->renderPreferences.backgroundColor; |
46 | 159 | glClearColor( |
160 | static_cast<float>(backgroundColor.redF()), | |
161 | static_cast<float>(backgroundColor.greenF()), | |
162 | static_cast<float>(backgroundColor.blueF()), | |
163 | 1.0f); | |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
164 | gl::setShaderUniform(&this->shaders, "useLighting", GL_TRUE); |
46 | 165 | } |
166 | else | |
167 | { | |
290
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
168 | glClearColor(1.0f, 1.0f, 1.0f, 1.0f); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
169 | gl::setShaderUniform(&this->shaders, "useLighting", GL_FALSE); |
46 | 170 | } |
53 | 171 | this->checkForGLErrors(); |
290
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
172 | const QColor qs = this->renderPreferences.selectedColor; |
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
173 | const glm::vec4 selectedColor{qs.redF(), qs.greenF(), qs.blueF(), 1.0f}; |
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
174 | gl::setShaderUniformVector(&this->shaders, "selectedColor", selectedColor); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
175 | gl::setShaderUniform(&this->shaders, "highlighted", this->highlighted.value); |
51 | 176 | this->checkForGLErrors(); |
26 | 177 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
178 | glEnable(GL_DEPTH_TEST); | |
179 | glEnable(GL_POLYGON_OFFSET_FILL); | |
180 | glPolygonOffset(1.0f, 1.0f); | |
44
c6114b3af3a6
added configurable line thickness
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
181 | glLineWidth(this->renderPreferences.lineThickness); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
182 | const auto renderAllArrays = [this](){ |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
183 | // Lines need to be rendered last so that anti-aliasing does not interfere with polygon rendering. |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
184 | this->renderVao(gl::ArrayClass::Triangles); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
185 | this->renderVao(gl::ArrayClass::Quads); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
186 | this->renderVao(gl::ArrayClass::Lines); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
187 | }; |
231
a9bf6bab5ea2
Add wireframe button
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
217
diff
changeset
|
188 | if (this->renderPreferences.wireframe and this->renderPreferences.style != gl::RenderStyle::PickScene) { |
a9bf6bab5ea2
Add wireframe button
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
217
diff
changeset
|
189 | glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); |
a9bf6bab5ea2
Add wireframe button
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
217
diff
changeset
|
190 | } |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
191 | switch (this->renderPreferences.style) |
18 | 192 | { |
193 | case gl::RenderStyle::Normal: | |
37 | 194 | this->setFragmentStyle(gl::FragmentStyle::Normal); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
195 | renderAllArrays(); |
37 | 196 | break; |
18 | 197 | case gl::RenderStyle::BfcRedGreen: |
37 | 198 | glEnable(GL_CULL_FACE); |
199 | glCullFace(GL_BACK); | |
200 | this->setFragmentStyle(gl::FragmentStyle::BfcGreen); | |
201 | renderVao(gl::ArrayClass::Triangles); | |
202 | renderVao(gl::ArrayClass::Quads); | |
203 | glCullFace(GL_FRONT); | |
204 | this->setFragmentStyle(gl::FragmentStyle::BfcRed); | |
205 | renderVao(gl::ArrayClass::Triangles); | |
206 | renderVao(gl::ArrayClass::Quads); | |
207 | glDisable(GL_CULL_FACE); | |
208 | this->setFragmentStyle(gl::FragmentStyle::Normal); | |
209 | renderVao(gl::ArrayClass::Lines); | |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
210 | break; |
18 | 211 | case gl::RenderStyle::RandomColors: |
37 | 212 | this->setFragmentStyle(gl::FragmentStyle::RandomColors); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
213 | renderAllArrays(); |
18 | 214 | break; |
46 | 215 | case gl::RenderStyle::PickScene: |
216 | glLineWidth(3.0f); | |
217 | this->setFragmentStyle(gl::FragmentStyle::Id); | |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
218 | renderAllArrays(); |
46 | 219 | break; |
119 | 220 | case gl::RenderStyle::VertexPickScene: |
221 | glLineWidth(1.0f); | |
222 | this->setFragmentStyle(gl::FragmentStyle::Black); | |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
223 | renderAllArrays(); |
119 | 224 | break; |
18 | 225 | } |
37 | 226 | glDisable(GL_POLYGON_OFFSET_FILL); |
231
a9bf6bab5ea2
Add wireframe button
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
217
diff
changeset
|
227 | glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); |
37 | 228 | } |
229 | ||
230 | ||
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
231 | void PartRenderer::updateViewMatrix() |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
232 | { |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
233 | // I'm not quite sure why using the exponent function on the zoom factor causes linear zoom behavior |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
234 | const float modelDistance = longestMeasure(this->boundingBox); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
235 | const double z = 2.0 * std::exp(this->zoom) * (1 + static_cast<double>(modelDistance)); |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
236 | this->viewMatrix = glm::lookAt(glm::vec3{0, 0, z}, {0, 0, 0}, {0, -1, 0}); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
237 | gl::setShaderUniformMatrix(&this->shaders, "viewMatrix", this->viewMatrix); |
112 | 238 | Q_EMIT this->viewMatrixChanged(this->viewMatrix); |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
239 | } |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
240 | |
55 | 241 | void PartRenderer::updateModelMatrix() |
242 | { | |
61
4585d8d7a7ec
moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents:
60
diff
changeset
|
243 | this->modelMatrix = glm::mat4_cast(this->modelQuaternion); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
244 | gl::setShaderUniformMatrix(&this->shaders, "modelMatrix", modelMatrix); |
112 | 245 | Q_EMIT this->modelMatrixChanged(this->modelMatrix); |
55 | 246 | this->update(); |
247 | } | |
248 | ||
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
249 | void PartRenderer::build() |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
250 | { |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
251 | this->needBuild = true; |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
252 | } |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
253 | |
27
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
254 | void PartRenderer::renderVao(const gl::ArrayClass arrayClass) |
26 | 255 | { |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
256 | gl::bindModelShaderVertexArray(&this->shaders, arrayClass); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
257 | const std::size_t vertexCount = gl::vertexCount(&this->shaders, arrayClass); |
51 | 258 | this->checkForGLErrors(); |
26 | 259 | glDrawArrays(getGlTypeForArrayClass(arrayClass), 0, static_cast<GLsizei>(vertexCount)); |
51 | 260 | this->checkForGLErrors(); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
261 | gl::releaseModelShaderVertexArray(&this->shaders, arrayClass); |
26 | 262 | this->checkForGLErrors(); |
263 | } | |
264 | ||
265 | void PartRenderer::checkForGLErrors() | |
266 | { | |
53 | 267 | gl::checkForGLErrors(this); |
268 | } | |
269 | ||
270 | void gl::checkForGLErrors(QWidget* parent) | |
271 | { | |
26 | 272 | GLenum glError; |
273 | QStringList errors; | |
274 | while ((glError = glGetError()) != GL_NO_ERROR) | |
21 | 275 | { |
100 | 276 | const QString glErrorString = QString::fromLatin1(reinterpret_cast<const char*>(gluErrorString(glError))); |
26 | 277 | errors.append(glErrorString); |
278 | } | |
279 | if (not errors.isEmpty()) | |
280 | { | |
53 | 281 | QMessageBox box{parent}; |
47 | 282 | box.setIcon(QMessageBox::Critical); |
53 | 283 | box.setText(QObject::tr("OpenGL error: %1").arg(errors.join("\n"))); |
284 | box.setWindowTitle(QObject::tr("OpenGL error")); | |
47 | 285 | box.setStandardButtons(QMessageBox::Close); |
53 | 286 | box.button(QMessageBox::Close)->setText(QObject::tr("Damn it")); |
47 | 287 | box.exec(); |
21 | 288 | } |
17 | 289 | } |
290 | ||
291 | void PartRenderer::mouseMoveEvent(QMouseEvent* event) | |
292 | { | |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
293 | if (not this->frozen) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
294 | const bool left = event->buttons() & Qt::LeftButton; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
295 | const QPoint move = event->pos() - this->lastMousePosition; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
296 | this->totalMouseMove += move.manhattanLength(); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
297 | if (left and not move.isNull()) |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
298 | { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
299 | // q_x is the rotation of the brick along the vertical y-axis, because turning the |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
300 | // vertical axis causes horizontal (=x) rotation. Likewise q_y is the rotation of the |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
301 | // brick along the horizontal x-axis, which causes vertical rotation. |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
302 | const auto scalar = 0.006f; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
303 | const float move_x = static_cast<float>(move.x()); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
304 | const float move_y = static_cast<float>(move.y()); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
305 | const glm::quat q_x = glm::angleAxis(scalar * move_x, glm::vec3{0, -1, 0}); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
306 | const glm::quat q_y = glm::angleAxis(scalar * move_y, glm::vec3{-1, 0, 0}); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
307 | this->modelQuaternion = q_x * q_y * this->modelQuaternion; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
308 | this->updateModelMatrix(); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
309 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
310 | this->lastMousePosition = event->pos(); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
311 | for (RenderLayer* layer : this->activeRenderLayers) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
312 | layer->mouseMoved(event); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
313 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
314 | this->update(); |
17 | 315 | } |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
316 | } |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
317 | |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
318 | void PartRenderer::mousePressEvent(QMouseEvent* event) |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
319 | { |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
320 | if (not this->frozen) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
321 | this->totalMouseMove = 0; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
322 | this->lastMousePosition = event->pos(); |
313
c24d87f64bed
Improve click handling
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
311
diff
changeset
|
323 | this->lastClickTime = steady_clock::now(); |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
324 | } |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
325 | } |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
326 | |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
327 | void PartRenderer::mouseReleaseEvent(QMouseEvent* event) |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
328 | { |
313
c24d87f64bed
Improve click handling
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
311
diff
changeset
|
329 | using namespace std::chrono_literals; |
c24d87f64bed
Improve click handling
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
311
diff
changeset
|
330 | if (true |
c24d87f64bed
Improve click handling
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
311
diff
changeset
|
331 | and not frozen |
c24d87f64bed
Improve click handling
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
311
diff
changeset
|
332 | and this->totalMouseMove < 50.0 / sqrt(2) |
c24d87f64bed
Improve click handling
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
311
diff
changeset
|
333 | and (steady_clock::now() - this->lastClickTime) < 0.5s |
c24d87f64bed
Improve click handling
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
311
diff
changeset
|
334 | ) { |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
335 | for (RenderLayer* layer : this->activeRenderLayers) { |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
336 | layer->mouseClick(event); |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
337 | } |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
338 | this->update(); |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
339 | } |
17 | 340 | } |
18 | 341 | |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
342 | void PartRenderer::keyReleaseEvent(QKeyEvent* event) |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
343 | { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
344 | if (event->key() == Qt::Key_Pause) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
345 | this->frozen = not this->frozen; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
346 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
347 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
348 | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
349 | void PartRenderer::wheelEvent(QWheelEvent* event) |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
350 | { |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
351 | if (not this->frozen) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
352 | static constexpr double WHEEL_STEP = 1 / 1000.0; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
353 | const double move = (-event->angleDelta().y()) * WHEEL_STEP; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
354 | this->zoom = std::clamp(this->zoom + move, MIN_ZOOM, MAX_ZOOM); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
355 | this->updateViewMatrix(); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
356 | this->update(); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
357 | } |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
358 | } |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
359 | |
215
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
360 | void PartRenderer::addRenderLayer(RenderLayer* layer) |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
361 | { |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
362 | this->activeRenderLayers.push_back(layer); |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
363 | layer->setRendererPointer(this); |
215
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
364 | this->update(); |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
365 | } |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
366 | |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
367 | void PartRenderer::setLayerEnabled(RenderLayer* layer, bool enabled) |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
368 | { |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
369 | auto& from = enabled ? this->inactiveRenderLayers : this->activeRenderLayers; |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
370 | auto& to = enabled ? this->activeRenderLayers : this->inactiveRenderLayers; |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
371 | auto it = std::find(from.begin(), from.end(), layer); |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
372 | if (it != from.end()) { |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
373 | from.erase(it); |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
374 | to.push_back(layer); |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
375 | } |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
376 | } |
34c6e7bc4ee1
Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
377 | |
58
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
378 | /** |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
379 | * @brief Converts the specified on the screen into the 3D world. The point is unprojected twice into 3D and the |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
380 | * intersection of the resulting line with the specified plane is returned. If the intersection point lies behind |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
381 | * the camera, no value is returned. |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
382 | * @param point 2D window co-ordinates to convert. |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
383 | * @param plane Plane to raycast against |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
384 | * @return world co-ordinates, or no value if the point is behind the camera. |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
385 | */ |
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:
205
diff
changeset
|
386 | std::optional<glm::vec3> PartRenderer::screenToModelCoordinates(const QPointF& point, const Plane& plane) const |
55 | 387 | { |
201
5d201ee4a9c3
Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
388 | const Line line = this->cameraLine(point); |
58
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
389 | std::optional<glm::vec3> result; |
201
5d201ee4a9c3
Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
390 | result = linePlaneIntersection(line, plane, 0.01f); |
59
60650929dc82
fixed testing of whether screenToModelCoordinates's result value is behind the camera
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
391 | // If the point lies behind the camera, do not return a result. |
66
77c819262b7a
added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
392 | if (result.has_value() and glm::dot(line.direction, *result - line.anchor) < 0) |
58
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
393 | { |
59
60650929dc82
fixed testing of whether screenToModelCoordinates's result value is behind the camera
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
394 | result.reset(); |
58
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
395 | } |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
396 | return result; |
55 | 397 | } |
398 | ||
58
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
399 | /** |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
400 | * @brief Converts the specified point to 2D window coordinates, with Y-coordinate inverted for Qt |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
401 | * @param point Point to unproject |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
402 | * @return screen coordinates |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
403 | */ |
66
77c819262b7a
added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
404 | QPointF PartRenderer::modelToScreenCoordinates(const glm::vec3& point) const |
55 | 405 | { |
58
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
406 | const glm::vec3 projected = glm::project( |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
407 | point, |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
408 | this->viewMatrix * this->modelMatrix, |
58
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
409 | this->projectionMatrix, |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
410 | this->viewportVector); |
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:
246
diff
changeset
|
411 | return toQPointF(glm::vec2{projected.x, static_cast<float>(this->height()) - projected.y}); |
55 | 412 | } |
413 | ||
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
414 | bool PartRenderer::isDark() const |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
415 | { |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
416 | return luma(this->renderPreferences.backgroundColor) < 0.25; |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
417 | } |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
418 | |
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:
205
diff
changeset
|
419 | Line<3> PartRenderer::cameraLine(const QPointF& point) const |
66
77c819262b7a
added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
420 | { |
77c819262b7a
added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
421 | const glm::vec3 p1 = this->unproject({point.x(), point.y(), 0}); |
77c819262b7a
added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
422 | const glm::vec3 p2 = this->unproject({point.x(), point.y(), 1}); |
201
5d201ee4a9c3
Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
423 | return lineFromPoints(p1, p2); |
66
77c819262b7a
added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
424 | } |
77c819262b7a
added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
425 | |
58
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
426 | /** |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
427 | * @brief Unprojects the specified window coordinates to model coordinates |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
428 | * @param win Window coordinates to project. Z-coordinate indicates depth |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
429 | * @return model coordinates |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
430 | */ |
66
77c819262b7a
added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
431 | glm::vec3 PartRenderer::unproject(const glm::vec3& win) const |
57
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
432 | { |
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
433 | return glm::unProject( |
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:
246
diff
changeset
|
434 | glm::vec3{win.x, static_cast<float>(this->height()) - win.y, win.z}, |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
435 | this->viewMatrix * this->modelMatrix, |
57
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
436 | this->projectionMatrix, |
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
437 | viewportVector); |
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
438 | } |
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
439 | |
309
d862721d19a3
Fixed ModelId being used to identify both models and elements, added ElementId to identify elements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
291
diff
changeset
|
440 | ElementId PartRenderer::pick(QPoint where) |
47 | 441 | { |
199
6988973515d2
Fix pick() picking from weird places on the screen with high DPI scaling
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
442 | // y is flipped, take that into account |
6988973515d2
Fix pick() picking from weird places on the screen with high DPI scaling
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
443 | where.setY(this->height() - where.y()); |
6988973515d2
Fix pick() picking from weird places on the screen with high DPI scaling
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
444 | // Since we are dealing with pixel data right from the framebuffer, its size |
6988973515d2
Fix pick() picking from weird places on the screen with high DPI scaling
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
445 | // will be affected by High DPI scaling. We need to take this into account |
6988973515d2
Fix pick() picking from weird places on the screen with high DPI scaling
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
446 | // and multiply the pixel positions by the screen pixel scaling factor. |
6988973515d2
Fix pick() picking from weird places on the screen with high DPI scaling
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
447 | where *= this->devicePixelRatioF(); |
47 | 448 | const gl::RenderStyle oldRenderStyle = this->renderPreferences.style; |
449 | this->renderPreferences.style = gl::RenderStyle::PickScene; | |
450 | this->makeCurrent(); | |
238
b8ad4c12d937
Fix pick scene rendering: we cannot use glReadPixels when using multisampling, so we need to render the pick scene to a separate frame buffer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
237
diff
changeset
|
451 | QOpenGLFramebufferObject fbo{this->width(), this->height(), QOpenGLFramebufferObject::CombinedDepthStencil}; |
b8ad4c12d937
Fix pick scene rendering: we cannot use glReadPixels when using multisampling, so we need to render the pick scene to a separate frame buffer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
237
diff
changeset
|
452 | fbo.bind(); |
47 | 453 | this->renderScene(); |
78
97c3ce5aa498
fixed signed vs unsigned nonsense in gl::Compiler::idFromColor
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
454 | std::array<GLubyte, 3> data; |
51 | 455 | this->checkForGLErrors(); |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
250
diff
changeset
|
456 | glReadPixels(where.x(), where.y(), 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &data[0]); |
51 | 457 | this->checkForGLErrors(); |
238
b8ad4c12d937
Fix pick scene rendering: we cannot use glReadPixels when using multisampling, so we need to render the pick scene to a separate frame buffer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
237
diff
changeset
|
458 | fbo.release(); |
47 | 459 | this->renderPreferences.style = oldRenderStyle; |
290
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
460 | return gl::idFromUcharColor(data); |
47 | 461 | } |
462 | ||
37 | 463 | /** |
464 | * @brief Changes the color of rendered fragments | |
465 | * @param newFragmentStyle new fragment style to use | |
466 | */ | |
467 | void PartRenderer::setFragmentStyle(gl::FragmentStyle newFragmentStyle) | |
468 | { | |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
469 | gl::setShaderUniform(&this->shaders, "fragmentStyle", static_cast<int>(newFragmentStyle)); |
37 | 470 | } |
471 | ||
472 | /** | |
473 | * @brief Changes the way the scene is rendered | |
474 | * @param newStyle new render style to use | |
475 | */ | |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
476 | void PartRenderer::setRenderPreferences(const gl::RenderPreferences& newPreferences) |
18 | 477 | { |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
478 | bool mainColorChanged = this->renderPreferences.mainColor != newPreferences.mainColor; |
44
c6114b3af3a6
added configurable line thickness
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
479 | bool backgroundColorChanged = this->renderPreferences.backgroundColor != newPreferences.backgroundColor; |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
480 | this->renderPreferences = newPreferences; |
44
c6114b3af3a6
added configurable line thickness
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
481 | if (mainColorChanged or backgroundColorChanged) |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
482 | { |
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
483 | this->build(); |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
484 | } |
112 | 485 | Q_EMIT this->renderPreferencesChanged(); |
18 | 486 | this->update(); |
487 | } | |
107
02f142b399b1
Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents:
100
diff
changeset
|
488 | |
02f142b399b1
Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents:
100
diff
changeset
|
489 | /** |
02f142b399b1
Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents:
100
diff
changeset
|
490 | * @return the currently highlighted object |
02f142b399b1
Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents:
100
diff
changeset
|
491 | */ |
200 | 492 | ModelId PartRenderer::getHighlightedObject() const |
107
02f142b399b1
Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents:
100
diff
changeset
|
493 | { |
02f142b399b1
Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents:
100
diff
changeset
|
494 | return this->highlighted; |
02f142b399b1
Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents:
100
diff
changeset
|
495 | } |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
496 | |
309
d862721d19a3
Fixed ModelId being used to identify both models and elements, added ElementId to identify elements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
291
diff
changeset
|
497 | void PartRenderer::setSelection(const QSet<ElementId>& selection) |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
498 | { |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
499 | Q_ASSERT(not selection.contains({0})); |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
500 | gl::setModelShaderSelectedObjects(&this->shaders, selection); |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
501 | this->update(); |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
502 | } |
313
c24d87f64bed
Improve click handling
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
311
diff
changeset
|
503 | |
c24d87f64bed
Improve click handling
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
311
diff
changeset
|
504 | glm::vec3 PartRenderer::cameraVector(const QPointF& point) const |
c24d87f64bed
Improve click handling
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
311
diff
changeset
|
505 | { |
c24d87f64bed
Improve click handling
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
311
diff
changeset
|
506 | const glm::vec3 p1 = this->unproject({point.x(), point.y(), 0}); |
c24d87f64bed
Improve click handling
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
311
diff
changeset
|
507 | const glm::vec3 p2 = this->unproject({point.x(), point.y(), 1}); |
c24d87f64bed
Improve click handling
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
311
diff
changeset
|
508 | return p2 - p1; |
c24d87f64bed
Improve click handling
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
311
diff
changeset
|
509 | } |