Tiny3D
Loading...
Searching...
No Matches
t3dmath.h File Reference
#include <libdragon.h>
Include dependency graph for t3dmath.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  T3DVec4FP
 
struct  __attribute__
 
struct  T3DFrustum
 

Macros

#define T3D_DEG_TO_RAD(deg)
 
#define T3D_F32_TO_FIXED(val)
 
#define T3D_PI   3.14159265358979f
 

Typedefs

typedef fm_vec3_t T3DVec3
 
typedef fm_vec4_t T3DVec4
 
typedef fm_quat_t T3DQuat
 
typedef fm_mat4_t T3DMat4
 

Functions

static float s1616_to_float (int16_t partI, uint16_t partF)
 Converts a 16.16 fixed-point number to a float.
 
static float t3d_lerp (float a, float b, float t)
 Interpolates between two floats by 't'.
 
static float t3d_lerp_angle (float a, float b, float t)
 Interpolates between two angles (radians) by 't'.
 
static void t3d_vec3_add (T3DVec3 *res, const T3DVec3 *a, const T3DVec3 *b)
 Sets 'res' to 'a + b'.
 
static void t3d_vec3_mul (T3DVec3 *res, const T3DVec3 *a, const T3DVec3 *b)
 Sets 'res' to 'a + b'.
 
static void t3d_vec3_scale (T3DVec3 *res, const T3DVec3 *a, float s)
 Sets 'res' to 'a * s'.
 
static void t3d_vec3_diff (T3DVec3 *res, const T3DVec3 *a, const T3DVec3 *b)
 Set 'res' to 'a - b'.
 
static float t3d_vec3_len2 (const T3DVec3 *vec)
 Returns the squared length of 'v'.
 
static float t3d_vec3_len (const T3DVec3 *vec)
 Returns the length of 'v'.
 
static float t3d_vec3_distance2 (const T3DVec3 *vecA, const T3DVec3 *vecB)
 Returns the squared distance between 'vecA' and 'vecB'.
 
static float t3d_vec3_distance (const T3DVec3 *vecA, const T3DVec3 *vecB)
 Returns the distance between 'vecA' and 'vecB'.
 
static void t3d_vec3_norm (T3DVec3 *res)
 Normalizes 'res', this does NOT check for a zero-length vector.
 
static void t3d_vec3_cross (T3DVec3 *res, const T3DVec3 *a, const T3DVec3 *b)
 Crosses 'a' with 'b' and stores it in 'res'.
 
static float t3d_vec3_dot (const T3DVec3 *a, const T3DVec3 *b)
 Returns the dot product of 'a' and 'b'.
 
static void t3d_vec3_lerp (T3DVec3 *res, const T3DVec3 *a, const T3DVec3 *b, float t)
 Linearly interpolates between 'a' and 'b' by 't' and stores it in 'res'.
 
static void t3d_quat_identity (T3DQuat *quat)
 
static void t3d_quat_from_euler (T3DQuat *quat, const float rotEuler[3])
 
static void t3d_quat_from_rotation (T3DQuat *quat, float axis[3], float angleRad)
 
static void t3d_quat_mul (T3DQuat *res, T3DQuat *a, T3DQuat *b)
 
static void t3d_quat_rotate_euler (T3DQuat *quat, float axis[3], float angleRad)
 
static float t3d_quat_dot (const T3DQuat *a, const T3DQuat *b)
 
static void t3d_quat_normalize (T3DQuat *quat)
 
void t3d_quat_nlerp (T3DQuat *res, const T3DQuat *a, const T3DQuat *b, float t)
 
void t3d_quat_slerp (T3DQuat *res, const T3DQuat *a, const T3DQuat *b, float t)
 
static void t3d_mat4_identity (T3DMat4 *mat)
 Initializes a matrix to the identity matrix.
 
static void t3d_mat4_scale (T3DMat4 *mat, float scaleX, float scaleY, float scaleZ)
 
static void t3d_mat4_translate (T3DMat4 *mat, float offsetX, float offsetY, float offsetZ)
 
void t3d_mat4_rotate (T3DMat4 *mat, const T3DVec3 *axis, float angleRad)
 
void t3d_mat4_from_srt (T3DMat4 *mat, const float scale[3], const float quat[4], const float translate[3])
 
void t3d_mat4_from_srt_euler (T3DMat4 *mat, const float scale[3], const float rot[3], const float translate[3])
 
