2021-03-18 00:03:35 +01:00
|
|
|
/* Copyright (C) 2021 Wildfire Games.
|
2023-12-03 01:30:12 +01:00
|
|
|
* This file is part of 0 A.D.
|
2009-04-18 19:00:33 +02:00
|
|
|
*
|
2023-12-03 01:30:12 +01:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2009-04-18 19:00:33 +02:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2023-12-03 01:30:12 +01:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2009-04-18 19:00:33 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2023-12-03 01:30:12 +01:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2009-04-18 19:00:33 +02:00
|
|
|
*/
|
|
|
|
|
2009-04-18 19:51:05 +02:00
|
|
|
/*
|
|
|
|
* CCamera holds a view and a projection matrix. It also has a frustum
|
|
|
|
* which can be used to cull objects for rendering.
|
2007-05-07 18:33:24 +02:00
|
|
|
*/
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2007-05-07 18:33:24 +02:00
|
|
|
#ifndef INCLUDED_CAMERA
|
|
|
|
#define INCLUDED_CAMERA
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2011-11-25 07:36:13 +01:00
|
|
|
#include "maths/BoundingBoxAligned.h"
|
2021-03-29 19:28:13 +02:00
|
|
|
#include "maths/Frustum.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "maths/Matrix3D.h"
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2019-07-25 01:08:02 +02:00
|
|
|
#include <array>
|
|
|
|
|
2005-12-02 22:08:10 +01:00
|
|
|
// view port
|
2004-05-29 22:56:24 +02:00
|
|
|
struct SViewPort
|
|
|
|
{
|
2013-06-11 02:05:57 +02:00
|
|
|
int m_X;
|
|
|
|
int m_Y;
|
|
|
|
int m_Width;
|
|
|
|
int m_Height;
|
2004-05-29 22:56:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class CCamera
|
|
|
|
{
|
|
|
|
public:
|
2019-07-25 01:08:02 +02:00
|
|
|
// Represents camera viewport or frustum side in 3D space.
|
|
|
|
using Quad = std::array<CVector3D, 4>;
|
|
|
|
|
2021-03-18 00:03:35 +01:00
|
|
|
enum class ProjectionType
|
2019-10-03 20:51:40 +02:00
|
|
|
{
|
|
|
|
CUSTOM,
|
2021-03-18 00:03:35 +01:00
|
|
|
ORTHO,
|
2019-10-03 20:51:40 +02:00
|
|
|
PERSPECTIVE,
|
|
|
|
};
|
|
|
|
|
2011-07-13 01:48:05 +02:00
|
|
|
CCamera();
|
|
|
|
~CCamera();
|
2016-11-23 12:18:37 +01:00
|
|
|
|
2011-07-13 01:48:05 +02:00
|
|
|
CMatrix3D& GetProjection() { return m_ProjMat; }
|
|
|
|
const CMatrix3D& GetProjection() const { return m_ProjMat; }
|
2019-06-27 00:54:05 +02:00
|
|
|
CMatrix3D GetViewProjection() const { return m_ProjMat * m_Orientation.GetInverse(); }
|
2019-10-03 20:51:40 +02:00
|
|
|
void SetProjection(const CMatrix3D& matrix);
|
2019-08-22 22:49:58 +02:00
|
|
|
void SetProjectionFromCamera(const CCamera& camera);
|
2021-03-18 00:03:35 +01:00
|
|
|
void SetOrthoProjection(float nearp, float farp, float scale);
|
2019-06-27 00:54:05 +02:00
|
|
|
void SetPerspectiveProjection(float nearp, float farp, float fov);
|
2019-10-03 20:51:40 +02:00
|
|
|
ProjectionType GetProjectionType() const { return m_ProjType; }
|
2011-07-13 01:48:05 +02:00
|
|
|
|
|
|
|
CMatrix3D& GetOrientation() { return m_Orientation; }
|
|
|
|
const CMatrix3D& GetOrientation() const { return m_Orientation; }
|
|
|
|
|
2005-12-02 22:08:10 +01:00
|
|
|
// Updates the frustum planes. Should be called
|
|
|
|
// everytime the view or projection matrices are
|
|
|
|
// altered.
|
2011-11-25 07:36:13 +01:00
|
|
|
void UpdateFrustum(const CBoundingBoxAligned& scissor = CBoundingBoxAligned(CVector3D(-1.0f, -1.0f, -1.0f), CVector3D(1.0f, 1.0f, 1.0f)));
|
2011-07-13 01:48:05 +02:00
|
|
|
void ClipFrustum(const CPlane& clipPlane);
|
|
|
|
const CFrustum& GetFrustum() const { return m_ViewFrustum; }
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2011-07-13 01:48:05 +02:00
|
|
|
void SetViewPort(const SViewPort& viewport);
|
|
|
|
const SViewPort& GetViewPort() const { return m_ViewPort; }
|
2019-07-25 01:08:02 +02:00
|
|
|
float GetAspectRatio() const;
|
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; }
|
2021-03-18 00:03:35 +01:00
|
|
|
float GetOrthoScale() const { return m_OrthoScale; }
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2019-07-25 01:08:02 +02:00
|
|
|
// Returns a quad of view in camera space at given distance from camera.
|
|
|
|
void GetViewQuad(float dist, Quad& quad) const;
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2021-03-19 18:39:34 +01:00
|
|
|
// Builds a ray passing through the screen coordinate (px, py), calculates
|
|
|
|
// origin and direction of the ray.
|
2010-08-13 15:26:29 +02:00
|
|
|
void BuildCameraRay(int px, int py, CVector3D& origin, CVector3D& dir) const;
|
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
|
|
|
|
2016-11-23 12:18:37 +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)
|
2006-09-10 22:38:39 +02:00
|
|
|
// The aboveWater parameter determines whether we want to stop at the water plane or also get underwater points
|
2010-08-13 15:26:29 +02:00
|
|
|
CVector3D GetWorldCoordinates(int px, int py, bool aboveWater=false) const;
|
2005-12-02 22:08:10 +01:00
|
|
|
// Get the point on the plane at height h corresponding to pixel (px,py)
|
2010-08-13 15:26:29 +02:00
|
|
|
CVector3D GetWorldCoordinates(int px, int py, float h) const;
|
2011-08-11 00:45:55 +02:00
|
|
|
// Get the point on the terrain (or water plane) the camera is pointing towards
|
|
|
|
CVector3D GetFocus() const;
|
2004-08-03 01:14:54 +02:00
|
|
|
|
2021-05-01 18:59:42 +02:00
|
|
|
CBoundingBoxAligned GetBoundsInViewPort(const CBoundingBoxAligned& boundigBox) const;
|
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
// Build an orientation matrix from camera position, camera focus point, and up-vector
|
2021-03-18 00:03:35 +01:00
|
|
|
void LookAt(const CVector3D& camera, const CVector3D& focus, const CVector3D& up);
|
2004-08-03 01:14:54 +02:00
|
|
|
|
|
|
|
// Build an orientation matrix from camera position, camera orientation, and up-vector
|
2021-03-18 00:03:35 +01:00
|
|
|
void LookAlong(const CVector3D& camera, CVector3D orientation, CVector3D up);
|
2004-07-22 18:18:12 +02:00
|
|
|
|
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
|
2019-10-03 20:51:40 +02:00
|
|
|
CMatrix3D m_Orientation;
|
2016-11-23 12:18:37 +01:00
|
|
|
|
2019-06-27 00:54:05 +02:00
|
|
|
private:
|
2019-10-03 20:51:40 +02:00
|
|
|
CMatrix3D m_ProjMat;
|
2021-03-18 00:03:35 +01:00
|
|
|
ProjectionType m_ProjType = ProjectionType::CUSTOM;
|
2019-10-03 20:51:40 +02:00
|
|
|
|
2021-03-18 00:03:35 +01:00
|
|
|
float m_NearPlane = 0.0f;
|
|
|
|
float m_FarPlane = 0.0f;
|
|
|
|
union
|
|
|
|
{
|
|
|
|
float m_FOV;
|
|
|
|
float m_OrthoScale;
|
|
|
|
};
|
|
|
|
SViewPort m_ViewPort;
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2021-03-18 00:03:35 +01:00
|
|
|
CFrustum m_ViewFrustum;
|
2004-05-29 22:56:24 +02:00
|
|
|
};
|
|
|
|
|
2019-01-05 23:40:56 +01:00
|
|
|
#endif // INCLUDED_CAMERA
|