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