Fix infinite loop detection when placing players randomly on the newer random map...
[0ad.git] / source / graphics / Camera.h
blob7b69ee2ca8452037af3ed4dcb326db764ee59c17
1 /* Copyright (C) 2013 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
19 * CCamera holds a view and a projection matrix. It also has a frustum
20 * which can be used to cull objects for rendering.
23 #ifndef INCLUDED_CAMERA
24 #define INCLUDED_CAMERA
26 #include "Frustum.h"
27 #include "maths/BoundingBoxAligned.h"
28 #include "maths/Matrix3D.h"
30 // view port
31 struct SViewPort
33 int m_X;
34 int m_Y;
35 int m_Width;
36 int m_Height;
39 class CCamera
41 public:
42 CCamera();
43 ~CCamera();
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; }
52 CMatrix3D& GetOrientation() { return m_Orientation; }
53 const CMatrix3D& GetOrientation() const { return m_Orientation; }
55 CMatrix3D GetViewProjection() const { return m_ProjMat * m_Orientation.GetInverse(); }
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; }
64 void SetViewPort(const SViewPort& viewport);
65 const SViewPort& GetViewPort() const { return m_ViewPort; }
67 // getters
68 float GetNearPlane() const { return m_NearPlane; }
69 float GetFarPlane() const { return m_FarPlane; }
70 float GetFOV() const { return m_FOV; }
72 // return four points in camera space at given distance from camera
73 void GetCameraPlanePoints(float dist,CVector3D pts[4]) const;
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;
81 // General helpers that seem to fit here
83 // Get the screen-space coordinates corresponding to a given world-space position
84 void GetScreenCoordinates(const CVector3D& world, float& x, float& y) const;
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;
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);
97 // Build an orientation matrix from camera position, camera orientation, and up-vector
98 void LookAlong(CVector3D camera, CVector3D focus, CVector3D up);
101 * Render: Renders the camera's frustum in world space.
102 * The caller should set the color using glColorXy before calling Render.
104 * @param intermediates determines how many intermediate distance planes should
105 * be hinted at between the near and far planes
107 void Render(int intermediates = 0) const;
109 public:
110 // This is the orientation matrix. The inverse of this
111 // is the view matrix
112 CMatrix3D m_Orientation;
114 // Should not be tweaked externally if possible
115 CMatrix3D m_ProjMat;
117 private:
118 float m_NearPlane;
119 float m_FarPlane;
120 float m_FOV;
121 SViewPort m_ViewPort;
123 CFrustum m_ViewFrustum;
126 #endif