GL3: Update HandMadeMath.h

This commit is contained in:
Daniel Gibson 2017-04-02 19:18:40 +02:00
parent ca5dccb881
commit dbf0563565

View file

@ -1,5 +1,5 @@
/*
HandmadeMath.h v1.0
HandmadeMath.h v1.1.2
This is a single header file with a bunch of useful functions for
basic game math operations.
@ -79,8 +79,11 @@
#define HMM_TANF MyTanF
#define HMM_EXPF MyExpF
#define HMM_LOGF MyLogF
#define HMM_ACOSF MyACosF
#define HMM_ATANF MyATanF
#define HMM_ATAN2F MYATan2F
Provide your own implementations of SinF, CosF, TanF, ExpF and LogF
Provide your own implementations of SinF, CosF, TanF, ACosF, ATanF, ATan2F, ExpF and LogF
in EXACTLY one C or C++ file that includes this header, BEFORE the
include, like this:
@ -89,6 +92,9 @@
#define HMM_TANF MyTanF
#define HMM_EXPF MyExpF
#define HMM_LOGF MyLogF
#define HMM_ACOSF MyACosF
#define HMM_ATANF MyATanF
#define HMM_ATAN2F MyATan2F
#define HANDMADE_MATH_IMPLEMENTATION
#define HANDMADE_MATH_CPP_MODE
#include "HandmadeMath.h"
@ -147,6 +153,26 @@
(*) Added HMM_NormalizeVec4
1.0
(*) Lots of testing!
1.1
(*) Quaternion support
(*) Added type hmm_quaternion
(*) Added HMM_Quaternion
(*) Added HMM_QuaternionV4
(*) Added HMM_AddQuaternion
(*) Added HMM_SubtractQuaternion
(*) Added HMM_MultiplyQuaternion
(*) Added HMM_MultiplyQuaternionF
(*) Added HMM_DivideQuaternionF
(*) Added HMM_InverseQuaternion
(*) Added HMM_DotQuaternion
(*) Added HMM_NormalizeQuaternion
(*) Added HMM_Slerp
(*) Added HMM_QuaternionToMat4
(*) Added HMM_QuaternionFromAxisAngle
1.1.1
(*) Resolved compiler warnings on gcc and g++
1.1.2
(*) Fixed invalid HMMDEF's in the function definitions
LICENSE
@ -164,6 +190,7 @@
FieryDrake (@fierydrake)
Gingerbill (@TheGingerBill)
Ben Visness (@bvisness)
Trinton Bullard (@Peliex_Dev)
Fixes:
Jeroen van Rijn (@J_vanRijn)
@ -171,7 +198,7 @@
Insofaras (@insofaras)
*/
#ifndef HANDMADE_MATH_NO_SSE
#ifndef HANDMADE_NO_SSE
#include <xmmintrin.h>
#endif
@ -182,8 +209,8 @@
#pragma warning(disable:4201)
#endif
//#ifdef __GNUC__
#ifdef __clang__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wgnu-anonymous-struct"
#endif
@ -192,7 +219,7 @@ extern "C"
{
#endif
#ifdef HANDMADEMATH_STATIC
#ifdef HANDMADE_MATH_STATIC
#define HMMDEF static
#else
#define HMMDEF extern
@ -207,7 +234,8 @@ extern "C"
#endif
#if !defined(HMM_SINF) || !defined(HMM_COSF) || !defined(HMM_TANF) || \
!defined(HMM_EXPF) || !defined(HMM_LOGF)
!defined(HMM_EXPF) || !defined(HMM_LOGF) || !defined(HMM_ACOSF) || \
!defined(HMM_ATANF)|| !defined(HMM_ATAN2F)
#include <math.h>
#endif
@ -231,12 +259,24 @@ extern "C"
#define HMM_LOGF logf
#endif
#ifndef HMM_ACOSF
#define HMM_ACOSF acosf
#endif
#ifndef HMM_ATANF
#define HMM_ATANF atanf
#endif
#ifndef HMM_ATAN2F
#define HMM_ATAN2F atan2f
#endif
#define HMM_PI32 3.14159265359f
#define HMM_PI 3.14159265358979323846
#define HMM_MIN(a, b) (a) > (b) ? (b) : (a)
#define HMM_MAX(a, b) (a) < (b) ? (b) : (a)
#define HMN_ABS(a) (a) < 0 ? -(a) : (a)
#define HMM_ABS(a) ((a) > 0 ? (a) : -(a))
#define HMM_MOD(a, m) ((a) % (m)) >= 0 ? ((a) % (m)) : (((a) % (m)) + (m))
#define HMM_SQUARE(x) ((x) * (x))
@ -362,6 +402,25 @@ typedef union hmm_mat4
float Elements[4][4];
} hmm_mat4;
typedef union hmm_quaternion
{
struct
{
union
{
hmm_vec3 XYZ;
struct
{
float X, Y, Z;
};
};
float W;
};
float Elements[4];
} hmm_quaternion;
typedef hmm_vec2 hmm_v2;
typedef hmm_vec3 hmm_v3;
typedef hmm_vec4 hmm_v4;
@ -369,7 +428,10 @@ typedef hmm_mat4 hmm_m4;
HMMDEF float HMM_SinF(float Angle);
HMMDEF float HMM_TanF(float Angle);
HMMDEF float HMM_ATanF(float Theta);
HMMDEF float HMM_ATan2F(float Theta, float Theta2);
HMMDEF float HMM_CosF(float Angle);
HMMDEF float HMM_ACosF(float Theta);
HMMDEF float HMM_ExpF(float Float);
HMMDEF float HMM_LogF(float Float);
@ -400,8 +462,8 @@ HMMDEF float HMM_DotVec4(hmm_vec4 VecOne, hmm_vec4 VecTwo);
HMMDEF hmm_vec3 HMM_Cross(hmm_vec3 VecOne, hmm_vec3 VecTwo);
HMMDEF hmm_vec2 HMM_Vec2i(int X, int Y);
HMMDEF hmm_vec2 HMM_Vec2(float X, float Y);
HMMDEF hmm_vec2 HMM_Vec2i(int X, int Y);
HMMDEF hmm_vec3 HMM_Vec3(float X, float Y, float Z);
HMMDEF hmm_vec3 HMM_Vec3i(int X, int Y, int Z);
HMMDEF hmm_vec4 HMM_Vec4(float X, float Y, float Z, float W);
@ -450,6 +512,21 @@ HMMDEF hmm_mat4 HMM_Scale(hmm_vec3 Scale);
HMMDEF hmm_mat4 HMM_LookAt(hmm_vec3 Eye, hmm_vec3 Center, hmm_vec3 Up);
HMMDEF hmm_quaternion HMM_Quaternion(float X, float Y, float Z, float W);
HMMDEF hmm_quaternion HMM_QuaternionV4(hmm_vec4 Vector);
HMMDEF hmm_quaternion HMM_AddQuaternion(hmm_quaternion Left, hmm_quaternion Right);
HMMDEF hmm_quaternion HMM_SubtractQuaternion(hmm_quaternion Left, hmm_quaternion Right);
HMMDEF hmm_quaternion HMM_MultiplyQuaternion(hmm_quaternion Left, hmm_quaternion Right);
HMMDEF hmm_quaternion HMM_MultiplyQuaternionF(hmm_quaternion Left, float Multiplicative);
HMMDEF hmm_quaternion HMM_DivideQuaternionF(hmm_quaternion Left, float Dividend);
HMMDEF hmm_quaternion HMM_InverseQuaternion(hmm_quaternion Left);
HMMDEF float HMM_DotQuaternion(hmm_quaternion Left, hmm_quaternion Right);
HMMDEF hmm_quaternion HMM_NormalizeQuaternion(hmm_quaternion Left);
HMMDEF hmm_quaternion HMM_NLerp(hmm_quaternion Left, float Time, hmm_quaternion Right);
HMMDEF hmm_quaternion HMM_Slerp(hmm_quaternion Left, float Time, hmm_quaternion Right);
HMMDEF hmm_mat4 HMM_QuaternionToMat4(hmm_quaternion Left);
HMMDEF hmm_quaternion HMM_QuaternionFromAxisAngle(hmm_vec3 Axis, float AngleOfRotation);
#ifdef __cplusplus
}
#endif
@ -467,20 +544,24 @@ HMMDEF float HMM_LengthSquared(hmm_vec4 A);
HMMDEF hmm_vec2 HMM_Normalize(hmm_vec2 A);
HMMDEF hmm_vec3 HMM_Normalize(hmm_vec3 A);
HMMDEF hmm_vec4 HMM_Normalize(hmm_vec4 A);
HMMDEF hmm_quaternion HMM_Normalize(hmm_quaternion A);
HMMDEF float HMM_Dot(hmm_vec2 VecOne, hmm_vec2 VecTwo);
HMMDEF float HMM_Dot(hmm_vec3 VecOne, hmm_vec3 VecTwo);
HMMDEF float HMM_Dot(hmm_vec4 VecOne, hmm_vec4 VecTwo);
HMMDEF float HMM_Dot(hmm_quaternion QuatOne, hmm_quaternion QuatTwo);
HMMDEF hmm_vec2 HMM_Add(hmm_vec2 Left, hmm_vec2 Right);
HMMDEF hmm_vec3 HMM_Add(hmm_vec3 Left, hmm_vec3 Right);
HMMDEF hmm_vec4 HMM_Add(hmm_vec4 Left, hmm_vec4 Right);
HMMDEF hmm_mat4 HMM_Add(hmm_mat4 Left, hmm_mat4 Right);
HMMDEF hmm_quaternion HMM_Add(hmm_quaternion Left, hmm_quaternion Right);
HMMDEF hmm_vec2 HMM_Subtract(hmm_vec2 Left, hmm_vec2 Right);
HMMDEF hmm_vec3 HMM_Subtract(hmm_vec3 Left, hmm_vec3 Right);
HMMDEF hmm_vec4 HMM_Subtract(hmm_vec4 Left, hmm_vec4 Right);
HMMDEF hmm_mat4 HMM_Subtract(hmm_mat4 Left, hmm_mat4 Right);
HMMDEF hmm_quaternion HMM_Subtract(hmm_quaternion Left, hmm_quaternion Right);
HMMDEF hmm_vec2 HMM_Multiply(hmm_vec2 Left, hmm_vec2 Right);
HMMDEF hmm_vec2 HMM_Multiply(hmm_vec2 Left, float Right);
@ -491,6 +572,8 @@ HMMDEF hmm_vec4 HMM_Multiply(hmm_vec4 Left, float Right);
HMMDEF hmm_mat4 HMM_Multiply(hmm_mat4 Left, hmm_mat4 Right);
HMMDEF hmm_mat4 HMM_Multiply(hmm_mat4 Left, float Right);
HMMDEF hmm_vec4 HMM_Multiply(hmm_mat4 Matrix, hmm_vec4 Vector);
HMMDEF hmm_quaternion HMM_Multiply(hmm_quaternion Left, hmm_quaternion Right);
HMMDEF hmm_quaternion HMM_Multiply(hmm_quaternion Left, float Right);
HMMDEF hmm_vec2 HMM_Divide(hmm_vec2 Left, hmm_vec2 Right);
HMMDEF hmm_vec2 HMM_Divide(hmm_vec2 Left, float Right);
@ -499,31 +582,38 @@ HMMDEF hmm_vec3 HMM_Divide(hmm_vec3 Left, float Right);
HMMDEF hmm_vec4 HMM_Divide(hmm_vec4 Left, hmm_vec4 Right);
HMMDEF hmm_vec4 HMM_Divide(hmm_vec4 Left, float Right);
HMMDEF hmm_mat4 HMM_Divide(hmm_mat4 Left, float Right);
HMMDEF hmm_quaternion HMM_Divide(hmm_quaternion Left, hmm_quaternion Right);
HMMDEF hmm_quaternion HMM_Divide(hmm_quaternion Left, float Right);
HMMDEF hmm_vec2 operator+(hmm_vec2 Left, hmm_vec2 Right);
HMMDEF hmm_vec3 operator+(hmm_vec3 Left, hmm_vec3 Right);
HMMDEF hmm_vec4 operator+(hmm_vec4 Left, hmm_vec4 Right);
HMMDEF hmm_mat4 operator+(hmm_mat4 Left, hmm_mat4 Right);
HMMDEF hmm_quaternion operator+(hmm_quaternion Left, hmm_quaternion Right);
HMMDEF hmm_vec2 operator-(hmm_vec2 Left, hmm_vec2 Right);
HMMDEF hmm_vec3 operator-(hmm_vec3 Left, hmm_vec3 Right);
HMMDEF hmm_vec4 operator-(hmm_vec4 Left, hmm_vec4 Right);
HMMDEF hmm_mat4 operator-(hmm_mat4 Left, hmm_mat4 Right);
HMMDEF hmm_quaternion operator-(hmm_quaternion Left, hmm_quaternion Right);
HMMDEF hmm_vec2 operator*(hmm_vec2 Left, hmm_vec2 Right);
HMMDEF hmm_vec3 operator*(hmm_vec3 Left, hmm_vec3 Right);
HMMDEF hmm_vec4 operator*(hmm_vec4 Left, hmm_vec4 Right);
HMMDEF hmm_mat4 operator*(hmm_mat4 Left, hmm_mat4 Right);
HMMDEF hmm_quaternion operator*(hmm_quaternion Left, hmm_quaternion Right);
HMMDEF hmm_vec2 operator*(hmm_vec2 Left, float Right);
HMMDEF hmm_vec3 operator*(hmm_vec3 Left, float Right);
HMMDEF hmm_vec4 operator*(hmm_vec4 Left, float Right);
HMMDEF hmm_mat4 operator*(hmm_mat4 Left, float Right);
HMMDEF hmm_quaternion operator*(hmm_quaternion Left, float Right);
HMMDEF hmm_vec2 operator*(float Left, hmm_vec2 Right);
HMMDEF hmm_vec3 operator*(float Left, hmm_vec3 Right);
HMMDEF hmm_vec4 operator*(float Left, hmm_vec4 Right);
HMMDEF hmm_mat4 operator*(float Left, hmm_mat4 Right);
HMMDEF hmm_quaternion operator*(float Left, hmm_quaternion Right);
HMMDEF hmm_vec4 operator*(hmm_mat4 Matrix, hmm_vec4 Vector);
@ -535,16 +625,19 @@ HMMDEF hmm_vec2 operator/(hmm_vec2 Left, float Right);
HMMDEF hmm_vec3 operator/(hmm_vec3 Left, float Right);
HMMDEF hmm_vec4 operator/(hmm_vec4 Left, float Right);
HMMDEF hmm_mat4 operator/(hmm_mat4 Left, float Right);
HMMDEF hmm_quaternion operator/(hmm_quaternion Left, float Right);
HMMDEF hmm_vec2 &operator+=(hmm_vec2 &Left, hmm_vec2 Right);
HMMDEF hmm_vec3 &operator+=(hmm_vec3 &Left, hmm_vec3 Right);
HMMDEF hmm_vec4 &operator+=(hmm_vec4 &Left, hmm_vec4 Right);
HMMDEF hmm_mat4 &operator+=(hmm_mat4 &Left, hmm_mat4 Right);
HMMDEF hmm_quaternion &operator+=(hmm_quaternion &Left, hmm_quaternion Right);
HMMDEF hmm_vec2 &operator-=(hmm_vec2 &Left, hmm_vec2 Right);
HMMDEF hmm_vec3 &operator-=(hmm_vec3 &Left, hmm_vec3 Right);
HMMDEF hmm_vec4 &operator-=(hmm_vec4 &Left, hmm_vec4 Right);
HMMDEF hmm_mat4 &operator-=(hmm_mat4 &Left, hmm_mat4 Right);
HMMDEF hmm_quaternion &operator-=(hmm_quaternion &Left, hmm_quaternion Right);
HMMDEF hmm_vec2 &operator*=(hmm_vec2 &Left, hmm_vec2 Right);
HMMDEF hmm_vec3 &operator*=(hmm_vec3 &Left, hmm_vec3 Right);
@ -554,6 +647,7 @@ HMMDEF hmm_vec2 &operator*=(hmm_vec2 &Left, float Right);
HMMDEF hmm_vec3 &operator*=(hmm_vec3 &Left, float Right);
HMMDEF hmm_vec4 &operator*=(hmm_vec4 &Left, float Right);
HMMDEF hmm_mat4 &operator*=(hmm_mat4 &Left, float Right);
HMMDEF hmm_quaternion &operator*=(hmm_quaternion &Left, float Right);
HMMDEF hmm_vec2 &operator/=(hmm_vec2 &Left, hmm_vec2 Right);
HMMDEF hmm_vec3 &operator/=(hmm_vec3 &Left, hmm_vec3 Right);
@ -563,9 +657,14 @@ HMMDEF hmm_vec2 &operator/=(hmm_vec2 &Left, float Right);
HMMDEF hmm_vec3 &operator/=(hmm_vec3 &Left, float Right);
HMMDEF hmm_vec4 &operator/=(hmm_vec4 &Left, float Right);
HMMDEF hmm_mat4 &operator/=(hmm_mat4 &Left, float Right);
HMMDEF hmm_quaternion &operator/=(hmm_quaternion &Left, float Right);
#endif /* HANDMADE_MATH_CPP */
#ifdef __clang__
#pragma GCC diagnostic pop
#endif
#endif /* HANDMADE_MATH_H */
#ifdef HANDMADE_MATH_IMPLEMENTATION
@ -597,6 +696,33 @@ HMM_TanF(float Radians)
return (Result);
}
HINLINE float
HMM_ACosF(float Radians)
{
float Result = 0.0f;
Result = HMM_ACOSF(Radians);
return (Result);
}
HINLINE float
HMM_ATanF(float Radians)
{
float Result = 0.0f;
Result = HMM_ATANF(Radians);
return (Result);
}
HINLINE float
HMM_Atan2F(float Left, float Right)
{
float Result = 0.0f;
Result = HMM_ATAN2F(Left, Right);
return (Result);
}
HINLINE float
HMM_ExpF(float Float)
{
@ -1208,7 +1334,7 @@ HMM_SubtractMat4(hmm_mat4 Left, hmm_mat4 Right)
return (Result);
}
hmm_mat4
HINLINE hmm_mat4
HMM_MultiplyMat4(hmm_mat4 Left, hmm_mat4 Right)
{
hmm_mat4 Result = HMM_Mat4();
@ -1233,7 +1359,7 @@ HMM_MultiplyMat4(hmm_mat4 Left, hmm_mat4 Right)
return (Result);
}
hmm_mat4
HINLINE hmm_mat4
HMM_MultiplyMat4f(hmm_mat4 Matrix, float Scalar)
{
hmm_mat4 Result = HMM_Mat4();
@ -1251,7 +1377,7 @@ HMM_MultiplyMat4f(hmm_mat4 Matrix, float Scalar)
return (Result);
}
hmm_vec4
HINLINE hmm_vec4
HMM_MultiplyMat4ByVec4(hmm_mat4 Matrix, hmm_vec4 Vector)
{
hmm_vec4 Result = {0};
@ -1271,7 +1397,7 @@ HMM_MultiplyMat4ByVec4(hmm_mat4 Matrix, hmm_vec4 Vector)
return (Result);
}
hmm_mat4
HINLINE hmm_mat4
HMM_DivideMat4f(hmm_mat4 Matrix, float Scalar)
{
hmm_mat4 Result = HMM_Mat4();
@ -1289,7 +1415,7 @@ HMM_DivideMat4f(hmm_mat4 Matrix, float Scalar)
return (Result);
}
hmm_mat4
HINLINE hmm_mat4
HMM_Transpose(hmm_mat4 Matrix)
{
hmm_mat4 Result = HMM_Mat4();
@ -1307,7 +1433,7 @@ HMM_Transpose(hmm_mat4 Matrix)
return (Result);
}
hmm_mat4
HINLINE hmm_mat4
HMM_Orthographic(float Left, float Right, float Bottom, float Top, float Near, float Far)
{
hmm_mat4 Result = HMM_Mat4d(1.0f);
@ -1323,7 +1449,7 @@ HMM_Orthographic(float Left, float Right, float Bottom, float Top, float Near, f
return (Result);
}
hmm_mat4
HINLINE hmm_mat4
HMM_Perspective(float FOV, float AspectRatio, float Near, float Far)
{
hmm_mat4 Result = HMM_Mat4d(1.0f);
@ -1340,7 +1466,7 @@ HMM_Perspective(float FOV, float AspectRatio, float Near, float Far)
return (Result);
}
hmm_mat4
HINLINE hmm_mat4
HMM_Translate(hmm_vec3 Translation)
{
hmm_mat4 Result = HMM_Mat4d(1.0f);
@ -1352,7 +1478,7 @@ HMM_Translate(hmm_vec3 Translation)
return (Result);
}
hmm_mat4
HINLINE hmm_mat4
HMM_Rotate(float Angle, hmm_vec3 Axis)
{
hmm_mat4 Result = HMM_Mat4d(1.0f);
@ -1378,7 +1504,7 @@ HMM_Rotate(float Angle, hmm_vec3 Axis)
return (Result);
}
hmm_mat4
HINLINE hmm_mat4
HMM_Scale(hmm_vec3 Scale)
{
hmm_mat4 Result = HMM_Mat4d(1.0f);
@ -1390,7 +1516,7 @@ HMM_Scale(hmm_vec3 Scale)
return (Result);
}
hmm_mat4
HINLINE hmm_mat4
HMM_LookAt(hmm_vec3 Eye, hmm_vec3 Center, hmm_vec3 Up)
{
hmm_mat4 Result = {0};
@ -1419,9 +1545,239 @@ HMM_LookAt(hmm_vec3 Eye, hmm_vec3 Center, hmm_vec3 Up)
return (Result);
}
HINLINE hmm_quaternion
HMM_Quaternion(float X, float Y, float Z, float W)
{
hmm_quaternion Result = {0};
Result.X = X;
Result.Y = Y;
Result.Z = Z;
Result.W = W;
return(Result);
}
HINLINE hmm_quaternion
HMM_QuaternionV4(hmm_vec4 Vector)
{
hmm_quaternion Result = {0};
Result.X = Vector.X;
Result.Y = Vector.Y;
Result.Z = Vector.Z;
Result.W = Vector.W;
return(Result);
}
HINLINE hmm_quaternion
HMM_AddQuaternion(hmm_quaternion Left, hmm_quaternion Right)
{
hmm_quaternion Result = {0};
Result.X = Left.X + Right.X;
Result.Y = Left.Y + Right.Y;
Result.Z = Left.Z + Right.Z;
Result.W = Left.W + Right.W;
return(Result);
}
HINLINE hmm_quaternion
HMM_SubtractQuaternion(hmm_quaternion Left, hmm_quaternion Right)
{
hmm_quaternion Result = {0};
Result.X = Left.X - Right.X;
Result.Y = Left.Y - Right.Y;
Result.Z = Left.Z - Right.Z;
Result.W = Left.W - Right.W;
return(Result);
}
HINLINE hmm_quaternion
HMM_MultiplyQuaternion(hmm_quaternion Left, hmm_quaternion Right)
{
hmm_quaternion Result = {0};
Result.X = (Left.X * Right.W) + (Left.Y * Right.Z) - (Left.Z * Right.Y) + (Left.W * Right.X);
Result.Y = (-Left.X * Right.Z) + (Left.Y * Right.W) + (Left.Z * Right.X) + (Left.W * Right.Y);
Result.Z = (Left.X * Right.Y) - (Left.Y * Right.X) + (Left.Z * Right.W) + (Left.W * Right.Z);
Result.W = (-Left.X * Right.X) - (Left.Y * Right.Y) - (Left.Z * Right.Z) + (Left.W * Right.W);
return(Result);
}
HINLINE hmm_quaternion
HMM_MultiplyQuaternionF(hmm_quaternion Left, float Multiplicative)
{
hmm_quaternion Result = {0};
Result.X = Left.X * Multiplicative;
Result.Y = Left.Y * Multiplicative;
Result.Z = Left.Z * Multiplicative;
Result.W = Left.W * Multiplicative;
return(Result);
}
HINLINE hmm_quaternion
HMM_DivideQuaternionF(hmm_quaternion Left, float Dividend)
{
hmm_quaternion Result = {0};
Result.X = Left.X / Dividend;
Result.Y = Left.Y / Dividend;
Result.Z = Left.Z / Dividend;
Result.W = Left.W / Dividend;
return(Result);
}
HINLINE hmm_quaternion
HMM_InverseQuaternion(hmm_quaternion Left)
{
hmm_quaternion Conjugate = {0};
hmm_quaternion Result = {0};
float Norm = 0;
float NormSquared = 0;
Conjugate.X = -Left.X;
Conjugate.Y = -Left.Y;
Conjugate.Z = -Left.Z;
Conjugate.W = Left.W;
Norm = HMM_SquareRootF(HMM_DotQuaternion(Left, Left));
NormSquared = Norm * Norm;
Result.X = Conjugate.X / NormSquared;
Result.Y = Conjugate.Y / NormSquared;
Result.Z = Conjugate.Z / NormSquared;
Result.W = Conjugate.W / NormSquared;
return(Result);
}
HINLINE float
HMM_DotQuaternion(hmm_quaternion Left, hmm_quaternion Right)
{
float Result = 0.0f;
Result = (Left.X * Right.X) + (Left.Y * Right.Y) + (Left.Z * Right.Z) + (Left.W * Right.W);
return(Result);
}
HINLINE hmm_quaternion
HMM_NormalizeQuaternion(hmm_quaternion Left)
{
hmm_quaternion Result = {0};
float Length = HMM_SquareRootF(HMM_DotQuaternion(Left, Left));
Result = HMM_DivideQuaternionF(Left, Length);
return(Result);
}
HINLINE hmm_quaternion
HMM_NLerp(hmm_quaternion Left, float Time, hmm_quaternion Right)
{
hmm_quaternion Result = {0};
Result.X = HMM_Lerp(Left.X, Time, Right.X);
Result.Y = HMM_Lerp(Left.Y, Time, Right.Y);
Result.Z = HMM_Lerp(Left.Z, Time, Right.Z);
Result.W = HMM_Lerp(Left.W, Time, Right.W);
HMM_NormalizeQuaternion(Result);
return(Result);
}
HINLINE hmm_quaternion
HMM_Slerp(hmm_quaternion Left, float Time, hmm_quaternion Right)
{
hmm_quaternion Result = {0};
hmm_quaternion QuaternionLeft = {0};
hmm_quaternion QuaternionRight = {0};
float Cos_Theta = HMM_DotQuaternion(Left, Right);
float Angle = HMM_ACosF(Cos_Theta);
float S1 = HMM_SinF((1.0f - Time) * Angle);
float S2 = HMM_SinF(Time * Angle);
float Is = 1.0f / HMM_SinF(Angle);
QuaternionLeft = HMM_MultiplyQuaternionF(Left, S1);
QuaternionRight = HMM_MultiplyQuaternionF(Right, S2);
Result = HMM_AddQuaternion(QuaternionLeft, QuaternionRight);
Result = HMM_MultiplyQuaternionF(Result, Is);
return(Result);
}
HINLINE hmm_mat4
HMM_QuaternionToMat4(hmm_quaternion Left)
{
hmm_mat4 Result = {0};
Result = HMM_Mat4d(1);
hmm_quaternion NormalizedQuaternion = HMM_NormalizeQuaternion(Left);
float XX, YY, ZZ,
XY, XZ, YZ,
WX, WY, WZ;
XX = NormalizedQuaternion.X * NormalizedQuaternion.X;
YY = NormalizedQuaternion.Y * NormalizedQuaternion.Y;
ZZ = NormalizedQuaternion.Z * NormalizedQuaternion.Z;
XY = NormalizedQuaternion.X * NormalizedQuaternion.Y;
XZ = NormalizedQuaternion.X * NormalizedQuaternion.Z;
YZ = NormalizedQuaternion.Y * NormalizedQuaternion.Z;
WX = NormalizedQuaternion.W * NormalizedQuaternion.X;
WY = NormalizedQuaternion.W * NormalizedQuaternion.Y;
WZ = NormalizedQuaternion.W * NormalizedQuaternion.Z;
Result.Elements[0][0] = 1.0f - 2.0f * (YY + ZZ);
Result.Elements[0][1] = 2.0f * (XY + WZ);
Result.Elements[0][2] = 2.0f * (XZ - WY);
Result.Elements[1][0] = 2.0f * (XY - WZ);
Result.Elements[1][1] = 1.0f - 2.0f * (XX + ZZ);
Result.Elements[1][2] = 2.0f * (YZ + WX);
Result.Elements[2][0] = 2.0f * (XZ + WY);
Result.Elements[2][1] = 2.0f * (YZ - WX);
Result.Elements[2][2] = 1.0f - 2.0f * (XX + YY);
return(Result);
}
HINLINE hmm_quaternion
HMM_QuaternionFromAxisAngle(hmm_vec3 Axis, float AngleOfRotation)
{
hmm_quaternion Result = {0};
float AxisNorm = 0;
float SineOfRotation = 0;
hmm_vec3 RotatedVector = {0};
AxisNorm = HMM_SquareRootF(HMM_DotVec3(Axis, Axis));
SineOfRotation = HMM_SinF(AngleOfRotation / 2.0f);
RotatedVector = HMM_MultiplyVec3f(Axis, SineOfRotation);
Result.W = HMM_CosF(AngleOfRotation / 2.0f);
Result.XYZ = HMM_DivideVec3f(RotatedVector, AxisNorm);
return(Result);
}
#ifdef HANDMADE_MATH_CPP_MODE
HMMDEF float
HINLINE float
HMM_Length(hmm_vec2 A)
{
float Result = 0.0f;
@ -1431,7 +1787,7 @@ HMM_Length(hmm_vec2 A)
return(Result);
}
HMMDEF float
HINLINE float
HMM_Length(hmm_vec3 A)
{
float Result = 0.0f;
@ -1441,7 +1797,7 @@ HMM_Length(hmm_vec3 A)
return(Result);
}
HMMDEF float
HINLINE float
HMM_Length(hmm_vec4 A)
{
float Result = 0.0f;
@ -1511,6 +1867,16 @@ HMM_Normalize(hmm_vec4 A)
return(Result);
}
HINLINE hmm_quaternion
HMM_Normalize(hmm_quaternion A)
{
hmm_quaternion Result = {0};
Result = HMM_NormalizeQuaternion(A);
return(Result);
}
HINLINE float
HMM_Dot(hmm_vec2 VecOne, hmm_vec2 VecTwo)
{
@ -1541,6 +1907,16 @@ HMM_Dot(hmm_vec4 VecOne, hmm_vec4 VecTwo)
return(Result);
}
HINLINE float
HMM_Dot(hmm_quaternion QuatOne, hmm_quaternion QuatTwo)
{
float Result = 0;
Result = HMM_DotQuaternion(QuatOne, QuatTwo);
return(Result);
}
HINLINE hmm_vec2
HMM_Add(hmm_vec2 Left, hmm_vec2 Right)
{
@ -1559,7 +1935,7 @@ HMM_Add(hmm_vec3 Left, hmm_vec3 Right)
return (Result);
}
HMMDEF HINLINE hmm_vec4
HINLINE hmm_vec4
HMM_Add(hmm_vec4 Left, hmm_vec4 Right)
{
hmm_vec4 Result = {0};
@ -1577,6 +1953,15 @@ HMM_Add(hmm_mat4 Left, hmm_mat4 Right)
return (Result);
}
HINLINE hmm_quaternion
HMM_Add(hmm_quaternion Left, hmm_quaternion Right)
{
hmm_quaternion Result = {0};
Result = HMM_AddQuaternion(Left, Right);
return(Result);
}
HINLINE hmm_vec2
HMM_Subtract(hmm_vec2 Left, hmm_vec2 Right)
{
@ -1613,6 +1998,15 @@ HMM_Subtract(hmm_mat4 Left, hmm_mat4 Right)
return (Result);
}
HINLINE hmm_quaternion
HMM_Subtract(hmm_quaternion Left, hmm_quaternion Right)
{
hmm_quaternion Result = {0};
Result = HMM_SubtractQuaternion(Left, Right);
return (Result);
}
HINLINE hmm_vec2
HMM_Multiply(hmm_vec2 Left, hmm_vec2 Right)
{
@ -1694,6 +2088,33 @@ HMM_Multiply(hmm_mat4 Matrix, hmm_vec4 Vector)
return (Result);
}
HINLINE hmm_quaternion
HMM_Multiply(hmm_quaternion Left, hmm_quaternion Right)
{
hmm_quaternion Result = {0};
Result = HMM_MultiplyQuaternion(Left, Right);
return (Result);
}
HINLINE hmm_quaternion
HMM_Multiply(hmm_quaternion Left, float Right)
{
hmm_quaternion Result = {0};
Result = HMM_MultiplyQuaternionF(Left, Right);
return (Result);
}
HINLINE hmm_quaternion
HMM_Multiply(float Left, hmm_quaternion Right)
{
hmm_quaternion Result = {0};
Result = HMM_MultiplyQuaternionF(Right, Left);
return (Result);
}
HINLINE hmm_vec2
HMM_Divide(hmm_vec2 Left, hmm_vec2 Right)
{
@ -1757,6 +2178,15 @@ HMM_Divide(hmm_mat4 Left, float Right)
return (Result);
}
HINLINE hmm_quaternion
HMM_Divide(hmm_quaternion Left, float Right)
{
hmm_quaternion Result = {0};
Result = HMM_DivideQuaternionF(Left, Right);
return (Result);
}
HINLINE hmm_vec2
operator+(hmm_vec2 Left, hmm_vec2 Right)
{
@ -1793,6 +2223,15 @@ operator+(hmm_mat4 Left, hmm_mat4 Right)
return (Result);
}
HINLINE hmm_quaternion
operator+(hmm_quaternion Left, hmm_quaternion Right)
{
hmm_quaternion Result = {0};
Result = HMM_Add(Left, Right);
return (Result);
}
HINLINE hmm_vec2
operator-(hmm_vec2 Left, hmm_vec2 Right)
{
@ -1829,6 +2268,15 @@ operator-(hmm_mat4 Left, hmm_mat4 Right)
return (Result);
}
HINLINE hmm_quaternion
operator-(hmm_quaternion Left, hmm_quaternion Right)
{
hmm_quaternion Result = {0};
Result = HMM_Subtract(Left, Right);
return (Result);
}
HINLINE hmm_vec2
operator*(hmm_vec2 Left, hmm_vec2 Right)
{
@ -1945,6 +2393,33 @@ operator*(hmm_mat4 Matrix, hmm_vec4 Vector)
return (Result);
}
HINLINE hmm_quaternion
operator*(hmm_quaternion Left, hmm_quaternion Right)
{
hmm_quaternion Result = {0};
Result = HMM_Multiply(Left, Right);
return (Result);
}
HINLINE hmm_quaternion
operator*(hmm_quaternion Left, float Right)
{
hmm_quaternion Result = {0};
Result = HMM_Multiply(Left, Right);
return (Result);
}
HINLINE hmm_quaternion
operator*(float Left, hmm_quaternion Right)
{
hmm_quaternion Result = {0};
Result = HMM_Multiply(Right, Left);
return (Result);
}
HINLINE hmm_vec2
operator/(hmm_vec2 Left, hmm_vec2 Right)
{
@ -1957,7 +2432,9 @@ operator/(hmm_vec2 Left, hmm_vec2 Right)
HINLINE hmm_vec3
operator/(hmm_vec3 Left, hmm_vec3 Right)
{
hmm_vec3 Result = HMM_Divide(Left, Right);
hmm_vec3 Result = {0};
Result = HMM_Divide(Left, Right);
return (Result);
}
@ -2007,6 +2484,15 @@ operator/(hmm_mat4 Left, float Right)
return (Result);
}
HINLINE hmm_quaternion
operator/(hmm_quaternion Left, float Right)
{
hmm_quaternion Result = {0};
Result = HMM_Divide(Left, Right);
return (Result);
}
HINLINE hmm_vec2 &
operator+=(hmm_vec2 &Left, hmm_vec2 Right)
{
@ -2031,6 +2517,12 @@ operator+=(hmm_mat4 &Left, hmm_mat4 Right)
return (Left = Left + Right);
}
HINLINE hmm_quaternion &
operator+=(hmm_quaternion &Left, hmm_quaternion Right)
{
return (Left = Left + Right);
}
HINLINE hmm_vec2 &
operator-=(hmm_vec2 &Left, hmm_vec2 Right)
{
@ -2055,6 +2547,12 @@ operator-=(hmm_mat4 &Left, hmm_mat4 Right)
return (Left = Left - Right);
}
HINLINE hmm_quaternion &
operator-=(hmm_quaternion &Left, hmm_quaternion Right)
{
return (Left = Left - Right);
}
HINLINE hmm_vec2 &
operator/=(hmm_vec2 &Left, hmm_vec2 Right)
{
@ -2097,6 +2595,12 @@ operator/=(hmm_mat4 &Left, float Right)
return (Left = Left / Right);
}
HINLINE hmm_quaternion &
operator/=(hmm_quaternion &Left, float Right)
{
return (Left = Left / Right);
}
HINLINE hmm_vec2 &
operator*=(hmm_vec2 &Left, hmm_vec2 Right)
{
@ -2139,6 +2643,12 @@ operator*=(hmm_mat4 &Left, float Right)
return (Left = Left * Right);
}
HINLINE hmm_quaternion &
operator*=(hmm_quaternion &Left, float Right)
{
return (Left = Left * Right);
}
#endif /* HANDMADE_MATH_CPP_MODE */
#endif /* HANDMADE_MATH_IMPLEMENTATION */