Skip to content

Instantly share code, notes, and snippets.

@v3c70r
Created September 21, 2014 18:47
Show Gist options
  • Save v3c70r/8716a84758781002fad6 to your computer and use it in GitHub Desktop.
Save v3c70r/8716a84758781002fad6 to your computer and use it in GitHub Desktop.
//This part of code should be in the else block of mouseMoveEvent() function of glMain.cpp
const int dx = x - _mouseX;
const int dy = y - _mouseY;
int fviewport[]={0,0,1,1};
double projection[16];
double modelview[16];
glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
double bl[3];
double br[3];
double tl[3];
gluUnProject(0, 0, 0, modelview, projection, fviewport, &bl[0], &bl[1], &bl[2]);
gluUnProject(1, 0, 0, modelview, projection, fviewport, &br[0], &br[1], &br[2]);
gluUnProject(0, 1, 0, modelview, projection, fviewport, &tl[0], &tl[1], &tl[2]);
double v1[]={br[0]-bl[0], br[1]-bl[1], br[2]-bl[2]};
double v2[]={tl[0]-bl[0], tl[1]-bl[1], tl[2]-bl[2]};
double cr[]={v1[1]*v2[2]-v1[2]*v2[1], v1[2]*v2[0]-v1[0]*v2[2], v1[0]*v2[1]-v1[1]*v2[0]};
double norm=sqrt(cr[0]*cr[0]+cr[1]*cr[1]+cr[2]*cr[2]);//rotation axis
cr[0]/=norm;cr[1]/=norm;cr[2]/=norm;
float q[4];
float v[]={(float)cr[0], (float)cr[1], (float)cr[2]};
axistoQuat(v, -dx/100.0, q); //Rotation axis to quaternion
//You can use quatToMat function provided in simpleMath library to translate quaternion to Matrix
//Here you can add your code to apply rotation to joint.
//Update mouse position
_mouseX = x;
_mouseY = y;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment