Pyrogenesis  trunk
Camera.h
Go to the documentation of this file.
1 /* Copyright (C) 2021 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 
27 #include "maths/Frustum.h"
28 #include "maths/Matrix3D.h"
29 
30 #include <array>
31 
32 // view port
33 struct SViewPort
34 {
35  int m_X;
36  int m_Y;
37  int m_Width;
38  int m_Height;
39 };
40 
41 class CCamera
42 {
43  public:
44  // Represents camera viewport or frustum side in 3D space.
45  using Quad = std::array<CVector3D, 4>;
46 
47  enum class ProjectionType
48  {
49  CUSTOM,
50  ORTHO,
51  PERSPECTIVE,
52  };
53 
54  CCamera();
55  ~CCamera();
56 
57  CMatrix3D& GetProjection() { return m_ProjMat; }
58  const CMatrix3D& GetProjection() const { return m_ProjMat; }
59  CMatrix3D GetViewProjection() const { return m_ProjMat * m_Orientation.GetInverse(); }
60  void SetProjection(const CMatrix3D& matrix);
61  void SetProjectionFromCamera(const CCamera& camera);
62  void SetOrthoProjection(float nearp, float farp, float scale);
63  void SetPerspectiveProjection(float nearp, float farp, float fov);
64  ProjectionType GetProjectionType() const { return m_ProjType; }
65 
66  CMatrix3D& GetOrientation() { return m_Orientation; }
67  const CMatrix3D& GetOrientation() const { return m_Orientation; }
68 
69  // Updates the frustum planes. Should be called
70  // everytime the view or projection matrices are
71  // altered.
72  void UpdateFrustum(const CBoundingBoxAligned& scissor = CBoundingBoxAligned(CVector3D(-1.0f, -1.0f, -1.0f), CVector3D(1.0f, 1.0f, 1.0f)));
73  void ClipFrustum(const CPlane& clipPlane);
74  const CFrustum& GetFrustum() const { return m_ViewFrustum; }
75 
76  void SetViewPort(const SViewPort& viewport);
77  const SViewPort& GetViewPort() const { return m_ViewPort; }
78  float GetAspectRatio() const;
79 
80  float GetNearPlane() const { return m_NearPlane; }
81  float GetFarPlane() const { return m_FarPlane; }
82  float GetFOV() const { return m_FOV; }
83  float GetOrthoScale() const { return m_OrthoScale; }
84 
85  // Returns a quad of view in camera space at given distance from camera.
86  void GetViewQuad(float dist, Quad& quad) const;
87 
88  // Builds a ray passing through the screen coordinate (px, py), calculates
89  // origin and direction of the ray.
90  void BuildCameraRay(int px, int py, CVector3D& origin, CVector3D& dir) const;
91 
92  // General helpers that seem to fit here
93 
94  // Get the screen-space coordinates corresponding to a given world-space position
95  void GetScreenCoordinates(const CVector3D& world, float& x, float& y) const;
96 
97  // Get the point on the terrain corresponding to pixel (px,py) (or the mouse coordinates)
98  // The aboveWater parameter determines whether we want to stop at the water plane or also get underwater points
99  CVector3D GetWorldCoordinates(int px, int py, bool aboveWater=false) const;
100  // Get the point on the plane at height h corresponding to pixel (px,py)
101  CVector3D GetWorldCoordinates(int px, int py, float h) const;
102  // Get the point on the terrain (or water plane) the camera is pointing towards
103  CVector3D GetFocus() const;
104 
105  CBoundingBoxAligned GetBoundsInViewPort(const CBoundingBoxAligned& boundigBox) const;
106 
107  // Build an orientation matrix from camera position, camera focus point, and up-vector
108  void LookAt(const CVector3D& camera, const CVector3D& focus, const CVector3D& up);
109 
110  // Build an orientation matrix from camera position, camera orientation, and up-vector
111  void LookAlong(const CVector3D& camera, CVector3D orientation, CVector3D up);
112 
113  public:
114  // This is the orientation matrix. The inverse of this
115  // is the view matrix
117 
118  private:
120  ProjectionType m_ProjType = ProjectionType::CUSTOM;
121 
122  float m_NearPlane = 0.0f;
123  float m_FarPlane = 0.0f;
124  union
125  {
126  float m_FOV;
128  };
130 
132 };
133 
134 #endif // INCLUDED_CAMERA
const CMatrix3D & GetProjection() const
Definition: Camera.h:58
Definition: Camera.h:33
ProjectionType
Definition: Camera.h:47
int m_Y
Definition: Camera.h:36
float GetFarPlane() const
Definition: Camera.h:81
Definition: Vector3D.h:30
Definition: Frustum.h:36
const CMatrix3D & GetOrientation() const
Definition: Camera.h:67
int m_Height
Definition: Camera.h:38
Definition: Matrix3D.h:33
float GetOrthoScale() const
Definition: Camera.h:83
CMatrix3D & GetOrientation()
Definition: Camera.h:66
float GetNearPlane() const
Definition: Camera.h:80
Definition: Camera.h:41
const SViewPort & GetViewPort() const
Definition: Camera.h:77
CMatrix3D & GetProjection()
Definition: Camera.h:57
CFrustum m_ViewFrustum
Definition: Camera.h:131
void GetInverse(CMatrix3D &dst) const
Definition: Matrix3D.cpp:303
Definition: BoundingBoxAligned.h:33
ProjectionType GetProjectionType() const
Definition: Camera.h:64
int m_Width
Definition: Camera.h:37
CMatrix3D GetViewProjection() const
Definition: Camera.h:59
CMatrix3D m_Orientation
Definition: Camera.h:116
SViewPort m_ViewPort
Definition: Camera.h:129
Definition: Plane.h:38
float m_OrthoScale
Definition: Camera.h:127
CMatrix3D m_ProjMat
Definition: Camera.h:119
const CFrustum & GetFrustum() const
Definition: Camera.h:74
std::array< CVector3D, 4 > Quad
Definition: Camera.h:45
int m_X
Definition: Camera.h:35
float GetFOV() const
Definition: Camera.h:82
float m_FOV
Definition: Camera.h:126