如何操作物体在3D空间中的位置呢,方法有很多。
这里总结一下最流行的矩阵操作。
void InitScaleTransform(Matrix4f& m) const { m.m[0][0] = m_scale.x; m.m[0][1] = 0.0f ; m.m[0][2] = 0.0f ; m.m[0][3] = 0.0f; m.m[1][0] = 0.0f ; m.m[1][1] = m_scale.y; m.m[1][2] = 0.0f ; m.m[1][3] = 0.0f; m.m[2][0] = 0.0f ; m.m[2][1] = 0.0f ; m.m[2][2] = m_scale.z; m.m[2][3] = 0.0f; m.m[3][0] = 0.0f ; m.m[3][1] = 0.0f ; m.m[3][2] = 0.0f ; m.m[3][3] = 1.0f; }这样填写了一个4X4的矩阵,就可以和物体位置相乘,达到放大或者缩小物体的目的
rx.m[0][0] = 1.0f; rx.m[0][1] = 0.0f ; rx.m[0][2] = 0.0f ; rx.m[0][3] = 0.0f; rx.m[1][0] = 0.0f; rx.m[1][1] = cosf(x); rx.m[1][2] = -sinf(x); rx.m[1][3] = 0.0f; rx.m[2][0] = 0.0f; rx.m[2][1] = sinf(x); rx.m[2][2] = cosf(x) ; rx.m[2][3] = 0.0f; rx.m[3][0] = 0.0f; rx.m[3][1] = 0.0f ; rx.m[3][2] = 0.0f ; rx.m[3][3] = 1.0f;
其中空出x行,是表示绕x轴选择。如需要绕y轴或者z轴,可以填写如下代码:
ry.m[0][0] = cosf(y); ry.m[0][1] = 0.0f; ry.m[0][2] = -sinf(y); ry.m[0][3] = 0.0f; ry.m[1][0] = 0.0f ; ry.m[1][1] = 1.0f; ry.m[1][2] = 0.0f ; ry.m[1][3] = 0.0f; ry.m[2][0] = sinf(y); ry.m[2][1] = 0.0f; ry.m[2][2] = cosf(y) ; ry.m[2][3] = 0.0f; ry.m[3][0] = 0.0f ; ry.m[3][1] = 0.0f; ry.m[3][2] = 0.0f ; ry.m[3][3] = 1.0f; rz.m[0][0] = cosf(z); rz.m[0][1] = -sinf(z); rz.m[0][2] = 0.0f; rz.m[0][3] = 0.0f; rz.m[1][0] = sinf(z); rz.m[1][1] = cosf(z) ; rz.m[1][2] = 0.0f; rz.m[1][3] = 0.0f; rz.m[2][0] = 0.0f ; rz.m[2][1] = 0.0f ; rz.m[2][2] = 1.0f; rz.m[2][3] = 0.0f; rz.m[3][0] = 0.0f ; rz.m[3][1] = 0.0f ; rz.m[3][2] = 0.0f; rz.m[3][3] = 1.0f;
#define M_PI 3.1415926 #define ToRadian(x) (float)(((x) * M_PI / 180.0f)) #define ToDegree(x) (float)(((x) * 180.0f / M_PI))
m = rz * ry * rx;
m.m[0][0] = 1.0f; m.m[0][1] = 0.0f; m.m[0][2] = 0.0f; m.m[0][3] = m_worldPos.x; m.m[1][0] = 0.0f; m.m[1][1] = 1.0f; m.m[1][2] = 0.0f; m.m[1][3] = m_worldPos.y; m.m[2][0] = 0.0f; m.m[2][1] = 0.0f; m.m[2][2] = 1.0f; m.m[2][3] = m_worldPos.z; m.m[3][0] = 0.0f; m.m[3][1] = 0.0f; m.m[3][2] = 0.0f; m.m[3][3] = 1.0f;
3D Transformation 3D物体位移函数,布布扣,bubuko.com
原文地址:http://blog.csdn.net/kenden23/article/details/28682369