ULRE/src/SceneGraph/Camera.cpp

55 lines
2.0 KiB
C++
Raw Normal View History

2019-05-21 21:28:33 +08:00
#include<hgl/graph/Camera.h>
namespace hgl
{
namespace graph
{
template<typename V>
inline Matrix4f LookAt(const V &eye,const V &target,const V &up)
2019-05-21 21:28:33 +08:00
{
V forward=target-eye;
2019-05-21 21:28:33 +08:00
normalize(forward);
V side=cross(forward,up);
2019-05-21 21:28:33 +08:00
normalize(side);
V nup=cross(side,forward);
2019-05-21 21:28:33 +08:00
Matrix4f result( side.x, side.y, side.z, 1.0f,
nup.x, nup.y, nup.z, 1.0f,
-forward.x, -forward.y, -forward.z/2.0f, 1.0f,
0.0f, 0.0f, 0.0f, 1.0f);
2019-07-12 10:13:01 +08:00
// ^^^^^^
// 某些引擎此项为0.5这个会影响最终输出的z值但我们这里必须为0
2019-05-21 21:28:33 +08:00
2019-06-11 15:51:13 +08:00
return result*translate(-eye.xyz());
2019-05-21 21:28:33 +08:00
}
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)
matrix.projection=perspective(fov,width/height,znear,zfar);
2019-05-27 22:45:38 +08:00
else
matrix.projection=ortho(width,height,znear,zfar); //这个算的不对
2019-05-21 21:28:33 +08:00
2019-06-13 14:07:39 +08:00
//matrix.inverse_projection=matrix.projection.Inverted();
matrix.modelview=hgl::graph::LookAt(eye,center,up_vector);
//matrix.modelview=Matrix4f::LookAt(eye.xyz(),center.xyz(),forward_vector.xyz(),up_vector.xyz(),up_vector.xyz());
2019-05-21 21:28:33 +08:00
matrix.mvp=matrix.projection*matrix.modelview;
2019-05-29 21:56:53 +08:00
//注意: C++中要 projection * model_view * local_to_world * position
//而GLSL中要 position * local_to_world * model_view * projection
2019-07-17 04:49:49 +08:00
matrix.ortho=ortho(width,height,znear,zfar);
2019-05-28 22:04:11 +08:00
matrix.view_pos=eye;
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