Skip to content

Commit

Permalink
Use vertical instead of horizontal FOV in HMM_Perspective (HandmadeMa…
Browse files Browse the repository at this point in the history
…th#101)

* Use vertical instead of horizontal FOV

* Update readme

* Fix tests
  • Loading branch information
bvisness authored Jul 10, 2019
1 parent 45c9170 commit 93e56be
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
8 changes: 5 additions & 3 deletions HandmadeMath.h
Original file line number Diff line number Diff line change
Expand Up @@ -1186,10 +1186,12 @@ HMM_INLINE hmm_mat4 HMM_Perspective(float FOV, float AspectRatio, float Near, fl
{
hmm_mat4 Result = HMM_Mat4();

float TanThetaOver2 = HMM_TanF(FOV * (HMM_PI32 / 360.0f));
// See https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml

float Cotangent = 1.0f / HMM_TanF(FOV * (HMM_PI32 / 360.0f));

Result.Elements[0][0] = 1.0f / TanThetaOver2;
Result.Elements[1][1] = AspectRatio / TanThetaOver2;
Result.Elements[0][0] = Cotangent / AspectRatio;
Result.Elements[1][1] = Cotangent;
Result.Elements[2][3] = -1.0f;
Result.Elements[2][2] = (Near + Far) / (Near - Far);
Result.Elements[3][2] = (2.0f * Near * Far) / (Near - Far);
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ To get started, go download [the latest release](https://github.com/HandmadeMath

Version | Changes |
----------------|----------------|
**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.
**1.7.1** | Changed operator[] to take a const ref int instead of an int.
Expand Down
8 changes: 4 additions & 4 deletions test/categories/Projection.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ TEST(Projection, Perspective)
{
hmm_vec3 original = HMM_Vec3(5.0f, 5.0f, -15.0f);
hmm_vec4 projected = HMM_MultiplyMat4ByVec4(projection, HMM_Vec4v(original, 1));
EXPECT_FLOAT_EQ(projected.X, 5.0f);
EXPECT_FLOAT_EQ(projected.Y, 10.0f);
EXPECT_FLOAT_EQ(projected.X, 2.5f);
EXPECT_FLOAT_EQ(projected.Y, 5.0f);
EXPECT_FLOAT_EQ(projected.Z, 15.0f);
EXPECT_FLOAT_EQ(projected.W, 15.0f);
}
{
hmm_vec3 original = HMM_Vec3(5.0f, 5.0f, -5.0f);
hmm_vec4 projected = HMM_MultiplyMat4ByVec4(projection, HMM_Vec4v(original, 1));
EXPECT_FLOAT_EQ(projected.X, 5.0f);
EXPECT_FLOAT_EQ(projected.Y, 10.0f);
EXPECT_FLOAT_EQ(projected.X, 2.5f);
EXPECT_FLOAT_EQ(projected.Y, 5.0f);
EXPECT_FLOAT_EQ(projected.Z, -5.0f);
EXPECT_FLOAT_EQ(projected.W, 5.0f);
}
Expand Down

0 comments on commit 93e56be

Please sign in to comment.