1

我正在尝试使用轨迹球旋转 opengl 场景。我遇到的问题是我的旋转方向与我在屏幕上的滑动方向相反。这是代码片段。

         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);
4

1 回答 1

0

我建议人为地将 prevPoint 和 currentPoint 设置为 (0,0) (0,1),然后单步执行代码(使用调试器或眼睛)以查看每个部分是否对您有意义,以及旋转角度和轴在块的末尾是你所期望的。

如果它们是您所期望的,那么我猜错误是在那之后发生的逻辑中。即,然后您获取角度和轴并将它们转换为矩阵,该矩阵乘以移动模型。此管道中发生了许多约定选择 - 如果交换可能会导致您遇到的错误类型:

  • 公式是否假设角度是围绕轴向左或向右缠绕。
  • 变换是要旋转世界中的对象还是要旋转相机。
  • 矩阵是要通过左乘还是右乘来操作。
  • 矩阵的行或列在内存中是否连续。
于 2015-01-10T18:00:21.577 回答