Merge pull request #37 from StrangeZak/development

Fixed various issues
This commit is contained in:
Zak Strange
2016-08-30 17:05:37 -04:00
committed by GitHub
3 changed files with 116 additions and 78 deletions

View File

@@ -1,5 +1,5 @@
/*
HandmadeMath.h v0.5.2
HandmadeMath.h v0.6
This is a single header file with a bunch of useful functions for
basic game math operations.
@@ -122,7 +122,15 @@
0.5.2
(*) Fixed SSE code in HMM_SqrtF
(*) Fixed SSE code in HMM_RSqrtF
0.6
(*) Added Unit testing
(*) Made HMM_Power faster
(*) Fixed possible efficiency problem with HMM_Normalize
(*) RENAMED HMM_LengthSquareRoot to HMM_LengthSquared
(*) RENAMED HMM_RSqrtF to HMM_RSquareRootF
(*) RENAMED HMM_SqrtF to HMM_SquareRootF
(*) REMOVED Inner function (user should use Dot now)
(*) REMOVED HMM_FastInverseSquareRoot function declaration
LICENSE
@@ -147,10 +155,6 @@
Insofaras (@insofaras)
*/
// NOTE(zak): I think this is the include for SSE 2 on
// MacOS, Windows, and Linux if im wrong just open up
// a issue and tell me, or submit a pull request
// with the fix.
#include <xmmintrin.h>
#ifndef HANDMADE_MATH_H
@@ -334,20 +338,21 @@ typedef hmm_mat4 hmm_m4;
HMMDEF float HMM_SinF(float Angle);
HMMDEF float HMM_TanF(float Angle);
HMMDEF float HMM_CosF(float Angle);
HMMDEF float HMM_SqrtF(float Angle);
HMMDEF float HMM_ToRadians(float Degrees);
HMMDEF float HMM_Inner(hmm_vec3 A, hmm_vec3 B);
HMMDEF float HMM_SquareRoot(float Float);
HMMDEF float HMM_LengthSquareRoot(hmm_vec3 A);
HMMDEF float HMM_FastInverseSquareRoot(float Number);
HMMDEF float HMM_SquareRootF(float Float);
HMMDEF float HMM_RSquareRootF(float Float);
HMMDEF float HMM_LengthSquared(hmm_vec3 A);
HMMDEF float HMM_Length(hmm_vec3 A);
HMMDEF float HMM_Power(float Base, int Exponent);
HMMDEF float HMM_Clamp(float Min, float Value, float Max);
HMMDEF hmm_vec3 HMM_Normalize(hmm_vec3 A);
HMMDEF hmm_vec3 HMM_Cross(hmm_vec3 VecOne, hmm_vec3 VecTwo);
HMMDEF float HMM_Dot(hmm_vec3 VecOne, hmm_vec3 VecTwo);
HMMDEF float HMM_DotVec2(hmm_vec2 VecOne, hmm_vec2 VecTwo);
HMMDEF float HMM_DotVec3(hmm_vec3 VecOne, hmm_vec3 VecTwo);
HMMDEF float HMM_DotVec4(hmm_vec4 VecOne, hmm_vec4 VecTwo);
HMMDEF hmm_vec2 HMM_Vec2i(int X, int Y);
HMMDEF hmm_vec2 HMM_Vec2(float X, float Y);
@@ -402,6 +407,11 @@ HMMDEF hmm_mat4 HMM_LookAt(hmm_vec3 Eye, hmm_vec3 Center, hmm_vec3 Up);
#ifdef HANDMADE_MATH_CPP_MODE
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 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);
@@ -528,7 +538,7 @@ HMM_TanF(float Radians)
}
HINLINE float
HMM_SqrtF(float Value)
HMM_SquareRootF(float Value)
{
float Result = 0;
@@ -544,7 +554,7 @@ HMM_SqrtF(float Value)
}
HINLINE float
HMM_RSqrtF(float Value)
HMM_RSquareRootF(float Value)
{
float Result = 0;
@@ -568,21 +578,42 @@ HMM_ToRadians(float Degrees)
return (Result);
}
HINLINE float
HMM_Inner(hmm_vec3 A, hmm_vec3 B)
HMM_DotVec2(hmm_vec2 VecOne, hmm_vec2 VecTwo)
{
float Result = 0;
Result = A.X * B.X + A.Y * B.Y + A.Z * B.Z;
Result = (VecOne.X * VecTwo.X) + (VecOne.Y * VecTwo.Y);
return (Result);
}
HINLINE float
HMM_LengthSquareRoot(hmm_vec3 A)
HMM_DotVec3(hmm_vec3 VecOne, hmm_vec3 VecTwo)
{
float Result = 0;
Result = HMM_Inner(A, A);
Result = (VecOne.X * VecTwo.X) + (VecOne.Y * VecTwo.Y) + (VecOne.Z * VecTwo.Z);
return (Result);
}
HINLINE float
HMM_DotVec4(hmm_vec4 VecOne, hmm_vec4 VecTwo)
{
float Result = 0;
Result = (VecOne.X * VecTwo.X) + (VecOne.Y * VecTwo.Y) + (VecOne.Z * VecTwo.Z) + (VecOne.W * VecTwo.W);
return (Result);
}
HINLINE float
HMM_LengthSquared(hmm_vec3 A)
{
float Result = 0;
Result = HMM_DotVec3(A, A);
return (Result);
}
@@ -591,7 +622,7 @@ HMM_Length(hmm_vec3 A)
{
float Result = 0;
Result = HMM_SqrtF(HMM_LengthSquareRoot(A));
Result = HMM_SquareRootF(HMM_LengthSquared(A));
return (Result);
}
@@ -646,9 +677,11 @@ HMM_Normalize(hmm_vec3 A)
{
hmm_vec3 Result = {0};
Result.X = A.X * (1.0f / HMM_Length(A));
Result.Y = A.Y * (1.0f / HMM_Length(A));
Result.Z = A.Z * (1.0f / HMM_Length(A));
float VectorLength = HMM_Length(A);
Result.X = A.X * (1.0f / VectorLength);
Result.Y = A.Y * (1.0f / VectorLength);
Result.Z = A.Z * (1.0f / VectorLength);
return (Result);
}
@@ -665,15 +698,6 @@ HMM_Cross(hmm_vec3 VecOne, hmm_vec3 VecTwo)
return (Result);
}
HINLINE float
HMM_Dot(hmm_vec3 VecOne, hmm_vec3 VecTwo)
{
float Result = 0;
Result = (VecOne.X * VecTwo.X) + (VecOne.Y * VecTwo.Y) + (VecOne.Z * VecTwo.Z);
return (Result);
}
HINLINE hmm_vec2
HMM_Vec2(float X, float Y)
{
@@ -1203,9 +1227,9 @@ HMM_LookAt(hmm_vec3 Eye, hmm_vec3 Center, hmm_vec3 Up)
Result.Elements[2][1] = U.Z;
Result.Elements[2][2] = -F.Z;
Result.Elements[3][0] = -HMM_Dot(S, Eye);
Result.Elements[3][1] = -HMM_Dot(U, Eye);
Result.Elements[3][2] = HMM_Dot(F, Eye);
Result.Elements[3][0] = -HMM_DotVec3(S, Eye);
Result.Elements[3][1] = -HMM_DotVec3(U, Eye);
Result.Elements[3][2] = HMM_DotVec3(F, Eye);
Result.Elements[3][3] = 1.0f;
return (Result);
@@ -1225,6 +1249,36 @@ HMM_Scale(hmm_vec3 Scale)
#ifdef HANDMADE_MATH_CPP_MODE
HINLINE float
HMM_Dot(hmm_vec2 VecOne, hmm_vec2 VecTwo)
{
float Result = 0;
Result = HMM_DotVec2(VecOne, VecTwo);
return(Result);
}
HINLINE float
HMM_Dot(hmm_vec3 VecOne, hmm_vec3 VecTwo)
{
float Result = 0;
Result = HMM_DotVec3(VecOne, VecTwo);
return(Result);
}
HMMDEF float
HMM_Dot(hmm_vec4 VecOne, hmm_vec4 VecTwo)
{
float Result = 0;
Result = HMM_DotVec4(VecOne, VecTwo);
return(Result);
}
HINLINE hmm_vec2
HMM_Add(hmm_vec2 Left, hmm_vec2 Right)
{

View File

@@ -1,11 +1,14 @@
# Handmade-Math
------
[![Build Status](https://travis-ci.org/StrangeZak/Handmade-Math.svg?branch=master)](https://travis-ci.org/StrangeZak/Handmade-Math)
Single-file cross-platform public domain game math library for C/C++
Version | Changes |
----------------|----------------|
**0.6** | Made HMM_Power faster, Fixed possible efficiency problem with HMM_Normalize, RENAMED HMM_LengthSquareRoot to HMM_LengthSquared, RENAMED HMM_RSqrtF to HMM_RSquareRootF, RENAMED HMM_SqrtF to HMM_SquareRootF, REMOVED Inner function (user should use Dot now), REMOVED HMM_FastInverseSquareRoot function declaration |
**0.5.2** | Fixed SSE code in HMM_SqrtF and HMM_RSqrtF |
**0.5.1** | Fixed HMM_Translate producing row-major matrices, ensured column-major order for matrices throughout |
**0.5** | Added scalar operations on vectors and matrices, added += and -= for hmm_mat4, reconciled headers and implementations, tidied up in general |
@@ -32,12 +35,3 @@ This library is in the public domain. You can do whatever you want with them.
**Where can I contact you to ask questions?**
You can email me at: Zak@Handmade.Network
## Testing
```shell
cd test
make
./hmm_test
```

View File

@@ -33,11 +33,6 @@ TEST(ScalarMath, Trigonometry)
// checking that things work by default.
}
TEST(ScalarMath, SqrtF)
{
EXPECT_FLOAT_EQ(HMM_SqrtF(16.0f), 4.0f);
}
TEST(ScalarMath, ToRadians)
{
EXPECT_FLOAT_EQ(HMM_ToRadians(0.0f), 0);
@@ -47,28 +42,12 @@ TEST(ScalarMath, ToRadians)
TEST(ScalarMath, SquareRoot)
{
// EXPECT_FLOAT_EQ(HMM_SquareRoot(16.0f), 4.0f);
FAIL() << "Bad header, function not defined. See commented line above.";
EXPECT_FLOAT_EQ(HMM_SquareRootF(16.0f), 4.0f);
}
TEST(ScalarMath, FastInverseSquareRoot)
TEST(ScalarMath, RSquareRootF)
{
// EXPECT_FLOAT_EQ(HMM_FastInverseSquareRoot(4.0f), 0.5f); // linker error, no function body
FAIL() << "Bad header, function not defined. See commented line above.";
}
TEST(ScalarMath, Power)
{
EXPECT_FLOAT_EQ(HMM_Power(2.0f, 0), 1.0f);
EXPECT_FLOAT_EQ(HMM_Power(2.0f, 4), 16.0f);
EXPECT_FLOAT_EQ(HMM_Power(2.0f, -2), 0.25f);
}
TEST(ScalarMath, Clamp)
{
EXPECT_FLOAT_EQ(HMM_Clamp(-2.0f, 0.0f, 2.0f), 0.0f);
EXPECT_FLOAT_EQ(HMM_Clamp(-2.0f, -3.0f, 2.0f), -2.0f);
EXPECT_FLOAT_EQ(HMM_Clamp(-2.0f, 3.0f, 2.0f), 2.0f);
EXPECT_FLOAT_EQ(HMM_RSquareRootF(10.0f), 0.31616211f);
}
TEST(Initialization, Vectors)
@@ -150,19 +129,11 @@ TEST(Initialization, MatrixDiagonal)
}
}
TEST(VectorOps, Inner)
{
hmm_vec3 v1 = HMM_Vec3(1.0f, 2.0f, 3.0f);
hmm_vec3 v2 = HMM_Vec3(4.0f, 5.0f, 6.0f);
EXPECT_FLOAT_EQ(HMM_Inner(v1, v2), 32.0f);
}
TEST(VectorOps, LengthSquareRoot)
{
hmm_vec3 v = HMM_Vec3(1.0f, -2.0f, 3.0f);
EXPECT_FLOAT_EQ(HMM_LengthSquareRoot(v), 14.0f);
EXPECT_FLOAT_EQ(HMM_LengthSquared(v), 14.0f);
}
TEST(VectorOps, Length)
@@ -194,12 +165,31 @@ TEST(VectorOps, Cross)
EXPECT_FLOAT_EQ(result.Z, -3.0f);
}
TEST(VectorOps, Dot)
TEST(VectorOps, DotVec2)
{
hmm_vec2 v1 = HMM_Vec2(1.0f, 2.0f);
hmm_vec2 v2 = HMM_Vec2(3.0f, 4.0f);
EXPECT_FLOAT_EQ(HMM_DotVec2(v1, v2), 11.0f);
EXPECT_FLOAT_EQ(HMM_Dot(v1, v2), 11.0f);
}
TEST(VectorOps, DotVec3)
{
hmm_vec3 v1 = HMM_Vec3(1.0f, 2.0f, 3.0f);
hmm_vec3 v2 = HMM_Vec3(4.0f, 5.0f, 6.0f);
EXPECT_FLOAT_EQ(HMM_Inner(v1, v2), 32.0f);
EXPECT_FLOAT_EQ(HMM_DotVec3(v1, v2), 32.0f);
EXPECT_FLOAT_EQ(HMM_Dot(v1, v2), 32.0f);
}
TEST(VectorOps, DotVec4)
{
hmm_vec4 v1 = HMM_Vec4(1.0f, 2.0f, 3.0f, 4.0f);
hmm_vec4 v2 = HMM_Vec4(5.0f, 6.0f, 7.0f, 8.0f);
EXPECT_FLOAT_EQ(HMM_DotVec4(v1, v2), 70.0f);
EXPECT_FLOAT_EQ(HMM_Dot(v1, v2), 70.0f);
}
TEST(Addition, Vec2)