citro3d
Macros
maths.h File Reference

Basic math library for matrix, vector, and quaternion operations. More...

#include "types.h"
#include <math.h>
#include <string.h>

Go to the source code of this file.

Macros

#define M_TAU   (6.28318530717958647692528676655900576)
 
#define M_PI   (M_TAU/2)
 
#define C3D_Angle(_angle)   ((_angle)*M_TAU)
 Convert an angle from revolutions to radians. More...
 
#define C3D_AngleFromDegrees(_angle)   ((_angle)*M_TAU/360.0f)
 Convert an angle from degrees to radians. More...
 
#define C3D_AspectRatioTop   (400.0f / 240.0f)
 Aspect ratio for 3DS top screen.
 
#define C3D_AspectRatioBot   (320.0f / 240.0f)
 Aspect ratio for 3DS bottom screen.
 

Functions

Vector Math
static C3D_FVec FVec4_New (float x, float y, float z, float w)
 Create a new FVec4. More...
 
static C3D_FVec FVec4_Add (C3D_FVec lhs, C3D_FVec rhs)
 Add two FVec4s. More...
 
static C3D_FVec FVec4_Subtract (C3D_FVec lhs, C3D_FVec rhs)
 Subtract two FVec4s. More...
 
static C3D_FVec FVec4_Negate (C3D_FVec v)
 Negate a FVec4. More...
 
static C3D_FVec FVec4_Scale (C3D_FVec v, float s)
 Scale a FVec4. More...
 
static C3D_FVec FVec4_PerspDivide (C3D_FVec v)
 Perspective divide. More...
 
static float FVec4_Dot (C3D_FVec lhs, C3D_FVec rhs)
 Dot product of two FVec4s. More...
 
static float FVec4_Magnitude (C3D_FVec v)
 Magnitude of a FVec4. More...
 
static C3D_FVec FVec4_Normalize (C3D_FVec v)
 Normalize a FVec4. More...
 
static C3D_FVec FVec3_New (float x, float y, float z)
 Create a new FVec3. More...
 
static float FVec3_Dot (C3D_FVec lhs, C3D_FVec rhs)
 Dot product of two FVec3s. More...
 
static float FVec3_Magnitude (C3D_FVec v)
 Magnitude of a FVec3. More...
 
static C3D_FVec FVec3_Normalize (C3D_FVec v)
 Normalize a FVec3. More...
 
static C3D_FVec FVec3_Add (C3D_FVec lhs, C3D_FVec rhs)
 Add two FVec3s. More...
 
static C3D_FVec FVec3_Subtract (C3D_FVec lhs, C3D_FVec rhs)
 Subtract two FVec3s. More...
 
static float FVec3_Distance (C3D_FVec lhs, C3D_FVec rhs)
 Distance between two 3D points. More...
 
static C3D_FVec FVec3_Scale (C3D_FVec v, float s)
 Scale a FVec3. More...
 
static C3D_FVec FVec3_Negate (C3D_FVec v)
 Negate a FVec3. More...
 
static C3D_FVec FVec3_Cross (C3D_FVec lhs, C3D_FVec rhs)
 Cross product of two FVec3s. More...
 
Matrix Math
Note
All matrices are 4x4 unless otherwise noted.
static void Mtx_Zeros (C3D_Mtx *out)
 Zero matrix. More...
 
static void Mtx_Copy (C3D_Mtx *out, const C3D_Mtx *in)
 Copy a matrix. More...
 
static void Mtx_Diagonal (C3D_Mtx *out, float x, float y, float z, float w)
 Creates a matrix with the diagonal using the given parameters. More...
 
static void Mtx_Identity (C3D_Mtx *out)
 Identity matrix. More...
 
void Mtx_Transpose (C3D_Mtx *out)
 Transposes the matrix. Row => Column, and vice versa. More...
 
static void Mtx_Add (C3D_Mtx *out, const C3D_Mtx *lhs, const C3D_Mtx *rhs)
 Matrix addition. More...
 
static void Mtx_Subtract (C3D_Mtx *out, const C3D_Mtx *lhs, const C3D_Mtx *rhs)
 Matrix subtraction. More...
 
void Mtx_Multiply (C3D_Mtx *out, const C3D_Mtx *a, const C3D_Mtx *b)
 Multiply two matrices. More...
 
float Mtx_Inverse (C3D_Mtx *out)
 Inverse a matrix. More...
 
C3D_FVec Mtx_MultiplyFVec3 (const C3D_Mtx *mtx, C3D_FVec v)
 Multiply 3x3 matrix by a FVec3. More...
 
C3D_FVec Mtx_MultiplyFVec4 (const C3D_Mtx *mtx, C3D_FVec v)
 Multiply 4x4 matrix by a FVec4. More...
 
static C3D_FVec Mtx_MultiplyFVecH (const C3D_Mtx *mtx, C3D_FVec v)
 Multiply 4x3 matrix by a FVec3. More...
 
void Mtx_FromQuat (C3D_Mtx *m, C3D_FQuat q)
 Get 4x4 matrix equivalent to Quaternion. More...
 
3D Transformation Matrix Math
Note
bRightSide is used to determine which side to perform the transformation. With an input matrix A and a transformation matrix B, bRightSide being true yields AB, while being false yield BA.
void Mtx_Translate (C3D_Mtx *mtx, float x, float y, float z, bool bRightSide)
 3D translation More...
 
