src/gl/partrenderer.cpp

Wed, 22 Jan 2020 01:17:11 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 22 Jan 2020 01:17:11 +0200
changeset 27
c57fb7a5ffa3
parent 26
3a9e761e4faa
child 28
c92c1daf735f
permissions
-rw-r--r--

commit work done on plugging vao to the gl renderer, renders nonsense for now

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>
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 #include <QMouseEvent>
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
21 #include <QMessageBox>
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 #include "partrenderer.h"
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
24 PartRenderer::PartRenderer(Model* model, DocumentManager* documents, const ColorTable& colorTable, QWidget* parent) :
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
25 QOpenGLWidget{parent},
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
26 model{model},
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
27 documents{documents},
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
28 colorTable{colorTable},
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
29 compiler{new gl::Compiler{this->colorTable, this}}
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 this->setMouseTracking(true);
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
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
34 PartRenderer::~PartRenderer()
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
35 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
36 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
37
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 void PartRenderer::initializeGL()
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->initializeOpenGLFunctions();
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
41 if (glGetError() != GL_NO_ERROR)
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 abort();
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 }
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
45 glEnableClientState(GL_NORMAL_ARRAY);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
46 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
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->initializeLighting();
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 this->initialized = true;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 this->rotation = QQuaternion::fromAxisAndAngle({1, 0, 0}, 30);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 this->rotation *= QQuaternion::fromAxisAndAngle({0, 1, 0}, 330);
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
53 glLineWidth(2.0);
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 }
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 * 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
58 */
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 static QMatrix4x4 padMatrix(const QMatrix3x3& stub)
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 return {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62 stub(0, 0), stub(0, 1), stub(0, 2), 0,
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 stub(1, 0), stub(1, 1), stub(1, 2), 0,
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 stub(2, 0), stub(2, 1), stub(2, 2), 0,
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 0, 0, 0, 1
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 }
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 void PartRenderer::initializeLighting()
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 GLfloat materialShininess[] = {5.0};
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 GLfloat lightPosition[] = {1.0, 1.0, 1.0, 0.0};
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 GLfloat ambientLightingLevel[] = {0.5, 0.5, 0.5, 1.0};
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 glShadeModel(GL_SMOOTH);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76 glMaterialfv(GL_FRONT, GL_SHININESS, materialShininess);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77 glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLightingLevel);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78 glLightfv(GL_LIGHT0, GL_DIFFUSE, ambientLightingLevel);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
80 glEnable(GL_LIGHTING);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
81 glEnable(GL_LIGHT0);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
82 glEnable(GL_COLOR_MATERIAL);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83 glEnable(GL_DEPTH_TEST);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
85
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
86 void PartRenderer::resizeGL(int width, int height)
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
87 {
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
88 constexpr GLdouble near = 1.0;
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
89 constexpr GLdouble far = 1e+05;
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
90 glViewport (0, 0, width, height);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
91 glMatrixMode(GL_PROJECTION);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
92 glLoadIdentity();
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
93 gluPerspective(45.0, static_cast<double>(width) / static_cast<double>(height), near, far);
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
94 glMatrixMode(GL_MODELVIEW);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
96
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
97 static GLenum getGlTypeForArrayClass(const gl::ArrayClass vboClass)
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
98 {
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
99 switch (vboClass)
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
100 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
101 case gl::ArrayClass::Lines:
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
102 case gl::ArrayClass::ConditionalLines:
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
103 return GL_LINES;
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
104 case gl::ArrayClass::Triangles:
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
105 return GL_TRIANGLES;
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
106 case gl::ArrayClass::Quads:
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
107 return GL_QUADS;
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
108 }
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
109 throw std::runtime_error{"Bad vbo class passed to getGlTypeForVboClass"};
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
110 }
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
111
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
112 void PartRenderer::paintGL()
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
113 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
114 /*
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
115 glEnable (GL_BLEND);
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
116 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
117 */
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
118 glEnable (GL_DEPTH_TEST);
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
119 glShadeModel (GL_SMOOTH);
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
120 glEnable (GL_MULTISAMPLE);
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
121 glEnable (GL_LINE_SMOOTH);
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
122 glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
123 this->renderScene();
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
124 }
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 void PartRenderer::renderScene()
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
127 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
128 glClearColor(0.8f, 0.8f, 0.8f, 1.0f);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
129 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
130 glMatrixMode(GL_MODELVIEW);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
131 glEnable(GL_DEPTH_TEST);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
132 glEnable(GL_LIGHTING);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
133 glLoadIdentity();
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
134 //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
135 //glTranslated(0.0, 0.0, -4.5);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
136 //glMultMatrixf(padMatrix(this->rotation.toRotationMatrix()).constData());
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
137 //xyz(glTranslatef, -this->compiler->modelCenter());
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
138 auto rotationMatrix = padMatrix(this->rotation.toRotationMatrix());
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
139 rotationMatrix(2, 3) = 0;
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
140 glEnable(GL_POLYGON_OFFSET_FILL);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
141 glPolygonOffset(1.0f, 1.0f);
18
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
142 switch (this->renderStyle)
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
143 {
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
144 case gl::RenderStyle::Normal:
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
145 case gl::RenderStyle::BfcRedGreen:
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
146 case gl::RenderStyle::RandomColors:
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
147 break;
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
148 case gl::RenderStyle::Wireframe:
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
149 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
150 break;
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
151 }
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
152 this->compiler->setUniform("CameraTransformation", rotationMatrix);
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
153 // 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
154 renderVao(gl::ArrayClass::Triangles);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
155 renderVao(gl::ArrayClass::Quads);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
156 renderVao(gl::ArrayClass::Lines);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
157 glDisable(GL_POLYGON_OFFSET_FILL);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
158 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
159
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
160 void PartRenderer::renderVao(const gl::ArrayClass arrayClass)
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
161 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
162 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
163 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
164 glDrawArrays(getGlTypeForArrayClass(arrayClass), 0, static_cast<GLsizei>(vertexCount));
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
165 this->compiler->releaseVertexArray(arrayClass);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
166 this->checkForGLErrors();
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
167 }
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 void PartRenderer::checkForGLErrors()
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
170 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
171 GLenum glError;
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
172 QStringList errors;
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
173 while ((glError = glGetError()) != GL_NO_ERROR)
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
174 {
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
175 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
176 errors.append(glErrorString);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
177 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
178 if (not errors.isEmpty())
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
179 {
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
180 QMessageBox::critical(
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
181 this,
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
182 tr("Rendering error"),
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
183 QString{"Failed to render.\n%1"}.arg(errors.join("\n")));
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
184 }
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
185 }
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 void PartRenderer::mouseMoveEvent(QMouseEvent* event)
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
188 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
189 const bool left = event->buttons() & Qt::LeftButton;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
190 const QPointF move = pointToPointF(event->pos()) - this->lastMousePosition;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
191 if (left and not move.isNull())
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
192 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
193 const QQuaternion versor = QQuaternion::fromAxisAndAngle(
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
194 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
195 0.6f * static_cast<float>(std::hypot(move.x(), move.y()))
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
196 );
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
197 this->rotation = versor * this->rotation;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
198 this->update();
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
199 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
200 this->lastMousePosition = pointToPointF(event->pos());
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
201 }
18
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
202
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
203 void PartRenderer::setCompiler(gl::Compiler* compiler)
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
204 {
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
205 this->compiler = compiler;
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
206 }
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
207
18
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
208 void PartRenderer::setRenderStyle(const gl::RenderStyle newStyle)
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
209 {
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
210 this->renderStyle = newStyle;
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
211 this->update();
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
212 }
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
213

mercurial