Skip to main content
GameDev.net gamedev.net
🔒 Locked

Rate my camera base interface

Started by GhostBlazer Oct 24, 2005 at 4:24 PM 5 replies 950+ views
Original Post
GhostBlazer
GhostBlazer
Hello, Recently I've been getting used to the whole polymorphic/OOP concept. I just wanted some criticism for my base camera class. Enjoy :)

#ifndef __IBASECAMERA_INCLUDED__
#define __IBASECAMERA_INCLUDED__

/**********************************************************************************************/

interface IBaseCamera
{

protected :

    D3DXMATRIX _mProj ;        // Projection matrix.

	float _fFov,               // Field of view, in the y-direction, in radians.
		  _fAspect,            // Aspect ratio, defined as view space width divided by height.
		  _fNear,              // Z-Value of the near plane.
		  _fFar ;              // Z-Value of the far plane.

	D3DXMATRIX _mView ;        // View space matrix.

	D3DXVECTOR3 _vEye,         // Defines the camera's position in world space.
		        _vAt,          // Defines where the camera is looking at.
		        _vRight,       // Defines where right is in view space.
				_vUp,          // Defines where up is in view space.
				_vView ;       // Line segment influenced by camera's position and view target.

	D3DXMATRIX _mWorld ;       // World space matrix.

	float _fPitch,             // Rotation about the x-axis.
		  _fYaw,               // Rotation about the y-axis.
		  _fRoll ;             // Rotation about the z-axis.

public :

	IBaseCamera() {

		_mProj = D3DXMATRIX( 1.0f, 0.0f, 0.0f, 0.0f,
			                 0.0f, 1.0f, 0.0f, 0.0f,
							 0.0f, 0.0f, 1.0f, 0.0f,
							 0.0f, 0.0f, 0.0f, 1.0f ) ;
		
		_fFov = _fAspect = _fNear = _fFar = 0.0f ;

		_mView = D3DXMATRIX( 1.0f, 0.0f, 0.0f, 0.0f,
			                 0.0f, 1.0f, 0.0f, 0.0f,
							 0.0f, 0.0f, 1.0f, 0.0f,
							 0.0f, 0.0f, 0.0f, 1.0f ) ;

		_vEye   = D3DXVECTOR3( 0.0f, 0.0f, 0.0f ),
		_vAt    = D3DXVECTOR3( 0.0f, 0.0f, 1.0f ),
		_vRight = D3DXVECTOR3( 1.0f, 0.0f, 0.0f ),
		_vUp    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f ),
		_vView  = D3DXVECTOR3( 0.0f, 0.0f, 1.0f ) ;

		_mWorld = D3DXMATRIX( 1.0f, 0.0f, 0.0f, 0.0f,
			                  0.0f, 1.0f, 0.0f, 0.0f,
				        	  0.0f, 0.0f, 1.0f, 0.0f,
				  			  0.0f, 0.0f, 0.0f, 1.0f ) ;

