fixed bug in perspective and lookat

This commit is contained in:
hyzboy 2024-03-15 00:52:25 +08:00
parent a29a35ecea
commit 430a2f67e0
2 changed files with 17 additions and 12 deletions

View File

@ -55,17 +55,19 @@ namespace hgl
/** /**
* *
*/ */
inline constexpr double deg2rad(const double deg) template<typename T>
inline constexpr T deg2rad(const T deg)
{ {
return deg*(HGL_PI/180.0f); return T(deg*(HGL_PI/180.0f));
} }
/** /**
* *
*/ */
inline constexpr double rad2deg(const double rad) template<typename T>
inline constexpr T rad2deg(const T rad)
{ {
return rad*(180.0f/HGL_PI); return T(rad*(180.0f/HGL_PI));
} }
/** /**

View File

@ -68,8 +68,8 @@ namespace hgl
/** /**
* *
* @param field_of_view ()
* @param aspect_ratio * @param aspect_ratio
* @param field_of_view
* @param znear * @param znear
* @param zfar * @param zfar
*/ */
@ -98,16 +98,17 @@ namespace hgl
0.0f, 0.0f,
0.0f, 0.0f,
(znear * zfar) / (znear - zfar), -(znear * zfar) / (zfar-znear),
0.0f 0.0f
); );
//经查证此代码等于glm::perspectiveRH_ZO之后将[1][1]乘-1在SaschaWillems的范例中如果反装Y则[1][1]确实要乘-1。
} }
Matrix4f lookat(const Vector3f &eye,const Vector3f &target,const Vector3f &up) Matrix4f lookat(const Vector3f &eye,const Vector3f &target,const Vector3f &up)
{ {
Vector3f forward=normalize(target-eye); Vector3f forward=normalize(target-eye);
Vector3f right =normalize(cross(forward,up)); Vector3f right =normalize(cross(forward,up));
Vector3f nup =cross(right,forward); Vector3f nup =cross(right,forward);
return Matrix4f( right.x, return Matrix4f( right.x,
@ -122,7 +123,7 @@ namespace hgl
right.z, right.z,
nup.z, nup.z,
-forward.z/2.0f, -forward.z,
0.0f, 0.0f,
-dot(eye,right ), -dot(eye,right ),
@ -130,5 +131,7 @@ namespace hgl
dot(eye,forward), dot(eye,forward),
1.0f 1.0f
); );
//经查证此代码完全等于glm::lookAtRH无任何差别
} }
}//namespace hgl }//namespace hgl