Pyrogenesis  trunk
Camera.h
Go to the documentation of this file.
1 /* Copyright (C) 2013 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  * CCamera holds a view and a projection matrix. It also has a frustum
20  * which can be used to cull objects for rendering.
21  */
22 
23 #ifndef INCLUDED_CAMERA
24 #define INCLUDED_CAMERA
25 
26 #include "Frustum.h"
28 #include "maths/Matrix3D.h"
29 
30 // view port
31 struct SViewPort
32 {
33  int m_X;
34  int m_Y;
35  int m_Width;
36  int m_Height;
37 };
38 
39 class CCamera
40 {
41  public:
42  CCamera();
43  ~CCamera();
44 
45  // Methods for projection
46  void SetProjection(float nearp, float farp, float fov);
47  void SetProjection(const CMatrix3D& matrix) { m_ProjMat = matrix; }
48  void SetProjectionTile(int tiles, int tile_x, int tile_y);
49  CMatrix3D& GetProjection() { return m_ProjMat; }
50  const CMatrix3D& GetProjection() const { return m_ProjMat; }
51 
52  CMatrix3D& GetOrientation() { return m_Orientation; }
53  const CMatrix3D& GetOrientation() const { return m_Orientation; }
54 
55  CMatrix3D GetViewProjection() const { return m_ProjMat * m_Orientation.GetInverse(); }
56 
57  // Updates the frustum planes. Should be called
58  // everytime the view or projection matrices are
59  // altered.
60  void UpdateFrustum(const CBoundingBoxAligned& scissor = CBoundingBoxAligned(CVector3D(-1.0f, -1.0f, -1.0f), CVector3D(1.0f, 1.0f, 1.0f)));
61  void ClipFrustum(const CPlane& clipPlane);
62  const CFrustum& GetFrustum() const { return m_ViewFrustum; }
63 
64  void SetViewPort(const SViewPort& viewport);
65  const SViewPort& GetViewPort() const { return m_ViewPort; }
66 
67  // getters
68  float GetNearPlane() const { return m_NearPlane; }
69  float GetFarPlane() const { return m_FarPlane; }
70  float GetFOV() const { return m_FOV; }
71 
72  // return four points in camera space at given distance from camera
73  void GetCameraPlanePoints(float dist,CVector3D pts[4]) const;
74 
75  // Build a ray passing through the screen coordinate (px, py) and the camera
76  /////////////////////////////////////////////////////////////////////////////////////////
77  // BuildCameraRay: calculate origin and ray direction of a ray through
78  // the pixel (px,py) on the screen
79  void BuildCameraRay(int px, int py, CVector3D& origin, CVector3D& dir) const;
80 
81  // General helpers that seem to fit here
82 
83  // Get the screen-space coordinates corresponding to a given world-space position
84  void GetScreenCoordinates(const CVector3D& world, float& x, float& y) const;
85 
86  // Get the point on the terrain corresponding to pixel (px,py) (or the mouse coordinates)
87  // The aboveWater parameter determines whether we want to stop at the water plane or also get underwater points
88  CVector3D GetWorldCoordinates(int px, int py, bool aboveWater=false) const;
89  // Get the point on the plane at height h corresponding to pixel (px,py)
90  CVector3D GetWorldCoordinates(int px, int py, float h) const;
91  // Get the point on the terrain (or water plane) the camera is pointing towards
92  CVector3D GetFocus() const;
93 
94  // Build an orientation matrix from camera position, camera focus point, and up-vector
95  void LookAt(const CVector3D& camera, const CVector3D& orientation, const CVector3D& up);
96 
97  // Build an orientation matrix from camera position, camera orientation, and up-vector
98  void LookAlong(CVector3D camera, CVector3D focus, CVector3D up);
99 
100  /**
101  * Render: Renders the camera's frustum in world space.
102  * The caller should set the color using glColorXy before calling Render.
103  *
104  * @param intermediates determines how many intermediate distance planes should
105  * be hinted at between the near and far planes
106  */
107  void Render(int intermediates = 0) const;
108 
109  public:
110  // This is the orientation matrix. The inverse of this
111  // is the view matrix
113 
114  // Should not be tweaked externally if possible
116 
117  private:
118  float m_NearPlane;
119  float m_FarPlane;
120  float m_FOV;
122 
124 };
125 
126 #endif
void SetProjection(const CMatrix3D &matrix)
Definition: Camera.h:47
Definition: Camera.h:31
float m_NearPlane
Definition: Camera.h:118
int m_Y
Definition: Camera.h:34
const CFrustum & GetFrustum() const
Definition: Camera.h:62
Definition: Vector3D.h:28
Definition: Frustum.h:39
float m_FarPlane
Definition: Camera.h:119
int m_Height
Definition: Camera.h:36
bool focus(JSContext *cx, uint argc, jsval *vp)
Definition: JSInterface_IGUIObject.cpp:635
Definition: Matrix3D.h:33
CMatrix3D & GetOrientation()
Definition: Camera.h:52
Definition: Camera.h:39
CMatrix3D & GetProjection()
Definition: Camera.h:49
const CMatrix3D & GetOrientation() const
Definition: Camera.h:53
float GetFOV() const
Definition: Camera.h:70
void Render()
Definition: GameSetup.cpp:201
float GetNearPlane() const
Definition: Camera.h:68
CFrustum m_ViewFrustum
Definition: Camera.h:123
const SViewPort & GetViewPort() const
Definition: Camera.h:65
Definition: BoundingBoxAligned.h:35
void GetInverse(CMatrix3D &dst) const
Definition: Matrix3D.cpp:277
int m_Width
Definition: Camera.h:35
CMatrix3D GetViewProjection() const
Definition: Camera.h:55
CMatrix3D m_Orientation
Definition: Camera.h:112
SViewPort m_ViewPort
Definition: Camera.h:121
float GetFarPlane() const
Definition: Camera.h:69
Definition: Plane.h:38
CMatrix3D m_ProjMat
Definition: Camera.h:115
const CMatrix3D & GetProjection() const
Definition: Camera.h:50
int m_X
Definition: Camera.h:33
float m_FOV
Definition: Camera.h:120