src/glrenderer.cpp

changeset 1393
f0fe7ea82b81
parent 1391
5fa4bf1fc781
child 1404
16eb4257e662
equal deleted inserted replaced
1392:0541d9b21968 1393:f0fe7ea82b81
212 // 212 //
213 void GLRenderer::resetAngles() 213 void GLRenderer::resetAngles()
214 { 214 {
215 if (m_initialized) 215 if (m_initialized)
216 { 216 {
217 // Why did I even bother trying to compute this by pen and paper? Let GL figure it out... 217 m_rotation = QQuaternion::fromAxisAndAngle({1, 0, 0}, 30);
218 glMatrixMode(GL_MODELVIEW); 218 m_rotation *= QQuaternion::fromAxisAndAngle({0, 1, 0}, 330);
219 glPushMatrix();
220 glLoadIdentity();
221 glRotatef(30, 1, 0, 0);
222 glRotatef(330, 0, 1, 0);
223 glGetFloatv(GL_MODELVIEW_MATRIX, m_rotationMatrix.data());
224 glPopMatrix();
225 } 219 }
226 currentCamera().setPanning(0, 0); 220 currentCamera().setPanning(0, 0);
227 needZoomToFit(); 221 needZoomToFit();
228 } 222 }
229 223
360 // Unfortunately Qt does not provide a resized() signal so we have to manually feed the information. 354 // Unfortunately Qt does not provide a resized() signal so we have to manually feed the information.
361 for (GLCamera& camera : m_cameras) 355 for (GLCamera& camera : m_cameras)
362 camera.rendererResized(width, height); 356 camera.rendererResized(width, height);
363 } 357 }
364 358
359 /*
360 * Pads a 3×3 matrix into a 4×4 one by adding cells from the identity matrix.
361 */
362 GLRotationMatrix padMatrix(const QMatrix3x3& stub)
363 {
364 return {
365 stub(0, 0), stub(0, 1), stub(0, 2), 0,
366 stub(1, 0), stub(1, 1), stub(1, 2), 0,
367 stub(2, 0), stub(2, 1), stub(2, 2), 0,
368 0, 0, 0, 1
369 };
370 }
371
365 // ============================================================================= 372 // =============================================================================
366 // 373 //
367 void GLRenderer::drawGLScene() 374 void GLRenderer::drawGLScene()
368 { 375 {
369 if (m_needZoomToFit) 376 if (m_needZoomToFit)
397 glMatrixMode(GL_MODELVIEW); 404 glMatrixMode(GL_MODELVIEW);
398 glPushMatrix(); 405 glPushMatrix();
399 glLoadIdentity(); 406 glLoadIdentity();
400 glTranslatef(0.0f, 0.0f, -2.0f); 407 glTranslatef(0.0f, 0.0f, -2.0f);
401 glTranslatef(panning (X), panning (Y), -zoom()); 408 glTranslatef(panning (X), panning (Y), -zoom());
402 glMultMatrixf(m_rotationMatrix.constData()); 409 glMultMatrixf(padMatrix(m_rotation.toRotationMatrix()).constData());
403 glTranslatef(-this->m_compiler->modelCenter()); 410 glTranslatef(-this->m_compiler->modelCenter());
404 } 411 }
405 412
406 glEnableClientState (GL_NORMAL_ARRAY); 413 glEnableClientState (GL_NORMAL_ARRAY);
407 glEnableClientState (GL_VERTEX_ARRAY); 414 glEnableClientState (GL_VERTEX_ARRAY);
643 m_panning = true; 650 m_panning = true;
644 m_isCameraMoving = true; 651 m_isCameraMoving = true;
645 } 652 }
646 else if (left and camera() == Camera::Free and (xMove != 0 or yMove != 0)) 653 else if (left and camera() == Camera::Free and (xMove != 0 or yMove != 0))
647 { 654 {
648 // Apply current rotation input to the rotation matrix 655 QQuaternion versor = QQuaternion::fromAxisAndAngle(yMove, xMove, 0, 0.6 * hypot(xMove, yMove));
649 // ref: https://forums.ldraw.org/thread-22006-post-24426.html#pid24426 656 m_rotation = versor * m_rotation;
650 glPushMatrix();
651 glLoadIdentity();
652 // 0.6 is an arbitrary rotation sensitivity scalar
653 glRotatef(0.6 * hypot(xMove, yMove), yMove, xMove, 0);
654 glMultMatrixf(m_rotationMatrix.constData());
655 glGetFloatv(GL_MODELVIEW_MATRIX, m_rotationMatrix.data());
656 glPopMatrix();
657 m_isCameraMoving = true; 657 m_isCameraMoving = true;
658 } 658 }
659 659
660 // Start the tool tip timer 660 // Start the tool tip timer
661 m_toolTipTimer->start (500); 661 m_toolTipTimer->start (500);
1019 double GLRenderer::zoom() 1019 double GLRenderer::zoom()
1020 { 1020 {
1021 return currentCamera().zoom(); 1021 return currentCamera().zoom();
1022 } 1022 }
1023 1023
1024 const QGenericMatrix<4, 4, GLfloat>& GLRenderer::rotationMatrix() const
1025 {
1026 return m_rotationMatrix;
1027 }
1028
1029 bool GLRenderer::isDrawingSelectionScene() const 1024 bool GLRenderer::isDrawingSelectionScene() const
1030 { 1025 {
1031 return m_isDrawingSelectionScene; 1026 return m_isDrawingSelectionScene;
1032 } 1027 }
1033 1028

mercurial