void t3d_mat4_rot_from_dir (T3DMat4 *mat, const T3DVec3 *dir, const T3DVec3 *up)
 
void t3d_mat4fp_from_srt_euler (T3DMat4FP *mat, const float scale[3], const float rot[3], const float translate[3])
 
void t3d_mat4fp_from_srt (T3DMat4FP *mat, const float scale[3], const float rotQuat[4], const float translate[3])
 
static void t3d_mat4fp_set_float (T3DMat4FP *mat, uint32_t column, uint32_t row, float val)
 Sets a value in a fixed-point matrix.
 
static void t3d_mat4fp_set_pos (T3DMat4FP *mat, const float pos[3])
 
static float t3d_mat4fp_get_float (const T3DMat4FP *mat, uint32_t y, uint32_t x)
 Gets a value from a fixed-point matrix.
 
static void t3d_mat4fp_identity (T3DMat4FP *mat)
 
void t3d_mat4_to_fixed (T3DMat4FP *matOut, const T3DMat4 *matIn)
 
void t3d_mat4_to_fixed_3x4 (T3DMat4FP *matOut, const T3DMat4 *matIn)
 
void t3d_mat4_perspective (T3DMat4 *mat, float fov, float aspect, float near, float far)
 
void t3d_mat4_ortho (T3DMat4 *mat, float left, float right, float bottom, float top, float near, float far)
 
void t3d_mat4_look_at (T3DMat4 *mat, const T3DVec3 *eye, const T3DVec3 *target, const T3DVec3 *up)
 Creates a look-at matrix from an eye and target vector.
 
void t3d_mat4_to_frustum (T3DFrustum *frustum, const T3DMat4 *mat)
 
void t3d_frustum_scale (T3DFrustum *frustum, float scale)
 
bool t3d_frustum_vs_aabb (const T3DFrustum *frustum, const T3DVec3 *min, const T3DVec3 *max)
 
bool t3d_frustum_vs_aabb_s16 (const T3DFrustum *frustum, const int16_t min[3], const int16_t max[3])
 
bool t3d_frustum_vs_sphere (const T3DFrustum *frustum, const T3DVec3 *center, const float radius)
 
static void t3d_mat4_mul (T3DMat4 *matRes, const T3DMat4 *matA, const T3DMat4 *matB)
 Multiplies the matrices 'matA' and 'matB' and stores it in 'matRes'.
 
static void t3d_mat3_mul_vec3 (T3DVec3 *vecOut, const T3DMat4 *mat, const T3DVec3 *vec)
 
static void t3d_mat4_mul_vec3 (T3DVec4 *vecOut, const T3DMat4 *mat, const T3DVec3 *vec)
 

Detailed Description

Macro Definition Documentation

◆ T3D_DEG_TO_RAD

#define T3D_DEG_TO_RAD ( deg)
Value:
(deg * 0.01745329252f)

◆ T3D_F32_TO_FIXED

#define T3D_F32_TO_FIXED ( val)
Value:
(int32_t)((val) * (float)(1<<16))

Function Documentation

◆ t3d_frustum_scale()

void t3d_frustum_scale ( T3DFrustum * frustum,
float scale )

Scales a frustum by a given factor. This can be used if you need to check scaled objects without having to transform the AABBs

Parameters
frustumfrustum to scale
scalescale factor (use the same as the model scale)

◆ t3d_frustum_vs_aabb()

bool t3d_frustum_vs_aabb ( const T3DFrustum * frustum,
const T3DVec3 * min,
const T3DVec3 * max )

Checks if an AABB is inside a frustum. Note that this function may choose to return false positives in favor of speed. So it should only be used where this is acceptable (e.g. culling) and not for collision detection.

Parameters
frustumfrustum
minAABB min
maxAABB max
Returns
true if the AABB is inside the frustum

◆ t3d_frustum_vs_aabb_s16()

bool t3d_frustum_vs_aabb_s16 ( const T3DFrustum * frustum,
const int16_t min[3],
const int16_t max[3] )

Checks if an s16 AABB is inside a frustum. Note that this function may choose to return false positives in favor of speed. So it should only be used where this is acceptable (e.g. culling) and not for collision detection.

Parameters
frustumfrustum
minAABB min
maxAABB max
Returns
true if the AABB is inside the frustum

◆ t3d_frustum_vs_sphere()

