OpenGL Angles to Axes
来源:互联网 发布:spring 嵌套事务 源码 编辑:程序博客网 时间:2024/06/01 09:00
http://www.songho.ca/opengl/gl_anglestoaxes.html
Updates: This page has been translated into Spanish language version by Maria Ramos from Webhostinghub.com/support/edu.
The rotation angles directly affects the first 3 columns of OpenGL GL_MODELVIEW matrix, preciselyleft, up and forward axis elements. For example, if a unit vector along X axis, (1, 0, 0) is multiplied by an arbitrary 3x3 rotation matrix, then the result of the vector after rotation is;
It means the first column (m0, m1, m2) of the rotation matrix represents the coordinates of theleft axis after rotated. Similarly, the second column is the up axis and the third column is theforward axis.
This article describes how to contruct GL_MODELVIEW matrix with rotation angles.
Axis Rotations
First, we look at a rotation around each axis; +X, +Y and +Z. We project three axes onto a plane in 3 different ways, so the axis that we want to rotate is facing toward you. The positive rotation direction becomes counter clockwise (right hand rule).
Rotation about Left(X) Axis (Pitch)
Initial value of up(Y) and forward(Z) axes are (0, 1, 0) and (0, 0, 1). If left(X) axis rotates A degree, then new up(Y') axis becomes (0, cosA, sinA) and forward(Z') becomes (0, -sinA, cosA). The new axes are inserted as column components of the 3x3 rotation matrix. Then, the rotation matrix becomes;
Rotation about Up(Y) Axis (Yaw, Heading)
Now, we rotate around up vector, which is facing toward you, with B angle . Left(X) axis is transformed from (1, 0, 0) to X' (cosB, 0, -sinB). And forward(Z) axis is from (0, 0, 1) to Z' (sinB, 0, cosB).
Rotation about Forward(Z) Axis (Roll)
If we rotate forward(Z) axis with angle C degree, the original left(X) (1, 0, 0) axis becomes X' (cosC, sinC, 0), and up(Y) (0, 1, 0) axis becomes Y' (-sinC, cosC, 0).
Angles To Axes
We can combine these separate axis rotations into one matrix by multiplying above 3 matrices together. Note that multiplication of matrices is not commutative, so a different order of matrix multiplication results in a different outcome. There are 6 different combinations are possible; RxRyRz, RxRzRy, RyRxRz, RyRzRx, RzRxRy and RzRyRx.
The left column of the combined rotation matrix is the left axis after rotated, the middle column is theup axis, and the right column is the forward axis.
Here is a C++ example code for RxRyRz combination. It performs 3 rotations in order of Rz (roll), Ry (yaw) then Rx (pitch). The results ofleft, up and forward axis can be used to construct GL_MODELVIEW matrix.
struct Vector3{ float x; float y; float z; Vector3() : x(0), y(0), z(0) {}; // initialze when created};///////////////////////////////////////////////////////////////////////////////// convert Euler angles(x,y,z) to axes(left, up, forward)// Each column of the rotation matrix represents left, up and forward axis.// The order of rotation is Roll->Yaw->Pitch (Rx*Ry*Rz)// Rx: rotation about X-axis, pitch// Ry: rotation about Y-axis, yaw(heading)// Rz: rotation about Z-axis, roll// Rx Ry Rz// |1 0 0| | Cy 0 Sy| |Cz -Sz 0| | CyCz -CySz Sy |// |0 Cx -Sx|*| 0 1 0|*|Sz Cz 0| = | SxSyCz+CxSz -SxSySz+CxCz -SxCy|// |0 Sx Cx| |-Sy 0 Cy| | 0 0 1| |-CxSyCz+SxSz CxSySz+SxCz CxCy|///////////////////////////////////////////////////////////////////////////////void anglesToAxes(const Vector3 angles, Vector3& left, Vector3& up, Vector3& forward){ const float DEG2RAD = 3.141593f / 180; float sx, sy, sz, cx, cy, cz, theta; // rotation angle about X-axis (pitch) theta = angles.x * DEG2RAD; sx = sinf(theta); cx = cosf(theta); // rotation angle about Y-axis (yaw) theta = angles.y * DEG2RAD; sy = sinf(theta); cy = cosf(theta); // rotation angle about Z-axis (roll) theta = angles.z * DEG2RAD; sz = sinf(theta); cz = cosf(theta); // determine left axis left.x = cy*cz; left.y = sx*sy*cz + cx*sz; left.z = -cx*sy*cz + sx*sz; // determine up axis up.x = -cy*sz; up.y = -sx*sy*sz + cx*cz; up.z = cx*sy*sz + sx*cz; // determine forward axis forward.x = sy; forward.y = -sx*cy; forward.z = cx*cy;}
- OpenGL Angles to Axes
- Rotation Matrix To Euler Angles
- matplotlib.axes.Axes.plot
- 欧拉角(Euler angles)
- 欧拉角(Euler Angles)
- jrMz and angles
- jrMz and angles
- HDU jrmz and angles
- jrMz and angles
- jrMz and angles 暴力
- 欧拉角(Euler angles)
- OpenGL: Render to Texture
- matplotlib.axes.Axes.errorbar 误差bar图像
- Axes Studio工作室章程
- XPath Axes(坐标轴)
- Matlab axes的使用
- XPath Axes(坐标轴)
- XPath Axes(坐标轴)
- app store提交过程小记
- 图的关键路径
- 编程算法 - 将排序数组按绝对值大小排序 代码(C)
- java正则表达式大全
- 2014西安赛区网络赛 5014 Number Sequence
- OpenGL Angles to Axes
- POJ3259 Wormholes(BF)
- linux12.04上安装dnw发送端
- 编程之美:第一章 1.10双线程高效下载
- 引用
- HDU 5014 Number Sequence(西安网络赛H题)
- TCP 的那些事儿(上)
- oracle数据库表解锁
- 编程之美:第一章 1.11一排石头的游戏