Google
 

Thursday, October 30, 2008

BaseCamera Class

As I have said before, this is identical to my XNA camera. I know, not the best camera in the world, but this is the base of other cameras I will be producing later. So, as before we have a header file and cpp file.

The header looks like this:
#include <d3d9.h> 
#include <d3dx9.h>

#include "RandomChaos3DUtility.h"

#pragma once

class
BaseCamera
{
public:
BaseCamera(void);
BaseCamera(LPDIRECT3DDEVICE9 *p_dx_Device,D3DXVECTOR3 pos,D3DXVECTOR3 worldUp,D3DXQUATERNION rot, UINT width,UINT height);
~BaseCamera(void);

D3DXMATRIX GetView();
D3DXMATRIX GetProjection();

void Update();

void Translate(D3DXVECTOR3 distance);
void Rotate(D3DXVECTOR3 axis, float angle);

void SetPosition(D3DXVECTOR3 newPos);
void SetTarget(D3DXVECTOR3 newTarget);
void SetRotation(D3DXQUATERNION newRotation);

UINT Width;
UINT Height;

D3DXVECTOR3 GetPosition(void);

protected:
LPDIRECT3DDEVICE9 *graphicsDevice;

D3DXVECTOR3 position;
D3DXVECTOR3 target;
D3DXVECTOR3 worldUpVector;
D3DXQUATERNION rotation;

D3DXMATRIX view;
D3DXMATRIX projection;
};



The ctor and dtor are there as expected, then two methods to get the cameras View and Projection Matrix, an update method, translate and rotate then methods to set the position, target and rotation, height and width (for the viewport)  and finally a method to get the camera position. Then the protected members.

The methods look like this:

#include "RandomChaos3DCamera.h"

BaseCamera::BaseCamera(void)
{
position = D3DXVECTOR3(0,0,0);
worldUpVector = D3DXVECTOR3(0,1,0);
rotation = D3DXQUATERNION(0,0,0,1);
}
BaseCamera::BaseCamera(LPDIRECT3DDEVICE9 *Device,D3DXVECTOR3 pos, D3DXVECTOR3 worldUp, D3DXQUATERNION rot,UINT width,UINT height)
{
graphicsDevice = Device;
position = pos;
worldUpVector = worldUp;
rotation = rot;
Width = width;
Height = height;
}
BaseCamera::~BaseCamera(void)
{
}
D3DXMATRIX BaseCamera::GetProjection()
{
return projection;
}
D3DXMATRIX BaseCamera::GetView()
{
return view;
}
void BaseCamera::Update()
{
D3DXMATRIX rotMat;
D3DXMATRIX posMat;

D3DXMatrixRotationQuaternion(&rotMat,&rotation);
D3DXMatrixTranslation(&posMat,position.x,position.y,position.z);

D3DXMatrixMultiply(&rotMat,&rotMat,&posMat);
float md = D3DXMatrixDeterminant(&rotMat);

D3DXMatrixInverse(&view,&md,&rotMat);
D3DXMatrixPerspectiveFovRH(&projection, D3DX_PI/3.0, (float)Width/(float)Height, 1, 10000);
}
void BaseCamera::Translate(D3DXVECTOR3 distance)
{
RC3DUtility::Translate(&position,distance,&rotation);

Update();

}
void BaseCamera::Rotate(D3DXVECTOR3 axis, float angle)
{
RC3DUtility::Rotate(&rotation,axis,angle,&rotation);
Update();
}
void BaseCamera::SetPosition(D3DXVECTOR3 newPos)
{
position = newPos;
}
void BaseCamera::SetRotation(D3DXQUATERNION newRotation)
{
rotation = newRotation;
}
D3DXVECTOR3 BaseCamera::GetPosition(void)
{
return position;
}



Not sure what else to add here, it is pretty simple stuff, the only method of real note is the Update method as this is where the View and Projection Matrices are set, but this is done exactly the same as before in my XNA Camera class.

This post has been a bit light I know, the terrain class will be next and then the main.cpp, this will show how it all hangs together and in that last post I will put up a solution to download so you can have a play with it.

As ever C&C are welcome :)

1 comment:

  1. I found this site using [url=http://google.com]google.com[/url] And i want to thank you for your work. You have done really very good site. Great work, great site! Thank you!

    Sorry for offtopic

    ReplyDelete

Note: Only a member of this blog may post a comment.