src/gl/partrenderer.cpp

Wed, 26 Feb 2020 02:21:07 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 26 Feb 2020 02:21:07 +0200
changeset 55
cb81ecb5fb23
parent 53
3af627f7a40f
child 56
fad4a5dd8dee
permissions
-rw-r--r--

grid stuff

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
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
19 #include <GL/glut.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>
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 #include <QMouseEvent>
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
23 #include <QMessageBox>
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
24 #include <QAbstractButton>
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
25 #include "geometry.h"
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 #include "partrenderer.h"
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27
35
98906a94732f renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents: 34
diff changeset
28 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
29 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
30 DocumentManager* documents,
98906a94732f renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents: 34
diff changeset
31 const ldraw::ColorTable& colorTable,
98906a94732f renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents: 34
diff changeset
32 QWidget* parent) :
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
33 QOpenGLWidget{parent},
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
34 model{model},
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
35 documents{documents},
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
36 colorTable{colorTable},
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
37 compiler{new gl::Compiler{this->colorTable, this}},
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
38 gridProgram{this}
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 this->setMouseTracking(true);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
43 PartRenderer::~PartRenderer()
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
44 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
45 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
46
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
47 static QVector3D vec3FromQColor(const QColor& color)
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
48 {
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
49 return {(float)color.redF(), (float)color.greenF(), (float)color.blueF()};
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
50 }
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
51
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 void PartRenderer::initializeGL()
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 this->initializeOpenGLFunctions();
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
55 if (glGetError() != GL_NO_ERROR)
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 abort();
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 }
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
59 this->gridProgram.emplace(this);
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
60 this->gridProgram->initialize();
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
61 this->compiler->initialize();
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents: 37
diff changeset
62 this->compiler->build(this->model, this->documents, this->renderPreferences);
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 this->initialized = true;
32
767592024ec5 fix default angle
Teemu Piippo <teemu@hecknology.net>
parents: 31
diff changeset
64 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
65 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
66 this->setupBackgroundColor();
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
67 this->updateModelMatrix();
31
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
68 this->updateViewMatrix();
30
1536f23cfab7 it works!
Teemu Piippo <teemu@hecknology.net>
parents: 28
diff changeset
69 this->update();
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 void PartRenderer::resizeGL(int width, int height)
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 {
28
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
74 glViewport(0, 0, width, height);
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
75 this->viewportMatrix = {
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
76 {1, 0, 0},
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
77 {0, 1, 0},
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
78 {width * 0.5f, height * 0.5f, 1}
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
79 };
28
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
80 this->projectionMatrix = glm::perspective(
31
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
81 glm::radians(45.0f),
28
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
82 static_cast<float>(width) / static_cast<float>(height),
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
83 0.1f,
31
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
84 10000.f);
34
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
85 this->compiler->setUniformMatrix("projectionMatrix", this->projectionMatrix);
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
86 this->gridProgram->setProjectionMatrix(this->projectionMatrix);
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
87 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
88
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
89 static GLenum getGlTypeForArrayClass(const gl::ArrayClass vboClass)
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
90 {
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
91 switch (vboClass)
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
92 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
93 case gl::ArrayClass::Lines:
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
94 case gl::ArrayClass::ConditionalLines:
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
95 return GL_LINES;
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
96 case gl::ArrayClass::Triangles:
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
97 return GL_TRIANGLES;
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
98 case gl::ArrayClass::Quads:
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
99 return GL_QUADS;
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
100 }
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
101 throw std::runtime_error{"Bad vbo class passed to getGlTypeForVboClass"};
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
102 }
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
103
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
104 void PartRenderer::paintGL()
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
105 {
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
106 glEnable(GL_DEPTH_TEST);
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
107 glShadeModel(GL_SMOOTH);
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
108 this->renderScene();
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
109 }
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
110
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
111 void PartRenderer::renderScene()
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
112 {
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
113 this->checkForGLErrors();
46
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
114 if (this->renderPreferences.lineAntiAliasing && this->renderPreferences.style != gl::RenderStyle::PickScene)
45
272c84c7c87e added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents: 44
diff changeset
115 {
272c84c7c87e added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents: 44
diff changeset
116 glEnable(GL_LINE_SMOOTH);
272c84c7c87e added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents: 44
diff changeset
117 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
272c84c7c87e added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents: 44
diff changeset
118 }
272c84c7c87e added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents: 44
diff changeset
119 else {
272c84c7c87e added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents: 44
diff changeset
120 glDisable(GL_LINE_SMOOTH);
272c84c7c87e added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents: 44
diff changeset
121 }
46
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
122 if (this->renderPreferences.style != gl::RenderStyle::PickScene)
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
123 {
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
124 const QColor& backgroundColor = this->renderPreferences.backgroundColor;
46
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
125 glClearColor(
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
126 static_cast<float>(backgroundColor.redF()),
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
127 static_cast<float>(backgroundColor.greenF()),
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
128 static_cast<float>(backgroundColor.blueF()),
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
129 1.0f);
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
130 this->compiler->setUniform("useLighting", GL_TRUE);
46
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
131 }
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
132 else
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
133 {
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
134 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
135 this->compiler->setUniform("useLighting", GL_FALSE);
46
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
136 }
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
137 this->checkForGLErrors();
48
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
138 this->compiler->setUniform("selectedColor", vec3FromQColor(this->renderPreferences.selectedColor));
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
139 this->compiler->setUniform("highlighted", this->highlighted.value);
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
140 this->checkForGLErrors();
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
141 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
142 glEnable(GL_DEPTH_TEST);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
143 glEnable(GL_POLYGON_OFFSET_FILL);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
144 glPolygonOffset(1.0f, 1.0f);
44
c6114b3af3a6 added configurable line thickness
Teemu Piippo <teemu@hecknology.net>
parents: 40
diff changeset
145 glLineWidth(this->renderPreferences.lineThickness);
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents: 37
diff changeset
146 switch (this->renderPreferences.style)
18
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
147 {
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
148 case gl::RenderStyle::Normal:
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
149 this->setFragmentStyle(gl::FragmentStyle::Normal);
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
150 this->renderAllArrays();
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
151 break;
18
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
152 case gl::RenderStyle::BfcRedGreen:
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
153 glEnable(GL_CULL_FACE);
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
154 glCullFace(GL_BACK);
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
155 this->setFragmentStyle(gl::FragmentStyle::BfcGreen);
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
156 renderVao(gl::ArrayClass::Triangles);
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
157 renderVao(gl::ArrayClass::Quads);
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
158 glCullFace(GL_FRONT);
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
159 this->setFragmentStyle(gl::FragmentStyle::BfcRed);
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
160 renderVao(gl::ArrayClass::Triangles);
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
161 renderVao(gl::ArrayClass::Quads);
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
162 glDisable(GL_CULL_FACE);
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
163 this->setFragmentStyle(gl::FragmentStyle::Normal);
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
164 renderVao(gl::ArrayClass::Lines);
18
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
165 case gl::RenderStyle::RandomColors:
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
166 this->setFragmentStyle(gl::FragmentStyle::RandomColors);
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
167 this->renderAllArrays();
18
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
168 break;
46
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
169 case gl::RenderStyle::PickScene:
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
170 glLineWidth(3.0f);
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
171 this->setFragmentStyle(gl::FragmentStyle::Id);
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
172 this->renderAllArrays();
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
173 break;
18
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
174 case gl::RenderStyle::Wireframe:
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
175 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
176 this->setFragmentStyle(gl::FragmentStyle::Normal);
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
177 this->renderAllArrays();
18
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
178 break;
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
179 }
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
180 glEnable(GL_BLEND);
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
181 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
182 this->gridProgram->draw();
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
183 glDisable(GL_BLEND);
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
184 glDisable(GL_POLYGON_OFFSET_FILL);
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
185 }
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
186
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
187 void PartRenderer::renderAllArrays()
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
188 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
189 // Lines need to be rendered last so that anti-aliasing does not interfere with polygon rendering.
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
190 renderVao(gl::ArrayClass::Triangles);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
191 renderVao(gl::ArrayClass::Quads);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
192 renderVao(gl::ArrayClass::Lines);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
193 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
194
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
195
31
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
196 void PartRenderer::updateViewMatrix()
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
197 {
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
198 // I'm not quite sure why using the exponent function on the zoom factor causes linear zoom behavior
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
199 const double z = 2 * std::exp(this->zoom) * (1 + this->compiler->modelDistance());
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
200 this->viewMatrix = glm::lookAt(glm::vec3{0, 0, z}, {0, 0, 0}, {0, -1, 0});
34
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
201 this->compiler->setUniformMatrix("viewMatrix", this->viewMatrix);
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
202 this->gridProgram->setViewMatrix(this->viewMatrix);
31
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
203 }
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
204
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
205 void PartRenderer::updateModelMatrix()
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
206 {
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
207 const glm::mat4 modelMatrix = glm::mat4_cast(this->modelQuaternion);
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
208 this->compiler->setUniformMatrix("modelMatrix", modelMatrix);
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
209 this->gridProgram->setModelMatrix(modelMatrix);
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
210 this->update();
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
211 }
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
212
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
213 void PartRenderer::setupBackgroundColor()
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
214 {
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
215 if (this->gridProgram.has_value())
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
216 {
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
217 const bool isDark = luma(this->renderPreferences.backgroundColor) < 0.25;
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
218 this->gridProgram->setGridColor(isDark ? Qt::white : Qt::black);
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
219 }
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
220 }
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
221
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
222 void PartRenderer::renderVao(const gl::ArrayClass arrayClass)
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
223 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
224 this->compiler->bindVertexArray(arrayClass);
34
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
225 const std::size_t vertexCount = this->compiler->vertexCount(arrayClass);
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
226 this->checkForGLErrors();
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
227 glDrawArrays(getGlTypeForArrayClass(arrayClass), 0, static_cast<GLsizei>(vertexCount));
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
228 this->checkForGLErrors();
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
229 this->compiler->releaseVertexArray(arrayClass);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
230 this->checkForGLErrors();
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
231 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
232
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
233 void PartRenderer::checkForGLErrors()
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
234 {
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
235 gl::checkForGLErrors(this);
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
236 }
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
237
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
238 void gl::checkForGLErrors(QWidget* parent)
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
239 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
240 GLenum glError;
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
241 QStringList errors;
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
242 while ((glError = glGetError()) != GL_NO_ERROR)
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
243 {
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
244 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
245 errors.append(glErrorString);
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 if (not errors.isEmpty())
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 QMessageBox box{parent};
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
250 box.setIcon(QMessageBox::Critical);
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
251 box.setText(QObject::tr("OpenGL error: %1").arg(errors.join("\n")));
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
252 box.setWindowTitle(QObject::tr("OpenGL error"));
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
253 box.setStandardButtons(QMessageBox::Close);
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
254 box.button(QMessageBox::Close)->setText(QObject::tr("Damn it"));
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
255 box.exec();
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
256 }
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
257 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
258
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
259 void PartRenderer::mouseMoveEvent(QMouseEvent* event)
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
260 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
261 const bool left = event->buttons() & Qt::LeftButton;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
262 const QPointF move = pointToPointF(event->pos()) - this->lastMousePosition;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
263 if (left and not move.isNull())
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
264 {
31
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
265 // q_x is the rotation of the brick along the vertical y-axis, because turning the
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
266 // vertical axis causes horizontal (=x) rotation. Likewise q_y is the rotation of the
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
267 // brick along the horizontal x-axis, which causes vertical rotation.
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
268 const auto scalar = 0.006f;
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
269 const float move_x = static_cast<float>(move.x());
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
270 const float move_y = static_cast<float>(move.y());
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
271 const glm::quat q_x = glm::angleAxis(scalar * move_x, glm::vec3{0, -1, 0});
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
272 const glm::quat q_y = glm::angleAxis(scalar * move_y, glm::vec3{-1, 0, 0});
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
273 this->modelQuaternion = q_x * q_y * this->modelQuaternion;
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
274 this->updateModelMatrix();
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
275 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
276 this->lastMousePosition = pointToPointF(event->pos());
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
277 }
18
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
278
31
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
279 void PartRenderer::wheelEvent(QWheelEvent* event)
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
280 {
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
281 static constexpr double WHEEL_STEP = 1 / 1000.0;
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
282 const double move = (-event->angleDelta().y()) * WHEEL_STEP;
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
283 this->zoom = std::clamp(this->zoom + move, MIN_ZOOM, MAX_ZOOM);
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
284 this->updateViewMatrix();
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
285 this->update();
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
286 }
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
287
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
288 std::optional<glm::vec3> PartRenderer::cameraToGrid(const QPoint& point)
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
289 {
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
290 glm::vec3 pp = {point.x(), this->height() - point.y(), 1};
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
291 pp = glm::inverse(this->viewportMatrix) * pp;
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
292 glm::mat4 matrix;
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
293 matrix = this->projectionMatrix * this->viewMatrix * glm::mat4_cast(this->modelQuaternion);
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
294 matrix = glm::transpose(glm::inverse(matrix));
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
295 auto p1 = matrix * glm::vec4{pp.x, pp.y, 0, 1};
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
296 auto p2 = matrix * glm::vec4{pp.x, pp.y, 1, 1};
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
297 geom::Line line = geom::lineFromPoints(p1, p2);
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
298 return geom::linePlaneIntersection(line, geom::XY);
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
299 }
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
300
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
301 QPointF PartRenderer::worldToCamera(const glm::vec3& point)
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
302 {
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
303 glm::vec4 p = {point, 1};
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
304 p = glm::mat4_cast(this->modelQuaternion) * p;
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
305 p = this->viewMatrix * p;
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
306 p = this->projectionMatrix * p;
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
307 glm::vec2 pp = this->viewportMatrix * glm::vec3{p.x, p.y, 1};
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
308 return toQPointF(pp);
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
309 /*
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
310 const glm::mat4 matrix = this->projectionMatrix * this->viewMatrix * glm::mat4_cast(this->modelQuaternion);
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
311 return toQPointF(matrix * glm::vec4{point, 1});
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
312 */
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
313 }
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
314
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
315 ldraw::Id PartRenderer::pick(const QPoint& where)
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
316 {
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
317 const gl::RenderStyle oldRenderStyle = this->renderPreferences.style;
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
318 this->renderPreferences.style = gl::RenderStyle::PickScene;
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
319 this->makeCurrent();
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
320 this->renderScene();
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
321 std::array<GLbyte, 3> data;
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
322 this->checkForGLErrors();
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
323 glReadPixels(where.x(), this->height() - 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
324 this->checkForGLErrors();
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
325 this->renderPreferences.style = oldRenderStyle;
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
326 this->update();
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
327 return gl::Compiler::idFromColor(data);
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
328 }
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
329
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
330 /**
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
331 * @brief Changes the color of rendered fragments
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
332 * @param newFragmentStyle new fragment style to use
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
333 */
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
334 void PartRenderer::setFragmentStyle(gl::FragmentStyle newFragmentStyle)
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
335 {
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
336 this->compiler->setUniform("fragmentStyle", static_cast<int>(newFragmentStyle));
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
337 }
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
338
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
339 /**
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
340 * @brief Changes the way the scene is rendered
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
341 * @param newStyle new render style to use
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
342 */
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents: 37
diff changeset
343 void PartRenderer::setRenderPreferences(const gl::RenderPreferences& newPreferences)
18
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
344 {
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents: 37
diff changeset
345 bool mainColorChanged = this->renderPreferences.mainColor != newPreferences.mainColor;
44
c6114b3af3a6 added configurable line thickness
Teemu Piippo <teemu@hecknology.net>
parents: 40
diff changeset
346 bool backgroundColorChanged = this->renderPreferences.backgroundColor != newPreferences.backgroundColor;
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents: 37
diff changeset
347 this->renderPreferences = newPreferences;
44
c6114b3af3a6 added configurable line thickness
Teemu Piippo <teemu@hecknology.net>
parents: 40
diff changeset
348 if (mainColorChanged or backgroundColorChanged)
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents: 37
diff changeset
349 {
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents: 37
diff changeset
350 this->compiler->build(this->model, this->documents, this->renderPreferences);
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
351 this->setupBackgroundColor();
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
352
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents: 37
diff changeset
353 }
18
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
354 this->update();
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
355 }

mercurial