ajay
ajay

Reputation: 11

opengl trackball

I am trying to rotate opengl scene using track ball. The problem i am having is i am getting rotations opposite to direction of my swipe on screen. Here is the snippet of code.

         prevPoint.y = viewPortHeight - prevPoint.y;
        currentPoint.y = viewPortHeight - currentPoint.y;

        prevPoint.x = prevPoint.x - centerx;
        prevPoint.y = prevPoint.y - centery;
        currentPoint.x = currentPoint.x - centerx;
        currentPoint.y = currentPoint.y - centery;

        double angle=0;
        if (prevPoint.x == currentPoint.x && prevPoint.y == currentPoint.y) {
            return;
        }
         double d, z, radius = viewPortHeight * 0.5;
        if(viewPortWidth > viewPortHeight) {
            radius = viewPortHeight * 0.5f;
        } else {
            radius = viewPortWidth * 0.5f;
        }

         d = (prevPoint.x * prevPoint.x + prevPoint.y * prevPoint.y);
         if (d <= radius * radius * 0.5 ) {    /* Inside sphere */
             z = sqrt(radius*radius - d);
         } else {           /* On hyperbola */
             z = (radius * radius * 0.5) / sqrt(d);
         }
        Vector refVector1(prevPoint.x,prevPoint.y,z);
        refVector1.normalize();
        d = (currentPoint.x * currentPoint.x + currentPoint.y * currentPoint.y);
        if (d <= radius * radius * 0.5 ) {    /* Inside sphere */
            z = sqrt(radius*radius - d);
        } else {           /* On hyperbola */
             z = (radius * radius * 0.5) / sqrt(d);
        }
        Vector refVector2(currentPoint.x,currentPoint.y,z);
        refVector2.normalize();
        Vector axisOfRotation = refVector1.cross(refVector2);
        axisOfRotation.normalize();
        angle = acos(refVector1*refVector2);

Upvotes: 1

Views: 776

Answers (1)

2-complex
2-complex

Reputation: 353

I recommend artificially setting prevPoint and currentPoint to (0,0) (0,1) and then stepping through the code (with a debugger or with your eyes) to see if each part makes sense to you, and the angle of rotation and axis at the end of the block are what you expect.

If they are what you expect, then I'm guessing the error is in the logic that occurs after that. i.e. you then take the angle and axis and convert them to a matrix which gets multiplied to move the model. A number of convention choices happen in this pipeline --which if swapped can lead to the type of bug you're having:

  • Whether the formula assumes the angle is winding left or right handedly around the axis.
  • Whether the transformation is meant to rotate an object in the world or meant to rotate the camera.
  • Whether the matrix is meant to operate by multiplication on the left or right.
  • Whether rows or columns of matrices are contiguous in memory.

Upvotes: 0

Related Questions