opengl轨迹球

发布于 2024-12-15 05:43:45 字数 1663 浏览 0 评论 0原文

我正在尝试使用轨迹球旋转 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);

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饮惑 2024-12-22 05:43:45

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

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

  • 公式是否假设角度是向左还是向右围绕轴缠绕。
  • 变换是为了旋转世界中的对象还是为了旋转相机。
  • 矩阵是否要通过左乘或右乘进行运算。
  • 矩阵的行或列在内存中是否连续。

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文