bool t3d_frustum_vs_sphere ( const T3DFrustum * frustum,
const T3DVec3 * center,
const float radius )

Checks if a Sphere is inside a frustum. Note that this function may choose to return false positives in favor of speed. So it should only be used where this is acceptable (e.g. culling) and not for collision detection.

Parameters
frustumfrustum
centerSphere center
radiusSphere radius
Returns
true if the Sphere is inside the frustum

◆ t3d_mat3_mul_vec3()

static void t3d_mat3_mul_vec3 ( T3DVec3 * vecOut,
const T3DMat4 * mat,
const T3DVec3 * vec )
inlinestatic

Multiplies a 3x3 matrix with a 3D vector

Parameters
vecOutresult
matmatrix
vecinput-vector

◆ t3d_mat4_from_srt()

void t3d_mat4_from_srt ( T3DMat4 * mat,
const float scale[3],
const float quat[4],
const float translate[3] )

Directly constructs a matrix from scale, rotation (quaternion) and translation For a euler version, see 't3d_mat4_from_srt_euler'.

Parameters
matresult
scalescale factors
rotrotation quaternion
translateoffsets

◆ t3d_mat4_from_srt_euler()

void t3d_mat4_from_srt_euler ( T3DMat4 * mat,
const float scale[3],
const float rot[3],
const float translate[3] )

Directly constructs a matrix from scale, rotation (euler) and translation For a quaternion version, see 't3d_mat4_from_srt'.

Parameters
mat
scale
rot
translate

◆ t3d_mat4_identity()

static void t3d_mat4_identity ( T3DMat4 * mat)
inlinestatic

Initializes a matrix to the identity matrix.

Parameters
matmatrix to be changed

◆ t3d_mat4_look_at()

void t3d_mat4_look_at ( T3DMat4 * mat,
const T3DVec3 * eye,
const T3DVec3 * target,
const T3DVec3 * up )

Creates a look-at matrix from an eye and target vector.

Parameters
matdestination matrix
eyecamera position
targetcamera target/focus point
upcamera up vector, expected to be {0,1,0} by default

◆ t3d_mat4_mul_vec3()

static void t3d_mat4_mul_vec3 ( T3DVec4 * vecOut,
const T3DMat4 * mat,
const T3DVec3 * vec )
inlinestatic

Multiplies a 4x4 matrix with a 3D vector. The W component of the vector is assumed to be 1.

Parameters
vecOutresult
matmatrix
vecinput-vector

◆ t3d_mat4_ortho()

void t3d_mat4_ortho ( T3DMat4 * mat,
float left,
float right,
float bottom,
float top,
float near,
float far )

Constructs an orthographic projection matrix

Parameters
matresult
left
right
bottom
top
nearnear plane distance
farfar plane distance

◆ t3d_mat4_perspective()

void t3d_mat4_perspective ( T3DMat4 * mat,
float fov,
float aspect,
float near,
float far )

Constructs a perspective projection matrix

Parameters
matresult
fovfov in radians
aspectaspect ratio
nearnear plane distance
farfar plane distance

◆ t3d_mat4_rot_from_dir()

void t3d_mat4_rot_from_dir ( T3DMat4 * mat,
const T3DVec3 * dir,
const T3DVec3 * up )

Constructs a matrix from a direction and up vector. This will only create a rotation matrix, the translation part will be identity.

Parameters
matresult
dirdirection vector
upup vector

◆ t3d_mat4_rotate()

void t3d_mat4_rotate ( T3DMat4 * mat,
const T3DVec3 * axis,
float angleRad )

Rotates a matrix around an axis

Parameters
matresult
axisaxis to rotate around
angleRadangle in radians

◆ t3d_mat4_scale()

static void t3d_mat4_scale ( T3DMat4 * mat,
float scaleX,
float scaleY,
float scaleZ )
inlinestatic

Scales a matrix by the given factors

Parameters
matresult
scaleX
scaleY
scaleZ

◆ t3d_mat4_to_fixed()

void t3d_mat4_to_fixed ( T3DMat4FP * matOut,
const T3DMat4 * matIn )

Converts a float matrix to a fixed-point matrix.

Parameters
matOutresult
matIninput

◆ t3d_mat4_to_fixed_3x4()

void t3d_mat4_to_fixed_3x4 ( T3DMat4FP * matOut,
const T3DMat4 * matIn )

