Commit 22848c7d authored by Frédéric Larue's avatar Frédéric Larue

NavigationControl function definitions moved to a separate file.

parent 40c61924
......@@ -21,261 +21,6 @@
void GLViewer::NavigationControl::mouseMoveEvent( double dx, double dy, QMouseEvent *evt )
{
GLViewer &v = *GetViewer();
if( v.dof() == DISPLAY_DOF_2D )
{
if( evt->buttons() & Qt::MidButton )
{
double proj[16];
glGetDoublev( GL_PROJECTION_MATRIX, proj );
double scaleFactor = 2.0*v.distToFocusPoint() / (v.width()*proj[0]);
double scaledDX = dx * scaleFactor;
double scaledDY = dy * scaleFactor;
QVector3D fpoint = v.focusPoint();
fpoint[0] += scaledDY*v.viewRotationMatrix()(1,0) - scaledDX*v.viewRotationMatrix()(0,0);
fpoint[1] += scaledDY*v.viewRotationMatrix()(1,1) - scaledDX*v.viewRotationMatrix()(0,1);
v.setFocusPoint( fpoint );
}
#if 0
else if( evt->buttons() & Qt::LeftButton )
{
const double orientation = (evt->x()-0.5*width())*dy - (evt->y()-0.5*height())*dx;
Matrix4Dd deltaRot;
if( orientation > 0.0 )
deltaRot.SetRotation( Vector3Dd(0,0,1), -0.5*M_PI/180.0*std::sqrt(dx*dx+dy*dy) );
else
deltaRot.SetRotation( Vector3Dd(0,0,1), 0.5*M_PI/180.0*std::sqrt(dx*dx+dy*dy) );
setViewRotationMatrix( viewRotationMatrix() * deltaRot );
update();
}
#endif
}
else
{
if( evt->buttons() & Qt::LeftButton && evt->buttons() & Qt::RightButton )
{
QVector3D axis( dy, dx, 0.0f );
double axisLen = axis.length();
QMatrix4x4 deltaRot;
deltaRot.rotate( 0.5f*axisLen, axis/axisLen );
if( v.isLightTrackingEye() )
v.setLightRotationMatrix( deltaRot * v.lightRotationMatrix() );
else
v.setLightRotationMatrix( v.viewRotationMatrix() * deltaRot * v.viewRotationMatrixInverse() * v.lightRotationMatrix() );
}
else if( evt->buttons() & Qt::MidButton )
{
double proj[16];
glGetDoublev( GL_PROJECTION_MATRIX, proj );
double scaleFactor = 2.0*v.distToFocusPoint() / (v.width()*proj[0]);
double scaledDX = dx * scaleFactor;
double scaledDY = dy * scaleFactor;
QVector3D fpoint = v.focusPoint();
fpoint[0] += scaledDY*v.viewRotationMatrix()(1,0) - scaledDX*v.viewRotationMatrix()(0,0);
fpoint[1] += scaledDY*v.viewRotationMatrix()(1,1) - scaledDX*v.viewRotationMatrix()(0,1);
fpoint[2] += scaledDY*v.viewRotationMatrix()(1,2) - scaledDX*v.viewRotationMatrix()(0,2);
v.setFocusPoint( fpoint );
v.emitScreenSpaceTranslation( QVector2D(-scaledDX,scaledDY) );
}
else if( evt->buttons() & Qt::LeftButton )
{
QMatrix4x4 deltaRot;
if( evt->pos().x() < 0.15f*v.width() )
deltaRot.rotate( 0.5f*dy, 0,0,1 );
else if( evt->pos().x() > 0.85f*v.width() )
deltaRot.rotate( -0.5f*dy, 0,0,1 );
else
{
QVector3D axis( dy, dx, 0.0f );
double axisLen = axis.length();
deltaRot.rotate( 0.5f*axisLen, axis/axisLen );
}
v.emitViewRotationMatrixUpdated( deltaRot );
v.setViewRotationMatrix( deltaRot * v.viewRotationMatrix() );
}
}
}
void GLViewer::NavigationControl::wheelEvent( double delta, QWheelEvent* evt )
{
GLViewer &v = *GetViewer();
delta *= -0.0005 * v.distToFocusPoint();
if( v.distToFocusPoint() + delta > 0.0 )
v.setDistToFocusPoint( v.distToFocusPoint() + delta );
}
void GLViewer::NavigationControl::mouseDoubleClickEvent( QMouseEvent *evt )
{
GLViewer &v = *GetViewer();
PickedPoint point;
if( v.getPickedPoint( evt->x(), v.height()-evt->y(), point, false ) )
{
if( evt->modifiers() & Qt::ControlModifier )
v.frameItem( point.srcObject );
else
v.setFocusPoint( point.global );
}
}
bool GLViewer::NavigationControl::keyPressEvent( QKeyEvent *evt )
{
GLViewer &v = *GetViewer();
if( evt->key() == Qt::Key_Space )
{
v.centerView();
return true;
}
bool keyGrabbed = true;
QMatrix4x4 rot;
if( v.dof() == DISPLAY_DOF_2D )
{
switch( evt->key() )
{
case Qt::Key_4:
{
rot.rotate( -15.0f, 0,0,1 );
v.setViewRotationMatrix( rot * v.viewRotationMatrix() );
break;
}
case Qt::Key_5:
{
v.setViewRotationMatrix( rot );
break;
}
case Qt::Key_6:
{
rot.rotate( 15.0f, 0,0,1 );
v.setViewRotationMatrix( rot * v.viewRotationMatrix() );
break;
}
default: keyGrabbed = false;
}
}
else // if( v.dof() == DISPLAY_DOF_3D )
{
switch( evt->key() )
{
case Qt::Key_5:
{
v.setOrthographic( !v.isProjectionOrthographic() );
break;
}
case Qt::Key_1:
{
if( evt->modifiers() & Qt::CTRL )
rot.rotate( 180.0f, 0,1,0 );
v.setViewRotationMatrix( rot );
break;
}
case Qt::Key_3:
{
rot.rotate( -90.0f, 0,1,0 );
if( evt->modifiers() & Qt::CTRL )
rot.rotate( 180.0f, 0,1,0 );
v.setViewRotationMatrix( rot );
break;
}
case Qt::Key_7:
{
rot.rotate( 90.0f, 1,0,0 );
if( evt->modifiers() & Qt::CTRL )
rot.rotate( 180.0f, 1,0,0 );
v.setViewRotationMatrix( rot );
break;
}
case Qt::Key_4:
{
rot.rotate( 15.0f, 0,1,0 );
v.setViewRotationMatrix( v.viewRotationMatrix() * rot );
break;
}
case Qt::Key_6:
{
rot.rotate( -15.0f, 0,1,0 );
v.setViewRotationMatrix( v.viewRotationMatrix() * rot );
break;
}
case Qt::Key_2:
{
rot.rotate( -15.0f, 1,0,0 );
v.setViewRotationMatrix( rot * v.viewRotationMatrix() );
break;
}
case Qt::Key_8:
{
rot.rotate( 15.0f, 1,0,0 );
v.setViewRotationMatrix( rot * v.viewRotationMatrix() );
break;
}
default: keyGrabbed = false;
}
}
return keyGrabbed;
}
void GLViewer::NavigationControl::updateProjectionMatrix( QMatrix4x4 &m )
{
GLViewer &v = *GetViewer();
m.fill( 0.0f );
if( v.isProjectionOrthographic() )
{
m(0,0) = 2.0f * v.height() / (v.distToFocusPoint() * v.width());
m(1,1) = 2.0f / v.distToFocusPoint();
m(2,2) = -2.0f / (v.zFar() - v.zNear());
m(2,3) = -(v.zFar() + v.zNear()) / (v.zFar() - v.zNear());
m(3,3) = 1.0f;
}
else
{
m(0,0) = v.focal() * v.height() / v.width();
m(1,1) = v.focal();
m(2,2) = (v.zNear() + v.zFar()) / (v.zNear() - v.zFar());
m(2,3) = 2.0f*v.zNear()*v.zFar() / (v.zNear() - v.zFar());
m(3,2) = -1.0f;
}
}
void GLViewer::NavigationControl::updateViewMatrix( QMatrix4x4 &m )
{
GLViewer &v = *GetViewer();
m.setToIdentity();
m.translate( 0.0, 0.0, -v.distToFocusPoint() );
m *= v.viewRotationMatrix();
m.translate( -v.focusPoint() );
}
void GLViewer::DisplayableInfo::Initialize( DisplayableInterface *displayable,
DisplayableList::iterator priorityPos,
UIParamSet &params )
......
/*
* (c) LSIIT, UMR CNRS/UdS
* Authors: F. Larue.
*
* See licence.txt for additional information.
*/
#include "GLViewer.h"
void GLViewer::NavigationControl::mouseMoveEvent( double dx, double dy, QMouseEvent *evt )
{
GLViewer &v = *GetViewer();
if( v.dof() == DISPLAY_DOF_2D )
{
if( evt->buttons() & Qt::MidButton )
{
double proj[16];
glGetDoublev( GL_PROJECTION_MATRIX, proj );
double scaleFactor = 2.0*v.distToFocusPoint() / (v.width()*proj[0]);
double scaledDX = dx * scaleFactor;
double scaledDY = dy * scaleFactor;
QVector3D fpoint = v.focusPoint();
fpoint[0] += scaledDY*v.viewRotationMatrix()(1,0) - scaledDX*v.viewRotationMatrix()(0,0);
fpoint[1] += scaledDY*v.viewRotationMatrix()(1,1) - scaledDX*v.viewRotationMatrix()(0,1);
v.setFocusPoint( fpoint );
}
#if 0
else if( evt->buttons() & Qt::LeftButton )
{
const double orientation = (evt->x()-0.5*width())*dy - (evt->y()-0.5*height())*dx;
Matrix4Dd deltaRot;
if( orientation > 0.0 )
deltaRot.SetRotation( Vector3Dd(0,0,1), -0.5*M_PI/180.0*std::sqrt(dx*dx+dy*dy) );
else
deltaRot.SetRotation( Vector3Dd(0,0,1), 0.5*M_PI/180.0*std::sqrt(dx*dx+dy*dy) );
setViewRotationMatrix( viewRotationMatrix() * deltaRot );
update();
}
#endif
}
else
{
if( evt->buttons() & Qt::LeftButton && evt->buttons() & Qt::RightButton )
{
QVector3D axis( dy, dx, 0.0f );
double axisLen = axis.length();
QMatrix4x4 deltaRot;
deltaRot.rotate( 0.5f*axisLen, axis/axisLen );
if( v.isLightTrackingEye() )
v.setLightRotationMatrix( deltaRot * v.lightRotationMatrix() );
else
v.setLightRotationMatrix( v.viewRotationMatrix() * deltaRot * v.viewRotationMatrixInverse() * v.lightRotationMatrix() );
}
else if( evt->buttons() & Qt::MidButton )
{
double proj[16];
glGetDoublev( GL_PROJECTION_MATRIX, proj );
double scaleFactor = 2.0*v.distToFocusPoint() / (v.width()*proj[0]);
double scaledDX = dx * scaleFactor;
double scaledDY = dy * scaleFactor;
QVector3D fpoint = v.focusPoint();
fpoint[0] += scaledDY*v.viewRotationMatrix()(1,0) - scaledDX*v.viewRotationMatrix()(0,0);
fpoint[1] += scaledDY*v.viewRotationMatrix()(1,1) - scaledDX*v.viewRotationMatrix()(0,1);
fpoint[2] += scaledDY*v.viewRotationMatrix()(1,2) - scaledDX*v.viewRotationMatrix()(0,2);
v.setFocusPoint( fpoint );
v.emitScreenSpaceTranslation( QVector2D(-scaledDX,scaledDY) );
}
else if( evt->buttons() & Qt::LeftButton )
{
QMatrix4x4 deltaRot;
if( evt->pos().x() < 0.15f*v.width() )
deltaRot.rotate( 0.5f*dy, 0,0,1 );
else if( evt->pos().x() > 0.85f*v.width() )
deltaRot.rotate( -0.5f*dy, 0,0,1 );
else
{
QVector3D axis( dy, dx, 0.0f );
double axisLen = axis.length();
deltaRot.rotate( 0.5f*axisLen, axis/axisLen );
}
v.emitViewRotationMatrixUpdated( deltaRot );
v.setViewRotationMatrix( deltaRot * v.viewRotationMatrix() );
}
}
}
void GLViewer::NavigationControl::wheelEvent( double delta, QWheelEvent* evt )
{
GLViewer &v = *GetViewer();
delta *= -0.0005 * v.distToFocusPoint();
if( v.distToFocusPoint() + delta > 0.0 )
v.setDistToFocusPoint( v.distToFocusPoint() + delta );
}
void GLViewer::NavigationControl::mouseDoubleClickEvent( QMouseEvent *evt )
{
GLViewer &v = *GetViewer();
PickedPoint point;
if( v.getPickedPoint( evt->x(), v.height()-evt->y(), point, false ) )
{
if( evt->modifiers() & Qt::ControlModifier )
v.frameItem( point.srcObject );
else
v.setFocusPoint( point.global );
}
}
bool GLViewer::NavigationControl::keyPressEvent( QKeyEvent *evt )
{
GLViewer &v = *GetViewer();
if( evt->key() == Qt::Key_Space )
{
v.centerView();
return true;
}
bool keyGrabbed = true;
QMatrix4x4 rot;
if( v.dof() == DISPLAY_DOF_2D )
{
switch( evt->key() )
{
case Qt::Key_4:
{
rot.rotate( -15.0f, 0,0,1 );
v.setViewRotationMatrix( rot * v.viewRotationMatrix() );
break;
}
case Qt::Key_5:
{
v.setViewRotationMatrix( rot );
break;
}
case Qt::Key_6:
{
rot.rotate( 15.0f, 0,0,1 );
v.setViewRotationMatrix( rot * v.viewRotationMatrix() );
break;
}
default: keyGrabbed = false;
}
}
else // if( v.dof() == DISPLAY_DOF_3D )
{
switch( evt->key() )
{
case Qt::Key_5:
{
v.setOrthographic( !v.isProjectionOrthographic() );
break;
}
case Qt::Key_1:
{
if( evt->modifiers() & Qt::CTRL )
rot.rotate( 180.0f, 0,1,0 );
v.setViewRotationMatrix( rot );
break;
}
case Qt::Key_3:
{
rot.rotate( -90.0f, 0,1,0 );
if( evt->modifiers() & Qt::CTRL )
rot.rotate( 180.0f, 0,1,0 );
v.setViewRotationMatrix( rot );
break;
}
case Qt::Key_7:
{
rot.rotate( 90.0f, 1,0,0 );
if( evt->modifiers() & Qt::CTRL )
rot.rotate( 180.0f, 1,0,0 );
v.setViewRotationMatrix( rot );
break;
}
case Qt::Key_4:
{
rot.rotate( 15.0f, 0,1,0 );
v.setViewRotationMatrix( v.viewRotationMatrix() * rot );
break;
}
case Qt::Key_6:
{
rot.rotate( -15.0f, 0,1,0 );
v.setViewRotationMatrix( v.viewRotationMatrix() * rot );
break;
}
case Qt::Key_2:
{
rot.rotate( -15.0f, 1,0,0 );
v.setViewRotationMatrix( rot * v.viewRotationMatrix() );
break;
}
case Qt::Key_8:
{
rot.rotate( 15.0f, 1,0,0 );
v.setViewRotationMatrix( rot * v.viewRotationMatrix() );
break;
}
default: keyGrabbed = false;
}
}
return keyGrabbed;
}
void GLViewer::NavigationControl::updateProjectionMatrix( QMatrix4x4 &m )
{
GLViewer &v = *GetViewer();
m.fill( 0.0f );
if( v.isProjectionOrthographic() )
{
m(0,0) = 2.0f * v.height() / (v.distToFocusPoint() * v.width());
m(1,1) = 2.0f / v.distToFocusPoint();
m(2,2) = -2.0f / (v.zFar() - v.zNear());
m(2,3) = -(v.zFar() + v.zNear()) / (v.zFar() - v.zNear());
m(3,3) = 1.0f;
}
else
{
m(0,0) = v.focal() * v.height() / v.width();
m(1,1) = v.focal();
m(2,2) = (v.zNear() + v.zFar()) / (v.zNear() - v.zFar());
m(2,3) = 2.0f*v.zNear()*v.zFar() / (v.zNear() - v.zFar());
m(3,2) = -1.0f;
}
}
void GLViewer::NavigationControl::updateViewMatrix( QMatrix4x4 &m )
{
GLViewer &v = *GetViewer();
m.setToIdentity();
m.translate( 0.0, 0.0, -v.distToFocusPoint() );
m *= v.viewRotationMatrix();
m.translate( -v.focusPoint() );
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment