00001 /* 00002 * Copyright(c) 2010 DarkPlaces. 00003 * Copyright(c) 2010 Quake2World. 00004 * 00005 * This program is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU General Public License 00007 * as published by the Free Software Foundation; either version 2 00008 * of the License, or(at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00013 * 00014 * See the GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00019 */ 00020 00021 #ifndef __MATRIXLIB_H__ 00022 #define __MATRIXLIB_H__ 00023 00024 /*#define MATRIX4x4_OPENGLORIENTATION */ 00025 00026 typedef struct matrix4x4_s 00027 { 00028 float m[4][4]; 00029 } matrix4x4_t; 00030 00031 extern const matrix4x4_t identitymatrix; 00032 00033 /* functions for manipulating 4x4 matrices */ 00034 00035 /* copy a matrix4x4 */ 00036 void Matrix4x4_Copy (matrix4x4_t *out, const matrix4x4_t *in); 00037 /* copy only the rotation portion of a matrix4x4 */ 00038 void Matrix4x4_CopyRotateOnly (matrix4x4_t *out, const matrix4x4_t *in); 00039 /* copy only the translate portion of a matrix4x4 */ 00040 void Matrix4x4_CopyTranslateOnly (matrix4x4_t *out, const matrix4x4_t *in); 00041 /* multiply two matrix4x4 together, combining their transformations 00042 * (warning: order matters - Concat(a, b, c) != Concat(a, c, b)) */ 00043 void Matrix4x4_Concat (matrix4x4_t *out, const matrix4x4_t *in1, const matrix4x4_t *in2); 00044 /* swaps the rows and columns of the matrix 00045 * (is this useful for anything?) */ 00046 void Matrix4x4_Transpose (matrix4x4_t *out, const matrix4x4_t *in1); 00047 /* creates a matrix that does the opposite of the matrix provided 00048 * this is a full matrix inverter, it should be able to invert any matrix that 00049 * is possible to invert 00050 * (non-uniform scaling, rotation, shearing, and translation, possibly others) 00051 * warning: this function is SLOW */ 00052 int Matrix4x4_Invert_Full (matrix4x4_t *out, const matrix4x4_t *in1); 00053 /* creates a matrix that does the opposite of the matrix provided 00054 * only supports translate, rotate, scale (not scale3) matrices */ 00055 void Matrix4x4_Invert_Simple (matrix4x4_t *out, const matrix4x4_t *in1); 00056 /* blends between two matrices, used primarily for animation interpolation 00057 * (note: it is recommended to follow this with Matrix4x4_Normalize, a method 00058 * known as nlerp rotation, often better for animation purposes than slerp) */ 00059 void Matrix4x4_Interpolate (matrix4x4_t *out, matrix4x4_t *in1, matrix4x4_t *in2, double frac); 00060 /* zeros all matrix components, used with Matrix4x4_Accumulate */ 00061 void Matrix4x4_Clear (matrix4x4_t *out); 00062 /* adds a weighted contribution from the supplied matrix, used to blend 3 or 00063 * more matrices with weighting, it is recommended that Matrix4x4_Normalize be 00064 * called afterward (a method known as nlerp rotation, often better for 00065 * animation purposes than slerp) */ 00066 void Matrix4x4_Accumulate (matrix4x4_t *out, matrix4x4_t *in, double weight); 00067 /* creates a matrix that does the same rotation and translation as the matrix 00068 * provided, but no uniform scaling, does not support scale3 matrices */ 00069 void Matrix4x4_Normalize (matrix4x4_t *out, matrix4x4_t *in1); 00070 /* creates a matrix with vectors normalized individually (use after 00071 * Matrix4x4_Accumulate) */ 00072 void Matrix4x4_Normalize3 (matrix4x4_t *out, matrix4x4_t *in1); 00073 /* modifies a matrix to have all vectors and origin reflected across the plane 00074 * to the opposite side (at least if axisscale is -2) */ 00075 void Matrix4x4_Reflect (matrix4x4_t *out, double normalx, double normaly, double normalz, double dist, double axisscale); 00076 00077 /* creates an identity matrix 00078 * (a matrix which does nothing) */ 00079 void Matrix4x4_CreateIdentity (matrix4x4_t *out); 00080 /* creates a translate matrix 00081 * (moves vectors) */ 00082 void Matrix4x4_CreateTranslate (matrix4x4_t *out, double x, double y, double z); 00083 /* creates a rotate matrix 00084 * (rotates vectors) */ 00085 void Matrix4x4_CreateRotate (matrix4x4_t *out, double angle, double x, double y, double z); 00086 /* creates a scaling matrix 00087 * (expands or contracts vectors) 00088 * (warning: do not apply this kind of matrix to direction vectors) */ 00089 void Matrix4x4_CreateScale (matrix4x4_t *out, double x); 00090 /* creates a squishing matrix 00091 * (expands or contracts vectors differently in different axis) 00092 * (warning: this is not reversed by Invert_Simple) 00093 * (warning: do not apply this kind of matrix to direction vectors) */ 00094 void Matrix4x4_CreateScale3 (matrix4x4_t *out, double x, double y, double z); 00095 /* creates a matrix for a quake entity */ 00096 void Matrix4x4_CreateFromQuakeEntity (matrix4x4_t *out, double x, double y, double z, double pitch, double yaw, double roll, double scale); 00097 00098 /* converts a matrix4x4 to a set of 3D vectors for the 3 axial directions, and the translate */ 00099 void Matrix4x4_ToVectors (const matrix4x4_t *in, float vx[3], float vy[3], float vz[3], float t[3]); 00100 /* creates a matrix4x4 from a set of 3D vectors for axial directions, and translate */ 00101 void Matrix4x4_FromVectors (matrix4x4_t *out, const float vx[3], const float vy[3], const float vz[3], const float t[3]); 00102 00103 /* converts a matrix4x4 to a double[16] array in the OpenGL orientation */ 00104 void Matrix4x4_ToArrayDoubleGL (const matrix4x4_t *in, double out[16]); 00105 /* creates a matrix4x4 from a double[16] array in the OpenGL orientation */ 00106 void Matrix4x4_FromArrayDoubleGL (matrix4x4_t *out, const double in[16]); 00107 /* converts a matrix4x4 to a double[16] array in the Direct3D orientation */ 00108 void Matrix4x4_ToArrayDoubleD3D (const matrix4x4_t *in, double out[16]); 00109 /* creates a matrix4x4 from a double[16] array in the Direct3D orientation */ 00110 void Matrix4x4_FromArrayDoubleD3D (matrix4x4_t *out, const double in[16]); 00111 00112 /* converts a matrix4x4 to a float[16] array in the OpenGL orientation */ 00113 void Matrix4x4_ToArrayFloatGL (const matrix4x4_t *in, float out[16]); 00114 /* creates a matrix4x4 from a float[16] array in the OpenGL orientation */ 00115 void Matrix4x4_FromArrayFloatGL (matrix4x4_t *out, const float in[16]); 00116 /* converts a matrix4x4 to a float[16] array in the Direct3D orientation */ 00117 void Matrix4x4_ToArrayFloatD3D (const matrix4x4_t *in, float out[16]); 00118 /* creates a matrix4x4 from a float[16] array in the Direct3D orientation */ 00119 void Matrix4x4_FromArrayFloatD3D (matrix4x4_t *out, const float in[16]); 00120 00121 /* converts a matrix4x4 to a float[12] array in the OpenGL orientation */ 00122 void Matrix4x4_ToArray12FloatGL (const matrix4x4_t *in, float out[12]); 00123 /* creates a matrix4x4 from a float[12] array in the OpenGL orientation */ 00124 void Matrix4x4_FromArray12FloatGL (matrix4x4_t *out, const float in[12]); 00125 /* converts a matrix4x4 to a float[12] array in the Direct3D orientation */ 00126 void Matrix4x4_ToArray12FloatD3D (const matrix4x4_t *in, float out[12]); 00127 /* creates a matrix4x4 from a float[12] array in the Direct3D orientation */ 00128 void Matrix4x4_FromArray12FloatD3D (matrix4x4_t *out, const float in[12]); 00129 00130 /* creates a matrix4x4 from an origin and quaternion (used mostly with skeletal model formats such as PSK) */ 00131 void Matrix4x4_FromOriginQuat (matrix4x4_t *m, double ox, double oy, double oz, double x, double y, double z, double w); 00132 /* creates an origin and quaternion from a matrix4x4_t, quat[3] is always positive */ 00133 void Matrix4x4_ToOrigin3Quat4Float (const matrix4x4_t *m, float *origin, float *quat); 00134 /* creates a matrix4x4 from an origin and canonical unit-length quaternion (used mostly with skeletal model formats such as MD5) */ 00135 void Matrix4x4_FromDoom3Joint (matrix4x4_t *m, double ox, double oy, double oz, double x, double y, double z); 00136 00137 /* creates a matrix4x4_t from an origin and canonical unit-length quaternion in short[6] normalized format */ 00138 void Matrix4x4_FromBonePose6s (matrix4x4_t *m, float originscale, const short *pose6s); 00139 /* creates a short[6] representation from normalized matrix4x4_t */ 00140 void Matrix4x4_ToBonePose6s (const matrix4x4_t *m, float origininvscale, short *pose6s); 00141 00142 /* blends two matrices together, at a given percentage (blend controls percentage of in2) */ 00143 void Matrix4x4_Blend (matrix4x4_t *out, const matrix4x4_t *in1, const matrix4x4_t *in2, double blend); 00144 00145 /* transforms a 3D vector through a matrix4x4 */ 00146 void Matrix4x4_Transform (const matrix4x4_t *in, const float v[3], float out[3]); 00147 /* transforms a 4D vector through a matrix4x4 00148 * (warning: if you don't know why you would need this, you don't need it) 00149 * (warning: the 4th component of the vector should be 1.0) */ 00150 void Matrix4x4_Transform4 (const matrix4x4_t *in, const float v[4], float out[4]); 00151 /* reverse transforms a 3D vector through a matrix4x4, at least for *simple* 00152 * cases (rotation and translation *ONLY*), this attempts to undo the results 00153 * of Transform */ 00154 /*void Matrix4x4_SimpleUntransform (const matrix4x4_t *in, const float v[3], float out[3]); */ 00155 /* transforms a direction vector through the rotation part of a matrix */ 00156 void Matrix4x4_Transform3x3 (const matrix4x4_t *in, const float v[3], float out[3]); 00157 /* transforms a positive distance plane (A*x+B*y+C*z-D=0) through a rotation or translation matrix */ 00158 void Matrix4x4_TransformPositivePlane (const matrix4x4_t *in, float x, float y, float z, float d, float *o); 00159 /* transforms a standard plane (A*x+B*y+C*z+D=0) through a rotation or translation matrix */ 00160 void Matrix4x4_TransformStandardPlane (const matrix4x4_t *in, float x, float y, float z, float d, float *o); 00161 00162 /* ease of use functions 00163 * immediately applies a Translate to the matrix */ 00164 void Matrix4x4_ConcatTranslate (matrix4x4_t *out, double x, double y, double z); 00165 /* immediately applies a Rotate to the matrix */ 00166 void Matrix4x4_ConcatRotate (matrix4x4_t *out, double angle, double x, double y, double z); 00167 /* immediately applies a Scale to the matrix */ 00168 void Matrix4x4_ConcatScale (matrix4x4_t *out, double x); 00169 /* immediately applies a Scale3 to the matrix */ 00170 void Matrix4x4_ConcatScale3 (matrix4x4_t *out, double x, double y, double z); 00171 00172 /* extracts origin vector (translate) from matrix */ 00173 void Matrix4x4_OriginFromMatrix (const matrix4x4_t *in, float *out); 00174 /* extracts scaling factor from matrix (only works for uniform scaling) */ 00175 double Matrix4x4_ScaleFromMatrix (const matrix4x4_t *in); 00176 00177 /* replaces origin vector (translate) in matrix */ 00178 void Matrix4x4_SetOrigin (matrix4x4_t *out, double x, double y, double z); 00179 /* moves origin vector (translate) in matrix by a simple translate */ 00180 void Matrix4x4_AdjustOrigin (matrix4x4_t *out, double x, double y, double z); 00181 /* scales vectors of a matrix in place and allows you to scale origin as well */ 00182 void Matrix4x4_Scale (matrix4x4_t *out, double rotatescale, double originscale); 00183 /* ensures each element of the 3x3 rotation matrix is facing in the + direction */ 00184 void Matrix4x4_Abs (matrix4x4_t *out); 00185 00186 #endif /* __MATRIX_H__ */