📄 mtxlib.cpp
字号:
tmp = b[cc][c]; b[cc][c] = b[cc][rowMax]; b[cc][rowMax] = tmp; } // Now everything we do is on row "c". // Set the max cell to 1 by dividing the entire row by that value tmp = a[c][c]; for (cc=0; cc<4; cc++) { a[cc][c] /= tmp; b[cc][c] /= tmp; } // Now do the other rows, so that this column only has a 1 and 0's for (row = 0; row < 4; row++) { if (row != c) { tmp = a[c][row]; for (cc=0; cc<4; cc++) { a[cc][row] -= a[cc][c] * tmp; b[cc][row] -= b[cc][c] * tmp; } } } } *this = b; return *this;}// Return a matrix44 set to the identity matrixmatrix44 IdentityMatrix44() { matrix44 ret; return ret.identity();}// Return the transpose of the matrix44matrix44 TransposeMatrix44(const matrix44 &m) { matrix44 ret(m); return ret.transpose();}// Return the inverted matrix44matrix44 InvertMatrix44(const matrix44 &m) { matrix44 ret(m); return ret.invert();}// Return a 3D axis-rotation matrix44// Pass in 'x', 'y', or 'z' for the axis.matrix44 RotateRadMatrix44(char axis, float rad) { matrix44 ret; float sinA, cosA; sinA = (float)sin(rad); cosA = (float)cos(rad); switch (axis) { case 'x': case 'X': ret[0][0] = 1.0F; ret[1][0] = 0.0F; ret[2][0] = 0.0F; ret[0][1] = 0.0F; ret[1][1] = cosA; ret[2][1] = -sinA; ret[0][2] = 0.0F; ret[1][2] = sinA; ret[2][2] = cosA; break; case 'y': case 'Y': ret[0][0] = cosA; ret[1][0] = 0.0F; ret[2][0] = sinA; ret[0][1] = 0.0F; ret[1][1] = 1.0F; ret[2][1] = 0.0F; ret[0][2] = -sinA; ret[1][2] = 0.0F; ret[2][2] = cosA; break; case 'z': case 'Z': ret[0][0] = cosA; ret[1][0] = -sinA; ret[2][0] = 0.0F; ret[0][1] = sinA; ret[1][1] = cosA; ret[2][1] = 0.0F; ret[0][2] = 0.0F; ret[1][2] = 0.0F; ret[2][2] = 1.0F; break; } ret[0][3] = 0.0F; ret[1][3] = 0.0F; ret[2][3] = 0.0F; ret[3][0] = 0.0F; ret[3][1] = 0.0F; ret[3][2] = 0.0F; ret[3][3] = 1.0F; return ret;}// Return a 3D axis-rotation matrix44// Pass in an arbitrary vector3 axis.matrix44 RotateRadMatrix44(const vector3 &axis, float rad) { matrix44 ret; float sinA, cosA; float invCosA; vector3 nrm = axis; float x, y, z; float xSq, ySq, zSq; nrm.normalize(); sinA = (float)sin(rad); cosA = (float)cos(rad); invCosA = 1.0F - cosA; x = nrm.x; y = nrm.y; z = nrm.z; xSq = x * x; ySq = y * y; zSq = z * z; ret[0][0] = (invCosA * xSq) + (cosA); ret[1][0] = (invCosA * x * y) - (sinA * z ); ret[2][0] = (invCosA * x * z) + (sinA * y ); ret[3][0] = 0.0F; ret[0][1] = (invCosA * x * y) + (sinA * z); ret[1][1] = (invCosA * ySq) + (cosA); ret[2][1] = (invCosA * y * z) - (sinA * x); ret[3][1] = 0.0F; ret[0][2] = (invCosA * x * z) - (sinA * y); ret[1][2] = (invCosA * y * z) + (sinA * x); ret[2][2] = (invCosA * zSq) + (cosA); ret[3][2] = 0.0F; ret[0][3] = 0.0F; ret[1][3] = 0.0F; ret[2][3] = 0.0F; ret[3][3] = 1.0F; return ret;}// Return a 3D translation matrix44matrix44 TranslateMatrix44(float x, float y, float z) { matrix44 ret; ret.identity(); ret[3][0] = x; ret[3][1] = y; ret[3][2] = z; return ret;}// Return a 3D/4D scale matrix44matrix44 ScaleMatrix44(float x, float y, float z, float w) { matrix44 ret; ret.identity(); ret[0][0] = x; ret[1][1] = y; ret[2][2] = z; ret[3][3] = w; return ret;}// Return a "lookat" matrix44 given the current camera position (vector3),// camera-up vector3, and camera-target vector3.matrix44 LookAtMatrix44(const vector3 &camPos, const vector3 &target, const vector3 &camUp ) { matrix44 ret; vector3 F = target - camPos; F.normalize(); vector3 S = CrossProduct(F, Normalized(camUp)); S.normalize(); vector3 U = CrossProduct(S, F); U.normalize(); ret[0][0] = S.x; ret[1][0] = S.y; ret[2][0] = S.z; ret[3][0] = 0.0; ret[0][1] = U.x; ret[1][1] = U.y; ret[2][1] = U.z; ret[3][1] = 0.0; ret[0][2] = -F.x; ret[1][2] = -F.y; ret[2][2] = -F.z; ret[3][2] = 0.0; ret[0][3] = 0.0F; ret[1][3] = 0.0F; ret[2][3] = 0.0F; ret[3][3] = 1.0F; ret *= TranslateMatrix44(-camPos.x, -camPos.y, -camPos.z); return ret;}// Return a frustum matrix44 given the left, right, bottom, top,// near, and far values for the frustum boundaries.matrix44 FrustumMatrix44(float l, float r, float b, float t, float n, float f) { matrix44 ret; float width = r-l; float height = t-b; float depth = f-n; ret[0][0] = (2*n) / width; ret[0][1] = 0.0F; ret[0][2] = 0.0F; ret[0][3] = 0.0F; ret[1][0] = 0.0F; ret[1][1] = (2*n) / height; ret[1][2] = 0.0F; ret[1][3] = 0.0F; ret[2][0] = (r + l) / width; ret[2][1] = (t + b) / height; ret[2][2] = -(f + n) / depth; ret[2][3] = -1.0F; ret[3][0] = 0.0F; ret[3][1] = 0.0F; ret[3][2] = -(2*f*n) / depth; ret[3][3] = 0.0F; return ret;}// Return a perspective matrix44 given the field-of-view in the Y// direction in degrees, the aspect ratio of Y/X, and near and// far plane distances.matrix44 PerspectiveMatrix44(float fovY, float aspect, float n, float f) { matrix44 ret; float angle; float cot; angle = fovY / 2.0F; angle = DegToRad( angle ); cot = (float) cos(angle) / (float) sin(angle); ret[0][0] = cot / aspect; ret[0][1] = 0.0F; ret[0][2] = 0.0F; ret[0][3] = 0.0F; ret[1][0] = 0.0F; ret[1][1] = cot; ret[1][2] = 0.0F; ret[1][3] = 0.0F; ret[2][0] = 0.0F; ret[2][1] = 0.0F; ret[2][2] = -(f + n) / (f - n); ret[2][3] = -1.0F; ret[3][0] = 0.0F; ret[3][1] = 0.0F; ret[3][2] = -(2*f*n) / (f - n); ret[3][3] = 0.0F; return ret;}// Return an orthographic matrix44 given the left, right, bottom, top,// near, and far values for the frustum boundaries.matrix44 OrthoMatrix44(float l, float r, float b, float t, float n, float f) { matrix44 ret; float width = r-l; float height = t-b; float depth = f-n; ret[0][0] = 2.0F / width; ret[0][1] = 0.0F; ret[0][2] = 0.0F; ret[0][3] = 0.0F; ret[1][0] = 0.0F; ret[1][1] = 2.0F / height; ret[1][2] = 0.0F; ret[1][3] = 0.0F; ret[2][0] = 0.0F; ret[2][1] = 0.0F; ret[2][2] = -(2.0F) / depth; ret[2][3] = 0.0F; ret[3][0] = -(r + l) / width; ret[1][3] = -(t + b) / height; ret[3][2] = -(f + n) / depth; ret[3][3] = 1.0F; return ret;}// Return an orientation matrix using 3 basis normalized vectorsmatrix44 OrthoNormalMatrix44(const vector3 &xdir, const vector3 &ydir, const vector3 &zdir){ matrix44 ret; ret[0] = (vector4)xdir; ret[1] = (vector4)ydir; ret[2] = (vector4)zdir; ret[3][3] = 1.0F; return ret;}////////////////////////////////////////////////////////////// Debug functions//// Print a vector2 to a filevoid vector2::fprint(FILE* file, char* str) const { fprintf(file, "%svector2: <%f, %f>\n", str, x, y);}// Print a vector3 to a filevoid vector3::fprint(FILE* file, char* str) const { fprintf(file, "%svector3: <%f, %f, %f>\n", str, x, y, z);}// Print a vector4 to a filevoid vector4::fprint(FILE* file, char* str) const { fprintf(file, "%svector4: <%f, %f, %f, %f>\n", str, x, y, z, w);}// Print a matrix33 to a filevoid matrix33::fprint(FILE* file, char * str) const { fprintf(file, "%smatrix33:\n", str); vector3 row0(col[0][0], col[1][0], col[2][0]); row0.fprint(file, "\t"); vector3 row1(col[0][1], col[1][1], col[2][1]); row1.fprint(file, "\t"); vector3 row2(col[0][2], col[1][2], col[2][2]); row2.fprint(file, "\t");}// Print a matrix44 to a filevoid matrix44::fprint(FILE* file, char* str) const { fprintf(file, "%smatrix44:\n", str); vector4 row0(col[0][0], col[1][0], col[2][0], col[3][0]); row0.fprint(file, "\t"); vector4 row1(col[0][1], col[1][1], col[2][1], col[3][1]); row1.fprint(file, "\t"); vector4 row2(col[0][2], col[1][2], col[2][2], col[3][2]); row2.fprint(file, "\t"); vector4 row3(col[0][3], col[1][3], col[2][3], col[3][3]); row3.fprint(file, "\t");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -