tags:

views:

51

answers:

1

Hi, I am currently trying to work on getting my virtual trackball to work from any angle. When I am looking at it from the z axis, it seems to work fine. I hold my mouse down, and move the mouse up... the rotation will move accordingly.

Now, if I change my viewing angle / position of my camera and try to move my mouse. The rotation will occur as if I were looking from the z axis. I cannot come up with a good way to get this to work.

Here is the code:

 void Renderer::mouseMoveEvent(QMouseEvent *e)
 {

         // Get coordinates
         int x = e->x();
         int y = e->y();

         if (isLeftButtonPressed)
         {
                 // project current screen coordinates onto hemi sphere
                 Point sphere = projScreenCoord(x,y);

                 // find axis by taking cross product of current and previous hemi points
                 axis = Point::cross(previousPoint, sphere);

                 // angle can be found from magnitude of cross product
                 double length = sqrt( axis.x * axis.x + axis.y * axis.y + axis.z * axis.z );

                 // Normalize
                 axis = axis / length;

                 double lengthPrev = sqrt( previousPoint.x * previousPoint.x + previousPoint.y * previousPoint.y + previousPoint.z * previousPoint.z );
                 double lengthCur = sqrt( sphere.x * sphere.x + sphere.y * sphere.y + sphere.z * sphere.z );
                 angle = asin(length / (lengthPrev * lengthCur));
                 // Convert into Degrees
                 angle = angle * 180 / M_PI;

                 // 'add' this rotation matrix to our 'total' rotation matrix
                 glPushMatrix(); // save the old matrix so we don't mess anything up
                 glLoadIdentity();
                 glRotatef(angle, axis[0], axis[1], axis[2]); // our newly calculated rotation
                 glMultMatrixf(rotmatrix); // our previous rotation matrix
                 glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat*) rotmatrix); // we've let OpenGL do our matrix mult for us, now get this result & store it
                 glPopMatrix(); // return modelview to its old value;

}

// Project screen coordinates onto a unit hemisphere
 Point Renderer::projScreenCoord(int x, int y)
 {
         // find projected x & y coordinates
         double xSphere = ((double)x/width)*2.0 - 1.0;
         double ySphere = ( 1 - ((double)y/height)) * 2.0 - 1.0;
         double temp = 1.0 - xSphere*xSphere - ySphere*ySphere;

         // Do a check so you dont do a sqrt of a negative number
         double zSphere;
         if (temp < 0){ zSphere = 0.0;}
         else
         {zSphere = sqrt(temp);}

         Point sphere(xSphere, ySphere, zSphere);



        // return the point on the sphere
         return sphere;
 }

I am still fairly new at this. Sorry for the trouble and thanks for all the help =)

A: 

The usual way involves quaternions. E.g., in sample code originally from SGI.

Jerry Coffin