OpenTK C#에서 객체 방향 가져오기 (Get orientation of object in OpenTK C#)


문제 설명

OpenTK C#에서 객체 방향 가져오기 (Get orientation of object in OpenTK C#)

여러 단계에서 개체를 회전했지만 개체의 실제 방향(Eular 각도)을 얻는 방법을 알 수 없습니다.

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

현재 개체의 방향은 무엇입니까?


참조 솔루션

방법 1:

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:

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:

Quaternion q = currentModelView.ExtractRotation();

Compute the Pitch, yaw, and roll angles from the 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:

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

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;

(by HamzaFarooqRabbid76)

참조 문서

  1. Get orientation of object in OpenTK C# (CC BY‑SA 2.5/3.0/4.0)

#opengl-compat #winforms #C# #opengl #opentk






관련 질문

레거시 OpenGL에서 독립적인 투명도 주문 (Order independent transparency in legacy OpenGL)

나는 검은 질감을 가지고있다. (I have a black texture)

시작/끝 각도 문제가 있는 OpenGL 타원 (OpenGL Ellipse with start/end angle issues)

행렬 기반의 OpenGL로 표시 (Displaying in openGL based on a matrix)

Python에서 OpenGL로 텍스처를 렌더링하는 것은 (Rendering a texture with OpenGL in Python is not)

OpenTK C#에서 객체 방향 가져오기 (Get orientation of object in OpenTK C#)







코멘트