00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "CCamera.h"
00014
00015 #include "gl/gl.h"
00016 #include "gl/glu.h"
00017 #pragma comment(lib,"OpenGL32.lib")
00018 #pragma comment(lib,"glu32.lib")
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 void CCamera::SetPerspective(const float &fFov, const float &fAspectRatio, const float &fNearZ, const float &fFarZ)
00030 {
00031 m_fov = fFov;
00032 m_nearPlane = fNearZ;
00033 m_farPlane = fFarZ;
00034 m_aspect = fAspectRatio;
00035 m_bPerspectiveValid = false;
00036 }
00037
00038 void CCamera::SetFOV(float newFOV){
00039 if(m_fov != newFOV){
00040 m_fov = newFOV;
00041 m_bPerspectiveValid = false;
00042 }
00043 }
00044
00045 void CCamera::SetAspect(const float &fAspectRatio){
00046 m_aspect = fAspectRatio;
00047 m_bPerspectiveValid = false;
00048 }
00049
00050 void CCamera::SetTransformOrder(byte order){
00051 m_TransformOrder = order;
00052 }
00053
00054 void CCamera::SetPos(const CVector3f &vPos)
00055 {
00056 m_vPos= vPos;
00057 m_bTransformValid = false;
00058 }
00059
00060 void CCamera::SetRot(const CVector3f &vRot)
00061 {
00062 m_vRot = vRot;
00063 m_bTransformValid = false;
00064 }
00065
00066 void CCamera::Log(word wTargetLog)
00067 {
00068 if(!m_bTransformValid)
00069 {
00070 Update();
00071 }
00072 }
00073
00074 void CCamera::Refresh()
00075 {
00076 glMatrixMode(GL_PROJECTION);
00077 glLoadIdentity();
00078 gluPerspective(m_fov,m_aspect,m_nearPlane,m_farPlane);
00079
00080 m_bPerspectiveValid = true;
00081
00082 glMatrixMode(GL_MODELVIEW);
00083 Update();
00084 glLoadMatrixf((GLfloat*)&m_View);
00085 }
00086
00087 void CCamera::UpdateView()
00088 {
00089 if(!m_bPerspectiveValid){
00090 glMatrixMode(GL_PROJECTION);
00091 glLoadIdentity();
00092 gluPerspective(m_fov,m_aspect,m_nearPlane,m_farPlane);
00093 m_bPerspectiveValid = true;
00094 }
00095 }
00096
00097 void CCamera::UpdateTransForm()
00098 {
00099 glMatrixMode(GL_MODELVIEW);
00100 if(!m_bTransformValid)
00101 {
00102 Update();
00103 }
00104 glLoadMatrixf((GLfloat*)&m_View);
00105 }
00106
00107 const CFrustum &CCamera::GetFrustum()
00108 {
00109 if(false){
00110 m_Frustum.CalculateFrustum(m_Projection,m_View);
00111 }
00112 return m_Frustum;
00113 }
00114
00115 const CMatrix44 &CCamera::GetTransformation()
00116 {
00117 if(!m_bTransformValid){
00118 Update();
00119 }
00120 return m_View;
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130 void CCamera::Clear()
00131 {
00132 m_vPos.Set(0,0,20);
00133 m_vRot.Set(0,0,0);
00134 m_fov = 70.0f;
00135 m_nearPlane = 0.2;
00136 m_farPlane = 100.0;
00137 SetPerspective(m_fov,1.0,m_nearPlane,m_farPlane);
00138 m_bPerspectiveValid = m_bTransformValid = false;
00139 }
00140
00141 void CCamera::Update()
00142 {
00143 m_View.Identity();
00144
00145 m_Pos.Translation(-m_vPos);
00146 m_Rot.RotationZYX(-m_vRot);
00147
00148 if(m_TransformOrder == RT){
00149 m_View.Multiply(m_Pos,m_Rot);
00150 }else{
00151 m_View.Multiply(m_Rot,m_Pos);
00152 }
00153 m_bTransformValid = true;
00154 }