mirror of
https://github.com/HandmadeMath/HandmadeMath.git
synced 2025-12-29 08:04:31 +00:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9a913ac95b | ||
|
|
43afc87fa7 | ||
|
|
5bbac2167e | ||
|
|
655c662528 | ||
|
|
1f0c6ba493 | ||
|
|
1d82b4f0bc | ||
|
|
2fa0b36715 | ||
|
|
ad169e649c |
@@ -1,6 +1,8 @@
|
||||
# Understanding the structure of Handmade Math
|
||||
|
||||
Most of the functions in Handmade Math are very short, and all are the kind of functions you want to be easily inlined for performance. Because of this, all functions in Handmade Math are defined with `HMM_INLINE`, which is defined as `static inline`.
|
||||
Most of the functions in Handmade Math are very short, and are the kind of functions you want to have inlined. Because of this, most functions in Handmade Math are defined with `HINLINE`, which is defined as `static inline`.
|
||||
|
||||
The exceptions are functions like `HMM_Rotate`, which are long enough that it doesn't make sense to inline them. These functions are defined with an `HEXTERN` prototype, and implemented in the `#ifdef HANDMADE_MATH_IMPLEMENTATION` block.
|
||||
|
||||
# Quick style guide
|
||||
|
||||
@@ -12,7 +14,7 @@ Most of the functions in Handmade Math are very short, and all are the kind of f
|
||||
0.5f;
|
||||
1.0f;
|
||||
3.14159f;
|
||||
|
||||
|
||||
// Bad
|
||||
1.f
|
||||
.0f
|
||||
|
||||
967
HandmadeMath.h
967
HandmadeMath.h
File diff suppressed because it is too large
Load Diff
@@ -10,6 +10,8 @@ To get started, go download [the latest release](https://github.com/HandmadeMath
|
||||
|
||||
Version | Changes |
|
||||
----------------|----------------|
|
||||
**1.12.1** | Added extra parentheses around some macros |
|
||||
**1.12.0** | Added Unary Minus operator for `HMM_Vec2`, `HMM_Vec3`, and `HMM_Vec4`. |
|
||||
**1.11.1** | Added HMM_PREFIX macro to a few functions that were missing it. |
|
||||
**1.11.0** | Added ability to customize or remove the default `HMM_` prefix on function names by defining a macro called `HMM_PREFIX(name)`. |
|
||||
**1.10.1** | Removed stdint.h, this doesn't exist on some really old compilers and we didn't really use it anyways. |
|
||||
|
||||
@@ -2,4 +2,6 @@
|
||||
#include "HandmadeTest.h"
|
||||
#endif
|
||||
|
||||
#define HANDMADE_MATH_IMPLEMENTATION
|
||||
#define HANDMADE_MATH_NO_INLINE
|
||||
#include "../HandmadeMath.h"
|
||||
|
||||
@@ -58,6 +58,7 @@
|
||||
#define HANDMADETEST_H
|
||||
|
||||
#include <float.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -152,7 +153,7 @@ INITIALIZER(_HMT_COVERCASE_FUNCNAME_INIT(name)) { \
|
||||
_HMT_CASE_START(); \
|
||||
float actual = (_actual); \
|
||||
float diff = actual - (_expected); \
|
||||
if (diff < -FLT_EPSILON || FLT_EPSILON < diff) { \
|
||||
if (isnan(actual) || diff < -FLT_EPSILON || FLT_EPSILON < diff) { \
|
||||
_HMT_CASE_FAIL(); \
|
||||
printf("Expected %f, got %f", (_expected), actual); \
|
||||
} \
|
||||
@@ -162,7 +163,7 @@ INITIALIZER(_HMT_COVERCASE_FUNCNAME_INIT(name)) { \
|
||||
_HMT_CASE_START(); \
|
||||
float actual = (_actual); \
|
||||
float diff = actual - (_expected); \
|
||||
if (diff < -(_epsilon) || (_epsilon) < diff) { \
|
||||
if (isnan(actual) || diff < -(_epsilon) || (_epsilon) < diff) { \
|
||||
_HMT_CASE_FAIL(); \
|
||||
printf("Expected %f, got %f", (_expected), actual); \
|
||||
} \
|
||||
|
||||
@@ -15,7 +15,7 @@ TEST(Projection, Orthographic)
|
||||
|
||||
TEST(Projection, Perspective)
|
||||
{
|
||||
hmm_mat4 projection = HMM_Perspective(HMM_ToRadians(90.0f), 2.0f, 5.0f, 15.0f);
|
||||
hmm_mat4 projection = HMM_Perspective(90.0f, 2.0f, 5.0f, 15.0f);
|
||||
|
||||
{
|
||||
hmm_vec3 original = HMM_Vec3(5.0f, 5.0f, -15.0f);
|
||||
|
||||
@@ -66,14 +66,39 @@ TEST(QuaternionOps, NLerp)
|
||||
|
||||
TEST(QuaternionOps, Slerp)
|
||||
{
|
||||
hmm_quaternion from = HMM_Quaternion(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
hmm_quaternion to = HMM_Quaternion(0.5f, 0.5f, -0.5f, 0.5f);
|
||||
// Normal operation
|
||||
{
|
||||
hmm_quaternion from = HMM_Quaternion(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
hmm_quaternion to = HMM_Quaternion(0.5f, 0.5f, -0.5f, 0.5f);
|
||||
|
||||
hmm_quaternion result = HMM_Slerp(from, 0.5f, to);
|
||||
EXPECT_FLOAT_EQ(result.X, 0.28867513f);
|
||||
EXPECT_FLOAT_EQ(result.Y, 0.28867513f);
|
||||
EXPECT_FLOAT_EQ(result.Z, -0.28867513f);
|
||||
EXPECT_FLOAT_EQ(result.W, 0.86602540f);
|
||||
hmm_quaternion result = HMM_Slerp(from, 0.5f, to);
|
||||
EXPECT_FLOAT_EQ(result.X, 0.28867513f);
|
||||
EXPECT_FLOAT_EQ(result.Y, 0.28867513f);
|
||||
EXPECT_FLOAT_EQ(result.Z, -0.28867513f);
|
||||
EXPECT_FLOAT_EQ(result.W, 0.86602540f);
|
||||
}
|
||||
|
||||
// Same quat twice
|
||||
{
|
||||
hmm_quaternion q = HMM_Quaternion(0.5f, 0.5f, 0.5f, 1.0f);
|
||||
|
||||
hmm_quaternion result = HMM_Slerp(q, 0.5f, q);
|
||||
EXPECT_FLOAT_EQ(result.X, 0.5f);
|
||||
EXPECT_FLOAT_EQ(result.Y, 0.5f);
|
||||
EXPECT_FLOAT_EQ(result.Z, 0.5f);
|
||||
EXPECT_FLOAT_EQ(result.W, 1.0f);
|
||||
}
|
||||
|
||||
// Identity quat twice
|
||||
{
|
||||
hmm_quaternion q = HMM_Quaternion(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
|
||||
hmm_quaternion result = HMM_Slerp(q, 0.5f, q);
|
||||
EXPECT_FLOAT_EQ(result.X, 0.0f);
|
||||
EXPECT_FLOAT_EQ(result.Y, 1.0f);
|
||||
EXPECT_FLOAT_EQ(result.Z, 0.0f);
|
||||
EXPECT_FLOAT_EQ(result.W, 1.0f);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(QuaternionOps, QuatToMat4)
|
||||
@@ -111,7 +136,7 @@ TEST(QuaternionOps, Mat4ToQuat)
|
||||
|
||||
// Rotate 90 degrees on the X axis
|
||||
{
|
||||
hmm_mat4 m = HMM_Rotate(HMM_ToRadians(90.0f), HMM_Vec3(1, 0, 0));
|
||||
hmm_mat4 m = HMM_Rotate(90, HMM_Vec3(1, 0, 0));
|
||||
hmm_quaternion result = HMM_Mat4ToQuaternion(m);
|
||||
|
||||
float cosf = 0.707107f; // cos(90/2 degrees)
|
||||
@@ -125,7 +150,7 @@ TEST(QuaternionOps, Mat4ToQuat)
|
||||
|
||||
// Rotate 90 degrees on the Y axis (axis not normalized, just for fun)
|
||||
{
|
||||
hmm_mat4 m = HMM_Rotate(HMM_ToRadians(90.0f), HMM_Vec3(0, 2, 0));
|
||||
hmm_mat4 m = HMM_Rotate(90, HMM_Vec3(0, 2, 0));
|
||||
hmm_quaternion result = HMM_Mat4ToQuaternion(m);
|
||||
|
||||
float cosf = 0.707107f; // cos(90/2 degrees)
|
||||
@@ -139,7 +164,7 @@ TEST(QuaternionOps, Mat4ToQuat)
|
||||
|
||||
// Rotate 90 degrees on the Z axis
|
||||
{
|
||||
hmm_mat4 m = HMM_Rotate(HMM_ToRadians(90.0f), HMM_Vec3(0, 0, 1));
|
||||
hmm_mat4 m = HMM_Rotate(90, HMM_Vec3(0, 0, 1));
|
||||
hmm_quaternion result = HMM_Mat4ToQuaternion(m);
|
||||
|
||||
float cosf = 0.707107f; // cos(90/2 degrees)
|
||||
@@ -153,7 +178,7 @@ TEST(QuaternionOps, Mat4ToQuat)
|
||||
|
||||
// Rotate 45 degrees on the X axis (this hits case 4)
|
||||
{
|
||||
hmm_mat4 m = HMM_Rotate(HMM_ToRadians(45.0f), HMM_Vec3(1, 0, 0));
|
||||
hmm_mat4 m = HMM_Rotate(45, HMM_Vec3(1, 0, 0));
|
||||
hmm_quaternion result = HMM_Mat4ToQuaternion(m);
|
||||
|
||||
float cosf = 0.9238795325f; // cos(90/2 degrees)
|
||||
|
||||
@@ -36,13 +36,6 @@ TEST(ScalarMath, Trigonometry)
|
||||
// checking that things work by default.
|
||||
}
|
||||
|
||||
TEST(ScalarMath, ToDegrees)
|
||||
{
|
||||
EXPECT_FLOAT_EQ(HMM_ToDegrees(0.0f), 0.0f);
|
||||
EXPECT_FLOAT_EQ(HMM_ToDegrees(HMM_PI32), 180.0f);
|
||||
EXPECT_FLOAT_EQ(HMM_ToDegrees(-HMM_PI32), -180.0f);
|
||||
}
|
||||
|
||||
TEST(ScalarMath, ToRadians)
|
||||
{
|
||||
EXPECT_FLOAT_EQ(HMM_ToRadians(0.0f), 0.0f);
|
||||
|
||||
@@ -199,3 +199,32 @@ TEST(Subtraction, Quaternion)
|
||||
EXPECT_FLOAT_EQ(q1.W, -4.0f);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
TEST(UnaryMinus, Vec2)
|
||||
{
|
||||
hmm_vec2 VectorOne = {1.0f, 2.0f};
|
||||
hmm_vec2 Result = -VectorOne;
|
||||
EXPECT_FLOAT_EQ(Result.X, -1.0f);
|
||||
EXPECT_FLOAT_EQ(Result.Y, -2.0f);
|
||||
}
|
||||
|
||||
TEST(UnaryMinus, Vec3)
|
||||
{
|
||||
hmm_vec3 VectorOne = {1.0f, 2.0f, 3.0f};
|
||||
hmm_vec3 Result = -VectorOne;
|
||||
EXPECT_FLOAT_EQ(Result.X, -1.0f);
|
||||
EXPECT_FLOAT_EQ(Result.Y, -2.0f);
|
||||
EXPECT_FLOAT_EQ(Result.Z, -3.0f);
|
||||
}
|
||||
|
||||
TEST(UnaryMinus, Vec4)
|
||||
{
|
||||
hmm_vec4 VectorOne = {1.0f, 2.0f, 3.0f, 4.0f};
|
||||
hmm_vec4 Result = -VectorOne;
|
||||
EXPECT_FLOAT_EQ(Result.X, -1.0f);
|
||||
EXPECT_FLOAT_EQ(Result.Y, -2.0f);
|
||||
EXPECT_FLOAT_EQ(Result.Z, -3.0f);
|
||||
EXPECT_FLOAT_EQ(Result.W, -4.0f);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -17,23 +17,21 @@ TEST(Transformations, Rotate)
|
||||
{
|
||||
hmm_vec3 original = HMM_Vec3(1.0f, 1.0f, 1.0f);
|
||||
|
||||
float angle = HMM_ToRadians(90.0f);
|
||||
|
||||
hmm_mat4 rotateX = HMM_Rotate(angle, HMM_Vec3(1, 0, 0));
|
||||
hmm_mat4 rotateX = HMM_Rotate(90, HMM_Vec3(1, 0, 0));
|
||||
hmm_vec4 rotatedX = HMM_MultiplyMat4ByVec4(rotateX, HMM_Vec4v(original, 1));
|
||||
EXPECT_FLOAT_EQ(rotatedX.X, 1.0f);
|
||||
EXPECT_FLOAT_EQ(rotatedX.Y, -1.0f);
|
||||
EXPECT_FLOAT_EQ(rotatedX.Z, 1.0f);
|
||||
EXPECT_FLOAT_EQ(rotatedX.W, 1.0f);
|
||||
|
||||
hmm_mat4 rotateY = HMM_Rotate(angle, HMM_Vec3(0, 1, 0));
|
||||
hmm_mat4 rotateY = HMM_Rotate(90, HMM_Vec3(0, 1, 0));
|
||||
hmm_vec4 rotatedY = HMM_MultiplyMat4ByVec4(rotateY, HMM_Vec4v(original, 1));
|
||||
EXPECT_FLOAT_EQ(rotatedY.X, 1.0f);
|
||||
EXPECT_FLOAT_EQ(rotatedY.Y, 1.0f);
|
||||
EXPECT_FLOAT_EQ(rotatedY.Z, -1.0f);
|
||||
EXPECT_FLOAT_EQ(rotatedY.W, 1.0f);
|
||||
|
||||
hmm_mat4 rotateZ = HMM_Rotate(angle, HMM_Vec3(0, 0, 1));
|
||||
hmm_mat4 rotateZ = HMM_Rotate(90, HMM_Vec3(0, 0, 1));
|
||||
hmm_vec4 rotatedZ = HMM_MultiplyMat4ByVec4(rotateZ, HMM_Vec4v(original, 1));
|
||||
EXPECT_FLOAT_EQ(rotatedZ.X, -1.0f);
|
||||
EXPECT_FLOAT_EQ(rotatedZ.Y, 1.0f);
|
||||
|
||||
@@ -12,16 +12,16 @@ if "%1%"=="travis" (
|
||||
if not exist "build" mkdir build
|
||||
pushd build
|
||||
|
||||
cl /Fehmm_test_c.exe ..\HandmadeMath.c ..\hmm_test.c || popd && exit /B
|
||||
cl /Fehmm_test_c.exe ..\HandmadeMath.c ..\hmm_test.c
|
||||
hmm_test_c
|
||||
|
||||
cl /Fehmm_test_c_no_sse.exe /DHANDMADE_MATH_NO_SSE ..\HandmadeMath.c ..\hmm_test.c || popd && exit /B
|
||||
cl /Fehmm_test_c_no_sse.exe /DHANDMADE_MATH_NO_SSE ..\HandmadeMath.c ..\hmm_test.c
|
||||
hmm_test_c_no_sse
|
||||
|
||||
cl /Fehmm_test_cpp.exe ..\HandmadeMath.cpp ..\hmm_test.cpp || popd && exit /B
|
||||
cl /Fehmm_test_cpp.exe ..\HandmadeMath.cpp ..\hmm_test.cpp
|
||||
hmm_test_cpp
|
||||
|
||||
cl /Fehmm_test_cpp_no_sse.exe /DHANDMADE_MATH_NO_SSE ..\HandmadeMath.cpp ..\hmm_test.cpp || popd && exit /B
|
||||
cl /Fehmm_test_cpp_no_sse.exe /DHANDMADE_MATH_NO_SSE ..\HandmadeMath.cpp ..\hmm_test.cpp
|
||||
hmm_test_cpp_no_sse
|
||||
|
||||
popd
|
||||
|
||||
Reference in New Issue
Block a user