2019-05-21 21:28:33 +08:00
|
|
|
|
#include<hgl/graph/Camera.h>
|
|
|
|
|
namespace hgl
|
|
|
|
|
{
|
|
|
|
|
namespace graph
|
|
|
|
|
{
|
|
|
|
|
inline Matrix4f LookAt(const Vector3f &eye,const Vector3f &target,const Vector3f &up)
|
|
|
|
|
{
|
|
|
|
|
Vector3f forward=target-eye;
|
|
|
|
|
|
|
|
|
|
normalize(forward);
|
|
|
|
|
|
|
|
|
|
Vector3f side=cross(forward,up);
|
|
|
|
|
|
|
|
|
|
normalize(side);
|
|
|
|
|
|
|
|
|
|
Vector3f nup=cross(side,forward);
|
|
|
|
|
|
|
|
|
|
Matrix4f result(side.x, side.y, side.z, 0.0f,
|
|
|
|
|
nup.x, nup.y, nup.z, 0.0f,
|
|
|
|
|
-forward.x, -forward.y, -forward.z, 0.0f,
|
|
|
|
|
0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
|
|
|
|
|
|
return result*translate(-eye);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-27 22:45:38 +08:00
|
|
|
|
void Camera::Refresh()
|
2019-05-21 21:28:33 +08:00
|
|
|
|
{
|
2019-05-27 22:45:38 +08:00
|
|
|
|
if(type==CameraType::Perspective)
|
2019-05-28 14:25:58 +08:00
|
|
|
|
projection=perspective(fov,width/height,znear,zfar);
|
2019-05-27 22:45:38 +08:00
|
|
|
|
else
|
|
|
|
|
projection=ortho(width,height,znear,zfar);
|
2019-05-21 21:28:33 +08:00
|
|
|
|
|
2019-05-27 22:45:38 +08:00
|
|
|
|
modelview=hgl::graph::LookAt(eye,center,up_vector);
|
2019-05-21 21:28:33 +08:00
|
|
|
|
|
2019-05-27 22:45:38 +08:00
|
|
|
|
frustum.SetVerticalFovAndAspectRatio(DegToRad(fov),width/height);
|
|
|
|
|
frustum.SetViewPlaneDistances(znear,zfar);
|
2019-05-21 21:28:33 +08:00
|
|
|
|
}
|
|
|
|
|
}//namespace graph
|
|
|
|
|
}//namespace hgl
|