src/gl/partrenderer.cpp

changeset 34
1de2b8d64e9f
parent 32
767592024ec5
child 35
98906a94732f
equal deleted inserted replaced
33:4c41bfe2ec6e 34:1de2b8d64e9f
44 { 44 {
45 abort(); 45 abort();
46 } 46 }
47 this->compiler->initialize(); 47 this->compiler->initialize();
48 this->compiler->build(this->model, this->documents); 48 this->compiler->build(this->model, this->documents);
49 this->initializeLighting();
50 this->initialized = true; 49 this->initialized = true;
51 this->modelQuaternion = glm::angleAxis(glm::radians(30.0f), glm::vec3{-1, 0, 0}); 50 this->modelQuaternion = glm::angleAxis(glm::radians(30.0f), glm::vec3{-1, 0, 0});
52 this->modelQuaternion *= glm::angleAxis(glm::radians(225.0f), glm::vec3{-0, 1, 0}); 51 this->modelQuaternion *= glm::angleAxis(glm::radians(225.0f), glm::vec3{-0, 1, 0});
53 this->updateViewMatrix(); 52 this->updateViewMatrix();
54 glLineWidth(2.0); 53 glLineWidth(2.0);
55 this->update(); 54 this->update();
56 }
57
58 void PartRenderer::initializeLighting()
59 {
60 GLfloat materialShininess[] = {5.0};
61 GLfloat lightPosition[] = {1.0, 1.0, 1.0, 0.0};
62 GLfloat ambientLightingLevel[] = {0.5, 0.5, 0.5, 1.0};
63 glShadeModel(GL_SMOOTH);
64 glMaterialfv(GL_FRONT, GL_SHININESS, materialShininess);
65 glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLightingLevel);
66 glLightfv(GL_LIGHT0, GL_DIFFUSE, ambientLightingLevel);
67 glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
68 glEnable(GL_LIGHTING);
69 glEnable(GL_LIGHT0);
70 glEnable(GL_COLOR_MATERIAL);
71 glEnable(GL_DEPTH_TEST);
72 } 55 }
73 56
74 void PartRenderer::resizeGL(int width, int height) 57 void PartRenderer::resizeGL(int width, int height)
75 { 58 {
76 glViewport(0, 0, width, height); 59 glViewport(0, 0, width, height);
77 this->projectionMatrix = glm::perspective( 60 this->projectionMatrix = glm::perspective(
78 glm::radians(45.0f), 61 glm::radians(45.0f),
79 static_cast<float>(width) / static_cast<float>(height), 62 static_cast<float>(width) / static_cast<float>(height),
80 0.1f, 63 0.1f,
81 10000.f); 64 10000.f);
65 this->compiler->setUniformMatrix("projectionMatrix", this->projectionMatrix);
82 } 66 }
83 67
84 static GLenum getGlTypeForArrayClass(const gl::ArrayClass vboClass) 68 static GLenum getGlTypeForArrayClass(const gl::ArrayClass vboClass)
85 { 69 {
86 switch (vboClass) 70 switch (vboClass)
126 break; 110 break;
127 case gl::RenderStyle::Wireframe: 111 case gl::RenderStyle::Wireframe:
128 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 112 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
129 break; 113 break;
130 } 114 }
131 this->compiler->setUniformMatrix("CameraTransformation", this->projectionMatrix * this->viewMatrix * glm::mat4_cast(this->modelQuaternion));
132 // Lines need to be rendered last so that anti-aliasing does not interfere with polygon rendering. 115 // Lines need to be rendered last so that anti-aliasing does not interfere with polygon rendering.
133 renderVao(gl::ArrayClass::Triangles); 116 renderVao(gl::ArrayClass::Triangles);
134 renderVao(gl::ArrayClass::Quads); 117 renderVao(gl::ArrayClass::Quads);
135 renderVao(gl::ArrayClass::Lines); 118 renderVao(gl::ArrayClass::Lines);
136 glDisable(GL_POLYGON_OFFSET_FILL); 119 glDisable(GL_POLYGON_OFFSET_FILL);
139 void PartRenderer::updateViewMatrix() 122 void PartRenderer::updateViewMatrix()
140 { 123 {
141 // I'm not quite sure why using the exponent function on the zoom factor causes linear zoom behavior 124 // I'm not quite sure why using the exponent function on the zoom factor causes linear zoom behavior
142 const double z = 2 * std::exp(this->zoom) * (1 + this->compiler->modelDistance()); 125 const double z = 2 * std::exp(this->zoom) * (1 + this->compiler->modelDistance());
143 this->viewMatrix = glm::lookAt(glm::vec3{0, 0, z}, {0, 0, 0}, {0, -1, 0}); 126 this->viewMatrix = glm::lookAt(glm::vec3{0, 0, z}, {0, 0, 0}, {0, -1, 0});
127 this->compiler->setUniformMatrix("viewMatrix", this->viewMatrix);
144 } 128 }
145 129
146 void PartRenderer::renderVao(const gl::ArrayClass arrayClass) 130 void PartRenderer::renderVao(const gl::ArrayClass arrayClass)
147 { 131 {
148 this->compiler->bindVertexArray(arrayClass); 132 this->compiler->bindVertexArray(arrayClass);
149 const std::size_t vertexCount = this->compiler->vboSize(arrayClass) / gl::FLOATS_PER_VERTEX; 133 const std::size_t vertexCount = this->compiler->vertexCount(arrayClass);
150 glDrawArrays(getGlTypeForArrayClass(arrayClass), 0, static_cast<GLsizei>(vertexCount)); 134 glDrawArrays(getGlTypeForArrayClass(arrayClass), 0, static_cast<GLsizei>(vertexCount));
151 this->compiler->releaseVertexArray(arrayClass); 135 this->compiler->releaseVertexArray(arrayClass);
152 this->checkForGLErrors(); 136 this->checkForGLErrors();
153 } 137 }
154 138
183 const float move_x = static_cast<float>(move.x()); 167 const float move_x = static_cast<float>(move.x());
184 const float move_y = static_cast<float>(move.y()); 168 const float move_y = static_cast<float>(move.y());
185 const glm::quat q_x = glm::angleAxis(scalar * move_x, glm::vec3{0, -1, 0}); 169 const glm::quat q_x = glm::angleAxis(scalar * move_x, glm::vec3{0, -1, 0});
186 const glm::quat q_y = glm::angleAxis(scalar * move_y, glm::vec3{-1, 0, 0}); 170 const glm::quat q_y = glm::angleAxis(scalar * move_y, glm::vec3{-1, 0, 0});
187 this->modelQuaternion = q_x * q_y * this->modelQuaternion; 171 this->modelQuaternion = q_x * q_y * this->modelQuaternion;
172 this->compiler->setUniformMatrix("modelMatrix", glm::mat4_cast(this->modelQuaternion));
188 this->update(); 173 this->update();
189 } 174 }
190 this->lastMousePosition = pointToPointF(event->pos()); 175 this->lastMousePosition = pointToPointF(event->pos());
191 } 176 }
192 177

mercurial