LCOV - code coverage report
Current view: top level - source/graphics - Camera.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 8 10 80.0 %
Date: 2026-12-03 01:19:39 Functions: 1 1 100.0 %

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

Generated by: LCOV version 1.16