src/gl/partrenderer.cpp

Tue, 11 Apr 2023 22:39:18 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Tue, 11 Apr 2023 22:39:18 +0300
changeset 376
3cef3b016330
parent 360
41b38b9e05a2
child 382
94d5587bb0c4
permissions
-rw-r--r--

Split GL preferences that affect GL build to a new build preferences structure, modifying that requires rebuild, modifying render preferences does not

24
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
1 /*
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
2 * LDForge: LDraw parts authoring CAD
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
3 * Copyright (C) 2013 - 2020 Teemu Piippo
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
4 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
5 * This program is free software: you can redistribute it and/or modify
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
6 * it under the terms of the GNU General Public License as published by
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
8 * (at your option) any later version.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
9 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
10 * This program is distributed in the hope that it will be useful,
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
13 * GNU General Public License for more details.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
14 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
15 * You should have received a copy of the GNU General Public License
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
17 */
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
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
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
20 #include <glm/ext/matrix_transform.hpp>
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
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
43ce3672648a fix build
Teemu Piippo <teemu@hecknology.net>
parents: 92
diff changeset
24 #include <GL/glu.h>
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 #include <QMouseEvent>
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
26 #include <QMessageBox>
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
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
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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;
376
3cef3b016330 Split GL preferences that affect GL build to a new build preferences structure, modifying that requires rebuild, modifying render preferences does not
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 360
diff changeset
36 const gl::RenderPreferences PartRenderer::default_render_preferences{};
3cef3b016330 Split GL preferences that affect GL build to a new build preferences structure, modifying that requires rebuild, modifying render preferences does not
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 360
diff changeset
37 const gl::build_preferences PartRenderer::default_build_preferences{};
259
c27612f0eac0 - Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 250
diff changeset
38
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 PartRenderer::PartRenderer(
338
719b909a7d2b Delete unused code
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 333
diff changeset
40 QTextDocument* model,
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 DocumentManager* documents,
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 201
diff changeset
42 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
43 QWidget* parent) :
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
44 QOpenGLWidget{parent},
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
45 model{model},
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
46 documents{documents},
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
47 colorTable{colorTable}
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 this->setMouseTracking(true);
232
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 231
diff changeset
50 this->setFocusPolicy(Qt::WheelFocus);
234
87ee9824210b Readd axis labels, fix antialiasing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 232
diff changeset
51 QSurfaceFormat surfaceFormat;
87ee9824210b Readd axis labels, fix antialiasing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 232
diff changeset
52 surfaceFormat.setSamples(8);
87ee9824210b Readd axis labels, fix antialiasing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 232
diff changeset
53 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
54 const auto setNeedBuild = [&]{this->needBuild = true;};
333
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 329
diff changeset
55 connect(model, &QTextDocument::contentsChange, 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
56 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
57 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
58 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
59 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
60 }
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 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
62 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
63 }
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 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
66 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
67 connect(this, &PartRenderer::projectionMatrixChanged, updateLayerMvpMatrix);
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
70 PartRenderer::~PartRenderer()
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
71 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
72 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
73
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 void PartRenderer::initializeGL()
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 {
291
42b4953dff85 Let's bring GLEW back after all
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 290
diff changeset
76 glewInit();
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
77 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
78 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
79 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
80 }
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 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
82 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
83 }
333
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 329
diff changeset
84 connect(this->model, &QTextDocument::contentsChanged, this, &PartRenderer::build);
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
85 this->initialized = true;
32
767592024ec5 fix default angle
Teemu Piippo <teemu@hecknology.net>
parents: 31
diff changeset
86 this->modelQuaternion = glm::angleAxis(glm::radians(30.0f), glm::vec3{-1, 0, 0});
767592024ec5 fix default angle
Teemu Piippo <teemu@hecknology.net>
parents: 31
diff changeset
87 this->modelQuaternion *= glm::angleAxis(glm::radians(225.0f), glm::vec3{-0, 1, 0});
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
88 this->updateModelMatrix();
31
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
89 this->updateViewMatrix();
30
1536f23cfab7 it works!
Teemu Piippo <teemu@hecknology.net>
parents: 28
diff changeset
90 this->update();
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
91 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
92
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
93 void PartRenderer::resizeGL(int width, int height)
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
94 {
57
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
95 this->viewportVector = {0, 0, width, height};
28
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
96 glViewport(0, 0, width, height);
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
97 this->projectionMatrix = glm::perspective(
31
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
98 glm::radians(45.0f),
28
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
99 static_cast<float>(width) / static_cast<float>(height),
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
100 0.1f,
31
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
101 10000.f);
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
102 gl::setShaderUniformMatrix(&this->shaders, "projectionMatrix", this->projectionMatrix);
112
5760cbb32bc0 use QT_NO_KEYWORDS
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
103 Q_EMIT projectionMatrixChanged(this->projectionMatrix);
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
104 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
105
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
106 void PartRenderer::paintGL()
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
107 {
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
108 glEnable(GL_DEPTH_TEST);
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
109 glShadeModel(GL_SMOOTH);
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
110 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
111 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
112 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
113 }
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
114 QPainter painter{this};
234
87ee9824210b Readd axis labels, fix antialiasing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 232
diff changeset
115 painter.setRenderHint(QPainter::Antialiasing);
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
116 for (RenderLayer* layer : this->activeRenderLayers) {
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
117 layer->overpaint(&painter);
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
118 }
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
119 }
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
120
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
121 void PartRenderer::renderScene()
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
122 {
86
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
123 if (this->needBuild)
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
124 {
376
3cef3b016330 Split GL preferences that affect GL build to a new build preferences structure, modifying that requires rebuild, modifying render preferences does not
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 360
diff changeset
125 gl::build(&this->shaders, this->model, this->colorTable, this->documents, this->build_preferences);
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
126 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
127 this->needBuild = false;
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
128 }
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
129 this->checkForGLErrors();
376
3cef3b016330 Split GL preferences that affect GL build to a new build preferences structure, modifying that requires rebuild, modifying render preferences does not
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 360
diff changeset
130 if (this->render_preferences->lineAntiAliasing)
3cef3b016330 Split GL preferences that affect GL build to a new build preferences structure, modifying that requires rebuild, modifying render preferences does not
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 360
diff changeset
131 {
45
272c84c7c87e added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents: 44
diff changeset
132 glEnable(GL_LINE_SMOOTH);
272c84c7c87e added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents: 44
diff changeset
133 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
272c84c7c87e added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents: 44
diff changeset
134 }
272c84c7c87e added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents: 44
diff changeset
135 else {
272c84c7c87e added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents: 44
diff changeset
136 glDisable(GL_LINE_SMOOTH);
272c84c7c87e added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents: 44
diff changeset
137 }
376
3cef3b016330 Split GL preferences that affect GL build to a new build preferences structure, modifying that requires rebuild, modifying render preferences does not
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 360
diff changeset
138 if (this->render_preferences->style != gl::RenderStyle::PickScene)
46
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
139 {
376
3cef3b016330 Split GL preferences that affect GL build to a new build preferences structure, modifying that requires rebuild, modifying render preferences does not
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 360
diff changeset
140 const QColor& backgroundColor = this->build_preferences->backgroundColor;
46
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
141 glClearColor(
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
142 static_cast<float>(backgroundColor.redF()),
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
143 static_cast<float>(backgroundColor.greenF()),
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
144 static_cast<float>(backgroundColor.blueF()),
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
145 1.0f);
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
146 gl::setShaderUniform(&this->shaders, "useLighting", GL_TRUE);
46
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
147 }
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
148 else
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
149 {
290
0fd926ebb03b Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
150 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
151 gl::setShaderUniform(&this->shaders, "useLighting", GL_FALSE);
46
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
152 }
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
153 this->checkForGLErrors();
376
3cef3b016330 Split GL preferences that affect GL build to a new build preferences structure, modifying that requires rebuild, modifying render preferences does not
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 360
diff changeset
154 const QColor qs = this->render_preferences->selectedColor;
290
0fd926ebb03b Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
155 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
156 gl::setShaderUniformVector(&this->shaders, "selectedColor", selectedColor);
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
157 gl::setShaderUniform(&this->shaders, "highlighted", this->highlighted.value);
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
158 this->checkForGLErrors();
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
159 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
160 glEnable(GL_DEPTH_TEST);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
161 glEnable(GL_POLYGON_OFFSET_FILL);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
162 glPolygonOffset(1.0f, 1.0f);
376
3cef3b016330 Split GL preferences that affect GL build to a new build preferences structure, modifying that requires rebuild, modifying render preferences does not
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 360
diff changeset
163 glLineWidth(this->render_preferences->lineThickness);
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
164 const auto renderAllArrays = [this](){
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
165 // Lines need to be rendered last so that anti-aliasing does not interfere with polygon rendering.
360
41b38b9e05a2 `PartRenderer::renderVao` no longer throws if bad array class is given, this is now checked on compile time
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 359
diff changeset
166 this->renderVao<gl::ArrayClass::Triangles>();
41b38b9e05a2 `PartRenderer::renderVao` no longer throws if bad array class is given, this is now checked on compile time
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 359
diff changeset
167 this->renderVao<gl::ArrayClass::Quads>();
41b38b9e05a2 `PartRenderer::renderVao` no longer throws if bad array class is given, this is now checked on compile time
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 359
diff changeset
168 this->renderVao<gl::ArrayClass::Lines>();
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
169 };
376
3cef3b016330 Split GL preferences that affect GL build to a new build preferences structure, modifying that requires rebuild, modifying render preferences does not
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 360
diff changeset
170 if (this->render_preferences->wireframe)
3cef3b016330 Split GL preferences that affect GL build to a new build preferences structure, modifying that requires rebuild, modifying render preferences does not
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 360
diff changeset
171 {
231
a9bf6bab5ea2 Add wireframe button
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
172 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
a9bf6bab5ea2 Add wireframe button
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
173 }
376
3cef3b016330 Split GL preferences that affect GL build to a new build preferences structure, modifying that requires rebuild, modifying render preferences does not
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 360
diff changeset
174 switch (this->render_preferences->style)
18
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
175 {
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
176 case gl::RenderStyle::Normal:
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
177 this->setFragmentStyle(gl::FragmentStyle::Normal);
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
178 renderAllArrays();
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
179 break;
18
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
180 case gl::RenderStyle::BfcRedGreen:
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
181 glEnable(GL_CULL_FACE);
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
182 glCullFace(GL_BACK);
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
183 this->setFragmentStyle(gl::FragmentStyle::BfcGreen);
360
41b38b9e05a2 `PartRenderer::renderVao` no longer throws if bad array class is given, this is now checked on compile time
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 359
diff changeset
184 renderVao<gl::ArrayClass::Triangles>();
41b38b9e05a2 `PartRenderer::renderVao` no longer throws if bad array class is given, this is now checked on compile time
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 359
diff changeset
185 renderVao<gl::ArrayClass::Quads>();
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
186 glCullFace(GL_FRONT);
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
187 this->setFragmentStyle(gl::FragmentStyle::BfcRed);
360
41b38b9e05a2 `PartRenderer::renderVao` no longer throws if bad array class is given, this is now checked on compile time
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 359
diff changeset
188 renderVao<gl::ArrayClass::Triangles>();
41b38b9e05a2 `PartRenderer::renderVao` no longer throws if bad array class is given, this is now checked on compile time
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 359
diff changeset
189 renderVao<gl::ArrayClass::Quads>();
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
190 glDisable(GL_CULL_FACE);
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
191 this->setFragmentStyle(gl::FragmentStyle::Normal);
360
41b38b9e05a2 `PartRenderer::renderVao` no longer throws if bad array class is given, this is now checked on compile time
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 359
diff changeset
192 renderVao<gl::ArrayClass::Lines>();
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
193 break;
18
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
194 case gl::RenderStyle::RandomColors:
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
195 this->setFragmentStyle(gl::FragmentStyle::RandomColors);
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
196 renderAllArrays();
18
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
197 break;
46
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
198 case gl::RenderStyle::PickScene:
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
199 this->setFragmentStyle(gl::FragmentStyle::Id);
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
200 renderAllArrays();
46
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
201 break;
119
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
202 case gl::RenderStyle::VertexPickScene:
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
203 glLineWidth(1.0f);
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
204 this->setFragmentStyle(gl::FragmentStyle::Black);
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
205 renderAllArrays();
119
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
206 break;
18
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
207 }
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
208 glDisable(GL_POLYGON_OFFSET_FILL);
231
a9bf6bab5ea2 Add wireframe button
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
209 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
210 }
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
211
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
212
31
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
213 void PartRenderer::updateViewMatrix()
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
214 {
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
215 // 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
216 const float modelDistance = longestMeasure(this->boundingBox);
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
217 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
218 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
219 gl::setShaderUniformMatrix(&this->shaders, "viewMatrix", this->viewMatrix);
112
5760cbb32bc0 use QT_NO_KEYWORDS
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
220 Q_EMIT this->viewMatrixChanged(this->viewMatrix);
31
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
221 }
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
222
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
223 void PartRenderer::updateModelMatrix()
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
224 {
359
73b6c478378e The renderer now centers on the grid origin, not the part origin
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 338
diff changeset
225 this->modelMatrix = glm::translate(glm::mat4_cast(this->modelQuaternion), -this->modelViewOrigin);
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
226 gl::setShaderUniformMatrix(&this->shaders, "modelMatrix", modelMatrix);
112
5760cbb32bc0 use QT_NO_KEYWORDS
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
227 Q_EMIT this->modelMatrixChanged(this->modelMatrix);
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
228 this->update();
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
229 }
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
230
86
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
231 void PartRenderer::build()
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
232 {
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
233 this->needBuild = true;
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
234 }
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
235
360
41b38b9e05a2 `PartRenderer::renderVao` no longer throws if bad array class is given, this is now checked on compile time
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 359
diff changeset
236 void PartRenderer::renderVao(const gl::ArrayClass arrayClass, const GLenum glType)
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
237 {
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
238 gl::bindModelShaderVertexArray(&this->shaders, arrayClass);
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
239 const std::size_t vertexCount = gl::vertexCount(&this->shaders, arrayClass);
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
240 this->checkForGLErrors();
360
41b38b9e05a2 `PartRenderer::renderVao` no longer throws if bad array class is given, this is now checked on compile time
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 359
diff changeset
241 glDrawArrays(glType, 0, static_cast<GLsizei>(vertexCount));
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
242 this->checkForGLErrors();
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
243 gl::releaseModelShaderVertexArray(&this->shaders, arrayClass);
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
244 this->checkForGLErrors();
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
245 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
246
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
247 void PartRenderer::checkForGLErrors()
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
248 {
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
249 gl::checkForGLErrors(this);
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
250 }
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
251
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
252 void gl::checkForGLErrors(QWidget* parent)
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
253 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
254 GLenum glError;
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
255 QStringList errors;
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
256 while ((glError = glGetError()) != GL_NO_ERROR)
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
257 {
100
43ce3672648a fix build
Teemu Piippo <teemu@hecknology.net>
parents: 92
diff changeset
258 const QString glErrorString = QString::fromLatin1(reinterpret_cast<const char*>(gluErrorString(glError)));
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
259 errors.append(glErrorString);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
260 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
261 if (not errors.isEmpty())
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
262 {
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
263 QMessageBox box{parent};
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
264 box.setIcon(QMessageBox::Critical);
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
265 box.setText(QObject::tr("OpenGL error: %1").arg(errors.join("\n")));
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
266 box.setWindowTitle(QObject::tr("OpenGL error"));
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
267 box.setStandardButtons(QMessageBox::Close);
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
268 box.button(QMessageBox::Close)->setText(QObject::tr("Damn it"));
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
269 box.exec();
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
270 }
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
271 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
272
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
273 void PartRenderer::mouseMoveEvent(QMouseEvent* event)
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
274 {
232
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 231
diff changeset
275 if (not this->frozen) {
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 231
diff changeset
276 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
277 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
278 this->totalMouseMove += move.manhattanLength();
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 231
diff changeset
279 if (left and not move.isNull())
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 231
diff changeset
280 {
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 231
diff changeset
281 // 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
282 // 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
283 // 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
284 const auto scalar = 0.006f;
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 231
diff changeset
285 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
286 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
287 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
288 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
289 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
290 this->updateModelMatrix();
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 231
diff changeset
291 }
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 231
diff changeset
292 this->lastMousePosition = event->pos();
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 231
diff changeset
293 for (RenderLayer* layer : this->activeRenderLayers) {
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 231
diff changeset
294 layer->mouseMoved(event);
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 231
diff changeset
295 }
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 231
diff changeset
296 this->update();
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
297 }
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
298 }
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
299
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
300 void PartRenderer::mousePressEvent(QMouseEvent* event)
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
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 this->totalMouseMove = 0;
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 231
diff changeset
304 this->lastMousePosition = event->pos();
313
c24d87f64bed Improve click handling
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 311
diff changeset
305 this->lastClickTime = steady_clock::now();
232
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 231
diff changeset
306 }
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
307 }
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
308
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
309 void PartRenderer::mouseReleaseEvent(QMouseEvent* event)
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
310 {
313
c24d87f64bed Improve click handling
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 311
diff changeset
311 using namespace std::chrono_literals;
c24d87f64bed Improve click handling
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 311
diff changeset
312 if (true
c24d87f64bed Improve click handling
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 311
diff changeset
313 and not frozen
c24d87f64bed Improve click handling
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 311
diff changeset
314 and this->totalMouseMove < 50.0 / sqrt(2)
c24d87f64bed Improve click handling
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 311
diff changeset
315 and (steady_clock::now() - this->lastClickTime) < 0.5s
c24d87f64bed Improve click handling
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 311
diff changeset
316 ) {
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
317 for (RenderLayer* layer : this->activeRenderLayers) {
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
318 layer->mouseClick(event);
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
319 }
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
320 this->update();
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
321 }
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
322 }
18
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
323
232
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 231
diff changeset
324 void PartRenderer::keyReleaseEvent(QKeyEvent* event)
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 231
diff changeset
325 {
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 231
diff changeset
326 if (event->key() == Qt::Key_Pause) {
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 231
diff changeset
327 this->frozen = not this->frozen;
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 231
diff changeset
328 }
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 231
diff changeset
329 }
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 231
diff changeset
330
31
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
331 void PartRenderer::wheelEvent(QWheelEvent* event)
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
332 {
232
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 231
diff changeset
333 if (not this->frozen) {
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 231
diff changeset
334 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
335 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
336 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
337 this->updateViewMatrix();
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 231
diff changeset
338 this->update();
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 231
diff changeset
339 }
31
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
340 }
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
341
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
342 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
343 {
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
344 this->activeRenderLayers.push_back(layer);
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
345 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
346 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
347 }
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
348
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
349 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
350 {
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
351 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
352 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
353 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
354 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
355 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
356 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
357 }
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
358 }
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
359
58
b7841cd31fb7 use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents: 57
diff changeset
360 /**
b7841cd31fb7 use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents: 57
diff changeset
361 * @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
362 * 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
363 * 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
364 * @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
365 * @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
366 * @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
367 */
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
368 std::optional<glm::vec3> PartRenderer::screenToModelCoordinates(const QPointF& point, const Plane& plane) const
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
369 {
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
370 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
371 std::optional<glm::vec3> result;
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
372 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
373 // 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
374 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
375 {
59
60650929dc82 fixed testing of whether screenToModelCoordinates's result value is behind the camera
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
376 result.reset();
58
b7841cd31fb7 use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents: 57
diff changeset
377 }
b7841cd31fb7 use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents: 57
diff changeset
378 return result;
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
379 }
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
380
58
b7841cd31fb7 use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents: 57
diff changeset
381 /**
b7841cd31fb7 use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents: 57
diff changeset
382 * @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
383 * @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
384 * @return screen coordinates
b7841cd31fb7 use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents: 57
diff changeset
385 */
66
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
386 QPointF PartRenderer::modelToScreenCoordinates(const glm::vec3& point) const
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
387 {
58
b7841cd31fb7 use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents: 57
diff changeset
388 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
389 point,
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
390 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
391 this->projectionMatrix,
b7841cd31fb7 use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents: 57
diff changeset
392 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
393 return toQPointF(glm::vec2{projected.x, static_cast<float>(this->height()) - projected.y});
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
394 }
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
395
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
396 bool PartRenderer::isDark() const
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
397 {
376
3cef3b016330 Split GL preferences that affect GL build to a new build preferences structure, modifying that requires rebuild, modifying render preferences does not
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 360
diff changeset
398 return luma(this->build_preferences->backgroundColor) < 0.25;
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
399 }
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
400
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
401 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
402 {
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
403 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
404 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
405 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
406 }
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
407
359
73b6c478378e The renderer now centers on the grid origin, not the part origin
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 338
diff changeset
408 void PartRenderer::setModelViewOrigin(const glm::vec3& newViewOrigin)
73b6c478378e The renderer now centers on the grid origin, not the part origin
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 338
diff changeset
409 {
73b6c478378e The renderer now centers on the grid origin, not the part origin
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 338
diff changeset
410 this->modelViewOrigin = newViewOrigin;
73b6c478378e The renderer now centers on the grid origin, not the part origin
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 338
diff changeset
411 this->updateModelMatrix();
73b6c478378e The renderer now centers on the grid origin, not the part origin
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 338
diff changeset
412 }
73b6c478378e The renderer now centers on the grid origin, not the part origin
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 338
diff changeset
413
58
b7841cd31fb7 use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents: 57
diff changeset
414 /**
b7841cd31fb7 use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents: 57
diff changeset
415 * @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
416 * @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
417 * @return model coordinates
b7841cd31fb7 use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents: 57
diff changeset
418 */
66
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
419 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
420 {
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
421 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
422 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
423 this->viewMatrix * this->modelMatrix,
57
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
424 this->projectionMatrix,
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
425 viewportVector);
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
426 }
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
427
333
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 329
diff changeset
428 int32_t PartRenderer::pick(QPoint where)
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
429 {
199
6988973515d2 Fix pick() picking from weird places on the screen with high DPI scaling
Teemu Piippo <teemu@hecknology.net>
parents: 189
diff changeset
430 // 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
431 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
432 // 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
433 // 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
434 // 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
435 where *= this->devicePixelRatioF();
376
3cef3b016330 Split GL preferences that affect GL build to a new build preferences structure, modifying that requires rebuild, modifying render preferences does not
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 360
diff changeset
436 static const gl::RenderPreferences pick_scene_preferences {
3cef3b016330 Split GL preferences that affect GL build to a new build preferences structure, modifying that requires rebuild, modifying render preferences does not
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 360
diff changeset
437 .style = gl::RenderStyle::PickScene,
3cef3b016330 Split GL preferences that affect GL build to a new build preferences structure, modifying that requires rebuild, modifying render preferences does not
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 360
diff changeset
438 .selectedColor = {},
3cef3b016330 Split GL preferences that affect GL build to a new build preferences structure, modifying that requires rebuild, modifying render preferences does not
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 360
diff changeset
439 .lineThickness = 3.0f,
3cef3b016330 Split GL preferences that affect GL build to a new build preferences structure, modifying that requires rebuild, modifying render preferences does not
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 360
diff changeset
440 .lineAntiAliasing = false,
3cef3b016330 Split GL preferences that affect GL build to a new build preferences structure, modifying that requires rebuild, modifying render preferences does not
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 360
diff changeset
441 .drawAxes = false,
3cef3b016330 Split GL preferences that affect GL build to a new build preferences structure, modifying that requires rebuild, modifying render preferences does not
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 360
diff changeset
442 .wireframe = false,
3cef3b016330 Split GL preferences that affect GL build to a new build preferences structure, modifying that requires rebuild, modifying render preferences does not
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 360
diff changeset
443 };
3cef3b016330 Split GL preferences that affect GL build to a new build preferences structure, modifying that requires rebuild, modifying render preferences does not
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 360
diff changeset
444 const gl::RenderPreferences* old_render_preferences = this->render_preferences;
3cef3b016330 Split GL preferences that affect GL build to a new build preferences structure, modifying that requires rebuild, modifying render preferences does not
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 360
diff changeset
445 this->render_preferences = &pick_scene_preferences;
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
446 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
447 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
448 fbo.bind();
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
449 this->renderScene();
78
97c3ce5aa498 fixed signed vs unsigned nonsense in gl::Compiler::idFromColor
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
450 std::array<GLubyte, 3> data;
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
451 this->checkForGLErrors();
259
c27612f0eac0 - Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 250
diff changeset
452 glReadPixels(where.x(), where.y(), 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &data[0]);
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
453 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
454 fbo.release();
376
3cef3b016330 Split GL preferences that affect GL build to a new build preferences structure, modifying that requires rebuild, modifying render preferences does not
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 360
diff changeset
455 this->render_preferences = old_render_preferences;
290
0fd926ebb03b Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
456 return gl::idFromUcharColor(data);
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
457 }
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
458
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
459 /**
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
460 * @brief Changes the color of rendered fragments
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
461 * @param newFragmentStyle new fragment style to use
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
462 */
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
463 void PartRenderer::setFragmentStyle(gl::FragmentStyle newFragmentStyle)
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
464 {
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
465 gl::setShaderUniform(&this->shaders, "fragmentStyle", static_cast<int>(newFragmentStyle));
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
466 }
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
467
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
468 /**
107
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
469 * @return the currently highlighted object
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
470 */
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 199
diff changeset
471 ModelId PartRenderer::getHighlightedObject() const
107
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
472 {
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
473 return this->highlighted;
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
474 }
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
475
333
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 329
diff changeset
476 void PartRenderer::setSelection(const QSet<int32_t>& selection)
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
477 {
333
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 329
diff changeset
478 #if 0
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
479 Q_ASSERT(not selection.contains({0}));
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
480 gl::setModelShaderSelectedObjects(&this->shaders, selection);
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
481 this->update();
333
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 329
diff changeset
482 #endif
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
483 }
313
c24d87f64bed Improve click handling
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 311
diff changeset
484
c24d87f64bed Improve click handling
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 311
diff changeset
485 glm::vec3 PartRenderer::cameraVector(const QPointF& point) const
c24d87f64bed Improve click handling
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 311
diff changeset
486 {
c24d87f64bed Improve click handling
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 311
diff changeset
487 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
488 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
489 return p2 - p1;
c24d87f64bed Improve click handling
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 311
diff changeset
490 }

mercurial