Code on this page show.
How to create and manage camera stuff in your own game app’s.
Using DirectX 7.
Simple sample how to use it.
/*SDICAMERA.H*/
// Class to manage camera in your own game APP
#ifndef SDICAMERA_H
#define SDICAMERA_H
#include "D3DApp.h"
class SDI_Camera
{
public:
SDI_Camera();//constructor
~SDI_Camera();//destructor
void SetCamPD(LPDIRECT3DDEVICE7 device,
char w,
D3DVECTOR tv,
FLOAT v,
bool old=false);
D3DVECTOR&GetCamPD(char w,
bool old=false);
void CamInit(LPDIRECT3DDEVICE7 device,
D3DVECTOR pv,
D3DVECTOR dv,
FLOAT dvv,
bool old=false);
D3DMATRIX&GetCamMat(bool old=false);
void RestoreCam(LPDIRECT3DDEVICE7 device,bool old=false);
private:
void UpdateCamPD(bool old=false);
bool m_camPDchanged,m_camPDchangedOld;
D3DVIEWPORT7 m_vp,m_vpOld;
//view port can be changed so you need it
D3DMATRIX m_matProj,m_matProjOld;
FLOAT m_Aspect,m_AspectOld;
//aspect device dependent
D3DVECTOR m_camP,m_camD;
//camera position and direction
D3DVECTOR m_camPOld,m_camDOld;
//camera position and direction if you want restore
//previous values
};
#endif
//SDICAMERA.cpp//
#define D3D_OVERLOADS
#include "sdicamera.h"
#include "D3DUtil.h"
#include "D3DMath.h"
SDI_Camera::SDI_Camera()
{
m_Aspect=m_AspectOld=0.0;
m_camP.x=m_camPOld.x=0.0;
m_camP.y=m_camPOld.y=0.0;
m_camP.z=m_camPOld.z=0.0;
m_camD.x=m_camDOld.x=0.0;
m_camD.y=m_camDOld.y=0.0;
m_camD.z=m_camDOld.z=0.0;
/*
or m_camP= m_camD= m_camDOld= m_camPOld=0.0;
*/
m_camPDchanged=m_camPDchangedOld=false;
}
SDI_Camera::~SDI_Camera()
{
;
//or you can make it virtual and so on
}
D3DMATRIX&SDI_Camera::GetCamMat(bool old)
{
if(!old)return m_matProj;
return m_matProjOld;
}
void SDI_Camera::RestoreCam(LPDIRECT3DDEVICE7 device,bool old)
{
if(!old)
device->SetTransform(D3DTRANSFORMSTATE_PROJECTION,&m_matProj);
else
device->SetTransform(D3DTRANSFORMSTATE_PROJECTION,&m_matProjOld);
}
void SDI_Camera::SetCamPD(LPDIRECT3DDEVICE7 device,
char w,
D3DVECTOR tv,
FLOAT v,
bool old)
{
D3DMATRIX mattrans,matrot;
if(!old)
{
if(D3D_OK!=device->GetViewport(&m_vp))return;
m_Aspect=((FLOAT)m_vp.dwHeight)/m_vp.dwWidth;
if(w=='p')//’p’-position
{
D3DUtil_SetTranslateMatrix(mattrans,tv);
D3DMath_MatrixMultiply(m_matProj,mattrans,m_matProj);
device->SetTransform (D3DTRANSFORMSTATE_PROJECTION,&m_matProj);
m_camPDchanged=true;
return;
}
if(w=='d')//’d’-direction
{
D3DUtil_SetRotationMatrix(matrot,tv,v);
D3DMath_MatrixMultiply(m_matProj,matrot,m_matProj);
device->SetTransform (D3DTRANSFORMSTATE_PROJECTION,&m_matProj);
m_camPDchanged=true;
return;
}
return;
}
if(D3D_OK!=device->GetViewport(&m_vpOld))return;
m_AspectOld=((FLOAT)m_vpOld.dwHeight)/m_vpOld.dwWidth;
if(w=='p')
{
D3DUtil_SetTranslateMatrix(mattrans,tv);
D3DMath_MatrixMultiply(m_matProjOld,mattrans,m_matProjOld);
device->SetTransform(D3DTRANSFORMSTATE_PROJECTION,&m_matProjOld);
m_camPDchangedOld=true;
return;
}
if(w=='d')
{
D3DUtil_SetRotationMatrix(matrot,tv,v);
D3DMath_MatrixMultiply(m_matProjOld,matrot,m_matProjOld);
device->SetTransform (D3DTRANSFORMSTATE_PROJECTION,&m_matProjOld);
m_camPDchangedOld=true;
return;
}
}
D3DVECTOR& SDI_Camera::GetCamPD(char w,
bool old)
{
if(!old)
{
if(m_camPDchanged)UpdateCamPD();
if(w=='p')return m_camP;
if(w=='d')return m_camD;
}
if(m_camPDchangedOld)UpdateCamPD(true);
if(w=='p')return m_camPOld;
return m_camDOld;
}
void SDI_Camera::CamInit(LPDIRECT3DDEVICE7 device,
D3DVECTOR pv,
D3DVECTOR dv,
FLOAT dvv,
bool old)
{
D3DMATRIX mattrans,matrot;
D3DUtil_SetTranslateMatrix(mattrans,pv);
D3DUtil_SetRotationMatrix(matrot,dv,dvv);
if(!old)
{
if(D3D_OK!=device->GetViewport(&m_vp))return;
m_Aspect=((FLOAT)m_vp.dwHeight)/m_vp.dwWidth;
D3DUtil_SetIdentityMatrix(m_matProj);
D3DMath_MatrixMultiply(m_matProj,mattrans,m_matProj);
D3DMath_MatrixMultiply(m_matProj,matrot,m_matProj);
D3DUtil_SetProjectionMatrix (m_matProj,g_PI/3,m_Aspect,1.0f,1000.0f);
device->SetTransform(D3DTRANSFORMSTATE_PROJECTION,&m_matProj);
m_camPDchanged=true;
UpdateCamPD();
return;
}
if(D3D_OK!=device->GetViewport(&m_vpOld))return;
m_AspectOld=((FLOAT)m_vpOld.dwHeight)/m_vpOld.dwWidth;
D3DUtil_SetIdentityMatrix(m_matProjOld);
D3DMath_MatrixMultiply(m_matProjOld,mattrans,m_matProjOld);
D3DMath_MatrixMultiply(m_matProjOld,matrot,m_matProjOld);
D3DUtil_SetProjectionMatrix(m_matProjOld,g_PI/3,m_AspectOld,1.0f,1000.0f);
device->SetTransform(D3DTRANSFORMSTATE_PROJECTION,&m_matProjOld);
m_camPDchangedOld=true;
UpdateCamPD(true);
}
void SDI_Camera::UpdateCamPD(bool old)
{
if(!old)
{
if(m_camPDchanged==true)
{
m_camP.x=m_matProj._41;
m_camP.y=m_matProj._42;
m_camP.z=m_matProj._43;
m_camD.x=m_matProj._11;
m_camD.y=m_matProj._22;
m_camD.z=m_matProj._33;
m_camPDchanged=false;
}
return;
}
if(m_camPDchangedOld)
{
m_camPOld.x=m_matProjOld._41;
m_camPOld.y=m_matProjOld._42;
m_camPOld.z=m_matProjOld._43;
m_camDOld.x=m_matProjOld._11;
m_camDOld.y=m_matProjOld._22;
m_camDOld.z=m_matProjOld._33;
m_camPDchangedOld=false;
}
}