mirror of
https://github.com/HandmadeMath/HandmadeMath.git
synced 2025-12-28 23:54:32 +00:00
Compare commits
9 Commits
general_ma
...
2-dev
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0f03d4ffb9 | ||
|
|
1900cc9275 | ||
|
|
ddb9971e71 | ||
|
|
341a376a17 | ||
|
|
d3d09f8352 | ||
|
|
c825fe48cf | ||
|
|
15bef820db | ||
|
|
68d2af495c | ||
|
|
fe32f081f2 |
@@ -1,8 +1,6 @@
|
||||
# Understanding the structure of Handmade Math
|
||||
|
||||
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.
|
||||
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`.
|
||||
|
||||
# Quick style guide
|
||||
|
||||
@@ -14,7 +12,7 @@ The exceptions are functions like `HMM_Rotate`, which are long enough that it do
|
||||
0.5f;
|
||||
1.0f;
|
||||
3.14159f;
|
||||
|
||||
|
||||
// Bad
|
||||
1.f
|
||||
.0f
|
||||
|
||||
1481
HandmadeMath.h
1481
HandmadeMath.h
File diff suppressed because it is too large
Load Diff
@@ -10,8 +10,9 @@ To get started, go download [the latest release](https://github.com/HandmadeMath
|
||||
|
||||
Version | Changes |
|
||||
----------------|----------------|
|
||||
**1.10.2** | Introduced safe floating point comparison in HMM_EqualsVec2, HMM_EqualsVec3, HMM_EqualsVec4. SIMD'd HMM_EqualsVec4 comparison |
|
||||
**1.10.1** | Removed use of stdint.h, this doesn't exist on some really old compilers and we didn't really use it anyways. |
|
||||
**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. |
|
||||
**1.10.0** | Made HMM_Perspective use vertical FOV instead of horizontal FOV for consistency with other graphics APIs. |
|
||||
**1.9.0** | Added SSE versions of quaternion operations. |
|
||||
**1.8.0** | Added fast vector normalization routines that use fast inverse square roots.
|
||||
|
||||
@@ -2,6 +2,4 @@
|
||||
#include "HandmadeTest.h"
|
||||
#endif
|
||||
|
||||
#define HANDMADE_MATH_IMPLEMENTATION
|
||||
#define HANDMADE_MATH_NO_INLINE
|
||||
#include "../HandmadeMath.h"
|
||||
|
||||
12
test/HandmadeMathDifferentPrefix.cpp
Normal file
12
test/HandmadeMathDifferentPrefix.cpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#define HMM_PREFIX(name) WOW_##name
|
||||
|
||||
#define HANDMADE_MATH_IMPLEMENTATION
|
||||
#define HANDMADE_MATH_NO_INLINE
|
||||
#include "../HandmadeMath.h"
|
||||
|
||||
int main() {
|
||||
hmm_vec4 a = WOW_Vec4(1, 2, 3, 4);
|
||||
hmm_vec4 b = WOW_Vec4(5, 6, 7, 8);
|
||||
|
||||
WOW_Add(a, b);
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
BUILD_DIR=./build
|
||||
|
||||
CXXFLAGS+=-g -Wall -Wextra -pthread -Wno-missing-braces -Wno-missing-field-initializers
|
||||
CXXFLAGS+=-g -Wall -Wextra -pthread -Wno-missing-braces -Wno-missing-field-initializers -Wfloat-equal
|
||||
|
||||
all: c c_no_sse cpp cpp_no_sse build_c_without_coverage build_cpp_without_coverage
|
||||
all: c c_no_sse cpp cpp_no_sse build_c_without_coverage build_cpp_without_coverage build_cpp_different_prefix
|
||||
|
||||
build_all: build_c build_c_no_sse build_cpp build_cpp_no_sse
|
||||
|
||||
@@ -58,7 +58,7 @@ build_cpp_no_sse: HandmadeMath.cpp test_impl
|
||||
|
||||
test_impl: hmm_test.cpp hmm_test.c
|
||||
|
||||
build_c_without_coverage: HandmadeMath.c
|
||||
build_c_without_coverage: HandmadeMath.c test_impl
|
||||
@echo "\nCompiling in C mode"
|
||||
mkdir -p $(BUILD_DIR)
|
||||
cd $(BUILD_DIR)\
|
||||
@@ -75,3 +75,11 @@ build_cpp_without_coverage: HandmadeMath.cpp test_impl
|
||||
&& $(CXX) $(CPPFLAGS) $(CXXFLAGS) -ohmm_test_cpp_no_sse \
|
||||
-DHANDMADE_MATH_CPP_MODE -DWITHOUT_COVERAGE \
|
||||
../HandmadeMath.cpp ../hmm_test.cpp
|
||||
|
||||
build_cpp_different_prefix: HandmadeMath.cpp
|
||||
@echo "\nCompiling C++ with different prefix"
|
||||
mkdir -p $(BUILD_DIR)
|
||||
cd $(BUILD_DIR) \
|
||||
&& $(CXX) $(CPPFLAGS) $(CXXFLAGS) -ohmm_test_cpp_different_prefix \
|
||||
-DHANDMADE_MATH_CPP_MODE -DDIFFERENT_PREFIX \
|
||||
../HandmadeMathDifferentPrefix.cpp
|
||||
|
||||
@@ -15,7 +15,7 @@ TEST(Projection, Orthographic)
|
||||
|
||||
TEST(Projection, Perspective)
|
||||
{
|
||||
hmm_mat4 projection = HMM_Perspective(90.0f, 2.0f, 5.0f, 15.0f);
|
||||
hmm_mat4 projection = HMM_Perspective(HMM_ToRadians(90.0f), 2.0f, 5.0f, 15.0f);
|
||||
|
||||
{
|
||||
hmm_vec3 original = HMM_Vec3(5.0f, 5.0f, -15.0f);
|
||||
|
||||
@@ -111,7 +111,7 @@ TEST(QuaternionOps, Mat4ToQuat)
|
||||
|
||||
// Rotate 90 degrees on the X axis
|
||||
{
|
||||
hmm_mat4 m = HMM_Rotate(90, HMM_Vec3(1, 0, 0));
|
||||
hmm_mat4 m = HMM_Rotate(HMM_ToRadians(90.0f), HMM_Vec3(1, 0, 0));
|
||||
hmm_quaternion result = HMM_Mat4ToQuaternion(m);
|
||||
|
||||
float cosf = 0.707107f; // cos(90/2 degrees)
|
||||
@@ -125,7 +125,7 @@ TEST(QuaternionOps, Mat4ToQuat)
|
||||
|
||||
// Rotate 90 degrees on the Y axis (axis not normalized, just for fun)
|
||||
{
|
||||
hmm_mat4 m = HMM_Rotate(90, HMM_Vec3(0, 2, 0));
|
||||
hmm_mat4 m = HMM_Rotate(HMM_ToRadians(90.0f), HMM_Vec3(0, 2, 0));
|
||||
hmm_quaternion result = HMM_Mat4ToQuaternion(m);
|
||||
|
||||
float cosf = 0.707107f; // cos(90/2 degrees)
|
||||
@@ -139,7 +139,7 @@ TEST(QuaternionOps, Mat4ToQuat)
|
||||
|
||||
// Rotate 90 degrees on the Z axis
|
||||
{
|
||||
hmm_mat4 m = HMM_Rotate(90, HMM_Vec3(0, 0, 1));
|
||||
hmm_mat4 m = HMM_Rotate(HMM_ToRadians(90.0f), HMM_Vec3(0, 0, 1));
|
||||
hmm_quaternion result = HMM_Mat4ToQuaternion(m);
|
||||
|
||||
float cosf = 0.707107f; // cos(90/2 degrees)
|
||||
@@ -153,7 +153,7 @@ TEST(QuaternionOps, Mat4ToQuat)
|
||||
|
||||
// Rotate 45 degrees on the X axis (this hits case 4)
|
||||
{
|
||||
hmm_mat4 m = HMM_Rotate(45, HMM_Vec3(1, 0, 0));
|
||||
hmm_mat4 m = HMM_Rotate(HMM_ToRadians(45.0f), HMM_Vec3(1, 0, 0));
|
||||
hmm_quaternion result = HMM_Mat4ToQuaternion(m);
|
||||
|
||||
float cosf = 0.9238795325f; // cos(90/2 degrees)
|
||||
|
||||
@@ -36,6 +36,13 @@ 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);
|
||||
@@ -74,9 +81,9 @@ TEST(ScalarMath, Power)
|
||||
|
||||
TEST(ScalarMath, PowerF)
|
||||
{
|
||||
EXPECT_FLOAT_EQ(HMM_PowerF(2.0f, 0), 1.0f);
|
||||
EXPECT_NEAR(HMM_PowerF(2.0f, 4.1), 17.148376f, 0.0001f);
|
||||
EXPECT_NEAR(HMM_PowerF(2.0f, -2.5), 0.176777f, 0.0001f);
|
||||
EXPECT_FLOAT_EQ(HMM_PowerF(2.0f, 0.0f), 1.0f);
|
||||
EXPECT_NEAR(HMM_PowerF(2.0f, 4.1f), 17.148376f, 0.0001f);
|
||||
EXPECT_NEAR(HMM_PowerF(2.0f, -2.5f), 0.176777f, 0.0001f);
|
||||
}
|
||||
|
||||
TEST(ScalarMath, Lerp)
|
||||
|
||||
@@ -17,21 +17,23 @@ TEST(Transformations, Rotate)
|
||||
{
|
||||
hmm_vec3 original = HMM_Vec3(1.0f, 1.0f, 1.0f);
|
||||
|
||||
hmm_mat4 rotateX = HMM_Rotate(90, HMM_Vec3(1, 0, 0));
|
||||
float angle = HMM_ToRadians(90.0f);
|
||||
|
||||
hmm_mat4 rotateX = HMM_Rotate(angle, 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(90, HMM_Vec3(0, 1, 0));
|
||||
hmm_mat4 rotateY = HMM_Rotate(angle, 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(90, HMM_Vec3(0, 0, 1));
|
||||
hmm_mat4 rotateZ = HMM_Rotate(angle, 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
|
||||
cl /Fehmm_test_c.exe ..\HandmadeMath.c ..\hmm_test.c || popd && exit /B
|
||||
hmm_test_c
|
||||
|
||||
cl /Fehmm_test_c_no_sse.exe /DHANDMADE_MATH_NO_SSE ..\HandmadeMath.c ..\hmm_test.c
|
||||
cl /Fehmm_test_c_no_sse.exe /DHANDMADE_MATH_NO_SSE ..\HandmadeMath.c ..\hmm_test.c || popd && exit /B
|
||||
hmm_test_c_no_sse
|
||||
|
||||
cl /Fehmm_test_cpp.exe ..\HandmadeMath.cpp ..\hmm_test.cpp
|
||||
cl /Fehmm_test_cpp.exe ..\HandmadeMath.cpp ..\hmm_test.cpp || popd && exit /B
|
||||
hmm_test_cpp
|
||||
|
||||
cl /Fehmm_test_cpp_no_sse.exe /DHANDMADE_MATH_NO_SSE ..\HandmadeMath.cpp ..\hmm_test.cpp
|
||||
cl /Fehmm_test_cpp_no_sse.exe /DHANDMADE_MATH_NO_SSE ..\HandmadeMath.cpp ..\hmm_test.cpp || popd && exit /B
|
||||
hmm_test_cpp_no_sse
|
||||
|
||||
popd
|
||||
|
||||
Reference in New Issue
Block a user