KallistiOS git master
Independent SDK for the Sega Dreamcast
|
SH4-optimized matrix and linear algebra routines More...
Files | |
file | matrix.h |
Basic matrix operations. | |
file | matrix3d.h |
3D matrix operations. | |
file | vec3f.h |
Basic matrix operations. | |
file | vector.h |
Primitive matrix, vector, and point types. | |
Data Structures | |
struct | vec3f_t |
3D floating-point vector More... | |
struct | vector_t |
4-part vector type. More... | |
Macros | |
#define | mat_trans_single(x, y, z) |
Macro to transform a single vertex by the internal matrix. | |
#define | mat_trans_single4(x, y, z, w) |
Macro to transform a single vertex by the internal matrix. | |
#define | mat_trans_single3(x, y, z) |
Macro to transform a single vertex by the internal matrix. | |
#define | mat_trans_nodiv(x, y, z, w) |
Macro to transform a single vertex by the internal matrix with no perspective division. | |
#define | mat_trans_single3_nodiv(x, y, z) |
Macro to transform a single 3d vertex coordinate by the internal matrix with no perspective division. | |
#define | mat_trans_single3_nomod(x, y, z, x2, y2, z2) |
Macro to transform a single 3d vertex coordinate by the internal matrix with perspective division. | |
#define | mat_trans_single3_nodiv_nomod(x, y, z, x2, y2, z2) |
Macro to transform a single 3d vertex coordinate by the internal matrix. | |
#define | mat_trans_single3_nodivw(x, y, z, w) |
Macro to transform a single 3d vertex coordinate by the internal matrix. | |
#define | mat_trans_single3_nodiv_div(x, y, z, xd, yd, zd) |
Macro to transform a single 3d vertex coordinate by the internal matrix both with and without perspective division. | |
#define | mat_trans_normal3(x, y, z) |
Macro to transform a single vertex normal by the internal matrix. | |
#define | mat_trans_normal3_nomod(x, y, z, x2, y2, z2) |
Macro to transform a single vertex normal by the internal matrix. | |
#define | vec3f_dot(x1, y1, z1, x2, y2, z2, w) |
Macro to return the scalar dot product of two 3d vectors. | |
#define | vec3f_length(x, y, z, w) |
Macro to return scalar Euclidean length of a 3d vector. | |
#define | vec3f_distance(x1, y1, z1, x2, y2, z2, w) |
Macro to return the Euclidean distance between two 3d vectors. | |
#define | vec3f_normalize(x, y, z) |
Macro to return the normalized version of a vector. | |
#define | vec3f_sub_normalize(x1, y1, z1, x2, y2, z2, x3, y3, z3) |
Macro to return the normalized version of a vector minus another vector. | |
#define | vec3f_rotr_xy(px, py, pz, cx, cy, cz, r) |
Macro to rotate a vector about its origin on the x, y plane. | |
#define | vec3f_rotr_xz(px, py, pz, cx, cy, cz, r) |
Macro to rotate a vector about its origin on the x, z plane. | |
#define | vec3f_rotr_yz(px, py, pz, cx, cy, cz, r) |
Macro to rotate a vector about its origin on the y, z plane. | |
#define | vec3f_rotd_xy(px, py, pz, cx, cy, cz, r) |
Macro to rotate a vector about its origin on the x, y plane. | |
#define | vec3f_rotd_xz(px, py, pz, cx, cy, cz, r) |
Macro to rotate a vector about its origin on the x, z plane. | |
#define | vec3f_rotd_yz(px, py, pz, cx, cy, cz, r) |
Macro to rotate a vector about its origin on the y, z plane. | |
Typedefs | |
typedef float | matrix_t[4][4] |
Basic 4x4 matrix type. | |
typedef vector_t | point_t |
4-part point type (alias to the vector_t type). | |
Functions | |
void | mat_store (matrix_t *out) |
Copy the internal matrix to a memory one. | |
void | mat_load (const matrix_t *src) |
Copy a memory matrix into the internal one. | |
void | mat_identity (void) |
Clear the internal matrix to identity. | |
void | mat_apply (const matrix_t *src) |
Apply a matrix. | |
void | mat_transform (const vector_t *invecs, vector_t *outvecs, int veccnt, int stride) |
Transform vectors by the internal matrix. | |
void | mat_transform_sq (void *input, void *output, int veccnt) |
Transform vectors by the internal matrix into the store queues. | |
void | mat_rotate_x (float r) |
Rotate around the X-axis. | |
void | mat_rotate_y (float r) |
Rotate around the Y-axis. | |
void | mat_rotate_z (float r) |
Rotate around the Z-axis. | |
void | mat_rotate (float xr, float yr, float zr) |
Rotate around all axes. | |
void | mat_translate (float x, float y, float z) |
Perform a 3D translation. | |
void | mat_scale (float x, float y, float z) |
Perform a 3D scale operation. | |
void | mat_perspective (float xcenter, float ycenter, float cot_fovy_2, float znear, float zfar) |
Set up a perspective view frustum. | |
void | mat_lookat (const point_t *eye, const point_t *center, const vector_t *up) |
Set up a "camera". | |
SH4-optimized matrix and linear algebra routines
#define mat_trans_nodiv | ( | x, | |
y, | |||
z, | |||
w ) |
Macro to transform a single vertex by the internal matrix with no perspective division.
This macro is an inline assembly operation to transform a single vertex. It works most efficiently if the x value is in fr0, y is in fr1, z is in fr2, and w is in fr3 before using the macro. This macro is similar to mat_trans_single(), but this one does not do any perspective division.
x | The X coordinate to transform. |
y | The Y coordinate to transform. |
z | The Z coordinate to transform. |
w | The W coordinate to transform. |
#define mat_trans_normal3 | ( | x, | |
y, | |||
z ) |
Macro to transform a single vertex normal by the internal matrix.
This macro is an inline assembly operation to transform a 3 float vertex normal. It works most efficiently if the x value is in fr8, y is in fr9, and z is in fr10 before using the macro. This macro is similar to mat_trans_nodiv(), but this one sets the W component to 0 in order to transform a vertex normal, rather than 1 for a vertex position.
x | The X normal to transform. |
y | The Y normal to transform. |
z | The Z normal to transform. |
#define mat_trans_normal3_nomod | ( | x, | |
y, | |||
z, | |||
x2, | |||
y2, | |||
z2 ) |
Macro to transform a single vertex normal by the internal matrix.
This macro is an inline assembly operation to transform a 3 float vertex normal. It works most efficiently if the x value is in fr8, y is in fr9, and z is in fr10 before using the macro. This macro is similar to mat_trans_normal3(), but this one does not modify the input operands, instead storing the transformed vector to the output operands.
x | The X normal to input transform. |
y | The Y normal to input transform. |
z | The Z normal to input transform. |
x2 | The X normal to output transform. |
y2 | The Y normal to output transform. |
z2 | The Z normal to output transform. |
#define mat_trans_single | ( | x, | |
y, | |||
z ) |
Macro to transform a single vertex by the internal matrix.
This macro is an inline assembly operation to transform a single vertex. It works most efficiently if the x value is in fr0, y is in fr1, and z is in fr2 before using the macro.
x | The X coordinate to transform. |
y | The Y coordinate to transform. |
z | The Z coordinate to transform. |
#define mat_trans_single3 | ( | x, | |
y, | |||
z ) |
Macro to transform a single vertex by the internal matrix.
This macro is an inline assembly operation to transform a single vertex. It works most efficiently if the x value is in fr0, y is in fr1, and z is in fr2 before using the macro. This macro is similar to mat_trans_single(), but this one leaves z/w instead of 1/w for the z component.
x | The X coordinate to transform. |
y | The Y coordinate to transform. |
z | The Z coordinate to transform. |
#define mat_trans_single3_nodiv | ( | x, | |
y, | |||
z ) |
Macro to transform a single 3d vertex coordinate by the internal matrix with no perspective division.
This macro is an inline assembly operation to transform a 3 float vertex coordinate. It works most efficiently if the x value is in fr12, y is in fr13, and z is in fr14 before using the macro. This macro is similar to mat_trans_nodiv(), but this one sets the W component to 1 for use with a 3d vector.
x | The X coordinate to transform. |
y | The Y coordinate to transform. |
z | The Z coordinate to transform. |
#define mat_trans_single3_nodiv_div | ( | x, | |
y, | |||
z, | |||
xd, | |||
yd, | |||
zd ) |
Macro to transform a single 3d vertex coordinate by the internal matrix both with and without perspective division.
This macro is an inline assembly operation to transform a 3 float vertex coordinate. It works most efficiently if the x value is in fr0, y is in fr1, and z is in fr2 before using the macro. This macro is similar to mat_trans_single(), but this one is used for transforming input vertex with and without perspective division.
x | The X coordinate to transform without perspective divide. |
y | The Y coordinate to transform without perspective divide. |
z | The Z coordinate to transform without perspective divide. |
xd | The X coordinate to output transform with perspective divide. |
yd | The Y coordinate to output transform with perspective divide. |
zd | The Z coordinate to output transform with perspective divide. |
#define mat_trans_single3_nodiv_nomod | ( | x, | |
y, | |||
z, | |||
x2, | |||
y2, | |||
z2 ) |
Macro to transform a single 3d vertex coordinate by the internal matrix.
This macro is an inline assembly operation to transform a 3 float vertex coordinate. It works most efficiently if the x value is in fr12, y is in fr13, and z is in fr14 before using the macro. This macro is similar to mat_trans_single3_nodiv(), but this one does not modify the input operands, instead storing the transformed vector to the output operands.
x | The X coordinate to input transform. |
y | The Y coordinate to input transform. |
z | The Z coordinate to input transform. |
x2 | The X coordinate to output transform. |
y2 | The Y coordinate to output transform. |
z2 | The Z coordinate to output transform. |
#define mat_trans_single3_nodivw | ( | x, | |
y, | |||
z, | |||
w ) |
Macro to transform a single 3d vertex coordinate by the internal matrix.
This macro is an inline assembly operation to transform a 3 float vertex coordinate. It works most efficiently if the x value is in fr12, y is in fr13, and z is in fr14 before using the macro. This macro is similar to mat_trans_single3_nodiv(), but this one stores the W component of transform for later perspective divide.
x | The X coordinate to transform. |
y | The Y coordinate to transform. |
z | The Z coordinate to transform. |
w | The W coordinate output of transform. |
#define mat_trans_single3_nomod | ( | x, | |
y, | |||
z, | |||
x2, | |||
y2, | |||
z2 ) |
Macro to transform a single 3d vertex coordinate by the internal matrix with perspective division.
This macro is an inline assembly operation to transform a 3 float vertex coordinate. It works most efficiently if the x value is in fr12, y is in fr13, and z is in fr14 before using the macro. This macro is similar to mat_trans_single(), but this one does not modify the input operands, instead storing the transformed vector to the output operands.
x | The X coordinate to input transform. |
y | The Y coordinate to input transform. |
z | The Z coordinate to input transform. |
x2 | The X coordinate to output transform. |
y2 | The Y coordinate to output transform. |
z2 | The Z coordinate to output transform. |
#define mat_trans_single4 | ( | x, | |
y, | |||
z, | |||
w ) |
Macro to transform a single vertex by the internal matrix.
This macro is an inline assembly operation to transform a single vertex. It works most efficiently if the x value is in fr0, y is in fr1, z is in fr2, and w is in fr3 before using the macro. This macro is similar to mat_trans_single(), but this one allows an input to and preserves the Z/W value.
x | The X coordinate to transform. |
y | The Y coordinate to transform. |
z | The Z coordinate to transform. |
w | The W coordinate to transform. |
#define vec3f_distance | ( | x1, | |
y1, | |||
z1, | |||
x2, | |||
y2, | |||
z2, | |||
w ) |
Macro to return the Euclidean distance between two 3d vectors.
This macro is an inline assembly operation using the SH4's fast (approximate) math instructions, and returns a single-precision floating-point value.
x1 | The X coordinate of first vector. |
y1 | The Y coordinate of first vector. |
z1 | The Z coordinate of first vector. |
x2 | The X coordinate of second vector. |
y2 | The Y coordinate of second vector. |
z2 | The Z coordinate of second vector. |
w | The result of the calculation. |
#define vec3f_dot | ( | x1, | |
y1, | |||
z1, | |||
x2, | |||
y2, | |||
z2, | |||
w ) |
Macro to return the scalar dot product of two 3d vectors.
This macro is an inline assembly operation using the SH4's fast (approximate) math instructions, and returns a single-precision floating-point value.
x1 | The X coordinate of first vector. |
y1 | The Y coordinate of first vector. |
z1 | The Z coordinate of first vector. |
x2 | The X coordinate of second vector. |
y2 | The Y coordinate of second vector. |
z2 | The Z coordinate of second vector. |
w | The result of the calculation. |
#define vec3f_length | ( | x, | |
y, | |||
z, | |||
w ) |
Macro to return scalar Euclidean length of a 3d vector.
This macro is an inline assembly operation using the SH4's fast (approximate) math instructions, and returns a single-precision floating-point value.
x | The X coordinate of vector. |
y | The Y coordinate of vector. |
z | The Z coordinate of vector. |
w | The result of the calculation. |
#define vec3f_normalize | ( | x, | |
y, | |||
z ) |
Macro to return the normalized version of a vector.
This macro is an inline assembly operation using the SH4's fast (approximate) math instructions to calculate a vector that is in the same direction as the input vector but with a Euclidean length of one. The input vector is modified by the operation as the resulting values.
x | The X coordinate of vector. |
y | The Y coordinate of vector. |
z | The Z coordinate of vector. |
#define vec3f_rotd_xy | ( | px, | |
py, | |||
pz, | |||
cx, | |||
cy, | |||
cz, | |||
r ) |
Macro to rotate a vector about its origin on the x, y plane.
This macro is an inline assembly operation using the SH4's fast (approximate) math instructions. The return vector is stored into the first vertex parameter: x1, y1, and z1.
px | The X coordinate of vector to rotate. |
py | The Y coordinate of vector to rotate. |
pz | The Z coordinate of vector to rotate. |
cx | The X coordinate of origin vector. |
cy | The Y coordinate of origin vector. |
cz | The Z coordinate of origin vector. |
r | The angle (in degrees) of rotation. |
#define vec3f_rotd_xz | ( | px, | |
py, | |||
pz, | |||
cx, | |||
cy, | |||
cz, | |||
r ) |
Macro to rotate a vector about its origin on the x, z plane.
This macro is an inline assembly operation using the SH4's fast (approximate) math instructions. The return vector is stored into the first vertex parameter: x1, y1, and z1.
px | The X coordinate of vector to rotate. |
py | The Y coordinate of vector to rotate. |
pz | The Z coordinate of vector to rotate. |
cx | The X coordinate of origin vector. |
cy | The Y coordinate of origin vector. |
cz | The Z coordinate of origin vector. |
r | The angle (in degrees) of rotation. |
#define vec3f_rotd_yz | ( | px, | |
py, | |||
pz, | |||
cx, | |||
cy, | |||
cz, | |||
r ) |
Macro to rotate a vector about its origin on the y, z plane.
This macro is an inline assembly operation using the SH4's fast (approximate) math instructions. The return vector is stored into the first vertex parameter: x1, y1, and z1.
px | The X coordinate of vector to rotate. |
py | The Y coordinate of vector to rotate. |
pz | The Z coordinate of vector to rotate. |
cx | The X coordinate of origin vector. |
cy | The Y coordinate of origin vector. |
cz | The Z coordinate of origin vector. |
r | The angle (in degrees) of rotation. |
#define vec3f_rotr_xy | ( | px, | |
py, | |||
pz, | |||
cx, | |||
cy, | |||
cz, | |||
r ) |
Macro to rotate a vector about its origin on the x, y plane.
This macro is an inline assembly operation using the SH4's fast (approximate) math instructions. The return vector is stored into the first vertex parameter: x1, y1, and z1.
px | The X coordinate of vector to rotate. |
py | The Y coordinate of vector to rotate. |
pz | The Z coordinate of vector to rotate. |
cx | The X coordinate of origin vector. |
cy | The Y coordinate of origin vector. |
cz | The Z coordinate of origin vector. |
r | The angle (in radians) of rotation. |
#define vec3f_rotr_xz | ( | px, | |
py, | |||
pz, | |||
cx, | |||
cy, | |||
cz, | |||
r ) |
Macro to rotate a vector about its origin on the x, z plane.
This macro is an inline assembly operation using the SH4's fast (approximate) math instructions. The return vector is stored into the first vertex parameter: x1, y1, and z1.
px | The X coordinate of vector to rotate. |
py | The Y coordinate of vector to rotate. |
pz | The Z coordinate of vector to rotate. |
cx | The X coordinate of origin vector. |
cy | The Y coordinate of origin vector. |
cz | The Z coordinate of origin vector. |
r | The angle (in radians) of rotation. |
#define vec3f_rotr_yz | ( | px, | |
py, | |||
pz, | |||
cx, | |||
cy, | |||
cz, | |||
r ) |
Macro to rotate a vector about its origin on the y, z plane.
This macro is an inline assembly operation using the SH4's fast (approximate) math instructions. The return vector is stored into the first vertex parameter: x1, y1, and z1.
px | The X coordinate of vector to rotate. |
py | The Y coordinate of vector to rotate. |
pz | The Z coordinate of vector to rotate. |
cx | The X coordinate of origin vector. |
cy | The Y coordinate of origin vector. |
cz | The Z coordinate of origin vector. |
r | The angle (in radians) of rotation. |
#define vec3f_sub_normalize | ( | x1, | |
y1, | |||
z1, | |||
x2, | |||
y2, | |||
z2, | |||
x3, | |||
y3, | |||
z3 ) |
Macro to return the normalized version of a vector minus another vector.
This macro is an inline assembly operation using the SH4's fast (approximate) math instructions. The return vector is stored into the third vertex parameter: x3, y3, and z3.
x1 | The X coordinate of first vector. |
y1 | The Y coordinate of first vector. |
z1 | The Z coordinate of first vector. |
x2 | The X coordinate of second vector. |
y2 | The Y coordinate of second vector. |
z2 | The Z coordinate of second vector. |
x3 | The X coordinate of output vector. |
y3 | The Y coordinate of output vector. |
z3 | The Z coordinate of output vector. |
typedef float matrix_t[4][4] |
Basic 4x4 matrix type.
void mat_apply | ( | const matrix_t * | src | ) |
Apply a matrix.
This function multiplies a matrix in memory onto the internal matrix.
src
MUST be at least 8-byte aligned!src
is recommended.src | A pointer to the matrix to multiply. |
void mat_identity | ( | void | ) |
Clear the internal matrix to identity.
This function clears the internal matrix to a standard identity matrix.
void mat_load | ( | const matrix_t * | src | ) |
Copy a memory matrix into the internal one.
This function loads the internal matrix with the values of one in memory.
out
MUST be at least 8-byte aligned!out
is recommended.src | A pointer to where to load the matrix from (must be at least 8-byte aligned, should be 32-byte aligned). |
Set up a "camera".
This function acts as the similarly named GL function to set up a "camera" by doing rotations/translations.
eye | The eye coordinate. |
center | The center coordinate. |
up | The up vector. |
void mat_perspective | ( | float | xcenter, |
float | ycenter, | ||
float | cot_fovy_2, | ||
float | znear, | ||
float | zfar ) |
Set up a perspective view frustum.
This function sets up a perspective view frustum for basic 3D usage.
xcenter | Center of the X direction. |
ycenter | Center of the Y direction. |
cot_fovy_2 | 1.0 / tan(view_angle / 2). |
znear | Near Z-plane. |
zfar | Far Z-plane. |
void mat_rotate | ( | float | xr, |
float | yr, | ||
float | zr ) |
Rotate around all axes.
This function sets up a rotation matrix around the X-axis, then around the Y, then around the Z.
xr | The angle to rotate around the X-axis, in radians. |
yr | The angle to rotate around the Y-axis, in radians. |
zr | The angle to rotate around the Z-axis, in radians. |
void mat_rotate_x | ( | float | r | ) |
Rotate around the X-axis.
This function sets up a rotation matrix around the X-axis.
r | The angle to rotate, in radians. |
void mat_rotate_y | ( | float | r | ) |
Rotate around the Y-axis.
This function sets up a rotation matrix around the Y-axis.
r | The angle to rotate, in radians. |
void mat_rotate_z | ( | float | r | ) |
Rotate around the Z-axis.
This function sets up a rotation matrix around the Z-axis.
r | The angle to rotate, in radians. |
void mat_scale | ( | float | x, |
float | y, | ||
float | z ) |
Perform a 3D scale operation.
This function sets up a scaling matrix with the specified parameters.
x | The ratio to scale in X. |
y | The ratio to scale in Y. |
z | The ratio to scale in Z. |
void mat_store | ( | matrix_t * | out | ) |
Copy the internal matrix to a memory one.
This function stores the current internal matrix to one in memory.
out
MUST be at least 8-byte aligned!out
is recommended.out | A pointer to where to store the matrix (must be at least 8-byte aligned, should be 32-byte aligned). |
Transform vectors by the internal matrix.
This function transforms zero or more sets of vectors by the current internal matrix. Each vector is 3 single-precision floats long.
invecs | The list of input vectors. |
outvecs | The list of output vectors. |
veccnt | How many vectors are in the list. |
stride | Number of bytes between vectors. |
void mat_transform_sq | ( | void * | input, |
void * | output, | ||
int | veccnt ) |
Transform vectors by the internal matrix into the store queues.
This function transforms one or more sets of vertices using the current internal matrix directly into the store queues. Each vertex is exactly 32-bytes long, and the non-xyz data that is with it will be copied over with the transformed coordinates. This is perfect, for instance, for transforming pvr_vertex_t vertices.
input | The list of input vertices. |
output | The output pointer (SQ address) |
veccnt | The number of vertices to transform. |
void mat_translate | ( | float | x, |
float | y, | ||
float | z ) |
Perform a 3D translation.
This function sets up a translation matrix with the specified parameters.
x | The amount to translate in X. |
y | The amount to translate in Y. |
z | The amount to translate in Z. |