简体   繁体   English

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

[英]Get orientation of object in OpenTK C#

I used to rotate an object in multiple stages but cant figure out how to get the actual orientation (Eular angles) of object. 我曾经以多个阶段旋转对象,但无法弄清楚如何获得对象的实际方向(欧拉角)。

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

What is the orientation of object now 现在物体的方向是什么

I recommend to change the order of the angles, when you apply them tho the current matrix: 当您在当前矩阵中应用角度时,建议更改角度的顺序:

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

Either read back the current matrix from the GPU: 从GPU读取当前矩阵:

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

or calculate a transformation matrix with the same rotations: 或计算旋转相同的变换矩阵:

Matrix4 currentModelView = 
    Matrix4.CreateRotationX(xRot * (float)Math.PI / 180.0f) *
    Matrix4.CreateRotationY(yRot * (float)Math.PI / 180.0f) *
    Matrix4.CreateRotationZ(zRot * (float)Math.PI / 180.0f) * 
    Matrix4.CreateRotationX(rotateCAx * (float)Math.PI / 180.0f) *
    Matrix4.CreateRotationY(rotateCAy * (float)Math.PI / 180.0f) *
    Matrix4.CreateRotationZ(rotateCAz * (float)Math.PI / 180.0f);

Convert the rotation component of the Matrix4 to a Quaternion : Matrix4的旋转分量转换为Quaternion

Quaternion q = currentModelView.ExtractRotation();

Compute the Pitch, yaw, and roll angles from the Quaternion . 根据Quaternion计算俯仰,偏航和侧倾角。 An algorithm fro that can be found at Maths - Conversion Quaternion to Euler . 可以在数学转换四元数到欧拉数中找到的算法。 I've used the OpenGL Mathematics implementation for glm::pitch , glm::yaw and glm::roll : 我已经将OpenGL Mathematics实现用于glm::pitchglm::yawglm::roll

const double epsi = 0.0001;
double y = 2.0 * (q.Y * q.Z + q.W * q.X);
double x = q.W * q.W - q.X * q.X - q.Y * q.Y + q.Z * q.Z;

double pitch = (Math.Abs(q.X) < epsi && Math.Abs(q.Y) < epsi) ? 2.0 * Math.Atan2(q.X, q.W) : Math.Atan2(y, x);
double yaw = Math.Asin(Math.Min(Math.Max(-2.0 * (q.X * q.Z - q.W * q.Y), -1.0), 1.0));
double roll = Math.Atan2(2.0 * (q.X * q.Y + q.W * q.Z), q.W * q.W + q.X * q.X - q.Y * q.Y - q.Z * q.Z);

The angles pitch , yaw and roll correspond to the current rotations around the x, y and z axis (in view space). 角度pitchyawroll对应于当前围绕x,y和z轴(在视图空间中)旋转。

float rot_x = pitch * 180.0f / (float)Math.PI; 
float rot_y = yaw   * 180.0f / (float)Math.PI; 
float rot_z = roll  * 180.0f / (float)Math.PI;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM