如何使用opengl中的四元数围绕屏幕中心旋转?

时间:2009-12-09 18:18:07

标签: opengl rotation quaternions arcball

我正在尝试实施arcball /轨迹球旋转,但我的中心有问题 回转。无论如何,我都希望中心成为我的屏幕的中心。

让我解释一下到目前为止我所做的事情。

我创造了一个quaterion         (旋转轴:vector_start x vector_end,angle:vector_start * vector_end)

从那个四元数我创建了一个旋转矩阵,以便与glMultMatrixf(矩阵)一起使用,并获得所需的旋转。

问题在于,虽然我的模型似乎是弧形旋转,但它应该始终围绕其本地原点旋转。我怎么能让它围绕我的屏幕中心旋转 无论其本地出身在哪里?

我想这个问题的解决方案可能是翻译整个旋转轴 在屏幕中央,然后应用旋转,但这可能吗? 我在这里想念一下吗?

2 个答案:

答案 0 :(得分:1)

您应该能够通过以正确的顺序应用旋转和平移矩阵来解决此问题。 在你的代码中你可以转换回原点T(-pos_x,-pos_y,-pos_z),应用你的旋转,并再次转换为对象中心T(pos_x,pos_y,pos_z)。这应该是一般的,与旋转矩阵的构造无关。

答案 1 :(得分:0)

这是我前一段时间为一个3级火箭发射观察者写的一些代码。我从http://www.euclideanspace.com/maths/geometry/rotations

获得了大部分信息

注意:根据您设置坐标系的方式,偏航,俯仰和滚动可能会改变

      // Assuming the angles are in radians.
            double p = curPitch * Math.PI/180.0 / 2.0;
            double y = curYaw * Math.PI/180.0 / 2.0;
            double r = curRoll * Math.PI/180.0 / 2.0;

            double sinp = Math.sin(p);
            double siny = Math.sin(y);
            double sinr = Math.sin(r);
            double cosp = Math.cos(p);
            double cosy = Math.cos(y);
            double cosr = Math.cos(r);

            Vector3 axis = new Vector3();

            //here's the important part: how you get your quaternion vector!
            axis.x = sinr * cosp * cosy - cosr * sinp * siny;
            axis.y = cosr * sinp * cosy + sinr * cosp * siny;
            axis.z = cosr * cosp * siny - sinr * sinp * cosy;

            //now normalize the vector in case we want to use it again later
            axis = Vector3.normalizeVector(axis);

            orientation[1] = axis.x;
            orientation[2] = axis.y;
            orientation[3] = axis.z;

            //w is omega: the angle to rotate about the quaternion
            double w = cosr * cosp * cosy + sinr * sinp * siny;

            w = Math.acos(w) * 2.0;

            orientation[0] = w;

            gl.glPushMatrix();

              //translate object first, then rotate it.
              gl.glTranslated(curDisplacement[0] + saveDisplacement[0], -curDisplacement[1] + saveDisplacement[2], curDisplacement[2] + saveDisplacement[1]);

              //this order might be messed up because I screwed up my coordinate system, but the idea is still there
              gl.glRotated(orientation[0]*180/Math.PI, orientation[2]*180/Math.PI, orientation[3]*180/Math.PI, orientation[1]*180/Math.PI);

             //place your objects
             gl.glPopMatrix();

希望这有帮助!