UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
r_matrix.h
Go to the documentation of this file.
1 /*
2  * Copyright(c) 2010 DarkPlaces.
3  * Copyright(c) 2010 Quake2World.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or(at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  *
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20 
21 #pragma once
22 
23 /*#define MATRIX4x4_OPENGLORIENTATION */
24 
25 typedef struct matrix4x4_s
26 {
27  float m[4][4];
28 } matrix4x4_t;
29 
30 extern const matrix4x4_t identitymatrix;
31 
32 /* functions for manipulating 4x4 matrices */
33 
34 /* copy a matrix4x4 */
35 void Matrix4x4_Copy (matrix4x4_t* out, const matrix4x4_t* in);
36 /* copy only the rotation portion of a matrix4x4 */
37 void Matrix4x4_CopyRotateOnly (matrix4x4_t* out, const matrix4x4_t* in);
38 /* copy only the translate portion of a matrix4x4 */
40 /* multiply two matrix4x4 together, combining their transformations
41  * (warning: order matters - Concat(a, b, c) != Concat(a, c, b)) */
42 void Matrix4x4_Concat (matrix4x4_t* out, const matrix4x4_t* in1, const matrix4x4_t* in2);
43 /* swaps the rows and columns of the matrix
44  * (is this useful for anything?) */
45 void Matrix4x4_Transpose (matrix4x4_t* out, const matrix4x4_t* in1);
46 /* creates a matrix that does the opposite of the matrix provided
47  * this is a full matrix inverter, it should be able to invert any matrix that
48  * is possible to invert
49  * (non-uniform scaling, rotation, shearing, and translation, possibly others)
50  * warning: this function is SLOW */
51 int Matrix4x4_Invert_Full (matrix4x4_t* out, const matrix4x4_t* in1);
52 /* creates a matrix that does the opposite of the matrix provided
53  * only supports translate, rotate, scale (not scale3) matrices */
54 void Matrix4x4_Invert_Simple (matrix4x4_t* out, const matrix4x4_t* in1);
55 /* blends between two matrices, used primarily for animation interpolation
56  * (note: it is recommended to follow this with Matrix4x4_Normalize, a method
57  * known as nlerp rotation, often better for animation purposes than slerp) */
58 void Matrix4x4_Interpolate (matrix4x4_t* out, matrix4x4_t* in1, matrix4x4_t* in2, double frac);
59 /* zeros all matrix components, used with Matrix4x4_Accumulate */
60 void Matrix4x4_Clear (matrix4x4_t* out);
61 /* adds a weighted contribution from the supplied matrix, used to blend 3 or
62  * more matrices with weighting, it is recommended that Matrix4x4_Normalize be
63  * called afterward (a method known as nlerp rotation, often better for
64  * animation purposes than slerp) */
65 void Matrix4x4_Accumulate (matrix4x4_t* out, matrix4x4_t* in, double weight);
66 /* creates a matrix that does the same rotation and translation as the matrix
67  * provided, but no uniform scaling, does not support scale3 matrices */
69 /* creates a matrix with vectors normalized individually (use after
70  * Matrix4x4_Accumulate) */
72 /* modifies a matrix to have all vectors and origin reflected across the plane
73  * to the opposite side (at least if axisscale is -2) */
74 void Matrix4x4_Reflect (matrix4x4_t* out, double normalx, double normaly, double normalz, double dist, double axisscale);
75 
76 /* creates an identity matrix
77  * (a matrix which does nothing) */
79 /* creates a translate matrix
80  * (moves vectors) */
81 void Matrix4x4_CreateTranslate (matrix4x4_t* out, double x, double y, double z);
82 /* creates a rotate matrix
83  * (rotates vectors) */
84 void Matrix4x4_CreateRotate (matrix4x4_t* out, double angle, double x, double y, double z);
85 /* creates a scaling matrix
86  * (expands or contracts vectors)
87  * (warning: do not apply this kind of matrix to direction vectors) */
88 void Matrix4x4_CreateScale (matrix4x4_t* out, double x);
89 /* creates a squishing matrix
90  * (expands or contracts vectors differently in different axis)
91  * (warning: this is not reversed by Invert_Simple)
92  * (warning: do not apply this kind of matrix to direction vectors) */
93 void Matrix4x4_CreateScale3 (matrix4x4_t* out, double x, double y, double z);
94 /* creates a matrix for a quake entity */
95 void Matrix4x4_CreateFromQuakeEntity (matrix4x4_t* out, double x, double y, double z, double pitch, double yaw, double roll, double scale);
96 
97 /* converts a matrix4x4 to a set of 3D vectors for the 3 axial directions, and the translate */
98 void Matrix4x4_ToVectors (const matrix4x4_t* in, float vx[3], float vy[3], float vz[3], float t[3]);
99 /* creates a matrix4x4 from a set of 3D vectors for axial directions, and translate */
100 void Matrix4x4_FromVectors (matrix4x4_t* out, const float vx[3], const float vy[3], const float vz[3], const float t[3]);
101 
102 /* converts a matrix4x4 to a double[16] array in the OpenGL orientation */
103 void Matrix4x4_ToArrayDoubleGL (const matrix4x4_t* in, double out[16]);
104 /* creates a matrix4x4 from a double[16] array in the OpenGL orientation */
105 void Matrix4x4_FromArrayDoubleGL (matrix4x4_t* out, const double in[16]);
106 /* converts a matrix4x4 to a double[16] array in the Direct3D orientation */
107 void Matrix4x4_ToArrayDoubleD3D (const matrix4x4_t* in, double out[16]);
108 /* creates a matrix4x4 from a double[16] array in the Direct3D orientation */
109 void Matrix4x4_FromArrayDoubleD3D (matrix4x4_t* out, const double in[16]);
110 
111 /* converts a matrix4x4 to a float[16] array in the OpenGL orientation */
112 void Matrix4x4_ToArrayFloatGL (const matrix4x4_t* in, float out[16]);
113 /* creates a matrix4x4 from a float[16] array in the OpenGL orientation */
114 void Matrix4x4_FromArrayFloatGL (matrix4x4_t* out, const float in[16]);
115 /* converts a matrix4x4 to a float[16] array in the Direct3D orientation */
116 void Matrix4x4_ToArrayFloatD3D (const matrix4x4_t* in, float out[16]);
117 /* creates a matrix4x4 from a float[16] array in the Direct3D orientation */
118 void Matrix4x4_FromArrayFloatD3D (matrix4x4_t* out, const float in[16]);
119 
120 /* converts a matrix4x4 to a float[12] array in the OpenGL orientation */
121 void Matrix4x4_ToArray12FloatGL (const matrix4x4_t* in, float out[12]);
122 /* creates a matrix4x4 from a float[12] array in the OpenGL orientation */
123 void Matrix4x4_FromArray12FloatGL (matrix4x4_t* out, const float in[12]);
124 /* converts a matrix4x4 to a float[12] array in the Direct3D orientation */
125 void Matrix4x4_ToArray12FloatD3D (const matrix4x4_t* in, float out[12]);
126 /* creates a matrix4x4 from a float[12] array in the Direct3D orientation */
127 void Matrix4x4_FromArray12FloatD3D (matrix4x4_t* out, const float in[12]);
128 
129 /* creates a matrix4x4 from an origin and quaternion (used mostly with skeletal model formats such as PSK) */
130 void Matrix4x4_FromOriginQuat (matrix4x4_t* m, double ox, double oy, double oz, double x, double y, double z, double w);
131 /* creates an origin and quaternion from a matrix4x4_t, quat[3] is always positive */
132 void Matrix4x4_ToOrigin3Quat4Float (const matrix4x4_t* m, float* origin, float* quat);
133 /* creates a matrix4x4 from an origin and canonical unit-length quaternion (used mostly with skeletal model formats such as MD5) */
134 void Matrix4x4_FromDoom3Joint (matrix4x4_t* m, double ox, double oy, double oz, double x, double y, double z);
135 
136 /* creates a matrix4x4_t from an origin and canonical unit-length quaternion in short[6] normalized format */
137 void Matrix4x4_FromBonePose6s (matrix4x4_t* m, float originscale, const short* pose6s);
138 /* creates a short[6] representation from normalized matrix4x4_t */
139 void Matrix4x4_ToBonePose6s (const matrix4x4_t* m, float origininvscale, short* pose6s);
140 
141 /* blends two matrices together, at a given percentage (blend controls percentage of in2) */
142 void Matrix4x4_Blend (matrix4x4_t* out, const matrix4x4_t* in1, const matrix4x4_t* in2, double blend);
143 
144 /* transforms a 3D vector through a matrix4x4 */
145 void Matrix4x4_Transform (const matrix4x4_t* in, const float v[3], float out[3]);
146 /* transforms a 4D vector through a matrix4x4
147  * (warning: if you don't know why you would need this, you don't need it)
148  * (warning: the 4th component of the vector should be 1.0) */
149 void Matrix4x4_Transform4 (const matrix4x4_t* in, const float v[4], float out[4]);
150 /* reverse transforms a 3D vector through a matrix4x4, at least for *simple*
151  * cases (rotation and translation *ONLY*), this attempts to undo the results
152  * of Transform */
153 /*void Matrix4x4_SimpleUntransform (const matrix4x4_t* in, const float v[3], float out[3]); */
154 /* transforms a direction vector through the rotation part of a matrix */
155 void Matrix4x4_Transform3x3 (const matrix4x4_t* in, const float v[3], float out[3]);
156 /* transforms a positive distance plane (A*x+B*y+C*z-D=0) through a rotation or translation matrix */
157 void Matrix4x4_TransformPositivePlane (const matrix4x4_t* in, float x, float y, float z, float d, float* o);
158 /* transforms a standard plane (A*x+B*y+C*z+D=0) through a rotation or translation matrix */
159 void Matrix4x4_TransformStandardPlane (const matrix4x4_t* in, float x, float y, float z, float d, float* o);
160 
161 /* ease of use functions
162  * immediately applies a Translate to the matrix */
163 void Matrix4x4_ConcatTranslate (matrix4x4_t* out, double x, double y, double z);
164 /* immediately applies a Rotate to the matrix */
165 void Matrix4x4_ConcatRotate (matrix4x4_t* out, double angle, double x, double y, double z);
166 /* immediately applies a Scale to the matrix */
167 void Matrix4x4_ConcatScale (matrix4x4_t* out, double x);
168 /* immediately applies a Scale3 to the matrix */
169 void Matrix4x4_ConcatScale3 (matrix4x4_t* out, double x, double y, double z);
170 
171 /* extracts origin vector (translate) from matrix */
172 void Matrix4x4_OriginFromMatrix (const matrix4x4_t* in, float* out);
173 /* extracts scaling factor from matrix (only works for uniform scaling) */
174 double Matrix4x4_ScaleFromMatrix (const matrix4x4_t* in);
175 
176 /* replaces origin vector (translate) in matrix */
177 void Matrix4x4_SetOrigin (matrix4x4_t* out, double x, double y, double z);
178 /* moves origin vector (translate) in matrix by a simple translate */
179 void Matrix4x4_AdjustOrigin (matrix4x4_t* out, double x, double y, double z);
180 /* scales vectors of a matrix in place and allows you to scale origin as well */
181 void Matrix4x4_Scale (matrix4x4_t* out, double rotatescale, double originscale);
182 /* ensures each element of the 3x3 rotation matrix is facing in the + direction */
183 void Matrix4x4_Abs (matrix4x4_t* out);
void Matrix4x4_ConcatScale(matrix4x4_t *out, double x)
Definition: r_matrix.cpp:1703
void Matrix4x4_FromArray12FloatGL(matrix4x4_t *out, const float in[12])
Definition: r_matrix.cpp:1332
void Matrix4x4_CreateScale(matrix4x4_t *out, double x)
Definition: r_matrix.cpp:708
void Matrix4x4_ConcatRotate(matrix4x4_t *out, double angle, double x, double y, double z)
Definition: r_matrix.cpp:1694
void Matrix4x4_FromArrayDoubleGL(matrix4x4_t *out, const double in[16])
Definition: r_matrix.cpp:1028
void Matrix4x4_SetOrigin(matrix4x4_t *out, double x, double y, double z)
Definition: r_matrix.cpp:1739
void Matrix4x4_FromDoom3Joint(matrix4x4_t *m, double ox, double oy, double oz, double x, double y, double z)
Definition: r_matrix.cpp:1497
void Matrix4x4_FromArray12FloatD3D(matrix4x4_t *out, const float in[12])
Definition: r_matrix.cpp:1402
double Matrix4x4_ScaleFromMatrix(const matrix4x4_t *in)
Definition: r_matrix.cpp:1733
void Matrix4x4_TransformPositivePlane(const matrix4x4_t *in, float x, float y, float z, float d, float *o)
Definition: r_matrix.cpp:1628
void Matrix4x4_ToArrayFloatD3D(const matrix4x4_t *in, float out[16])
Definition: r_matrix.cpp:1223
static const vec3_t scale
void Matrix4x4_CopyRotateOnly(matrix4x4_t *out, const matrix4x4_t *in)
Definition: r_matrix.cpp:31
voidpf uLong int origin
Definition: ioapi.h:45
void Matrix4x4_Invert_Simple(matrix4x4_t *out, const matrix4x4_t *in1)
Definition: r_matrix.cpp:462
void Matrix4x4_Accumulate(matrix4x4_t *out, matrix4x4_t *in, double weight)
Definition: r_matrix.cpp:529
void Matrix4x4_CreateIdentity(matrix4x4_t *out)
Definition: r_matrix.cpp:597
int Matrix4x4_Invert_Full(matrix4x4_t *out, const matrix4x4_t *in1)
Definition: r_matrix.cpp:170
void Matrix4x4_CreateFromQuakeEntity(matrix4x4_t *out, double x, double y, double z, double pitch, double yaw, double roll, double scale)
Definition: r_matrix.cpp:748
void Matrix4x4_Normalize3(matrix4x4_t *out, matrix4x4_t *in1)
Definition: r_matrix.cpp:545
void Matrix4x4_Blend(matrix4x4_t *out, const matrix4x4_t *in1, const matrix4x4_t *in2, double blend)
Definition: r_matrix.cpp:1566
void Matrix4x4_Copy(matrix4x4_t *out, const matrix4x4_t *in)
Definition: r_matrix.cpp:26
float m[4][4]
Definition: r_matrix.h:27
void Matrix4x4_Scale(matrix4x4_t *out, double rotatescale, double originscale)
Definition: r_matrix.cpp:1765
void Matrix4x4_Clear(matrix4x4_t *out)
Definition: r_matrix.cpp:522
struct matrix4x4_s matrix4x4_t
void Matrix4x4_FromBonePose6s(matrix4x4_t *m, float originscale, const short *pose6s)
Definition: r_matrix.cpp:1526
void Matrix4x4_FromVectors(matrix4x4_t *out, const float vx[3], const float vy[3], const float vz[3], const float t[3])
Definition: r_matrix.cpp:950
void Matrix4x4_ToArrayDoubleGL(const matrix4x4_t *in, double out[16])
Definition: r_matrix.cpp:989
void Matrix4x4_ToArrayDoubleD3D(const matrix4x4_t *in, double out[16])
Definition: r_matrix.cpp:1067
void Matrix4x4_ConcatScale3(matrix4x4_t *out, double x, double y, double z)
Definition: r_matrix.cpp:1712
void Matrix4x4_Concat(matrix4x4_t *out, const matrix4x4_t *in1, const matrix4x4_t *in2)
Definition: r_matrix.cpp:90
void Matrix4x4_CreateRotate(matrix4x4_t *out, double angle, double x, double y, double z)
Definition: r_matrix.cpp:656
void Matrix4x4_Reflect(matrix4x4_t *out, double normalx, double normaly, double normalz, double dist, double axisscale)
Definition: r_matrix.cpp:570
void Matrix4x4_OriginFromMatrix(const matrix4x4_t *in, float *out)
Definition: r_matrix.cpp:1720
const matrix4x4_t identitymatrix
Definition: r_matrix.cpp:24
void Matrix4x4_CopyTranslateOnly(matrix4x4_t *out, const matrix4x4_t *in)
Definition: r_matrix.cpp:51
void Matrix4x4_Interpolate(matrix4x4_t *out, matrix4x4_t *in1, matrix4x4_t *in2, double frac)
Definition: r_matrix.cpp:515
void Matrix4x4_Transform(const matrix4x4_t *in, const float v[3], float out[3])
Definition: r_matrix.cpp:1587
void Matrix4x4_ToBonePose6s(const matrix4x4_t *m, float origininvscale, short *pose6s)
Definition: r_matrix.cpp:1541
void Matrix4x4_FromArrayDoubleD3D(matrix4x4_t *out, const double in[16])
Definition: r_matrix.cpp:1106
void Matrix4x4_Normalize(matrix4x4_t *out, matrix4x4_t *in1)
Definition: r_matrix.cpp:536
void Matrix4x4_ToArrayFloatGL(const matrix4x4_t *in, float out[16])
Definition: r_matrix.cpp:1145
void Matrix4x4_FromOriginQuat(matrix4x4_t *m, double ox, double oy, double oz, double x, double y, double z, double w)
Definition: r_matrix.cpp:1441
void Matrix4x4_Transform3x3(const matrix4x4_t *in, const float v[3], float out[3])
Definition: r_matrix.cpp:1615
void Matrix4x4_Abs(matrix4x4_t *out)
Definition: r_matrix.cpp:1787
void Matrix4x4_ToOrigin3Quat4Float(const matrix4x4_t *m, float *origin, float *quat)
Definition: r_matrix.cpp:1471
void Matrix4x4_AdjustOrigin(matrix4x4_t *out, double x, double y, double z)
Definition: r_matrix.cpp:1752
void Matrix4x4_FromArrayFloatD3D(matrix4x4_t *out, const float in[16])
Definition: r_matrix.cpp:1262
void Matrix4x4_CreateTranslate(matrix4x4_t *out, double x, double y, double z)
Definition: r_matrix.cpp:617
void Matrix4x4_ToVectors(const matrix4x4_t *in, float vx[3], float vy[3], float vz[3], float t[3])
Definition: r_matrix.cpp:919
void Matrix4x4_Transpose(matrix4x4_t *out, const matrix4x4_t *in1)
Definition: r_matrix.cpp:145
void Matrix4x4_ToArray12FloatD3D(const matrix4x4_t *in, float out[12])
Definition: r_matrix.cpp:1371
void Matrix4x4_TransformStandardPlane(const matrix4x4_t *in, float x, float y, float z, float d, float *o)
Definition: r_matrix.cpp:1645
void Matrix4x4_FromArrayFloatGL(matrix4x4_t *out, const float in[16])
Definition: r_matrix.cpp:1184
void Matrix4x4_Transform4(const matrix4x4_t *in, const float v[4], float out[4])
Definition: r_matrix.cpp:1600
void Matrix4x4_ToArray12FloatGL(const matrix4x4_t *in, float out[12])
Definition: r_matrix.cpp:1301
void Matrix4x4_ConcatTranslate(matrix4x4_t *out, double x, double y, double z)
Definition: r_matrix.cpp:1685
QGL_EXTERN int GLboolean GLfloat * v
Definition: r_gl.h:120
static struct mdfour * m
Definition: md4.cpp:35
void Matrix4x4_CreateScale3(matrix4x4_t *out, double x, double y, double z)
Definition: r_matrix.cpp:728