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"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "maths/Matrix3D.h"
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2005-01-23 18:35:36 +01:00
|
|
|
extern int g_mouse_x, g_mouse_y;
|
2004-07-20 21:30:35 +02:00
|
|
|
|
2005-12-02 22:08:10 +01:00
|
|
|
// view port
|
2004-05-29 22:56:24 +02:00
|
|
|
struct SViewPort
|
|
|
|
{
|
|
|
|
unsigned int m_X;
|
|
|
|
unsigned int m_Y;
|
|
|
|
unsigned int m_Width;
|
|
|
|
unsigned int m_Height;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class CCamera
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CCamera ();
|
|
|
|
~CCamera ();
|
|
|
|
|
2005-12-02 22:08:10 +01:00
|
|
|
// Methods for projection
|
2004-05-29 22:56:24 +02:00
|
|
|
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; }
|
|
|
|
|
2005-12-02 22:08:10 +01:00
|
|
|
// Updates the frustum planes. Should be called
|
|
|
|
// everytime the view or projection matrices are
|
|
|
|
// altered.
|
2004-05-29 22:56:24 +02:00
|
|
|
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; }
|
|
|
|
|
2005-12-02 22:08:10 +01:00
|
|
|
// getters
|
2004-05-29 22:56:24 +02:00
|
|
|
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
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
2005-12-02 22:08:10 +01:00
|
|
|
// 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)
|
2004-07-20 21:30:35 +02:00
|
|
|
{
|
2005-12-02 22:08:10 +01:00
|
|
|
BuildCameraRay(g_mouse_x, g_mouse_y, origin, dir);
|
2004-07-20 21:30:35 +02:00
|
|
|
}
|
|
|
|
|
2005-12-02 22:08:10 +01:00
|
|
|
// General helpers that seem to fit here
|
2004-08-03 01:14:54 +02:00
|
|
|
|
2005-12-02 22:08:10 +01:00
|
|
|
// Get the screen-space coordinates corresponding to a given world-space position
|
2006-05-29 05:28:54 +02:00
|
|
|
void GetScreenCoordinates(const CVector3D& world, float& x, float& y) const;
|
2004-07-22 18:18:12 +02:00
|
|
|
|
2005-12-02 22:08:10 +01:00
|
|
|
// Get the point on the terrain corresponding to pixel (px,py) (or the mouse coordinates)
|
|
|
|
CVector3D GetWorldCoordinates(int px, int py);
|
|
|
|
CVector3D GetWorldCoordinates() { return GetWorldCoordinates(g_mouse_x, g_mouse_y); }
|
|
|
|
// Get the point on the plane at height h corresponding to pixel (px,py)
|
|
|
|
CVector3D GetWorldCoordinates(int px, int py, float h);
|
|
|
|
// Get the point on the terrain the camera is pointing towards
|
2004-08-03 01:14:54 +02:00
|
|
|
CVector3D GetFocus();
|
|
|
|
|
|
|
|
// Build an orientation matrix from camera position, camera focus point, and up-vector
|
2005-12-02 22:08:10 +01:00
|
|
|
void LookAt(const CVector3D& camera, const CVector3D& orientation, const CVector3D& up);
|
2004-08-03 01:14:54 +02:00
|
|
|
|
|
|
|
// Build an orientation matrix from camera position, camera orientation, and up-vector
|
2005-12-02 22:08:10 +01:00
|
|
|
void LookAlong(CVector3D camera, CVector3D focus, CVector3D up);
|
2004-07-22 18:18:12 +02:00
|
|
|
|
2006-01-22 20:12:30 +01:00
|
|
|
/**
|
|
|
|
* Render: Renders the camera's frustum in world space.
|
|
|
|
* The caller should set the color using glColorXy before calling Render.
|
|
|
|
*
|
|
|
|
* @param intermediates determines how many intermediate distance planes should
|
|
|
|
* be hinted at between the near and far planes
|
|
|
|
*/
|
|
|
|
void Render(uint intermediates = 0) const;
|
|
|
|
|
2004-05-29 22:56:24 +02:00
|
|
|
public:
|
2005-12-02 22:08:10 +01:00
|
|
|
// This is the orientation matrix. The inverse of this
|
|
|
|
// is the view matrix
|
2004-05-29 22:56:24 +02:00
|
|
|
CMatrix3D m_Orientation;
|
2006-06-01 00:37:23 +02:00
|
|
|
|
|
|
|
// Should not be tweaked externally if possible
|
2004-05-29 22:56:24 +02:00
|
|
|
CMatrix3D m_ProjMat;
|
|
|
|
|
2006-06-01 00:37:23 +02:00
|
|
|
private:
|
2004-05-29 22:56:24 +02:00
|
|
|
float m_NearPlane;
|
|
|
|
float m_FarPlane;
|
|
|
|
float m_FOV;
|
|
|
|
SViewPort m_ViewPort;
|
|
|
|
|
|
|
|
CFrustum m_ViewFrustum;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|