在OpenTK C#中获取对象的方向

问题描述 投票:1回答:1

我曾经分多个阶段旋转对象,但无法弄清楚如何获得对象的实际方向(欧拉角)。

GL.Rotate(rotateCAx, new Vector3d(1, 0, 0));
GL.Rotate(rotateCAy, new Vector3d(0, 1, 0));
GL.Rotate(rotateCAz, new Vector3d(0, 0, 1));
GL.Rotate(xRot, new Vector3d(1, 0, 0));
GL.Rotate(yRot, new Vector3d(0, 1, 0));
GL.Rotate(zRot, new Vector3d(0, 0, 1));

现在对象的方向是什么

c# winforms opengl opentk
1个回答
0
投票

要么从GPU读取当前矩阵:

Matrix4 currentModelView;
GL.GetFloat(GetPName.ModelviewMatrix, out currentModelView);

或计算具有相同旋转度的变换矩阵:

Matrix4 currentModelView = 
    Matrix4.CreateRotationX(zRot) *
    Matrix4.CreateRotationY(yRot) *
    Matrix4.CreateRotationZ(xRot) * 
    Matrix4.CreateRotationX(rotateCAz) *
    Matrix4.CreateRotationY(rotateCAy) *
    Matrix4.CreateRotationZ(rotateCAx);

Matrix4的旋转分量转换为Matrix4

Quaternion

Quaternion计算Quaternion q = currentModelView.ExtractRotation(); 角度。可以在Pitch, yaw, and roll处找到的算法。我已经将Quaternion实现用于Maths - Conversion Quaternion to EulerOpenGL Mathematicsglm::pitch

glm::pitch

角度glm::yawglm::yawglm::roll对应于当前绕x,y和z轴(在视图空间中)旋转。

© www.soinside.com 2019 - 2024. All rights reserved.