#include #include #define PI_OVER_180 0.01745329251994329444f #define EPSILON_E3 (float)(1E-3) #define CompareFloat(a, b) ((fabs(a-b) fabs(temp[i][i])) { swap = j; } } if (swap != i) { /* ** Swap rows. */ for (k = 0; k < 4; k++) { t = temp[i][k]; temp[i][k] = temp[swap][k]; temp[swap][k] = t; t = inverse[i*4+k]; inverse[i*4+k] = inverse[swap*4+k]; inverse[swap*4+k] = t; } } if (temp[i][i] == 0) { /* ** No non-zero pivot. The matrix is singular, which shouldn't ** happen. This means the user gave us a bad matrix. */ return INS_FALSE; } t = temp[i][i]; for (k = 0; k < 4; k++) { temp[i][k] /= t; inverse[i*4+k] /= t; } for (j = 0; j < 4; j++) { if (j != i) { t = temp[j][i]; for (k = 0; k < 4; k++) { temp[j][k] -= temp[i][k]*t; inverse[j*4+k] -= inverse[i*4+k]*t; } } } } return INS_TRUE; } GLfloat *SetRotateMatrix4x4_X_Axis(GLfloat *m, GLfloat angle) { float radians = PI_OVER_180*angle; LoadIdentityMatrix(m); m[5] = cosf(radians); m[6] = sinf(radians); m[9] = -m[6];//-sinf(radians); m[10] = m[5];//cosf(radians); return m; } GLfloat *RotateMatrix4x4_X_Axis(GLfloat *m, GLfloat angle) { GLfloat tm[4*4]; SetRotateMatrix4x4_X_Axis(tm, angle); MultMatrix4x4(m, tm, m); return m; } GLfloat *SetRotateMatrix4x4_Y_Axis(GLfloat *m, GLfloat angle) { float radians = PI_OVER_180*angle; LoadIdentityMatrix(m); m[0] = cosf(radians); m[2] = -sinf(radians); m[8] = -m[2];//sinf(radians); m[10] = m[0];//cosf(radians); return m; } GLfloat *RotateMatrix4x4_Y_Axis(GLfloat *m, GLfloat angle) { GLfloat tm[4*4]; SetRotateMatrix4x4_Y_Axis(tm, angle); MultMatrix4x4(m, tm, m); return m; } GLfloat *SetRotateMatrix4x4_Z_Axis(GLfloat *m, GLfloat angle) { float radians = PI_OVER_180*angle; LoadIdentityMatrix(m); m[0] = cosf(radians); m[1] = sinf(radians); m[4] = -m[1];//-sinf(radians); m[5] = m[0];//cosf(radians); return m; } GLfloat *RotateMatrix4x4_Z_Axis(GLfloat *m, GLfloat angle) { GLfloat tm[4*4]; SetRotateMatrix4x4_Z_Axis(tm, angle); MultMatrix4x4(m, tm, m); return m; } /* * 绕任意轴向量axis_nor逆时针旋转angle度 */ GLfloat *SetRotateMat4x4(GLfloat *mat4x4, Vec3f *axis_nor, GLfloat angle) { float c, s, one_c; float xx, yy, zz; float xs, ys, zs; float xy, xz, yz; c = cosf(angle*PI_OVER_180); s = sinf(angle*PI_OVER_180); one_c = 1.0 - c; xx = axis_nor->x * axis_nor->x; yy = axis_nor->y * axis_nor->y; zz = axis_nor->z * axis_nor->z; xs = axis_nor->x * s; ys = axis_nor->y * s; zs = axis_nor->z * s; xy = axis_nor->x * axis_nor->y; xz = axis_nor->x * axis_nor->z; yz = axis_nor->y * axis_nor->z; mat4x4[ 0] = (one_c * xx) + c; mat4x4[ 1] = (one_c * xy) + zs; mat4x4[ 2] = (one_c * xz) - ys; mat4x4[ 3] = 0.0; mat4x4[ 4] = (one_c * xy) - zs; mat4x4[ 5] = (one_c * yy) + c; mat4x4[ 6] = (one_c * yz) + xs; mat4x4[ 7] = 0.0; mat4x4[ 8] = (one_c * xz) + ys; mat4x4[ 9] = (one_c * yz) - xs; mat4x4[10] = (one_c * zz) + c; mat4x4[11] = 0.0; mat4x4[12] = 0.0; mat4x4[13] = 0.0; mat4x4[14] = 0.0; mat4x4[15] = 1.0; return mat4x4; } /* * 绕任意轴向量axis_nor逆时针旋转angle度 */ GLfloat *RotateMat4x4(GLfloat *mat4x4, Vec3f *axis_nor, GLfloat angle) { GLfloat tm[4*4]; SetRotateMat4x4(tm, axis_nor, angle); MultMatrix4x4(mat4x4, tm, mat4x4); return mat4x4; } GLfloat *SetTranslateMatrix4x4 (GLfloat *m, GLfloat trX, GLfloat trY, GLfloat trZ) { LoadIdentityMatrix(m); m[12] = trX; m[13] = trY; m[14] = trZ; return m; } GLfloat *TranslateMatrix4x4 (GLfloat *m, GLfloat trX, GLfloat trY, GLfloat trZ) { GLfloat tm[4*4]; SetTranslateMatrix4x4(tm, trX, trY, trZ); MultMatrix4x4(m, tm, m); return m; } GLfloat *SetScaleMatrix4x4 (GLfloat *m, GLfloat scaleX, GLfloat scaleY, GLfloat scaleZ) { LoadIdentityMatrix(m); m[ 0] = scaleX; m[ 5] = scaleY; m[10] = scaleZ; return m; } GLfloat *ScaleMatrix4x4 (GLfloat *m, GLfloat scaleX, GLfloat scaleY, GLfloat scaleZ) { GLfloat tm[4*4]; SetScaleMatrix4x4(tm, scaleX, scaleY, scaleZ); MultMatrix4x4(m, tm, m); return m; }