2004-05-29 22:56:24 +02:00
|
|
|
//***********************************************************
|
|
|
|
//
|
|
|
|
// Name: Camera.H
|
|
|
|
// Last Update: 24/2/02
|
|
|
|
// Author: Poya Manouchehri
|
|
|
|
//
|
|
|
|
// Description: CCamera holds a view and a projection matrix.
|
|
|
|
// It also has a frustum which can be used to
|
|
|
|
// cull objects for rendering.
|
|
|
|
//
|
|
|
|
//***********************************************************
|
|
|
|
|
|
|
|
#ifndef CAMERA_H
|
|
|
|
#define CAMERA_H
|
|
|
|
|
|
|
|
#include "Frustum.h"
|
|
|
|
#include "Matrix3D.h"
|
|
|
|
|
2005-01-23 18:35:36 +01:00
|
|
|
extern int g_mouse_x, g_mouse_y;
|
2004-07-20 21:30:35 +02:00
|
|
|
|
2004-05-29 22:56:24 +02:00
|
|
|
//view port
|
|
|
|
struct SViewPort
|
|
|
|
{
|
|
|
|
unsigned int m_X;
|
|
|
|
unsigned int m_Y;
|
|
|
|
unsigned int m_Width;
|
|
|
|
unsigned int m_Height;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class CCamera
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CCamera ();
|
|
|
|
~CCamera ();
|
|
|
|
|
|
|
|
//Methods for projection
|
|
|
|
void SetProjection (CMatrix3D *proj) { m_ProjMat = *proj; }
|
|
|
|
void SetProjection (float nearp, float farp, float fov);
|
2005-10-24 03:53:03 +02:00
|
|
|
void SetProjectionTile (int tiles, int tile_x, int tile_y);
|
2004-05-29 22:56:24 +02:00
|
|
|
CMatrix3D GetProjection () { return m_ProjMat; }
|
|
|
|
|
|
|
|
//Updates the frustum planes. Should be called
|
|
|
|
//everytime the view or projection matrices are
|
|
|
|
//altered.
|
|
|
|
void UpdateFrustum ();
|
2004-07-27 23:00:53 +02:00
|
|
|
CFrustum GetFrustum () { return m_ViewFrustum; }
|
2004-05-29 22:56:24 +02:00
|
|
|
|
|
|
|
void SetViewPort (SViewPort *viewport);
|
|
|
|
SViewPort GetViewPort () { return m_ViewPort; }
|
|
|
|
|
|
|
|
//getters
|
|
|
|
float GetNearPlane() const { return m_NearPlane; }
|
|
|
|
float GetFarPlane() const { return m_FarPlane; }
|
|
|
|
float GetFOV() const { return m_FOV; }
|
2004-05-30 02:46:58 +02:00
|
|
|
|
|
|
|
// calculate and return the position of the 8 points of the frustum in world space
|
|
|
|
void GetFrustumPoints(CVector3D pts[8]) const;
|
|
|
|
|
|
|
|
// return four points in camera space at given distance from camera
|
|
|
|
void GetCameraPlanePoints(float dist,CVector3D pts[4]) const;
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2004-07-20 21:30:35 +02:00
|
|
|
// Build a ray passing through the screen coordinate (px, py) and the camera
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// BuildCameraRay: calculate origin and ray direction of a ray through
|
|
|
|
// the pixel (px,py) on the screen
|
|
|
|
void BuildCameraRay( int px, int py, CVector3D& origin, CVector3D& dir );
|
|
|
|
// BuildCameraRay: as previous, using global mouse position
|
|
|
|
void BuildCameraRay( CVector3D& origin, CVector3D& dir )
|
|
|
|
{
|
2005-01-23 18:35:36 +01:00
|
|
|
BuildCameraRay( g_mouse_x, g_mouse_y, origin, dir );
|
2004-07-20 21:30:35 +02:00
|
|
|
}
|
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
// General helpers that seem to fit here
|
|
|
|
|
|
|
|
// Get the screen-space coordinates corresponding to a given world-space position
|
2004-07-20 21:30:35 +02:00
|
|
|
void GetScreenCoordinates( const CVector3D& world, float& x, float& y );
|
2004-07-22 18:18:12 +02:00
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
// Get the point on the terrain corresponding to pixel (px,py) (or the mouse coordinates)
|
|
|
|
CVector3D GetWorldCoordinates( int px, int py );
|
2005-01-23 18:35:36 +01:00
|
|
|
CVector3D GetWorldCoordinates() { return( GetWorldCoordinates( g_mouse_x, g_mouse_y ) ); }
|
2004-08-03 01:14:54 +02:00
|
|
|
// Get the point on the terrain the camera is pointing towards
|
|
|
|
CVector3D GetFocus();
|
|
|
|
|
|
|
|
// Build an orientation matrix from camera position, camera focus point, and up-vector
|
|
|
|
void LookAt( const CVector3D& camera, const CVector3D& orientation, const CVector3D& up );
|
|
|
|
|
|
|
|
// Build an orientation matrix from camera position, camera orientation, and up-vector
|
|
|
|
void LookAlong( CVector3D camera, CVector3D focus, CVector3D up );
|
2004-07-22 18:18:12 +02:00
|
|
|
|
2004-05-29 22:56:24 +02:00
|
|
|
public:
|
|
|
|
//This is the orientation matrix. The inverse of this
|
|
|
|
//is the view matrix
|
|
|
|
CMatrix3D m_Orientation;
|
|
|
|
|
|
|
|
private:
|
|
|
|
//keep the projection matrix private
|
|
|
|
//so we can't fiddle with it.
|
|
|
|
CMatrix3D m_ProjMat;
|
|
|
|
|
|
|
|
float m_NearPlane;
|
|
|
|
float m_FarPlane;
|
|
|
|
float m_FOV;
|
|
|
|
SViewPort m_ViewPort;
|
|
|
|
|
|
|
|
CFrustum m_ViewFrustum;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|