19 #include <GL/glut.h> |
19 #include <GL/glut.h> |
20 #include <QMouseEvent> |
20 #include <QMouseEvent> |
21 #include <QMessageBox> |
21 #include <QMessageBox> |
22 #include "partrenderer.h" |
22 #include "partrenderer.h" |
23 |
23 |
24 |
|
25 static const char* vertexShaderSource = R"( |
|
26 #version 330 core |
|
27 |
|
28 layout(location=0) in vec3 position; |
|
29 layout(location=1) in vec4 color; |
|
30 out vec4 vColor; |
|
31 uniform mat4 CameraTransformation; |
|
32 |
|
33 void main() |
|
34 { |
|
35 vColor = color; |
|
36 gl_Position = CameraTransformation * vec4(position, 1.0); |
|
37 } |
|
38 )"; |
|
39 |
|
40 static const char* fragmentShaderSource = R"( |
|
41 #version 330 core |
|
42 |
|
43 in vec4 vColor; |
|
44 out vec4 fColor; |
|
45 |
|
46 void main() |
|
47 { |
|
48 fColor = vColor; |
|
49 } |
|
50 )"; |
|
51 |
|
52 PartRenderer::PartRenderer(Model* model, DocumentManager* documents, const ColorTable& colorTable, QWidget* parent) : |
24 PartRenderer::PartRenderer(Model* model, DocumentManager* documents, const ColorTable& colorTable, QWidget* parent) : |
53 QOpenGLWidget{parent}, |
25 QOpenGLWidget{parent}, |
54 model{model}, |
26 model{model}, |
55 documents{documents}, |
27 documents{documents}, |
56 colorTable{colorTable}, |
28 colorTable{colorTable}, |
71 { |
42 { |
72 abort(); |
43 abort(); |
73 } |
44 } |
74 glEnableClientState(GL_NORMAL_ARRAY); |
45 glEnableClientState(GL_NORMAL_ARRAY); |
75 glEnableClientState(GL_VERTEX_ARRAY); |
46 glEnableClientState(GL_VERTEX_ARRAY); |
76 //this->compiler->initialize(); |
47 this->compiler->initialize(); |
77 //this->compiler->build(this->model, this->documents); |
48 this->compiler->build(this->model, this->documents); |
78 this->initializeLighting(); |
49 this->initializeLighting(); |
79 this->initialized = true; |
50 this->initialized = true; |
80 this->rotation = QQuaternion::fromAxisAndAngle({1, 0, 0}, 30); |
51 this->rotation = QQuaternion::fromAxisAndAngle({1, 0, 0}, 30); |
81 this->rotation *= QQuaternion::fromAxisAndAngle({0, 1, 0}, 330); |
52 this->rotation *= QQuaternion::fromAxisAndAngle({0, 1, 0}, 330); |
82 glLineWidth(2.0); |
53 glLineWidth(2.0); |
83 this->objects.program = new QOpenGLShaderProgram; |
|
84 this->objects.program->create(); |
|
85 this->checkForGLErrors(); |
|
86 this->objects.program->addShaderFromSourceCode(QOpenGLShader::Vertex, ::vertexShaderSource); |
|
87 this->checkForGLErrors(); |
|
88 this->objects.program->addShaderFromSourceCode(QOpenGLShader::Fragment, ::fragmentShaderSource); |
|
89 this->checkForGLErrors(); |
|
90 this->objects.program->link(); |
|
91 this->checkForGLErrors(); |
|
92 this->objects.program->bind(); |
|
93 this->checkForGLErrors(); |
|
94 this->objects.buffer.create(); |
|
95 this->checkForGLErrors(); |
|
96 this->objects.buffer.bind(); |
|
97 this->checkForGLErrors(); |
|
98 this->objects.buffer.setUsagePattern(QOpenGLBuffer::StaticDraw); |
|
99 this->checkForGLErrors(); |
|
100 /* |
|
101 GLfloat data[] = { |
|
102 20.0f, 20.0f, 6.0f, 1.0f, 0.0f, 0.0f, 1.0f, |
|
103 30.0f, 20.0f, 6.0f, 0.0f, 1.0f, 0.0f, 1.0f, |
|
104 30.0f, 30.0f, 6.0f, 0.0f, 0.0f, 1.0f, 1.0f, |
|
105 }; |
|
106 */ |
|
107 GLfloat data[] = { |
|
108 0.00f, 0.75f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, |
|
109 -0.75f, -0.75f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, |
|
110 0.75f, -0.75f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f |
|
111 }; |
|
112 this->objects.buffer.allocate(data, sizeof data); |
|
113 this->checkForGLErrors(); |
|
114 this->objects.vertexArray.create(); |
|
115 this->checkForGLErrors(); |
|
116 this->objects.vertexArray.bind(); |
|
117 this->checkForGLErrors(); |
|
118 this->objects.program->enableAttributeArray(0); |
|
119 this->checkForGLErrors(); |
|
120 this->objects.program->enableAttributeArray(1); |
|
121 this->checkForGLErrors(); |
|
122 this->objects.program->setAttributeBuffer(0, GL_FLOAT, 0, 3); |
|
123 this->checkForGLErrors(); |
|
124 this->objects.program->setAttributeBuffer(1, GL_FLOAT, 3, 4); |
|
125 this->checkForGLErrors(); |
|
126 this->objects.vertexArray.release(); |
|
127 this->checkForGLErrors(); |
|
128 this->objects.buffer.release(); |
|
129 this->checkForGLErrors(); |
|
130 this->objects.program->release(); |
|
131 this->checkForGLErrors(); |
|
132 } |
54 } |
133 |
55 |
134 /* |
56 /* |
135 * Pads a 3×3 matrix into a 4×4 one by adding cells from the identity matrix. |
57 * Pads a 3×3 matrix into a 4×4 one by adding cells from the identity matrix. |
136 */ |
58 */ |
225 break; |
147 break; |
226 case gl::RenderStyle::Wireframe: |
148 case gl::RenderStyle::Wireframe: |
227 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); |
149 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); |
228 break; |
150 break; |
229 } |
151 } |
230 this->objects.program->bind(); |
152 this->compiler->setUniform("CameraTransformation", rotationMatrix); |
231 this->checkForGLErrors(); |
|
232 const int cameraTransformationUniform = glGetUniformLocation(this->objects.program->programId(), "CameraTransformation"); |
|
233 this->checkForGLErrors(); |
|
234 this->objects.program->setUniformValue(cameraTransformationUniform, rotationMatrix); |
|
235 this->checkForGLErrors(); |
|
236 this->objects.vertexArray.bind(); |
|
237 this->checkForGLErrors(); |
|
238 glDrawArrays(GL_TRIANGLES, 0, 3); |
|
239 this->checkForGLErrors(); |
|
240 this->objects.vertexArray.release(); |
|
241 this->checkForGLErrors(); |
|
242 this->objects.program->release(); |
|
243 this->checkForGLErrors(); |
|
244 #if 0 |
|
245 // Lines need to be rendered last so that anti-aliasing does not interfere with polygon rendering. |
153 // Lines need to be rendered last so that anti-aliasing does not interfere with polygon rendering. |
246 renderVao(gl::ArrayClass::Triangles); |
154 renderVao(gl::ArrayClass::Triangles); |
247 renderVao(gl::ArrayClass::Quads); |
155 renderVao(gl::ArrayClass::Quads); |
248 renderVao(gl::ArrayClass::Lines); |
156 renderVao(gl::ArrayClass::Lines); |
249 #endif |
|
250 glDisable(GL_POLYGON_OFFSET_FILL); |
157 glDisable(GL_POLYGON_OFFSET_FILL); |
251 } |
158 } |
252 |
159 |
253 void PartRenderer::renderVao(const gl::ArrayClass /*arrayClass*/) |
160 void PartRenderer::renderVao(const gl::ArrayClass arrayClass) |
254 { |
161 { |
255 /* |
|
256 this->compiler->bindVertexArray(arrayClass); |
162 this->compiler->bindVertexArray(arrayClass); |
257 const std::size_t vertexCount = this->compiler->vboSize({arrayClass, gl::VboSubclass::VertexData}) / gl::FLOATS_PER_VERTEX; |
163 const std::size_t vertexCount = this->compiler->vboSize(arrayClass) / gl::FLOATS_PER_VERTEX; |
258 glDrawArrays(getGlTypeForArrayClass(arrayClass), 0, static_cast<GLsizei>(vertexCount)); |
164 glDrawArrays(getGlTypeForArrayClass(arrayClass), 0, static_cast<GLsizei>(vertexCount)); |
259 this->compiler->releaseVertexArray(arrayClass); |
165 this->compiler->releaseVertexArray(arrayClass); |
260 this->checkForGLErrors(); |
166 this->checkForGLErrors(); |
261 */ |
|
262 } |
167 } |
263 |
168 |
264 void PartRenderer::checkForGLErrors() |
169 void PartRenderer::checkForGLErrors() |
265 { |
170 { |
266 GLenum glError; |
171 GLenum glError; |