ULRE/inc/hgl/graph/Camera.h

139 lines
4.5 KiB
C
Raw Normal View History

2019-05-21 21:28:33 +08:00
#ifndef HGL_GRAPH_CAMERA_INCLUDE
#define HGL_GRAPH_CAMERA_INCLUDE
#include<hgl/math/Math.h>
namespace hgl
{
namespace graph
{
2019-06-25 01:36:47 +08:00
/**
*
*/
2019-05-27 22:45:38 +08:00
enum class CameraType
{
Perspective,
Ortho
2019-06-25 01:36:47 +08:00
};//enum class CameraType
2019-05-27 22:45:38 +08:00
2019-05-21 21:28:33 +08:00
/**
*
*/
struct Camera
{
2019-05-27 22:45:38 +08:00
CameraType type=CameraType::Perspective;
2019-05-21 21:28:33 +08:00
float width; ///<视图宽
float height; ///<视图高
2019-06-14 19:41:14 +08:00
float fov=60; ///<水平FOV
2020-01-10 18:04:52 +08:00
float znear=16,zfar=256; ///<Z轴上离眼睛的距离(注因znear会参与计算请不要使用0或过于接近0的值)
2019-05-21 21:28:33 +08:00
2019-06-11 15:51:13 +08:00
Vector4f eye; ///<眼睛坐标
Vector4f center; ///<视点坐标
2019-06-11 16:18:49 +08:00
Vector4f up_vector =Vector4f(0,0,1,0); ///<向上量(默认0,0,1)
2019-06-25 01:36:47 +08:00
Vector4f forward_vector =Vector4f(0,1,0,0); ///<向前量(默认0,1,0)
2019-06-11 16:18:49 +08:00
Vector4f right_vector =Vector4f(1,0,0,0); ///<向右量(默认0,0,1)
2019-05-21 21:28:33 +08:00
2019-05-27 22:45:38 +08:00
public:
2019-05-21 21:28:33 +08:00
WorldMatrix matrix;
2019-05-27 22:45:38 +08:00
Frustum frustum;
public:
void Refresh();
2019-06-25 01:36:47 +08:00
void operator = (const Camera &cam)
{
type =cam.type;
width =cam.width;
height =cam.height;
fov =cam.fov;
znear =cam.znear;
zfar =cam.zfar;
eye =cam.eye;
center =cam.center;
up_vector =cam.up_vector;
forward_vector =cam.forward_vector;
right_vector =cam.right_vector;
matrix =cam.matrix;
frustum =cam.frustum;
}
2019-05-27 22:45:38 +08:00
};//struct Camera
2019-05-21 21:28:33 +08:00
/**
*
2019-05-21 21:28:33 +08:00
*/
struct ControlCamera:public Camera
2019-05-21 21:28:33 +08:00
{
2019-06-11 15:51:13 +08:00
protected:
/**
*
* @param move_dist
*/
void Move(const Vector4f &move_dist)
{
eye+=move_dist;
center+=move_dist;
2019-06-11 15:51:13 +08:00
}
/**
*
* @param ang
* @param axis
*/
void Rotate(double ang,Vector4f axis)
2019-06-11 15:51:13 +08:00
{
normalize(axis);
2019-06-11 15:51:13 +08:00
center=eye+(center-eye)*rotate(hgl_ang2rad(ang),axis);
}
/**
*
* @param ang
* @param axis
*/
2019-06-25 01:36:47 +08:00
void WrapRotate(double ang,Vector4f axis)
{
normalize(axis);
2019-06-11 15:51:13 +08:00
eye=center+(eye-center)*rotate(hgl_ang2rad(ang),axis);
2019-06-25 01:36:47 +08:00
}
2019-06-11 15:51:13 +08:00
2019-05-21 21:28:33 +08:00
public: //方法
virtual void Backward(float step=0.01){Move((eye-center)*step/length(eye,center));} ///<向后
2019-05-21 21:28:33 +08:00
virtual void Forward(float step=0.01){Backward(-step);} ///<向前
virtual void Up(float step=0.01){Move(step*up_vector);} ///<向上
2019-05-21 21:28:33 +08:00
virtual void Down(float step=0.01){Up(-step);} ///<向下
virtual void Right(float step=0.01){Move(normalized(cross(center-eye,up_vector))*step);}///<向右
2019-05-21 21:28:33 +08:00
virtual void Left(float step=0.01){Right(-step);} ///<向左
public: //以自身为中心旋转
2019-06-15 13:11:05 +08:00
virtual void VertRotate(float ang=5){Rotate(ang,cross(center-eye,up_vector));} ///<垂直方向前后旋转
virtual void HorzRotate(float ang=5){Rotate(ang,up_vector);} ///<水平方向左右旋转
2019-05-21 21:28:33 +08:00
public: //以目标点为中心旋转
2019-06-15 13:11:05 +08:00
virtual void WrapVertRotate(float ang=5){WrapRotate(ang,cross(center-eye,up_vector));} ///<以目标点为中心上下旋转
virtual void WrapHorzRotate(float ang=5){WrapRotate(ang,up_vector);} ///<以目标点为中心左右旋转
2019-05-21 21:28:33 +08:00
public: //距离
2019-06-11 15:51:13 +08:00
virtual void Distance(float pos) ///<调整距离
{
if(pos==1.0)return;
eye=center+(eye-center)*pos;
}
};//struct ControlCamera
2019-05-21 21:28:33 +08:00
}//namespace graph
}//namespace hgl
#endif//HGL_GRAPH_CAMERA_INCLUDE