src/gl/partrenderer.cpp

Tue, 28 Jan 2020 23:34:49 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 28 Jan 2020 23:34:49 +0200
changeset 34
1de2b8d64e9f
parent 32
767592024ec5
child 35
98906a94732f
permissions
-rw-r--r--

added some sort of lighting

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>
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 #include "partrenderer.h"
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
26 PartRenderer::PartRenderer(Model* model, DocumentManager* documents, const ColorTable& colorTable, QWidget* parent) :
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
27 QOpenGLWidget{parent},
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
28 model{model},
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
29 documents{documents},
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
30 colorTable{colorTable},
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
31 compiler{new gl::Compiler{this->colorTable, this}}
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 this->setMouseTracking(true);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
36 PartRenderer::~PartRenderer()
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
37 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
38 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
39
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 void PartRenderer::initializeGL()
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 this->initializeOpenGLFunctions();
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
43 if (glGetError() != GL_NO_ERROR)
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 abort();
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 }
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
47 this->compiler->initialize();
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
48 this->compiler->build(this->model, this->documents);
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 this->initialized = true;
32
767592024ec5 fix default angle
Teemu Piippo <teemu@hecknology.net>
parents: 31
diff changeset
50 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
51 this->modelQuaternion *= glm::angleAxis(glm::radians(225.0f), glm::vec3{-0, 1, 0});
31
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
52 this->updateViewMatrix();
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
53 glLineWidth(2.0);
30
1536f23cfab7 it works!
Teemu Piippo <teemu@hecknology.net>
parents: 28
diff changeset
54 this->update();
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 }
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 void PartRenderer::resizeGL(int width, int height)
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 {
28
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
59 glViewport(0, 0, width, height);
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
60 this->projectionMatrix = glm::perspective(
31
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
61 glm::radians(45.0f),
28
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
62 static_cast<float>(width) / static_cast<float>(height),
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
63 0.1f,
31
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
64 10000.f);
34
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
65 this->compiler->setUniformMatrix("projectionMatrix", this->projectionMatrix);
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
68 static GLenum getGlTypeForArrayClass(const gl::ArrayClass vboClass)
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
69 {
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
70 switch (vboClass)
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
71 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
72 case gl::ArrayClass::Lines:
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
73 case gl::ArrayClass::ConditionalLines:
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
74 return GL_LINES;
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
75 case gl::ArrayClass::Triangles:
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
76 return GL_TRIANGLES;
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
77 case gl::ArrayClass::Quads:
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
78 return GL_QUADS;
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
79 }
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
80 throw std::runtime_error{"Bad vbo class passed to getGlTypeForVboClass"};
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
81 }
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
82
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83 void PartRenderer::paintGL()
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
85 /*
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
86 glEnable (GL_BLEND);
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
87 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
88 */
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
89 glEnable (GL_DEPTH_TEST);
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
90 glShadeModel (GL_SMOOTH);
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
91 glEnable (GL_MULTISAMPLE);
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
92 glEnable (GL_LINE_SMOOTH);
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
93 glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
94 this->renderScene();
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
95 }
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
96
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
97 void PartRenderer::renderScene()
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
98 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
99 glClearColor(0.8f, 0.8f, 0.8f, 1.0f);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
100 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
101 glEnable(GL_DEPTH_TEST);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
102 glEnable(GL_LIGHTING);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
103 glEnable(GL_POLYGON_OFFSET_FILL);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
104 glPolygonOffset(1.0f, 1.0f);
18
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
105 switch (this->renderStyle)
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
106 {
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
107 case gl::RenderStyle::Normal:
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
108 case gl::RenderStyle::BfcRedGreen:
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
109 case gl::RenderStyle::RandomColors:
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
110 break;
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
111 case gl::RenderStyle::Wireframe:
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
112 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
113 break;
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
114 }
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
115 // 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
116 renderVao(gl::ArrayClass::Triangles);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
117 renderVao(gl::ArrayClass::Quads);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
118 renderVao(gl::ArrayClass::Lines);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
119 glDisable(GL_POLYGON_OFFSET_FILL);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
120 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
121
31
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
122 void PartRenderer::updateViewMatrix()
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
123 {
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
124 // 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
125 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
126 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
127 this->compiler->setUniformMatrix("viewMatrix", this->viewMatrix);
31
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
128 }
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
129
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
130 void PartRenderer::renderVao(const gl::ArrayClass arrayClass)
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
131 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
132 this->compiler->bindVertexArray(arrayClass);
34
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
133 const std::size_t vertexCount = this->compiler->vertexCount(arrayClass);
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
134 glDrawArrays(getGlTypeForArrayClass(arrayClass), 0, static_cast<GLsizei>(vertexCount));
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
135 this->compiler->releaseVertexArray(arrayClass);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
136 this->checkForGLErrors();
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
137 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
138
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
139 void PartRenderer::checkForGLErrors()
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
140 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
141 GLenum glError;
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
142 QStringList errors;
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
143 while ((glError = glGetError()) != GL_NO_ERROR)
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
144 {
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
145 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
146 errors.append(glErrorString);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
147 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
148 if (not errors.isEmpty())
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
149 {
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
150 QMessageBox::critical(
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
151 this,
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
152 tr("Rendering error"),
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
153 QString{"Failed to render.\n%1"}.arg(errors.join("\n")));
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
154 }
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
155 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
156
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
157 void PartRenderer::mouseMoveEvent(QMouseEvent* event)
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
158 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
159 const bool left = event->buttons() & Qt::LeftButton;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
160 const QPointF move = pointToPointF(event->pos()) - this->lastMousePosition;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
161 if (left and not move.isNull())
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
162 {
31
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
163 // 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
164 // 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
165 // 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
166 const auto scalar = 0.006f;
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
167 const float move_x = static_cast<float>(move.x());
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
168 const float move_y = static_cast<float>(move.y());
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
169 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
170 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
171 this->modelQuaternion = q_x * q_y * this->modelQuaternion;
34
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
172 this->compiler->setUniformMatrix("modelMatrix", glm::mat4_cast(this->modelQuaternion));
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
173 this->update();
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
174 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
175 this->lastMousePosition = pointToPointF(event->pos());
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
176 }
18
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
177
31
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
178 void PartRenderer::wheelEvent(QWheelEvent* event)
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
179 {
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
180 static constexpr double WHEEL_STEP = 1 / 1000.0;
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
181 const double move = (-event->angleDelta().y()) * WHEEL_STEP;
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
182 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
183 this->updateViewMatrix();
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
184 this->update();
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
185 }
b6df269a2c6b fix remaining rendering control issues
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
186
18
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
187 void PartRenderer::setRenderStyle(const gl::RenderStyle newStyle)
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
188 {
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
189 this->renderStyle = newStyle;
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
190 this->update();
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
191 }
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
192

mercurial