2010-06-03 21:07:59 +02:00
|
|
|
/* Copyright (C) 2010 Wildfire Games.
|
2009-04-18 19:00:33 +02:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* 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
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
|
|
|
#include "Frustum.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "maths/Matrix3D.h"
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2005-12-02 22:08:10 +01:00
|
|
|
// view port
|
2004-05-29 22:56:24 +02:00
|
|
|
struct SViewPort
|
|
|
|
{
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
size_t m_X;
|
|
|
|
size_t m_Y;
|
|
|
|
size_t m_Width;
|
|
|
|
size_t m_Height;
|
2004-05-29 22:56:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class CCamera
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CCamera ();
|
|
|
|
~CCamera ();
|
|
|
|
|
2005-12-02 22:08:10 +01:00
|
|
|
// Methods for projection
|
2004-05-29 22:56:24 +02:00
|
|
|
void SetProjection (float nearp, float farp, float fov);
|
2005-10-24 03:53:03 +02:00
|
|
|
void SetProjectionTile (int tiles, int tile_x, int tile_y);
|
2006-09-14 22:06:21 +02:00
|
|
|
CMatrix3D& GetProjection () { return m_ProjMat; }
|
|
|
|
const CMatrix3D& GetProjection () const { return m_ProjMat; }
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2005-12-02 22:08:10 +01:00
|
|
|
// Updates the frustum planes. Should be called
|
|
|
|
// everytime the view or projection matrices are
|
|
|
|
// altered.
|
2004-05-29 22:56:24 +02:00
|
|
|
void UpdateFrustum ();
|
2010-08-13 15:26:29 +02:00
|
|
|
const CFrustum& GetFrustum () const { return m_ViewFrustum; }
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2010-06-03 21:07:59 +02:00
|
|
|
void SetViewPort (const SViewPort& viewport);
|
2006-09-14 22:06:21 +02:00
|
|
|
const SViewPort& GetViewPort () const { return m_ViewPort; }
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2005-12-02 22:08:10 +01:00
|
|
|
// getters
|
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; }
|
2004-05-30 02:46:58 +02:00
|
|
|
|
|
|
|
// return four points in camera space at given distance from camera
|
|
|
|
void GetCameraPlanePoints(float dist,CVector3D pts[4]) const;
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2004-07-20 21:30:35 +02:00
|
|
|
// Build a ray passing through the screen coordinate (px, py) and the camera
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
2005-12-02 22:08:10 +01:00
|
|
|
// BuildCameraRay: calculate origin and ray direction of a ray through
|
|
|
|
// the pixel (px,py) on the screen
|
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
|
|
|
|
2005-12-02 22:08:10 +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;
|
2005-12-02 22:08:10 +01:00
|
|
|
// Get the point on the terrain the camera is pointing towards
|
2004-08-03 01:14:54 +02:00
|
|
|
CVector3D GetFocus();
|
|
|
|
|
|
|
|
// Build an orientation matrix from camera position, camera focus point, and up-vector
|
2005-12-02 22:08:10 +01:00
|
|
|
void LookAt(const CVector3D& camera, const CVector3D& orientation, const CVector3D& up);
|
2004-08-03 01:14:54 +02:00
|
|
|
|
|
|
|
// Build an orientation matrix from camera position, camera orientation, and up-vector
|
2005-12-02 22:08:10 +01:00
|
|
|
void LookAlong(CVector3D camera, CVector3D focus, CVector3D up);
|
2004-07-22 18:18:12 +02:00
|
|
|
|
2006-01-22 20:12:30 +01:00
|
|
|
/**
|
|
|
|
* Render: Renders the camera's frustum in world space.
|
|
|
|
* The caller should set the color using glColorXy before calling Render.
|
|
|
|
*
|
|
|
|
* @param intermediates determines how many intermediate distance planes should
|
|
|
|
* be hinted at between the near and far planes
|
|
|
|
*/
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
void Render(int intermediates = 0) const;
|
2006-01-22 20:12:30 +01: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
|
2004-05-29 22:56:24 +02:00
|
|
|
CMatrix3D m_Orientation;
|
2006-06-01 00:37:23 +02:00
|
|
|
|
|
|
|
// Should not be tweaked externally if possible
|
2004-05-29 22:56:24 +02:00
|
|
|
CMatrix3D m_ProjMat;
|
|
|
|
|
2006-06-01 00:37:23 +02:00
|
|
|
private:
|
2004-05-29 22:56:24 +02:00
|
|
|
float m_NearPlane;
|
|
|
|
float m_FarPlane;
|
|
|
|
float m_FOV;
|
|
|
|
SViewPort m_ViewPort;
|
|
|
|
|
|
|
|
CFrustum m_ViewFrustum;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|