void Mtx_Scale (C3D_Mtx *mtx, float x, float y, float z)
 3D Scale More...
 
void Mtx_Rotate (C3D_Mtx *mtx, C3D_FVec axis, float angle, bool bRightSide)
 3D Rotation More...
 
void Mtx_RotateX (C3D_Mtx *mtx, float angle, bool bRightSide)
 3D Rotation about the X axis More...
 
void Mtx_RotateY (C3D_Mtx *mtx, float angle, bool bRightSide)
 3D Rotation about the Y axis More...
 
void Mtx_RotateZ (C3D_Mtx *mtx, float angle, bool bRightSide)
 3D Rotation about the Z axis More...
 
3D Projection Matrix Math
void Mtx_Ortho (C3D_Mtx *mtx, float left, float right, float bottom, float top, float near, float far, bool isLeftHanded)
 Orthogonal projection. More...
 
void Mtx_Persp (C3D_Mtx *mtx, float fovy, float aspect, float near, float far, bool isLeftHanded)
 Perspective projection. More...
 
void Mtx_PerspStereo (C3D_Mtx *mtx, float fovy, float aspect, float near, float far, float iod, float screen, bool isLeftHanded)
 Stereo perspective projection. More...
 
void Mtx_OrthoTilt (C3D_Mtx *mtx, float left, float right, float bottom, float top, float near, float far, bool isLeftHanded)
 Orthogonal projection, tilted to account for the 3DS screen rotation. More...
 
void Mtx_PerspTilt (C3D_Mtx *mtx, float fovy, float aspect, float near, float far, bool isLeftHanded)
 Perspective projection, tilted to account for the 3DS screen rotation. More...
 
void Mtx_PerspStereoTilt (C3D_Mtx *mtx, float fovy, float aspect, float near, float far, float iod, float screen, bool isLeftHanded)
 Stereo perspective projection, tilted to account for the 3DS screen rotation. More...
 
void Mtx_LookAt (C3D_Mtx *out, C3D_FVec cameraPosition, C3D_FVec cameraTarget, C3D_FVec cameraUpVector, bool isLeftHanded)
 Look-At matrix, based on DirectX implementation. More...
 

Quaternion Math

#define Quat_New(i, j, k, r)   FVec4_New(i,j,k,r)
 Create a new Quaternion. More...
 
#define Quat_Negate(q)   FVec4_Negate(q)
 Negate a Quaternion. More...
 
#define Quat_Add(lhs, rhs)   FVec4_Add(lhs,rhs)
 Add two Quaternions. More...
 
#define Quat_Subtract(lhs, rhs)   FVec4_Subtract(lhs,rhs)
 Subtract two Quaternions. More...
 
#define Quat_Scale(q, s)   FVec4_Scale(q,s)
 Scale a Quaternion. More...
 
#define Quat_Normalize(q)   FVec4_Normalize(q)
 Normalize a Quaternion. More...
 
#define Quat_Dot(lhs, rhs)   FVec4_Dot(lhs,rhs)
 Dot product of two Quaternions. More...
 
C3D_FQuat Quat_Multiply (C3D_FQuat lhs, C3D_FQuat rhs)
 Multiply two Quaternions. More...
 
C3D_FQuat Quat_Pow (C3D_FQuat q, float p)
 Raise Quaternion to a power. More...
 
C3D_FVec Quat_CrossFVec3 (C3D_FQuat q, C3D_FVec v)
 Cross product of Quaternion and FVec3. More...
 
C3D_FQuat Quat_Rotate (C3D_FQuat q, C3D_FVec axis, float r, bool bRightSide)
 3D Rotation More...
 
C3D_FQuat Quat_RotateX (C3D_FQuat q, float r, bool bRightSide)
 3D Rotation about the X axis More...
 
C3D_FQuat Quat_RotateY (C3D_FQuat q, float r, bool bRightSide)
 3D Rotation about the Y axis More...
 
C3D_FQuat Quat_RotateZ (C3D_FQuat q, float r, bool bRightSide)
 3D Rotation about the Z axis More...
 
C3D_FQuat Quat_FromMtx (const C3D_Mtx *m)
 Get Quaternion equivalent to 4x4 matrix. More...
 
static C3D_FQuat Quat_Identity (void)
 Identity Quaternion. More...
 
static C3D_FQuat Quat_Conjugate (C3D_FQuat q)
 Quaternion conjugate. More...
 
static C3D_FQuat Quat_Inverse (C3D_FQuat q)
 Quaternion inverse. More...
 
static C3D_FVec FVec3_CrossQuat (C3D_FVec v, C3D_FQuat q)
 Cross product of FVec3 and Quaternion. More...
 
C3D_FQuat Quat_FromPitchYawRoll (float pitch, float yaw, float roll, bool bRightSide)
 Converting Pitch, Yaw, and Roll to Quaternion equivalent. More...
 
C3D_FQuat Quat_LookAt (C3D_FVec source, C3D_FVec target, C3D_FVec forwardVector, C3D_FVec upVector)
 Quaternion Look-At. More...
 
C3D_FQuat Quat_FromAxisAngle (C3D_FVec axis, float angle)
 Quaternion, created from a given axis and angle in radians. More...
 

Detailed Description

Basic math library for matrix, vector, and quaternion operations.