src/gl/partrenderer.cpp

changeset 21
0133e565e072
parent 18
918b6c0f8b5b
child 22
6da867fa5429
equal deleted inserted replaced
20:cef43609a374 21:0133e565e072
1 #include <QMouseEvent> 1 #include <QMouseEvent>
2 #include <GL/glut.h> // teapot 2 #include <GL/glut.h> // teapot
3 #include "partrenderer.h" 3 #include "partrenderer.h"
4 4
5 PartRenderer::PartRenderer(QWidget* parent) : 5 PartRenderer::PartRenderer(Model* model, DocumentManager* documents, QWidget* parent) :
6 QOpenGLWidget{parent} 6 QOpenGLWidget{parent},
7 model{model},
8 documents{documents},
9 compiler{new gl::Compiler{this}}
7 { 10 {
8 this->setMouseTracking(true); 11 this->setMouseTracking(true);
9 } 12 }
10 13
11 void PartRenderer::initializeGL() 14 void PartRenderer::initializeGL()
17 } 20 }
18 this->initializeLighting(); 21 this->initializeLighting();
19 this->initialized = true; 22 this->initialized = true;
20 this->rotation = QQuaternion::fromAxisAndAngle({1, 0, 0}, 30); 23 this->rotation = QQuaternion::fromAxisAndAngle({1, 0, 0}, 30);
21 this->rotation *= QQuaternion::fromAxisAndAngle({0, 1, 0}, 330); 24 this->rotation *= QQuaternion::fromAxisAndAngle({0, 1, 0}, 330);
25 this->compiler->build(this->model, this->documents);
22 } 26 }
23 27
24 /* 28 /*
25 * Pads a 3×3 matrix into a 4×4 one by adding cells from the identity matrix. 29 * Pads a 3×3 matrix into a 4×4 one by adding cells from the identity matrix.
26 */ 30 */
60 glLoadIdentity(); 64 glLoadIdentity();
61 gluPerspective(45.0f, static_cast<double>(width) / static_cast<double>(height), near, far); 65 gluPerspective(45.0f, static_cast<double>(width) / static_cast<double>(height), near, far);
62 glMatrixMode(GL_MODELVIEW); 66 glMatrixMode(GL_MODELVIEW);
63 } 67 }
64 68
69 static int getGlTypeForVboClass(const gl::VboClass vboClass)
70 {
71 switch (vboClass)
72 {
73 case gl::VboClass::Lines:
74 case gl::VboClass::ConditionalLines:
75 return GL_LINES;
76 case gl::VboClass::Triangles:
77 return GL_TRIANGLES;
78 case gl::VboClass::Quads:
79 return GL_QUADS;
80 }
81 throw std::runtime_error{"Bad vbo class passed to getGlTypeForVboClass"};
82 }
83
65 // https://www.codemiles.com/c-opengl-examples/drawing-teapot-using-opengl-t9010.html?mobile=on 84 // https://www.codemiles.com/c-opengl-examples/drawing-teapot-using-opengl-t9010.html?mobile=on
85 #include <QMessageBox>
66 void PartRenderer::paintGL() 86 void PartRenderer::paintGL()
67 { 87 {
68 switch (this->renderStyle) 88 switch (this->renderStyle)
69 { 89 {
70 case gl::RenderStyle::Normal: 90 case gl::RenderStyle::Normal:
75 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 95 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
76 break; 96 break;
77 } 97 }
78 glMatrixMode(GL_MODELVIEW); 98 glMatrixMode(GL_MODELVIEW);
79 // clear the drawing buffer. 99 // clear the drawing buffer.
80 glClear(GL_COLOR_BUFFER_BIT); 100 glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
101 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
102 glEnable(GL_DEPTH_TEST);
103 glEnable(GL_LIGHTING);
81 // clear the identity matrix. 104 // clear the identity matrix.
82 glLoadIdentity(); 105 glLoadIdentity();
83 // traslate the draw by z = -4.0 106 // traslate the draw by z = -4.0
84 // Note this when you decrease z like -8.0 the drawing will looks far , or smaller. 107 // Note this when you decrease z like -8.0 the drawing will looks far , or smaller.
85 glTranslatef(0.0,0.0,-4.5); 108 glTranslatef(0.0,0.0,-4.5);
86 // Red color used to draw. 109 // Red color used to draw.
87 glColor3f(0.8, 0.2, 0.1); 110 glColor3f(0.8, 0.2, 0.1);
88 glMultMatrixf(padMatrix(this->rotation.toRotationMatrix()).constData()); 111 glMultMatrixf(padMatrix(this->rotation.toRotationMatrix()).constData());
89 glutSolidTeapot(1.0); 112 //glutSolidTeapot(1.0);
90 glFlush(); 113 glEnableClientState(GL_VERTEX_ARRAY);
114 glEnableClientState(GL_COLOR_ARRAY);
115 for (const gl::VboClass vboClass : {gl::VboClass::Lines, gl::VboClass::Triangles, gl::VboClass::Quads})
116 {
117 const int vboSurfaces = this->compiler->vbo({vboClass, gl::VboSubclass::Surfaces});
118 const int vboColors = this->compiler->vbo({vboClass, gl::VboSubclass::RegularColors});
119 const int vboNormals = this->compiler->vbo({vboClass, gl::VboSubclass::Normals});
120 const int count = this->compiler->vboSize({vboClass, gl::VboSubclass::Surfaces}) / 3;
121 glBindBuffer(GL_ARRAY_BUFFER, vboSurfaces);
122 glVertexPointer(3, GL_FLOAT, 0, nullptr);
123 glBindBuffer(GL_ARRAY_BUFFER, vboColors);
124 glColorPointer(4, GL_FLOAT, 0, nullptr);
125 //glBindBuffer(GL_ARRAY_BUFFER, vboNormals);
126 //glNormalPointer(GL_FLOAT, 0, nullptr);
127 glDrawArrays(getGlTypeForVboClass(vboClass), 0, count);
128 }
129 glBindBuffer(GL_ARRAY_BUFFER, 0);
130 glDisableClientState(GL_VERTEX_ARRAY);
131 glDisableClientState(GL_COLOR_ARRAY);
132
133 //glFlush();
134 const int glError = this->glGetError();
135 if (glError != GL_NO_ERROR)
136 {
137 const QString glErrorString = QString::fromLatin1(reinterpret_cast<const char*>(::gluErrorString(glError)));
138 QMessageBox::critical(
139 this,
140 tr("Rendering error"),
141 QString{"Failed to render: %1"}.arg(glErrorString));
142 }
91 } 143 }
92 144
93 static QPointF pointToPointF(const QPoint& point) 145 static QPointF pointToPointF(const QPoint& point)
94 { 146 {
95 return {static_cast<qreal>(point.x()), static_cast<qreal>(point.y())}; 147 return {static_cast<qreal>(point.x()), static_cast<qreal>(point.y())};
116 this->update(); 168 this->update();
117 } 169 }
118 this->lastMousePosition = pointToPointF(event->pos()); 170 this->lastMousePosition = pointToPointF(event->pos());
119 } 171 }
120 172
173 void PartRenderer::setCompiler(gl::Compiler* compiler)
174 {
175 this->compiler = compiler;
176 }
177
121 void PartRenderer::setRenderStyle(const gl::RenderStyle newStyle) 178 void PartRenderer::setRenderStyle(const gl::RenderStyle newStyle)
122 { 179 {
123 this->renderStyle = newStyle; 180 this->renderStyle = newStyle;
124 this->update(); 181 this->update();
125 } 182 }

mercurial