		_fPitch = _fYaw = _fRoll = 0.0f ;
	}

	virtual ~IBaseCamera() { }

	// Sets the projection matrix by passing a pointer to an already created one.
	virtual void SetProjection(LPD3DXMATRIX pmProj) { _mProj = (*pmProj) ; }

	// Sets the properties of a projection matrix.
	virtual void SetProjection(FLOAT fFov, FLOAT fAspect, FLOAT fNear, FLOAT fFar) {

		_fFov    = fFov,
		_fAspect = fAspect,
		_fNear   = fNear,
		_fFar    = fFar ;
	}

	// Sets the view matrix by passing a pointer to an already created one.
	virtual void SetView(LPD3DXMATRIX pmView) { _mView = (*pmView) ; }

	// Sets the properties of the view space matrix.
	virtual void SetView(LPD3DXVECTOR3 pvEye, LPD3DXVECTOR3 pvAt, LPD3DXVECTOR3 pvUp) {

		_vEye   = *pvEye,
		_vAt    = *pvAt,
		_vUp    = *pvUp,
		_vView  = (*pvAt) - (*pvEye),
		_vRight = *D3DXVec3Cross( &_vRight, &_vUp, &_vView ) ;
		
		D3DXVec3Normalize( &_vView, &_vView ) ;
		D3DXVec3Normalize( &_vRight, &_vRight ) ;
	}

	// Sets the world matrix by passing a pointer to an already created one.
	virtual void SetWorld(LPD3DXMATRIX pmWorld) { _mWorld = (*pmWorld) ; }

	// Using known variables, this method constructs the projection, view, and world matrix.
	virtual void Ready(void) = 0 ;

	// This method updates the camera's view as many times as it's called.
	virtual void Focus(LPDIRECT3DDEVICE9 pDevice) = 0 ;

	// Retrieves the projection matrix.
	virtual LPD3DXMATRIX GetProj(void)  { return &_mProj ; }
	
	// Retrieves the view matrix.
	virtual LPD3DXMATRIX GetView(void)  { return &_mView ; }
	
	// Retrieves the world matrix.
	virtual LPD3DXMATRIX GetWorld(void) { return &_mWorld ; }

	// Retrieves the camera's position.
	virtual LPD3DXVECTOR3 GetEyeVector(void) { return &_vEye ; }

	// Retrieves the view target vector.
	virtual LPD3DXVECTOR3 GetViewTarget(void) { return &_vAt ; }

	// Retrieves the camera's up orientation.
	virtual LPD3DXVECTOR3 GetUpVector(void) { return &_vUp ; }

	// Retrieves the camera's view vector.
	virtual LPD3DXVECTOR3 GetViewVector(void) { return &_vView ; }

	// Retrieves the camera's right/strafe orientation.
	virtual LPD3DXVECTOR3 GetRightVector(void) { return &_vRight ; }	

	// Retrieves camera's rotation about the x-axis.
	virtual float GetPitch(void) CONST{ return _fPitch ; }
	
	// Retrieves camera's rotation about the y-axis.
	virtual float GetYaw(void)   CONST{ return _fYaw ; }
	
	// Retrieves camera's rotation about the z-axis.
	virtual float GetRoll(void)  CONST{ return _fRoll ; }

} ;

/**********************************************************************************************/

#endif

Programmer16
Programmer16
The class itself looks solid to me, but why make this class an interface? I've barely even gotten into 3D topics, so I don't understand why you would have to change the inners of a camera class.
circlesoft
circlesoft
Quote:
Original post by Programmer16
The class itself looks solid to me, but why make this class an interface? I've barely even gotten into 3D topics, so I don't understand why you would have to change the inners of a camera class.

Well, you can have multiple kinds of cameras. Most are related to the genre of the game:

- First person (ie for a FPS)
- Third person (needs specific code for following objects, avoiding walls, ect)
- Model viewer (where moving the camera essentially only moves the object)

There are many specific situations where you would want to use this kind of interface. It can pretty useful for when you switch between them in-game (ie in a driving game, switching from inside the car to outside). Also, you can have multiple implementations of a third-person camera (one that strictly follows a character, one that revolves around it, ect...)
tksuoran
tksuoran
You have overlapping data in there, and an interface which lets you get that data out of sync. You should probably have a super class, frame, node, whatever, that does not have the camera specific parts (fov and projection).
Muhammad Haggag
Muhammad Haggag
By the way, an interface is typically defined to be a group of pure virtual functions. What you have is an abstract base class, which is a class with at least one pure virtual function.

Hesterumpe
Hesterumpe
A couple of points:

* The methods of a class like this should be put into a cpp file. If for no other reason, then because if you change something in a header it might not tricker a rebuild.

* Your mixing float and FLOAT. If ms goes ahead and redefine FLOAT then you could potentially be in trouble. I know it's unlikely, but thats the idea behind the custom FLOAT type.

* Whats with the CONST? The C++ way is const. But then, you might have a reason :)

* I wouldn't make all those get'ers. Instead I would give the camera methods to do the functions that needs those values. Eg. instead of having getForward, getUp, getRight, I would have a rotate( point ). It's cleaner, easier to maintain and you have the relevant code where it belongs.

Other than that, its OK :)
GhostBlazer
GhostBlazer
Quote:
Original post by Hesterumpe
A couple of points:

* The methods of a class like this should be put into a cpp file. If for no other reason, then because if you change something in a header it might not tricker a rebuild.

* Your mixing float and FLOAT. If ms goes ahead and redefine FLOAT then you could potentially be in trouble. I know it's unlikely, but thats the idea behind the custom FLOAT type.

* Whats with the CONST? The C++ way is const. But then, you might have a reason :)

* I wouldn't make all those get'ers. Instead I would give the camera methods to do the functions that needs those values. Eg. instead of having getForward, getUp, getRight, I would have a rotate( point ). It's cleaner, easier to maintain and you have the relevant code where it belongs.

Other than that, its OK :)


Thanks for your input. I really appreciate all of your suggestions and am putting them into serious consideration. However, I'm not quite sure I understand your last suggestion. Care to elaborate on that?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.