Cleaned up function declarations, and fixed C compliance.

This commit is contained in:
StrangeZak
2016-05-20 12:01:31 -07:00
parent 87360e6adc
commit c9c60ba25b
3 changed files with 107 additions and 62 deletions

View File

@@ -1,5 +1,5 @@
/* /*
HandmadeMath.h v0.1 HandmadeMath.h v0.2
This is a single header file with a bunch of useful functions for This is a single header file with a bunch of useful functions for
basic game math operations. basic game math operations.
@@ -32,6 +32,13 @@
All other files should just #include "HandmadeMath.h" without the #define. All other files should just #include "HandmadeMath.h" without the #define.
========================================================================== ==========================================================================
Version History:
0.2 (*) Updated documentation
() Better C compliance
() Prefix all handmade math functions
() Remove use of math.h
() Better operator overloading
LICENSE LICENSE
This software is in the public domain. Where that dedication is not This software is in the public domain. Where that dedication is not
@@ -40,11 +47,12 @@
CREDITS CREDITS
Written by Zakary Strange (zak@strangedev.net && @strangezak) Written by Zakary Strange (zak@handmade.network && @strangezak)
Functionality: Functionality:
Matt Mascarenhas (@miblo_) Matt Mascarenhas (@miblo_)
Aleph Aleph
FieryDrake (@fierydrake)
Fixes: Fixes:
Jeroen van Rijn (@J_vanRijn) Jeroen van Rijn (@J_vanRijn)
@@ -55,7 +63,6 @@
#ifndef HANDMADE_MATH_H #ifndef HANDMADE_MATH_H
#define HANDMADE_MATH_H #define HANDMADE_MATH_H
#include <math.h> // TODO(zak): Remove this later on #include <math.h> // TODO(zak): Remove this later on
#ifdef __cplusplus #ifdef __cplusplus
@@ -205,8 +212,19 @@ typedef union mat4
float Elements[4][4]; float Elements[4][4];
} mat4; } mat4;
typedef vec2 v2;
typedef vec3 v3;
typedef vec4 v4;
typedef mat4 m4;
HMMDEF float ToRadians(float Degrees);
HMMDEF float Inner(vec3 A, vec3 B);
HMMDEF float SquareRoot(float Float);
HMMDEF float LengthSquareRoot(vec3 A);
HMMDEF float Length(vec3 A);
HMMDEF float Power(float Base, int Exponent); HMMDEF float Power(float Base, int Exponent);
HMMDEF float Clamp(float Min, float Value, float Max); HMMDEF float Clamp(float Min, float Value, float Max);
HMMDEF vec3 Normalize(vec3 A); HMMDEF vec3 Normalize(vec3 A);
HMMDEF vec3 Cross(vec3 VecOne, vec3 VecTwo); HMMDEF vec3 Cross(vec3 VecOne, vec3 VecTwo);
HMMDEF float Dot(vec3 VecOne, vec3 VecTwo); HMMDEF float Dot(vec3 VecOne, vec3 VecTwo);
@@ -294,7 +312,7 @@ HMMDEF vec4 operator/(vec4 Left, vec4 Right);
#ifdef HANDMADE_MATH_IMPLEMENTATION #ifdef HANDMADE_MATH_IMPLEMENTATION
HMMDEF HINLINE float HINLINE float
ToRadians(float Degrees) ToRadians(float Degrees)
{ {
float Result = Degrees * (Pi32 / 180.0f); float Result = Degrees * (Pi32 / 180.0f);
@@ -302,7 +320,7 @@ ToRadians(float Degrees)
return (Result); return (Result);
} }
HMMDEF HINLINE float HINLINE float
Inner(vec3 A, vec3 B) Inner(vec3 A, vec3 B)
{ {
float Result = A.X * B.X + A.Y * B.Y + A.Z * B.Z; float Result = A.X * B.X + A.Y * B.Y + A.Z * B.Z;
@@ -310,7 +328,7 @@ Inner(vec3 A, vec3 B)
return (Result); return (Result);
} }
HMMDEF HINLINE float HINLINE float
SquareRoot(float Float) SquareRoot(float Float)
{ {
float Result = sqrtf(Float); float Result = sqrtf(Float);
@@ -318,35 +336,37 @@ SquareRoot(float Float)
return (Result); return (Result);
} }
HMMDEF HINLINE float HINLINE float
LengthSq(vec3 A) LengthSquareRoot(vec3 A)
{ {
float Result = Inner(A, A); float Result = Inner(A, A);
return (Result); return (Result);
} }
HMMDEF HINLINE float HINLINE float
Length(vec3 A) Length(vec3 A)
{ {
float Result = SquareRoot(LengthSq(A)); float Result = SquareRoot(LengthSquareRoot(A));
return (Result); return (Result);
} }
HMMDEF HINLINE float HINLINE float
Power(float Base, int Exponent) Power(float Base, int Exponent)
{ {
float Result = 1; float Result = 1;
if(Exponent > 0) if(Exponent > 0)
{ {
for(int i = 0; i < Exponent; ++i) int i;
for(i = 0; i < Exponent; ++i)
{ {
Result *= Base; Result *= Base;
} }
} }
else else
{ {
for(int i = 0; i > Exponent; --i) int i;
for(i = 0; i > Exponent; --i)
{ {
Result /= Base; Result /= Base;
} }
@@ -354,7 +374,7 @@ Power(float Base, int Exponent)
return (Result); return (Result);
} }
HMMDEF HINLINE float HINLINE float
Lerp(float A, float Time, float B) Lerp(float A, float Time, float B)
{ {
float Result = (1.0f - Time) * A + Time * B; float Result = (1.0f - Time) * A + Time * B;
@@ -362,7 +382,7 @@ Lerp(float A, float Time, float B)
return (Result); return (Result);
} }
HMMDEF HINLINE float HINLINE float
Clamp(float Min, float Value, float Max) Clamp(float Min, float Value, float Max)
{ {
float Result = Value; float Result = Value;
@@ -379,15 +399,19 @@ Clamp(float Min, float Value, float Max)
return (Result); return (Result);
} }
HMMDEF HINLINE vec3 HINLINE vec3
Normalize(vec3 A) Normalize(vec3 A)
{ {
vec3 Result = A * (1.0f / Length(A)); vec3 Result = {0};
Result.X = A.X * (1.0f / Length(A));
Result.Y = A.Y * (1.0f / Length(A));
Result.Z = A.Z * (1.0f / Length(A));
return (Result); return (Result);
} }
HMMDEF HINLINE vec3 HINLINE vec3
Cross(vec3 VecOne, vec3 VecTwo) Cross(vec3 VecOne, vec3 VecTwo)
{ {
vec3 Result; vec3 Result;
@@ -399,7 +423,7 @@ Cross(vec3 VecOne, vec3 VecTwo)
return (Result); return (Result);
} }
HMMDEF HINLINE float HINLINE float
Dot(vec3 VecOne, vec3 VecTwo) Dot(vec3 VecOne, vec3 VecTwo)
{ {
float Result = 0; float Result = 0;
@@ -409,7 +433,7 @@ Dot(vec3 VecOne, vec3 VecTwo)
return (Result); return (Result);
} }
HMMDEF HINLINE vec2 HINLINE vec2
Vec2(float X, float Y) Vec2(float X, float Y)
{ {
vec2 Result; vec2 Result;
@@ -420,7 +444,7 @@ Vec2(float X, float Y)
return (Result); return (Result);
} }
HMMDEF HINLINE vec2 HINLINE vec2
Vec2i(int X, int Y) Vec2i(int X, int Y)
{ {
vec2 Result; vec2 Result;
@@ -431,7 +455,7 @@ Vec2i(int X, int Y)
return (Result); return (Result);
} }
HMMDEF HINLINE vec3 HINLINE vec3
Vec3(float X, float Y, float Z) Vec3(float X, float Y, float Z)
{ {
vec3 Result; vec3 Result;
@@ -443,7 +467,7 @@ Vec3(float X, float Y, float Z)
return (Result); return (Result);
} }
HMMDEF HINLINE vec3 HINLINE vec3
Vec3i(int X, int Y, int Z) Vec3i(int X, int Y, int Z)
{ {
vec3 Result; vec3 Result;
@@ -455,7 +479,7 @@ Vec3i(int X, int Y, int Z)
return (Result); return (Result);
} }
HMMDEF HINLINE vec4 HINLINE vec4
Vec4(float X, float Y, float Z, float W) Vec4(float X, float Y, float Z, float W)
{ {
vec4 Result; vec4 Result;
@@ -468,7 +492,7 @@ Vec4(float X, float Y, float Z, float W)
return (Result); return (Result);
} }
HMMDEF HINLINE vec4 HINLINE vec4
Vec4i(int X, int Y, int Z, int W) Vec4i(int X, int Y, int Z, int W)
{ {
vec4 Result; vec4 Result;
@@ -481,7 +505,7 @@ Vec4i(int X, int Y, int Z, int W)
return (Result); return (Result);
} }
HMMDEF HINLINE vec2 HINLINE vec2
AddVec2(vec2 Left, vec2 Right) AddVec2(vec2 Left, vec2 Right)
{ {
vec2 Result; vec2 Result;
@@ -492,7 +516,7 @@ AddVec2(vec2 Left, vec2 Right)
return (Result); return (Result);
} }
HMMDEF HINLINE vec3 HINLINE vec3
AddVec3(vec3 Left, vec3 Right) AddVec3(vec3 Left, vec3 Right)
{ {
vec3 Result; vec3 Result;
@@ -504,7 +528,7 @@ AddVec3(vec3 Left, vec3 Right)
return (Result); return (Result);
} }
HMMDEF HINLINE vec4 HINLINE vec4
AddVec4(vec4 Left, vec4 Right) AddVec4(vec4 Left, vec4 Right)
{ {
vec4 Result; vec4 Result;
@@ -517,7 +541,7 @@ AddVec4(vec4 Left, vec4 Right)
return (Result); return (Result);
} }
HMMDEF HINLINE vec2 HINLINE vec2
SubtractVec2(vec2 Left, vec2 Right) SubtractVec2(vec2 Left, vec2 Right)
{ {
vec2 Result; vec2 Result;
@@ -528,7 +552,7 @@ SubtractVec2(vec2 Left, vec2 Right)
return (Result); return (Result);
} }
HMMDEF HINLINE vec3 HINLINE vec3
SubtractVec3(vec3 Left, vec3 Right) SubtractVec3(vec3 Left, vec3 Right)
{ {
vec3 Result; vec3 Result;
@@ -540,7 +564,7 @@ SubtractVec3(vec3 Left, vec3 Right)
return (Result); return (Result);
} }
HMMDEF HINLINE vec4 HINLINE vec4
SubtractVec4(vec4 Left, vec4 Right) SubtractVec4(vec4 Left, vec4 Right)
{ {
vec4 Result; vec4 Result;
@@ -553,7 +577,7 @@ SubtractVec4(vec4 Left, vec4 Right)
return (Result); return (Result);
} }
HMMDEF HINLINE vec2 HINLINE vec2
MultiplyVec2(vec2 Left, vec2 Right) MultiplyVec2(vec2 Left, vec2 Right)
{ {
vec2 Result; vec2 Result;
@@ -564,7 +588,7 @@ MultiplyVec2(vec2 Left, vec2 Right)
return (Result); return (Result);
} }
HMMDEF HINLINE vec3 HINLINE vec3
MultiplyVec3(vec3 Left, vec3 Right) MultiplyVec3(vec3 Left, vec3 Right)
{ {
vec3 Result; vec3 Result;
@@ -576,7 +600,7 @@ MultiplyVec3(vec3 Left, vec3 Right)
return (Result); return (Result);
} }
HMMDEF HINLINE vec4 HINLINE vec4
MultiplyVec4(vec4 Left, vec4 Right) MultiplyVec4(vec4 Left, vec4 Right)
{ {
vec4 Result; vec4 Result;
@@ -589,7 +613,7 @@ MultiplyVec4(vec4 Left, vec4 Right)
return (Result); return (Result);
} }
HMMDEF HINLINE vec2 HINLINE vec2
DivideVec2(vec2 Left, vec2 Right) DivideVec2(vec2 Left, vec2 Right)
{ {
vec2 Result; vec2 Result;
@@ -600,7 +624,7 @@ DivideVec2(vec2 Left, vec2 Right)
return (Result); return (Result);
} }
HMMDEF HINLINE vec3 HINLINE vec3
DivideVec3(vec3 Left, vec3 Right) DivideVec3(vec3 Left, vec3 Right)
{ {
vec3 Result; vec3 Result;
@@ -612,7 +636,7 @@ DivideVec3(vec3 Left, vec3 Right)
return (Result); return (Result);
} }
HMMDEF HINLINE vec4 HINLINE vec4
DivideVec4(vec4 Left, vec4 Right) DivideVec4(vec4 Left, vec4 Right)
{ {
vec4 Result; vec4 Result;
@@ -625,30 +649,24 @@ DivideVec4(vec4 Left, vec4 Right)
return (Result); return (Result);
} }
HMMDEF mat4 HINLINE mat4
Mat4() Mat4()
{ {
mat4 Result; mat4 Result = {0};
for(int Rows = 0; Rows < 4; ++Rows)
{
for(int Columns = 0; Columns < 4; ++Columns)
{
Result.Elements[Rows][Columns] = 0.0f;
}
}
return (Result); return (Result);
} }
HMMDEF mat4 mat4
Mat4d(float Diagonal) Mat4d(float Diagonal)
{ {
mat4 Result; mat4 Result;
for(int Rows = 0; Rows < 4; ++Rows) int Rows;
for(Rows = 0; Rows < 4; ++Rows)
{ {
for(int Columns = 0; Columns < 4; ++Columns) int Columns;
for(Columns = 0; Columns < 4; ++Columns)
{ {
Result.Elements[Rows][Columns] = 0.0f; Result.Elements[Rows][Columns] = 0.0f;
} }
@@ -662,17 +680,20 @@ Mat4d(float Diagonal)
return (Result); return (Result);
} }
HMMDEF mat4 mat4
MultiplyMat4(mat4 Left, mat4 Right) MultiplyMat4(mat4 Left, mat4 Right)
{ {
mat4 Result = Mat4(); mat4 Result = Mat4();
for(int Rows = 0; Rows < 4; ++Rows) int Rows;
for(Rows = 0; Rows < 4; ++Rows)
{ {
for(int Columns = 0; Columns < 4; ++Columns) int Columns;
for(Columns = 0; Columns < 4; ++Columns)
{ {
float Sum = 0; float Sum = 0;
for(int CurrentMatrice = 0; CurrentMatrice < 4; ++CurrentMatrice) int CurrentMatrice;
for(CurrentMatrice = 0; CurrentMatrice < 4; ++CurrentMatrice)
{ {
Sum += Right.Elements[Rows][CurrentMatrice] * Left.Elements[CurrentMatrice][Columns]; Sum += Right.Elements[Rows][CurrentMatrice] * Left.Elements[CurrentMatrice][Columns];
} }
@@ -684,7 +705,7 @@ MultiplyMat4(mat4 Left, mat4 Right)
return (Result); return (Result);
} }
HMMDEF mat4 mat4
Orthographic(float Left, float Right, float Bottom, float Top, float Near, float Far) Orthographic(float Left, float Right, float Bottom, float Top, float Near, float Far)
{ {
mat4 Result = Mat4d(1.0f); mat4 Result = Mat4d(1.0f);
@@ -700,7 +721,7 @@ Orthographic(float Left, float Right, float Bottom, float Top, float Near, float
return (Result); return (Result);
} }
HMMDEF mat4 mat4
Perspective(float FOV, float AspectRatio, float Near, float Far) Perspective(float FOV, float AspectRatio, float Near, float Far)
{ {
mat4 Result = Mat4d(1.0f); mat4 Result = Mat4d(1.0f);
@@ -715,7 +736,7 @@ Perspective(float FOV, float AspectRatio, float Near, float Far)
return (Result); return (Result);
} }
HMMDEF mat4 mat4
Translate(vec3 Translation) Translate(vec3 Translation)
{ {
mat4 Result = Mat4d(1.0f); mat4 Result = Mat4d(1.0f);
@@ -727,7 +748,7 @@ Translate(vec3 Translation)
return (Result); return (Result);
} }
HMMDEF mat4 mat4
Rotate(float Angle, vec3 Axis) Rotate(float Angle, vec3 Axis)
{ {
mat4 Result = Mat4d(1.0f); mat4 Result = Mat4d(1.0f);
@@ -747,12 +768,12 @@ Rotate(float Angle, vec3 Axis)
return (Result); return (Result);
} }
HMMDEF mat4 mat4
LookAt(vec3 Eye, vec3 Center, vec3 Up) LookAt(vec3 Eye, vec3 Center, vec3 Up)
{ {
mat4 Result = {}; mat4 Result = {0};
vec3 F = Normalize(Center - Eye); vec3 F = Normalize(SubtractVec3(Center, Eye));
vec3 S = Normalize(Cross(F, Up)); vec3 S = Normalize(Cross(F, Up));
vec3 U = Cross(S, F); vec3 U = Cross(S, F);
@@ -776,7 +797,7 @@ LookAt(vec3 Eye, vec3 Center, vec3 Up)
return (Result); return (Result);
} }
HMMDEF mat4 mat4
Scale(vec3 Scale) Scale(vec3 Scale)
{ {
mat4 Result = Mat4d(1.0f); mat4 Result = Mat4d(1.0f);

13
build.bat Normal file
View File

@@ -0,0 +1,13 @@
@echo off
IF NOT EXIST build mkdir build
pushd build
REM C Build
cl -nologo -FC -Z7 -Tc ..\main.c
REM C++ Build
cl -nologo -FC -Z7 ..\main.cpp
popd

11
main.c Normal file
View File

@@ -0,0 +1,11 @@
#define HANDMADE_MATH_IMPLEMENTATION
#include "HandmadeMath.h"
int
main(void)
{
v2 VecOne = Vec2(1.0f, 1.0f);
v2 VecTwo = Vec2(3.0f, 3.0f);
v2 Result = AddVec2(VecOne, VecTwo);
}