Use vertical instead of horizontal FOV in HMM_Perspective (#101)

* Use vertical instead of horizontal FOV

* Update readme

* Fix tests
This commit is contained in:
Ben Visness
2019-07-10 11:29:51 -05:00
committed by GitHub
parent 45c91702a9
commit 93e56be543
3 changed files with 10 additions and 7 deletions

View File

@@ -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);

View File

@@ -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.

View File

@@ -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);
}