2019-05-21 21:28:33 +08:00
|
|
|
|
#include<hgl/graph/Camera.h>
|
|
|
|
|
namespace hgl
|
|
|
|
|
{
|
|
|
|
|
namespace graph
|
|
|
|
|
{
|
2019-06-24 21:00:55 +08:00
|
|
|
|
template<typename V>
|
|
|
|
|
inline Matrix4f LookAt(const V &eye,const V &target,const V &up)
|
2019-05-21 21:28:33 +08:00
|
|
|
|
{
|
2019-06-24 21:00:55 +08:00
|
|
|
|
V forward=target-eye;
|
2019-05-21 21:28:33 +08:00
|
|
|
|
|
|
|
|
|
normalize(forward);
|
|
|
|
|
|
2019-06-24 21:00:55 +08:00
|
|
|
|
V side=cross(forward,up);
|
2019-05-21 21:28:33 +08:00
|
|
|
|
|
|
|
|
|
normalize(side);
|
|
|
|
|
|
2019-06-24 21:00:55 +08:00
|
|
|
|
V nup=cross(side,forward);
|
2019-05-21 21:28:33 +08:00
|
|
|
|
|
2019-07-12 17:33:38 +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,
|
2019-07-12 01:57:18 +08:00
|
|
|
|
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)
|
2019-05-29 21:48:56 +08:00
|
|
|
|
matrix.projection=perspective(fov,width/height,znear,zfar);
|
2019-05-27 22:45:38 +08:00
|
|
|
|
else
|
2019-05-29 21:48:56 +08:00
|
|
|
|
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();
|
|
|
|
|
|
2019-05-29 21:48:56 +08:00
|
|
|
|
matrix.modelview=hgl::graph::LookAt(eye,center,up_vector);
|
2019-06-24 21:00:55 +08:00
|
|
|
|
//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
|
|
|
|
|
2019-05-29 21:48:56 +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
|
|
|
|
|
2019-06-26 20:44:53 +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
|