| Home | Rumble | Youtube | Twitter/X | Kofi | Contact / Crypto |
|---|
|
Matrixes in c3dlas only come in single-precision floats. Yes, "matrixes". English has some dumb historical irregularities and it behooves
us to right them. There are structs for 2x2, 3x3, 4x4, 4x3, and 3x4 matrixes but only 4x4 has a full set of functions as the others only have
niche uses in 3D games.
This documentation is intentionally written for an audience with minimal mathematical knowledge. Matrixes can be used to represent transformations on vectors such as translation (movement), rotation, skewing, and scaling. These operations can be combined together through matrix multiplication, allowing you to apply a great number of operations all at once to a vector instead of having to do them all sequentially.
Matrixes are stored in column major order, which is to say that the first four floats in memory of a 4x4 matrix correspond to the
values of the first (leftmost) column when written down, from top to bottom. The next four elements are the second row, top to bottom, and so forth.
The type is a union consisting of a flat array of floats called
The long struct names, to avoid clobbering other libraries, have the form
vec3 vMatrixMul3(vec3 in, mat4* m)
void vMatrixMul3p(vec3* in, mat4* m, vec3* out)
void vMatrixMulf3p(float x, float y, float z, mat* m, vec3* out)
vec4 vMatrixMul4(vec4 in, mat4* m) Multiplies the input vector by the matrix, applying the transformations to it.
vec3 vMatrixMulProjectedMagic3(vec3 in, mat* m) Multiplies the input vector by the matrix, but has some extra logic to give the expected locations "behind" a perspective camera.
void mIdent(mat4* m) Sets m to the identity matrix, which is a matrix that makes no changes to a vector.
void mMul(mat4* l, mat4* r, mat4* out) Multiplies l by r and stores the result in out. out cannot alias (point to) l or r as temporary results are
accumulated there. Use mMul instead.
void mMul(mat4* a, mat4* out) Multiplies a into out, using temporary memory to prevent clobbering the original values of out before the multiplication is done.
void mBlend(mat4* in, float t, mat4* out) Multiplies every element of in by t then adds the result into out
void mPrint(mat4* m, void* arg) Prints the matrix in a pretty, square format using the function set by the macro C3DLAS_fprintf, using arg as the first argument (file handle).
By default this is fprintf.
Transformationsvoid mTransv(vec3* v, mat4* out)
void mTrans3f(float x, float y, float z, mat4* out) Applies a translation to the matrix in out which moves (adds) any applied vector by in. This moves a mesh or vector around in space.
These functions accumulates the operation into out..
void mScalev(vec3* v, mat4* out)
void mScale3f(float x, float y, float z, mat4* out) Applies scaling to the matrix in out which scales (multiplies) any applied vector by in. This makes a mesh bigger or smaller. Asymmetric
scaling factors (where the values of each dimension are not equal) may create problems in some algorithms that cause more costly operations
to be needed. These functions accumulates the operation into out..
void mRotv(vec3* v, float theta, mat4* out)
void mRot3f(float x, float y, float z, float theta, mat4* out)
void mRotX(float theta, mat4* out)
void mRotY(float theta, mat4* out)
void mRotZ(float theta, mat4* out) Applies a rotation to the matrix in out which rotates (does a bunch of trig on) any applied vector by in. This rotates a mesh or vector
about the origin in space. Note that the order of applied rotations matters. These functions accumulates the operation into out..
void mTranspose(mat4* in, mat4* out) Transposes in to temporary memory then copies it to out. In and out can point to the same memory. For affine matrixes, the transpose is equivalent
to the inverse but much faster to calculate.
.
void mTransposeFast(mat4* in, mat4* out) Transposes in and copies it directly to out. In and out must not point to the same memory. For affine matrixes, the transpose is equivalent
to the inverse but much faster to calculate..
void mRotationOnly(mat4* in, mat4* out) Extracts just the rotational component of in. Used for rotating normals without scaling or translating them..
void mDecompose(mat4* mat, vec3* trans, quat* rot, vec3* scale) Extracts the individual translation, rotation (as a quaternion), and scaling components from a matrix that has only those components.
void mRecompose(vec3* trans, quat* rot, vec3* scale, mat4* out) Reconstructs a matrix containing the translation, rotation (as a quaternion), and scale, applied in the correct order.
Camera UtilitiesThese functions perform the necessary series of transformations to orient the camera in the given way without you having to work out what all those operations are and what order to do them in.void mLookAt(vec3 eye, vec3 center, vec3 up, mat4* out) Points the camera at another point. Analgous to gluLookAt(). Eye is the eye position, center is the point being looked at,
and up is the direction of up from
the camera&$39;s point of view. Up is not required to be orthogonal to anything, so long as it's not parallel to anything.
void mLookDir(vec3 eye, vec3 dir, vec3 up, mat4* out) Points the camera in a certain direction. Eye is the eye position, dir is the direciton to look, and up is the direction of up from
the camera&$39;s point of view. Up is not required to be orthogonal to anything, so long as it's not parallel to anything.
Projection Matrix UtilitiesThese functions are used to create projection matrixes for 3D rendering. A projection matrix transforms a point in view space to normalized device coordinates, the specification of which varies depending on your rendering system. As such, there are several versions of some functions corresponding to OpenGL (GL), Vulkan (VK), reverse depth buffers (RDepth), and "Z-up" world spaces (ZUp).
Math Operationsfloat mDeterminate(mat4* m) Calculates the determinate of the matrix, which is useful in some other mathematical operations. The determinate has no exact physical
meaning for an arbitrary matrix but it does represent the "degree" or "magnitude" of the complete transformation inside the matrix. For a
pure translation matrix, the determinate is the distance of the transformation. For a symmetric pure scaling matrix, it's the
amount of scaling..
int mInverse(mat4* in, mat4* out) Calculates the inverse of the matrix, which does the reverse operation. Multiplying a matrix with its inverse results in the identity matrix..
float mTrace(mat4* m) Returns the trace of the matrix, which is the sum of the diagonal elements..
void mAdd(mat4* a, mat4* b, mat4* out) Simple element-wise addition of a and b into out. This is an uncommon operation with no general physical meaning..
void mScalarMul(mat4* a, float f, mat4* out) Multiply every element of a by f and store into out. This is an uncommon operation with no general physical meaning..
Functions In Other DimensionsThere are sporadic functions on the other sizes of matrixes. One day I'll fill them all in. Check the source in the meantime.
|
|||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||