Converts a float 4x4 matrix to a fixed-point 4x4 matrix. The last row of the matrix is assumed to be {0,0,0,1}. This should be preferred over 't3d_mat4_to_fixed' when possible as it is faster.

Parameters
matOutresult
matIninput

◆ t3d_mat4_to_frustum()

void t3d_mat4_to_frustum ( T3DFrustum * frustum,
const T3DMat4 * mat )

Extracts the frustum planes from a 4x4 view/camera matrix

Parameters
frustumresult
matview/camera matrix

◆ t3d_mat4_translate()

static void t3d_mat4_translate ( T3DMat4 * mat,
float offsetX,
float offsetY,
float offsetZ )
inlinestatic

Translates a matrix by the given offsets

Parameters
matresult
offsetX
offsetY
offsetZ

◆ t3d_mat4fp_from_srt()

void t3d_mat4fp_from_srt ( T3DMat4FP * mat,
const float scale[3],
const float rotQuat[4],
const float translate[3] )

Directly constructs a matrix from scale, rotation (quaternion) and translation Same as 't3d_mat4_from_srt', but instead directly writes to a fixed-point matrix.

Parameters
mat
scale
rot
translate

◆ t3d_mat4fp_from_srt_euler()

void t3d_mat4fp_from_srt_euler ( T3DMat4FP * mat,
const float scale[3],
const float rot[3],
const float translate[3] )

Directly constructs a matrix from scale, rotation (euler) and translation Same as 't3d_mat4_from_srt_euler', but instead directly writes to a fixed-point matrix.

Parameters
mat
scale
rot
translate

◆ t3d_mat4fp_get_float()

static float t3d_mat4fp_get_float ( const T3DMat4FP * mat,
uint32_t y,
uint32_t x )
inlinestatic

Gets a value from a fixed-point matrix.

Parameters
matmatrix to be read
yrow
xcolumn
Returns
value as a float

◆ t3d_mat4fp_set_float()

static void t3d_mat4fp_set_float ( T3DMat4FP * mat,
uint32_t column,
uint32_t row,
float val )
inlinestatic

Sets a value in a fixed-point matrix.

Parameters
matmatrix to be changed
column
row
valvalue to be set as a float

◆ t3d_mat4fp_set_pos()

static void t3d_mat4fp_set_pos ( T3DMat4FP * mat,
const float pos[3] )
inlinestatic

Sets the position of a fixed-point matrix. Note: that this will just set the values without any further checks/calculations.

Parameters
matmatrix to be changed
posposition as a float[3]

◆ t3d_quat_dot()

static float t3d_quat_dot ( const T3DQuat * a,
const T3DQuat * b )
inlinestatic

Dot product of a quaternion as a vec4

Parameters
aquaternion
bquaternion
Returns
dot product

◆ t3d_quat_from_euler()

static void t3d_quat_from_euler ( T3DQuat * quat,
const float rotEuler[3] )
inlinestatic

Creates a quaternion from euler angles (radians)

Parameters
quatresult (in XYZW order)
rotEuler

◆ t3d_quat_from_rotation()

static void t3d_quat_from_rotation ( T3DQuat * quat,
float axis[3],
float angleRad )
inlinestatic

Creates a quaternion from a rotation (radians) around an axis

Parameters
quatresult
axisrotation axis
angleRadangle in radians

◆ t3d_quat_mul()

static void t3d_quat_mul ( T3DQuat * res,
T3DQuat * a,
T3DQuat * b )
inlinestatic

Multiplies two quaternions and stores the result in 'res'. NOTE: if 'a' or 'b' point to the same quat. as 'res', the result will be incorrect.

Parameters
resresult
afirst quaternion
bsecond quaternion

◆ t3d_quat_nlerp()

void t3d_quat_nlerp ( T3DQuat * res,
const T3DQuat * a,
const T3DQuat * b,
float t )

Interpolates between two quaternions using a normalized linear interpolation.

Parameters
resinterpolated quaternion
afirst quaternion
bsecond quaternion
tinterpolation factor

◆ t3d_quat_normalize()

static void t3d_quat_normalize ( T3DQuat * quat)
inlinestatic

Normalizes a quaternion

Parameters
quat

◆ t3d_quat_rotate_euler()

static void t3d_quat_rotate_euler ( T3DQuat * quat,
float axis[3],
float angleRad )
inlinestatic

Rotates a quaternion around an axis

Parameters
quatquat to modify
axisrotation axis
angleRadangle in radians