第一人称相机旋转3D

时间:2011-01-06 09:25:17

标签: android opengl-es 3d

我为android编写了第一人称相机类。

该类非常简单,相机对象有三个轴 X,y和Z

并且有创建ModelView矩阵的函数(即calculateModelViewMatrix()), 沿X和Y轴旋转相机 并沿Z轴平移相机。

我认为我的ModelViewMatrix校准是正确的,我也可以沿Z轴平移相机。

沿x轴的旋转似乎有效,但沿着Y轴它会产生奇怪的结果。 旋转的另一个问题似乎是,不是相机被旋转,我的3D模型开始沿其轴旋转。

我已经根据看点编写了另一个实现,并使用openGL ES的GLU.gluLookAt()函数来获取ModelView矩阵,但是它似乎也遇到了完全相同的问题。

修改

首先感谢您的回复。

我实际上已经实现了Camera类的第二个实现,这次使用了android.opengl.Matrix类中提供的旋转函数。 我提供了下面的代码,这更简单。

令我惊讶的是,结果“完全”相同。 这意味着我的旋转函数和Android的旋转函数产生相同的结果。

我做了一个简单的测试并查看了我的数据。 我只是围绕Y轴旋转LookAt点1-dgree并查看坐标。似乎我的LookAt点落后于精确的旋转角度,例如在20度,它只有10到12度的驯化。 在45度之后它开始反转

1 个答案:

答案 0 :(得分:2)

有一个类android.opengl.Matrix,它是静态方法的集合,可以在你传入的float [16]上完成所需的一切。我强烈建议你使用这些函数而不是自己编写。您可能想要setLookAtM与从您的摄像机角度计算的外观点(使用sin,cos,就像您在代码中所做的那样 - 我假设您知道如何执行此操作。)

- 编辑以回应新答案 -

(顺便说一句,你应该编辑原来的问题 - 你的回答是另一个问题让我感到困惑)

好的,所以这是一种方法。这是未经编译和未经测试的。我决定手动构建矩阵;也许这会给出更多有关正在发生的事情的信息......

class TomCamera {
     // These are our inputs - eye position, and the orientation of the camera.
     public float mEyeX, mEyeY, mEyeZ; // position
     public float mYaw, mPitch, mRoll; // euler angles.

     // this is the outputted matrix to pass to OpenGL.
     public float mCameraMatrix[] = new float [16];

     // convert inputs to outputs.
     public void createMatrix() {
         // create a camera matrix (YXZ order is pretty standard)
         // you may want to negate some of these constant 1s to match expectations.
         Matrix.setRotateM(mCameraMatrix, 0, mYaw, 0, 1, 0);
         Matrix.rotateM(mCameraMatrix, 0, mPitch, 1, 0, 0);
         Matrix.rotateM(mCameraMatrix, 0, mRoll, 0, 0, 1);
         Matrix.translateM(mCameraMatrix, 0, -mEyeX, -mEyeY, -mEyeZ);
     }
}
相关问题