Mon, 20 Jun 2022 17:27:30 +0300
Add a basic message log
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 | ||
28 | 19 | #include <glm/ext/matrix_transform.hpp> |
20 | #include <glm/ext/matrix_clip_space.hpp> | |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
21 | #include <QPainter> |
100 | 22 | #include <GL/glu.h> |
17 | 23 | #include <QMouseEvent> |
26 | 24 | #include <QMessageBox> |
47 | 25 | #include <QAbstractButton> |
55 | 26 | #include "geometry.h" |
17 | 27 | #include "partrenderer.h" |
77
028798a72591
added some meta stuff, simplified quadrilateral splitting and tested it
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
28 | #include "model.h" |
200 | 29 | #include "gl/compiler.h" |
17 | 30 | |
120
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
31 | static constexpr double MIN_ZOOM = -3.0; |
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
32 | static constexpr double MAX_ZOOM = 3.0; |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
33 | QOpenGLFunctions glfunc; |
120
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
34 | |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
34
diff
changeset
|
35 | 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
|
36 | 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
|
37 | DocumentManager* documents, |
205 | 38 | 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
|
39 | QWidget* parent) : |
21 | 40 | QOpenGLWidget{parent}, |
41 | model{model}, | |
42 | documents{documents}, | |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
43 | colorTable{colorTable} |
17 | 44 | { |
45 | this->setMouseTracking(true); | |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
46 | this->setFocusPolicy(Qt::WheelFocus); |
234
87ee9824210b
Readd axis labels, fix antialiasing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
232
diff
changeset
|
47 | QSurfaceFormat surfaceFormat; |
87ee9824210b
Readd axis labels, fix antialiasing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
232
diff
changeset
|
48 | surfaceFormat.setSamples(8); |
87ee9824210b
Readd axis labels, fix antialiasing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
232
diff
changeset
|
49 | this->setFormat(surfaceFormat); |
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
107
diff
changeset
|
50 | connect(model, &Model::rowsInserted, [&]{ |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
107
diff
changeset
|
51 | this->needBuild = true; |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
107
diff
changeset
|
52 | }); |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
107
diff
changeset
|
53 | connect(model, &Model::rowsRemoved, [&]{ this->needBuild = true; }); |
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
|
54 | 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
|
55 | 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
|
56 | 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
|
57 | 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
|
58 | } |
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->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
|
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 | }; |
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 | 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
|
64 | 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
|
65 | connect(this, &PartRenderer::projectionMatrixChanged, updateLayerMvpMatrix); |
17 | 66 | } |
67 | ||
26 | 68 | PartRenderer::~PartRenderer() |
69 | { | |
70 | } | |
71 | ||
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
72 | static QVector3D calcQVector3DFromQColor(const QColor& color) |
53 | 73 | { |
61
4585d8d7a7ec
moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents:
60
diff
changeset
|
74 | return { |
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
|
75 | float_cast(color.redF()), |
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
|
76 | float_cast(color.greenF()), |
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
|
77 | float_cast(color.blueF()), |
61
4585d8d7a7ec
moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents:
60
diff
changeset
|
78 | }; |
53 | 79 | } |
80 | ||
17 | 81 | void PartRenderer::initializeGL() |
82 | { | |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
83 | ::glfunc.initializeOpenGLFunctions(); |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
84 | if (glGetError() != GL_NO_ERROR) |
17 | 85 | { |
86 | abort(); | |
87 | } | |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
88 | 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
|
89 | 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
|
90 | 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
|
91 | } |
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
|
92 | 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
|
93 | 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
|
94 | } |
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
95 | connect(this->model, &Model::dataChanged, this, &PartRenderer::build); |
17 | 96 | this->initialized = true; |
32 | 97 | this->modelQuaternion = glm::angleAxis(glm::radians(30.0f), glm::vec3{-1, 0, 0}); |
98 | this->modelQuaternion *= glm::angleAxis(glm::radians(225.0f), glm::vec3{-0, 1, 0}); | |
55 | 99 | this->updateModelMatrix(); |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
100 | this->updateViewMatrix(); |
30 | 101 | this->update(); |
17 | 102 | } |
103 | ||
104 | void PartRenderer::resizeGL(int width, int height) | |
105 | { | |
57
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
106 | this->viewportVector = {0, 0, width, height}; |
28 | 107 | glViewport(0, 0, width, height); |
108 | this->projectionMatrix = glm::perspective( | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
109 | glm::radians(45.0f), |
28 | 110 | static_cast<float>(width) / static_cast<float>(height), |
111 | 0.1f, | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
112 | 10000.f); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
113 | gl::setShaderUniformMatrix(&this->shaders, "projectionMatrix", this->projectionMatrix); |
112 | 114 | Q_EMIT projectionMatrixChanged(this->projectionMatrix); |
17 | 115 | } |
116 | ||
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
117 | static constexpr GLenum getGlTypeForArrayClass(const gl::ArrayClass vboClass) |
21 | 118 | { |
119 | switch (vboClass) | |
120 | { | |
26 | 121 | case gl::ArrayClass::Lines: |
122 | case gl::ArrayClass::ConditionalLines: | |
21 | 123 | return GL_LINES; |
26 | 124 | case gl::ArrayClass::Triangles: |
21 | 125 | return GL_TRIANGLES; |
26 | 126 | case gl::ArrayClass::Quads: |
21 | 127 | return GL_QUADS; |
128 | } | |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
129 | throw std::runtime_error{"bad value for vboClass"}; |
21 | 130 | } |
131 | ||
17 | 132 | void PartRenderer::paintGL() |
133 | { | |
47 | 134 | glEnable(GL_DEPTH_TEST); |
135 | glShadeModel(GL_SMOOTH); | |
136 | 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
|
137 | 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
|
138 | 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
|
139 | } |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
140 | QPainter painter{this}; |
234
87ee9824210b
Readd axis labels, fix antialiasing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
232
diff
changeset
|
141 | painter.setRenderHint(QPainter::Antialiasing); |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
142 | for (RenderLayer* layer : this->activeRenderLayers) { |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
143 | layer->overpaint(&painter); |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
144 | } |
47 | 145 | } |
146 | ||
147 | void PartRenderer::renderScene() | |
148 | { | |
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
149 | if (this->needBuild) |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
150 | { |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
151 | 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
|
152 | 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
|
153 | this->needBuild = false; |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
154 | } |
53 | 155 | this->checkForGLErrors(); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
156 | if (true |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
157 | and this->renderPreferences.lineAntiAliasing |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
158 | and this->renderPreferences.style != gl::RenderStyle::PickScene |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
159 | ) { |
45
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
160 | glEnable(GL_LINE_SMOOTH); |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
161 | glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
162 | } |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
163 | else { |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
164 | glDisable(GL_LINE_SMOOTH); |
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
165 | } |
46 | 166 | if (this->renderPreferences.style != gl::RenderStyle::PickScene) |
167 | { | |
47 | 168 | const QColor& backgroundColor = this->renderPreferences.backgroundColor; |
46 | 169 | glClearColor( |
170 | static_cast<float>(backgroundColor.redF()), | |
171 | static_cast<float>(backgroundColor.greenF()), | |
172 | static_cast<float>(backgroundColor.blueF()), | |
173 | 1.0f); | |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
174 | gl::setShaderUniform(&this->shaders, "useLighting", GL_TRUE); |
46 | 175 | } |
176 | else | |
177 | { | |
178 | glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
179 | gl::setShaderUniform(&this->shaders, "useLighting", GL_FALSE); |
46 | 180 | } |
53 | 181 | this->checkForGLErrors(); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
182 | const QVector3D color = calcQVector3DFromQColor(this->renderPreferences.selectedColor); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
183 | gl::setShaderUniform(&this->shaders, "selectedColor", color); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
184 | gl::setShaderUniform(&this->shaders, "highlighted", this->highlighted.value); |
51 | 185 | this->checkForGLErrors(); |
26 | 186 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
187 | glEnable(GL_DEPTH_TEST); | |
188 | glEnable(GL_POLYGON_OFFSET_FILL); | |
189 | glPolygonOffset(1.0f, 1.0f); | |
44
c6114b3af3a6
added configurable line thickness
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
190 | glLineWidth(this->renderPreferences.lineThickness); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
191 | const auto renderAllArrays = [this](){ |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
192 | // 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
|
193 | this->renderVao(gl::ArrayClass::Triangles); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
194 | this->renderVao(gl::ArrayClass::Quads); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
195 | this->renderVao(gl::ArrayClass::Lines); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
196 | }; |
231
a9bf6bab5ea2
Add wireframe button
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
217
diff
changeset
|
197 | 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
|
198 | glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); |
a9bf6bab5ea2
Add wireframe button
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
217
diff
changeset
|
199 | } |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
200 | switch (this->renderPreferences.style) |
18 | 201 | { |
202 | case gl::RenderStyle::Normal: | |
37 | 203 | this->setFragmentStyle(gl::FragmentStyle::Normal); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
204 | renderAllArrays(); |
37 | 205 | break; |
18 | 206 | case gl::RenderStyle::BfcRedGreen: |
37 | 207 | glEnable(GL_CULL_FACE); |
208 | glCullFace(GL_BACK); | |
209 | this->setFragmentStyle(gl::FragmentStyle::BfcGreen); | |
210 | renderVao(gl::ArrayClass::Triangles); | |
211 | renderVao(gl::ArrayClass::Quads); | |
212 | glCullFace(GL_FRONT); | |
213 | this->setFragmentStyle(gl::FragmentStyle::BfcRed); | |
214 | renderVao(gl::ArrayClass::Triangles); | |
215 | renderVao(gl::ArrayClass::Quads); | |
216 | glDisable(GL_CULL_FACE); | |
217 | this->setFragmentStyle(gl::FragmentStyle::Normal); | |
218 | renderVao(gl::ArrayClass::Lines); | |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
219 | break; |
18 | 220 | case gl::RenderStyle::RandomColors: |
37 | 221 | this->setFragmentStyle(gl::FragmentStyle::RandomColors); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
222 | renderAllArrays(); |
18 | 223 | break; |
46 | 224 | case gl::RenderStyle::PickScene: |
225 | glLineWidth(3.0f); | |
226 | this->setFragmentStyle(gl::FragmentStyle::Id); | |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
227 | renderAllArrays(); |
46 | 228 | break; |
119 | 229 | case gl::RenderStyle::VertexPickScene: |
230 | glLineWidth(1.0f); | |
231 | this->setFragmentStyle(gl::FragmentStyle::Black); | |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
232 | renderAllArrays(); |
119 | 233 | break; |
18 | 234 | } |
37 | 235 | glDisable(GL_POLYGON_OFFSET_FILL); |
231
a9bf6bab5ea2
Add wireframe button
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
217
diff
changeset
|
236 | glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); |
37 | 237 | } |
238 | ||
239 | ||
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
240 | void PartRenderer::updateViewMatrix() |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
241 | { |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
242 | // 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
|
243 | const float modelDistance = longestMeasure(this->boundingBox); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
244 | 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
|
245 | 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
|
246 | gl::setShaderUniformMatrix(&this->shaders, "viewMatrix", this->viewMatrix); |
112 | 247 | Q_EMIT this->viewMatrixChanged(this->viewMatrix); |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
248 | } |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
249 | |
55 | 250 | void PartRenderer::updateModelMatrix() |
251 | { | |
61
4585d8d7a7ec
moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents:
60
diff
changeset
|
252 | 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
|
253 | gl::setShaderUniformMatrix(&this->shaders, "modelMatrix", modelMatrix); |
112 | 254 | Q_EMIT this->modelMatrixChanged(this->modelMatrix); |
55 | 255 | this->update(); |
256 | } | |
257 | ||
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
258 | void PartRenderer::build() |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
259 | { |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
260 | this->needBuild = true; |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
261 | } |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
262 | |
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
|
263 | void PartRenderer::renderVao(const gl::ArrayClass arrayClass) |
26 | 264 | { |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
265 | gl::bindModelShaderVertexArray(&this->shaders, arrayClass); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
266 | const std::size_t vertexCount = gl::vertexCount(&this->shaders, arrayClass); |
51 | 267 | this->checkForGLErrors(); |
26 | 268 | glDrawArrays(getGlTypeForArrayClass(arrayClass), 0, static_cast<GLsizei>(vertexCount)); |
51 | 269 | this->checkForGLErrors(); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
270 | gl::releaseModelShaderVertexArray(&this->shaders, arrayClass); |
26 | 271 | this->checkForGLErrors(); |
272 | } | |
273 | ||
274 | void PartRenderer::checkForGLErrors() | |
275 | { | |
53 | 276 | gl::checkForGLErrors(this); |
277 | } | |
278 | ||
279 | void gl::checkForGLErrors(QWidget* parent) | |
280 | { | |
26 | 281 | GLenum glError; |
282 | QStringList errors; | |
283 | while ((glError = glGetError()) != GL_NO_ERROR) | |
21 | 284 | { |
100 | 285 | const QString glErrorString = QString::fromLatin1(reinterpret_cast<const char*>(gluErrorString(glError))); |
26 | 286 | errors.append(glErrorString); |
287 | } | |
288 | if (not errors.isEmpty()) | |
289 | { | |
53 | 290 | QMessageBox box{parent}; |
47 | 291 | box.setIcon(QMessageBox::Critical); |
53 | 292 | box.setText(QObject::tr("OpenGL error: %1").arg(errors.join("\n"))); |
293 | box.setWindowTitle(QObject::tr("OpenGL error")); | |
47 | 294 | box.setStandardButtons(QMessageBox::Close); |
53 | 295 | box.button(QMessageBox::Close)->setText(QObject::tr("Damn it")); |
47 | 296 | box.exec(); |
21 | 297 | } |
17 | 298 | } |
299 | ||
300 | void PartRenderer::mouseMoveEvent(QMouseEvent* event) | |
301 | { | |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
302 | if (not this->frozen) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
303 | 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
|
304 | 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
|
305 | this->totalMouseMove += move.manhattanLength(); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
306 | if (left and not move.isNull()) |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
307 | { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
308 | // 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
|
309 | // 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
|
310 | // 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
|
311 | const auto scalar = 0.006f; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
312 | 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
|
313 | 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
|
314 | 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
|
315 | 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
|
316 | 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
|
317 | this->updateModelMatrix(); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
318 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
319 | this->lastMousePosition = event->pos(); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
320 | for (RenderLayer* layer : this->activeRenderLayers) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
321 | layer->mouseMoved(event); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
322 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
323 | this->update(); |
17 | 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::mousePressEvent(QMouseEvent* event) |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
328 | { |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
329 | if (not this->frozen) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
330 | this->totalMouseMove = 0; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
331 | this->lastMousePosition = event->pos(); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
332 | } |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
333 | } |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
334 | |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
335 | void PartRenderer::mouseReleaseEvent(QMouseEvent* event) |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
336 | { |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
337 | if (not frozen and this->totalMouseMove < (2.0 / sqrt(2)) * 5.0) |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
338 | { |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
339 | for (RenderLayer* layer : this->activeRenderLayers) { |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
340 | layer->mouseClick(event); |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
341 | } |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
342 | this->update(); |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
343 | } |
17 | 344 | } |
18 | 345 | |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
346 | void PartRenderer::keyReleaseEvent(QKeyEvent* event) |
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 | if (event->key() == Qt::Key_Pause) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
349 | this->frozen = not this->frozen; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
350 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
351 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
352 | |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
353 | void PartRenderer::wheelEvent(QWheelEvent* event) |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
354 | { |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
355 | if (not this->frozen) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
356 | 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
|
357 | 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
|
358 | 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
|
359 | this->updateViewMatrix(); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
360 | this->update(); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
231
diff
changeset
|
361 | } |
31
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
362 | } |
b6df269a2c6b
fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
363 | |
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 | 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
|
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 | this->activeRenderLayers.push_back(layer); |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
367 | 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
|
368 | 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
|
369 | } |
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 | |
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 | 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
|
372 | { |
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 | 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
|
374 | 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
|
375 | 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
|
376 | 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
|
377 | 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
|
378 | 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
|
379 | } |
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
|
380 | } |
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
|
381 | |
58
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
382 | /** |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
383 | * @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
|
384 | * 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
|
385 | * 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
|
386 | * @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
|
387 | * @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
|
388 | * @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
|
389 | */ |
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
|
390 | std::optional<glm::vec3> PartRenderer::screenToModelCoordinates(const QPointF& point, const Plane& plane) const |
55 | 391 | { |
201
5d201ee4a9c3
Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
392 | 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
|
393 | std::optional<glm::vec3> result; |
201
5d201ee4a9c3
Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
394 | 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
|
395 | // 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
|
396 | 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
|
397 | { |
59
60650929dc82
fixed testing of whether screenToModelCoordinates's result value is behind the camera
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
398 | result.reset(); |
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 | return result; |
55 | 401 | } |
402 | ||
58
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
403 | /** |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
404 | * @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
|
405 | * @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
|
406 | * @return screen coordinates |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
407 | */ |
66
77c819262b7a
added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
408 | QPointF PartRenderer::modelToScreenCoordinates(const glm::vec3& point) const |
55 | 409 | { |
58
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
410 | 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
|
411 | point, |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
412 | 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
|
413 | this->projectionMatrix, |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
414 | this->viewportVector); |
60 | 415 | return toQPointF(glm::vec2{projected.x, this->height() - projected.y}); |
55 | 416 | } |
417 | ||
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
418 | bool PartRenderer::isDark() const |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
419 | { |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
420 | 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
|
421 | } |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
422 | |
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
|
423 | 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
|
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 | 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
|
426 | 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
|
427 | 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
|
428 | } |
77c819262b7a
added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
429 | |
58
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
430 | /** |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
431 | * @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
|
432 | * @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
|
433 | * @return model coordinates |
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
434 | */ |
66
77c819262b7a
added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
435 | 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
|
436 | { |
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
437 | return glm::unProject( |
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
438 | glm::vec3{win.x, 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
|
439 | this->viewMatrix * this->modelMatrix, |
57
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
440 | this->projectionMatrix, |
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
441 | viewportVector); |
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
442 | } |
5c0005f63319
use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents:
56
diff
changeset
|
443 | |
200 | 444 | ModelId PartRenderer::pick(QPoint where) |
47 | 445 | { |
199
6988973515d2
Fix pick() picking from weird places on the screen with high DPI scaling
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
446 | // 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
|
447 | 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
|
448 | // 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
|
449 | // 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
|
450 | // 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
|
451 | where *= this->devicePixelRatioF(); |
47 | 452 | const gl::RenderStyle oldRenderStyle = this->renderPreferences.style; |
453 | this->renderPreferences.style = gl::RenderStyle::PickScene; | |
454 | this->makeCurrent(); | |
455 | this->renderScene(); | |
78
97c3ce5aa498
fixed signed vs unsigned nonsense in gl::Compiler::idFromColor
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
456 | std::array<GLubyte, 3> data; |
51 | 457 | this->checkForGLErrors(); |
199
6988973515d2
Fix pick() picking from weird places on the screen with high DPI scaling
Teemu Piippo <teemu@hecknology.net>
parents:
189
diff
changeset
|
458 | glfunc.glReadPixels(where.x(), where.y(), 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &data[0]); |
51 | 459 | this->checkForGLErrors(); |
47 | 460 | this->renderPreferences.style = oldRenderStyle; |
461 | this->update(); | |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
462 | return gl::idFromColor(data); |
47 | 463 | } |
464 | ||
37 | 465 | /** |
466 | * @brief Changes the color of rendered fragments | |
467 | * @param newFragmentStyle new fragment style to use | |
468 | */ | |
469 | void PartRenderer::setFragmentStyle(gl::FragmentStyle newFragmentStyle) | |
470 | { | |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
471 | gl::setShaderUniform(&this->shaders, "fragmentStyle", static_cast<int>(newFragmentStyle)); |
37 | 472 | } |
473 | ||
474 | /** | |
475 | * @brief Changes the way the scene is rendered | |
476 | * @param newStyle new render style to use | |
477 | */ | |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
478 | void PartRenderer::setRenderPreferences(const gl::RenderPreferences& newPreferences) |
18 | 479 | { |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
480 | bool mainColorChanged = this->renderPreferences.mainColor != newPreferences.mainColor; |
44
c6114b3af3a6
added configurable line thickness
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
481 | bool backgroundColorChanged = this->renderPreferences.backgroundColor != newPreferences.backgroundColor; |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
482 | this->renderPreferences = newPreferences; |
44
c6114b3af3a6
added configurable line thickness
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
483 | if (mainColorChanged or backgroundColorChanged) |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
484 | { |
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
78
diff
changeset
|
485 | this->build(); |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
486 | } |
112 | 487 | Q_EMIT this->renderPreferencesChanged(); |
18 | 488 | this->update(); |
489 | } | |
107
02f142b399b1
Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents:
100
diff
changeset
|
490 | |
02f142b399b1
Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents:
100
diff
changeset
|
491 | /** |
02f142b399b1
Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents:
100
diff
changeset
|
492 | * @return the currently highlighted object |
02f142b399b1
Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents:
100
diff
changeset
|
493 | */ |
200 | 494 | ModelId PartRenderer::getHighlightedObject() const |
107
02f142b399b1
Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents:
100
diff
changeset
|
495 | { |
02f142b399b1
Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents:
100
diff
changeset
|
496 | return this->highlighted; |
02f142b399b1
Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents:
100
diff
changeset
|
497 | } |
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 | void PartRenderer::setSelection(const QSet<ModelId>& selection) |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
500 | { |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
501 | Q_ASSERT(not selection.contains({0})); |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
502 | gl::setModelShaderSelectedObjects(&this->shaders, selection); |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
503 | this->update(); |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
504 | } |