src/gl/partrenderer.cpp

Wed, 22 Jan 2020 22:43:28 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 22 Jan 2020 22:43:28 +0200
changeset 29
4cc6b582fde8
parent 28
c92c1daf735f
child 30
1536f23cfab7
permissions
-rw-r--r--

added FindGLM.cmake

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 }
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
47 glEnableClientState(GL_NORMAL_ARRAY);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
48 glEnableClientState(GL_VERTEX_ARRAY);
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
49 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
50 this->compiler->build(this->model, this->documents);
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 this->initializeLighting();
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 this->initialized = true;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 this->rotation = QQuaternion::fromAxisAndAngle({1, 0, 0}, 30);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 this->rotation *= QQuaternion::fromAxisAndAngle({0, 1, 0}, 330);
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
55 glLineWidth(2.0);
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
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 /*
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 * Pads a 3×3 matrix into a 4×4 one by adding cells from the identity matrix.
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 */
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61 static QMatrix4x4 padMatrix(const QMatrix3x3& stub)
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 return {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 stub(0, 0), stub(0, 1), stub(0, 2), 0,
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 stub(1, 0), stub(1, 1), stub(1, 2), 0,
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 stub(2, 0), stub(2, 1), stub(2, 2), 0,
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 0, 0, 0, 1
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 }
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::initializeLighting()
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 GLfloat materialShininess[] = {5.0};
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 GLfloat lightPosition[] = {1.0, 1.0, 1.0, 0.0};
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76 GLfloat ambientLightingLevel[] = {0.5, 0.5, 0.5, 1.0};
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77 glShadeModel(GL_SMOOTH);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78 glMaterialfv(GL_FRONT, GL_SHININESS, materialShininess);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLightingLevel);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
80 glLightfv(GL_LIGHT0, GL_DIFFUSE, ambientLightingLevel);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
81 glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
82 glEnable(GL_LIGHTING);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83 glEnable(GL_LIGHT0);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84 glEnable(GL_COLOR_MATERIAL);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
85 glEnable(GL_DEPTH_TEST);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
86 }
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 void PartRenderer::resizeGL(int width, int height)
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
89 {
28
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
90 glViewport(0, 0, width, height);
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
91 this->projectionMatrix = glm::perspective(
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
92 glm::radians(90.0f),
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
93 static_cast<float>(width) / static_cast<float>(height),
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
94 0.1f,
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
95 100.f);
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
96 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
97
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
98 static GLenum getGlTypeForArrayClass(const gl::ArrayClass vboClass)
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
99 {
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
100 switch (vboClass)
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
101 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
102 case gl::ArrayClass::Lines:
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
103 case gl::ArrayClass::ConditionalLines:
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
104 return GL_LINES;
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
105 case gl::ArrayClass::Triangles:
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
106 return GL_TRIANGLES;
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
107 case gl::ArrayClass::Quads:
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
108 return GL_QUADS;
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
109 }
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
110 throw std::runtime_error{"Bad vbo class passed to getGlTypeForVboClass"};
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
111 }
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
112
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
113 void PartRenderer::paintGL()
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
114 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
115 /*
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
116 glEnable (GL_BLEND);
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
117 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
118 */
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
119 glEnable (GL_DEPTH_TEST);
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
120 glShadeModel (GL_SMOOTH);
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
121 glEnable (GL_MULTISAMPLE);
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
122 glEnable (GL_LINE_SMOOTH);
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
123 glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
124 this->renderScene();
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
125 }
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
126
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
127 void PartRenderer::renderScene()
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
128 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
129 glClearColor(0.8f, 0.8f, 0.8f, 1.0f);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
130 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
131 glMatrixMode(GL_MODELVIEW);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
132 glEnable(GL_DEPTH_TEST);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
133 glEnable(GL_LIGHTING);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
134 glLoadIdentity();
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
135 //glTranslated(0.0, 0.0, -4.5 * this->compiler->modelDistance());
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
136 //glTranslated(0.0, 0.0, -4.5);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
137 //glMultMatrixf(padMatrix(this->rotation.toRotationMatrix()).constData());
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
138 //xyz(glTranslatef, -this->compiler->modelCenter());
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
139 auto rotationMatrix = padMatrix(this->rotation.toRotationMatrix());
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
140 rotationMatrix(2, 3) = 0;
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
141 glEnable(GL_POLYGON_OFFSET_FILL);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
142 glPolygonOffset(1.0f, 1.0f);
18
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
143 switch (this->renderStyle)
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
144 {
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
145 case gl::RenderStyle::Normal:
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
146 case gl::RenderStyle::BfcRedGreen:
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
147 case gl::RenderStyle::RandomColors:
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
148 break;
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
149 case gl::RenderStyle::Wireframe:
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
150 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
151 break;
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
152 }
28
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
153 this->compiler->setUniform("CameraTransformation", gl::toQMatrix(this->projectionMatrix * this->viewMatrix));
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
154 // 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
155 renderVao(gl::ArrayClass::Triangles);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
156 renderVao(gl::ArrayClass::Quads);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
157 renderVao(gl::ArrayClass::Lines);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
158 glDisable(GL_POLYGON_OFFSET_FILL);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
159 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
160
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
161 void PartRenderer::renderVao(const gl::ArrayClass arrayClass)
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
162 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
163 this->compiler->bindVertexArray(arrayClass);
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
164 const std::size_t vertexCount = this->compiler->vboSize(arrayClass) / gl::FLOATS_PER_VERTEX;
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
165 glDrawArrays(getGlTypeForArrayClass(arrayClass), 0, static_cast<GLsizei>(vertexCount));
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
166 this->compiler->releaseVertexArray(arrayClass);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
167 this->checkForGLErrors();
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
168 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
169
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
170 void PartRenderer::checkForGLErrors()
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
171 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
172 GLenum glError;
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
173 QStringList errors;
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
174 while ((glError = glGetError()) != GL_NO_ERROR)
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
175 {
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
176 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
177 errors.append(glErrorString);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
178 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
179 if (not errors.isEmpty())
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
180 {
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
181 QMessageBox::critical(
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
182 this,
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
183 tr("Rendering error"),
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
184 QString{"Failed to render.\n%1"}.arg(errors.join("\n")));
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
185 }
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
186 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
187
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
188 void PartRenderer::mouseMoveEvent(QMouseEvent* event)
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
189 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
190 const bool left = event->buttons() & Qt::LeftButton;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
191 const QPointF move = pointToPointF(event->pos()) - this->lastMousePosition;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
192 if (left and not move.isNull())
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
193 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
194 const QQuaternion versor = QQuaternion::fromAxisAndAngle(
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
195 QVector3D{static_cast<float>(move.y()), static_cast<float>(move.x()), 0.0f},
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
196 0.6f * static_cast<float>(std::hypot(move.x(), move.y()))
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
197 );
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
198 this->rotation = versor * this->rotation;
28
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
199 QVector3D cameraPosition = this->rotation.rotatedVector({0, 0, 4.5});
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
200 glm::vec3 cameraPosition_glm = {cameraPosition.x(), cameraPosition.y(), cameraPosition.z()};
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
201 this->viewMatrix = glm::lookAt(cameraPosition_glm, {0, 0, 0}, {0, -1, 0});
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
202 this->update();
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
203 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
204 this->lastMousePosition = pointToPointF(event->pos());
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
205 }
18
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
206
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
207 void PartRenderer::setCompiler(gl::Compiler* compiler)
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
208 {
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
209 this->compiler = compiler;
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
210 }
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
211
18
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
212 void PartRenderer::setRenderStyle(const gl::RenderStyle newStyle)
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
213 {
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
214 this->renderStyle = newStyle;
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
215 this->update();
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
216 }
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
217

mercurial