Browse Source

Move method for perspective projection matrix to Mat44::sPerspective (#859)

Marvin Friedrich 1 year ago
parent
commit
935e43de34
3 changed files with 14 additions and 12 deletions
  1. 3 0
      Jolt/Math/Mat44.h
  2. 9 0
      Jolt/Math/Mat44.inl
  3. 2 12
      TestFramework/Renderer/Renderer.cpp

+ 3 - 0
Jolt/Math/Mat44.h

@@ -87,6 +87,9 @@ public:
 	/// @param inUp Up vector
 	static JPH_INLINE Mat44		sLookAt(Vec3Arg inPos, Vec3Arg inTarget, Vec3Arg inUp);
 
+	/// Returns a right-handed perspective projection matrix
+	static JPH_INLINE Mat44		sPerspective(float inFovY, float inAspect, float inNear, float inFar);
+
 	/// Get float component by element index
 	JPH_INLINE float			operator () (uint inRow, uint inColumn) const			{ JPH_ASSERT(inRow < 4); JPH_ASSERT(inColumn < 4); return mCol[inColumn].mF32[inRow]; }
 	JPH_INLINE float &			operator () (uint inRow, uint inColumn)					{ JPH_ASSERT(inRow < 4); JPH_ASSERT(inColumn < 4); return mCol[inColumn].mF32[inRow]; }

+ 9 - 0
Jolt/Math/Mat44.inl

@@ -213,6 +213,15 @@ Mat44 Mat44::sLookAt(Vec3Arg inPos, Vec3Arg inTarget, Vec3Arg inUp)
 	return Mat44(Vec4(right, 0), Vec4(up, 0), Vec4(-direction, 0), Vec4(inPos, 1)).InversedRotationTranslation();
 }
 
+Mat44 Mat44::sPerspective(float inFovY, float inAspect, float inNear, float inFar)
+{
+	float height = 1.0f / Tan(0.5f * inFovY);
+	float width = height / inAspect;
+	float range = inFar / (inNear - inFar);
+
+	return Mat44(Vec4(width, 0.0f, 0.0f, 0.0f), Vec4(0.0f, height, 0.0f, 0.0f), Vec4(0.0f, 0.0f, range, -1.0f), Vec4(0.0f, 0.0f, range * inNear, 0.0f));
+}
+
 bool Mat44::operator == (Mat44Arg inM2) const
 {
 	return UVec4::sAnd(

+ 2 - 12
TestFramework/Renderer/Renderer.cpp

@@ -438,16 +438,6 @@ void Renderer::OnWindowResize()
 	CreateDepthBuffer();
 }
 
-/// Construct a perspective matrix
-static inline Mat44 sPerspective(float inFovY, float inAspect, float inNear, float inFar)
-{
-    float height = 1.0f / Tan(0.5f * inFovY);
-    float width = height / inAspect;
-    float range = inFar / (inNear - inFar);
-
-    return Mat44(Vec4(width, 0.0f, 0.0f, 0.0f), Vec4(0.0f, height, 0.0f, 0.0f), Vec4(0.0f, 0.0f, range, -1.0f), Vec4(0.0f, 0.0f, range * inNear, 0.0f));
-}
-
 void Renderer::BeginFrame(const CameraState &inCamera, float inWorldScale)
 {
 	JPH_PROFILE_FUNCTION();
@@ -506,13 +496,13 @@ void Renderer::BeginFrame(const CameraState &inCamera, float inWorldScale)
 	VertexShaderConstantBuffer *vs = mVertexShaderConstantBufferProjection[mFrameIndex]->Map<VertexShaderConstantBuffer>();
 
 	// Camera projection and view
-	vs->mProjection = sPerspective(camera_fovy, camera_aspect, camera_near, camera_far);
+	vs->mProjection = Mat44::sPerspective(camera_fovy, camera_aspect, camera_near, camera_far);
 	Vec3 cam_pos = Vec3(inCamera.mPos - mBaseOffset);
 	Vec3 tgt = cam_pos + inCamera.mForward;
 	vs->mView = Mat44::sLookAt(cam_pos, tgt, inCamera.mUp);
 
 	// Light projection and view
-	vs->mLightProjection = sPerspective(light_fov, 1.0f, light_near, light_far);
+	vs->mLightProjection = Mat44::sPerspective(light_fov, 1.0f, light_near, light_far);
 	vs->mLightView = Mat44::sLookAt(light_pos, light_tgt, light_up);
 
 	mVertexShaderConstantBufferProjection[mFrameIndex]->Unmap();