2019-01-05 23:40:56 +01:00
|
|
|
/* Copyright (C) 2019 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-30 02:46:58 +02:00
|
|
|
|
2004-06-03 20:38:14 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
#include "Camera.h"
|
2010-07-25 16:11:21 +02:00
|
|
|
|
|
|
|
#include "graphics/HFTracer.h"
|
|
|
|
#include "graphics/Terrain.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "lib/ogl.h"
|
2010-07-25 16:11:21 +02:00
|
|
|
#include "maths/MathUtil.h"
|
2011-01-28 22:51:59 +01:00
|
|
|
#include "maths/Vector4D.h"
|
2010-07-25 16:11:21 +02:00
|
|
|
#include "ps/Game.h"
|
2006-03-21 21:55:45 +01:00
|
|
|
#include "ps/World.h"
|
2010-07-25 16:11:21 +02:00
|
|
|
#include "renderer/Renderer.h"
|
|
|
|
#include "renderer/WaterManager.h"
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2011-07-13 01:48:05 +02:00
|
|
|
CCamera::CCamera()
|
2019-10-03 20:51:40 +02:00
|
|
|
: m_NearPlane(0.0f), m_FarPlane(0.0f), m_FOV(0.0f), m_ProjType(CUSTOM)
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
2019-08-22 22:49:58 +02:00
|
|
|
// Set viewport to something anything should handle, but should be initialised
|
|
|
|
// to window size before use.
|
2004-05-30 02:46:58 +02:00
|
|
|
m_ViewPort.m_X = 0;
|
|
|
|
m_ViewPort.m_Y = 0;
|
|
|
|
m_ViewPort.m_Width = 800;
|
|
|
|
m_ViewPort.m_Height = 600;
|
|
|
|
}
|
|
|
|
|
2019-01-05 23:40:56 +01:00
|
|
|
CCamera::~CCamera() = default;
|
2005-10-24 03:53:03 +02:00
|
|
|
|
2019-10-03 20:51:40 +02:00
|
|
|
void CCamera::SetProjection(const CMatrix3D& matrix)
|
|
|
|
{
|
|
|
|
m_ProjType = CUSTOM;
|
|
|
|
m_ProjMat = matrix;
|
|
|
|
}
|
|
|
|
|
2019-08-22 22:49:58 +02:00
|
|
|
void CCamera::SetProjectionFromCamera(const CCamera& camera)
|
|
|
|
{
|
2019-10-03 20:51:40 +02:00
|
|
|
m_ProjType = camera.m_ProjType;
|
2019-08-22 22:49:58 +02:00
|
|
|
m_NearPlane = camera.m_NearPlane;
|
|
|
|
m_FarPlane = camera.m_FarPlane;
|
2019-10-03 20:51:40 +02:00
|
|
|
if (m_ProjType == PERSPECTIVE)
|
|
|
|
{
|
|
|
|
m_FOV = camera.m_FOV;
|
|
|
|
}
|
2019-08-22 22:49:58 +02:00
|
|
|
m_ProjMat = camera.m_ProjMat;
|
|
|
|
}
|
|
|
|
|
2019-06-27 00:54:05 +02:00
|
|
|
void CCamera::SetPerspectiveProjection(float nearp, float farp, float fov)
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
2019-10-03 20:51:40 +02:00
|
|
|
m_ProjType = PERSPECTIVE;
|
2004-05-30 02:46:58 +02:00
|
|
|
m_NearPlane = nearp;
|
|
|
|
m_FarPlane = farp;
|
|
|
|
m_FOV = fov;
|
2006-01-22 20:12:30 +01:00
|
|
|
|
2019-07-25 01:08:02 +02:00
|
|
|
m_ProjMat.SetPerspective(m_FOV, GetAspectRatio(), m_NearPlane, m_FarPlane);
|
2005-10-24 03:53:03 +02:00
|
|
|
}
|
|
|
|
|
2019-01-05 23:40:56 +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 CCamera::UpdateFrustum(const CBoundingBoxAligned& scissor)
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
|
|
|
CMatrix3D MatFinal;
|
|
|
|
CMatrix3D MatView;
|
|
|
|
|
|
|
|
m_Orientation.GetInverse(MatView);
|
2006-01-22 20:12:30 +01:00
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
MatFinal = m_ProjMat * MatView;
|
|
|
|
|
2011-07-13 01:48:05 +02:00
|
|
|
m_ViewFrustum.SetNumPlanes(6);
|
|
|
|
|
2014-06-25 03:11:10 +02:00
|
|
|
// get the RIGHT plane
|
2011-07-13 01:48:05 +02:00
|
|
|
m_ViewFrustum.m_aPlanes[0].m_Norm.X = scissor[1].X*MatFinal._41 - MatFinal._11;
|
|
|
|
m_ViewFrustum.m_aPlanes[0].m_Norm.Y = scissor[1].X*MatFinal._42 - MatFinal._12;
|
|
|
|
m_ViewFrustum.m_aPlanes[0].m_Norm.Z = scissor[1].X*MatFinal._43 - MatFinal._13;
|
|
|
|
m_ViewFrustum.m_aPlanes[0].m_Dist = scissor[1].X*MatFinal._44 - MatFinal._14;
|
|
|
|
|
|
|
|
// get the LEFT plane
|
|
|
|
m_ViewFrustum.m_aPlanes[1].m_Norm.X = -scissor[0].X*MatFinal._41 + MatFinal._11;
|
|
|
|
m_ViewFrustum.m_aPlanes[1].m_Norm.Y = -scissor[0].X*MatFinal._42 + MatFinal._12;
|
|
|
|
m_ViewFrustum.m_aPlanes[1].m_Norm.Z = -scissor[0].X*MatFinal._43 + MatFinal._13;
|
|
|
|
m_ViewFrustum.m_aPlanes[1].m_Dist = -scissor[0].X*MatFinal._44 + MatFinal._14;
|
|
|
|
|
|
|
|
// get the BOTTOM plane
|
|
|
|
m_ViewFrustum.m_aPlanes[2].m_Norm.X = -scissor[0].Y*MatFinal._41 + MatFinal._21;
|
|
|
|
m_ViewFrustum.m_aPlanes[2].m_Norm.Y = -scissor[0].Y*MatFinal._42 + MatFinal._22;
|
|
|
|
m_ViewFrustum.m_aPlanes[2].m_Norm.Z = -scissor[0].Y*MatFinal._43 + MatFinal._23;
|
|
|
|
m_ViewFrustum.m_aPlanes[2].m_Dist = -scissor[0].Y*MatFinal._44 + MatFinal._24;
|
|
|
|
|
|
|
|
// get the TOP plane
|
|
|
|
m_ViewFrustum.m_aPlanes[3].m_Norm.X = scissor[1].Y*MatFinal._41 - MatFinal._21;
|
|
|
|
m_ViewFrustum.m_aPlanes[3].m_Norm.Y = scissor[1].Y*MatFinal._42 - MatFinal._22;
|
|
|
|
m_ViewFrustum.m_aPlanes[3].m_Norm.Z = scissor[1].Y*MatFinal._43 - MatFinal._23;
|
|
|
|
m_ViewFrustum.m_aPlanes[3].m_Dist = scissor[1].Y*MatFinal._44 - MatFinal._24;
|
|
|
|
|
|
|
|
// get the FAR plane
|
|
|
|
m_ViewFrustum.m_aPlanes[4].m_Norm.X = scissor[1].Z*MatFinal._41 - MatFinal._31;
|
|
|
|
m_ViewFrustum.m_aPlanes[4].m_Norm.Y = scissor[1].Z*MatFinal._42 - MatFinal._32;
|
|
|
|
m_ViewFrustum.m_aPlanes[4].m_Norm.Z = scissor[1].Z*MatFinal._43 - MatFinal._33;
|
|
|
|
m_ViewFrustum.m_aPlanes[4].m_Dist = scissor[1].Z*MatFinal._44 - MatFinal._34;
|
|
|
|
|
|
|
|
// get the NEAR plane
|
|
|
|
m_ViewFrustum.m_aPlanes[5].m_Norm.X = -scissor[0].Z*MatFinal._41 + MatFinal._31;
|
|
|
|
m_ViewFrustum.m_aPlanes[5].m_Norm.Y = -scissor[0].Z*MatFinal._42 + MatFinal._32;
|
|
|
|
m_ViewFrustum.m_aPlanes[5].m_Norm.Z = -scissor[0].Z*MatFinal._43 + MatFinal._33;
|
|
|
|
m_ViewFrustum.m_aPlanes[5].m_Dist = -scissor[0].Z*MatFinal._44 + MatFinal._34;
|
2014-06-01 20:05:46 +02:00
|
|
|
|
|
|
|
for (size_t i = 0; i < 6; ++i)
|
|
|
|
m_ViewFrustum.m_aPlanes[i].Normalize();
|
2011-07-13 01:48:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCamera::ClipFrustum(const CPlane& clipPlane)
|
|
|
|
{
|
2014-06-01 20:05:46 +02:00
|
|
|
CPlane normClipPlane = clipPlane;
|
|
|
|
normClipPlane.Normalize();
|
|
|
|
m_ViewFrustum.AddPlane(normClipPlane);
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
|
2011-07-13 01:48:05 +02:00
|
|
|
void CCamera::SetViewPort(const SViewPort& viewport)
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
2010-06-03 21:07:59 +02:00
|
|
|
m_ViewPort.m_X = viewport.m_X;
|
|
|
|
m_ViewPort.m_Y = viewport.m_Y;
|
|
|
|
m_ViewPort.m_Width = viewport.m_Width;
|
|
|
|
m_ViewPort.m_Height = viewport.m_Height;
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
|
2019-07-25 01:08:02 +02:00
|
|
|
float CCamera::GetAspectRatio() const
|
|
|
|
{
|
|
|
|
return static_cast<float>(m_ViewPort.m_Width) / static_cast<float>(m_ViewPort.m_Height);
|
|
|
|
}
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2019-07-25 01:08:02 +02:00
|
|
|
void CCamera::GetViewQuad(float dist, Quad& quad) const
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
2019-10-03 20:51:40 +02:00
|
|
|
ENSURE(m_ProjType == PERSPECTIVE);
|
2019-07-25 01:08:02 +02:00
|
|
|
const float y = dist * tanf(m_FOV * 0.5f);
|
|
|
|
const float x = y * GetAspectRatio();
|
|
|
|
|
|
|
|
quad[0].X = -x;
|
|
|
|
quad[0].Y = -y;
|
|
|
|
quad[0].Z = dist;
|
|
|
|
quad[1].X = x;
|
|
|
|
quad[1].Y = -y;
|
|
|
|
quad[1].Z = dist;
|
|
|
|
quad[2].X = x;
|
|
|
|
quad[2].Y = y;
|
|
|
|
quad[2].Z = dist;
|
|
|
|
quad[3].X = -x;
|
|
|
|
quad[3].Y = y;
|
|
|
|
quad[3].Z = dist;
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
|
2010-08-13 15:26:29 +02:00
|
|
|
void CCamera::BuildCameraRay(int px, int py, CVector3D& origin, CVector3D& dir) const
|
2004-07-20 21:30:35 +02:00
|
|
|
{
|
2019-07-25 01:08:02 +02:00
|
|
|
// Coordinates relative to the camera plane.
|
|
|
|
const float dx = static_cast<float>(px) / g_Renderer.GetWidth();
|
|
|
|
const float dy = 1.0f - static_cast<float>(py) / g_Renderer.GetHeight();
|
2004-07-20 21:30:35 +02:00
|
|
|
|
2019-07-25 01:08:02 +02:00
|
|
|
Quad points;
|
|
|
|
GetViewQuad(m_FarPlane, points);
|
2004-07-20 21:30:35 +02:00
|
|
|
|
2019-07-25 01:08:02 +02:00
|
|
|
// Transform from camera space to world space.
|
|
|
|
for (CVector3D& point : points)
|
|
|
|
point = m_Orientation.Transform(point);
|
2004-07-20 21:30:35 +02:00
|
|
|
|
2019-07-25 01:08:02 +02:00
|
|
|
// Get world space position of mouse point at the far clipping plane.
|
|
|
|
CVector3D basisX = points[1] - points[0];
|
|
|
|
CVector3D basisY = points[3] - points[0];
|
|
|
|
CVector3D targetPoint = points[0] + (basisX * dx) + (basisY * dy);
|
2004-07-20 21:30:35 +02:00
|
|
|
|
|
|
|
origin = m_Orientation.GetTranslation();
|
2019-07-25 01:08:02 +02:00
|
|
|
|
|
|
|
// Build direction for the camera origin to the target point.
|
|
|
|
dir = targetPoint - origin;
|
2004-07-20 21:30:35 +02:00
|
|
|
dir.Normalize();
|
|
|
|
}
|
|
|
|
|
2010-08-13 15:26:29 +02:00
|
|
|
void CCamera::GetScreenCoordinates(const CVector3D& world, float& x, float& y) const
|
2004-07-20 21:30:35 +02:00
|
|
|
{
|
2011-04-01 20:33:47 +02:00
|
|
|
CMatrix3D transform = m_ProjMat * m_Orientation.GetInverse();
|
2004-07-20 21:30:35 +02:00
|
|
|
|
2011-01-28 22:51:59 +01:00
|
|
|
CVector4D screenspace = transform.Transform(CVector4D(world.X, world.Y, world.Z, 1.0f));
|
2004-07-20 21:30:35 +02:00
|
|
|
|
2012-04-08 17:55:06 +02:00
|
|
|
x = screenspace.X / screenspace.W;
|
|
|
|
y = screenspace.Y / screenspace.W;
|
2010-08-13 15:26:29 +02:00
|
|
|
x = (x + 1) * 0.5f * g_Renderer.GetWidth();
|
|
|
|
y = (1 - y) * 0.5f * g_Renderer.GetHeight();
|
2004-07-22 18:18:12 +02:00
|
|
|
}
|
|
|
|
|
2010-08-13 15:26:29 +02:00
|
|
|
CVector3D CCamera::GetWorldCoordinates(int px, int py, bool aboveWater) const
|
2004-08-03 01:14:54 +02:00
|
|
|
{
|
2010-08-13 15:26:29 +02:00
|
|
|
CHFTracer tracer(g_Game->GetWorld()->GetTerrain());
|
2006-05-29 05:28:54 +02:00
|
|
|
int x, z;
|
2006-09-10 22:38:39 +02:00
|
|
|
CVector3D origin, dir, delta, terrainPoint, waterPoint;
|
2006-01-22 20:12:30 +01:00
|
|
|
|
2010-08-13 15:26:29 +02:00
|
|
|
BuildCameraRay(px, py, origin, dir);
|
2004-08-03 01:14:54 +02:00
|
|
|
|
|
|
|
|
2010-08-13 15:26:29 +02:00
|
|
|
bool gotTerrain = tracer.RayIntersect(origin, dir, x, z, terrainPoint);
|
2006-09-10 22:38:39 +02:00
|
|
|
|
2010-08-13 15:26:29 +02:00
|
|
|
if (!aboveWater)
|
2006-09-10 22:38:39 +02:00
|
|
|
{
|
2010-08-13 15:26:29 +02:00
|
|
|
if (gotTerrain)
|
2006-09-10 22:38:39 +02:00
|
|
|
return terrainPoint;
|
|
|
|
|
|
|
|
// Off the edge of the world?
|
|
|
|
// Work out where it /would/ hit, if the map were extended out to infinity with average height.
|
2010-08-13 15:26:29 +02:00
|
|
|
return GetWorldCoordinates(px, py, 50.0f);
|
2006-09-10 22:38:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CPlane plane;
|
|
|
|
plane.Set(CVector3D(0.f, 1.f, 0.f), // upwards normal
|
|
|
|
CVector3D(0.f, g_Renderer.GetWaterManager()->m_WaterHeight, 0.f)); // passes through water plane
|
|
|
|
|
|
|
|
bool gotWater = plane.FindRayIntersection( origin, dir, &waterPoint );
|
|
|
|
|
2010-07-25 16:11:21 +02:00
|
|
|
// Clamp the water intersection to within the map's bounds, so that
|
|
|
|
// we'll always return a valid position on the map
|
|
|
|
ssize_t mapSize = g_Game->GetWorld()->GetTerrain()->GetVerticesPerSide();
|
|
|
|
if (gotWater)
|
|
|
|
{
|
2019-09-18 10:34:36 +02:00
|
|
|
waterPoint.X = Clamp(waterPoint.X, 0.f, static_cast<float>((mapSize - 1) * TERRAIN_TILE_SIZE));
|
|
|
|
waterPoint.Z = Clamp(waterPoint.Z, 0.f, static_cast<float>((mapSize - 1) * TERRAIN_TILE_SIZE));
|
2010-07-25 16:11:21 +02:00
|
|
|
}
|
|
|
|
|
2010-08-13 15:26:29 +02:00
|
|
|
if (gotTerrain)
|
2006-09-10 22:38:39 +02:00
|
|
|
{
|
2010-08-13 15:26:29 +02:00
|
|
|
if (gotWater)
|
2006-09-10 22:38:39 +02:00
|
|
|
{
|
|
|
|
// Intersecting both heightmap and water plane; choose the closest of those
|
2010-08-13 15:26:29 +02:00
|
|
|
if ((origin - terrainPoint).LengthSquared() < (origin - waterPoint).LengthSquared())
|
2006-09-10 22:38:39 +02:00
|
|
|
return terrainPoint;
|
|
|
|
else
|
|
|
|
return waterPoint;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Intersecting heightmap but parallel to water plane
|
|
|
|
return terrainPoint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-08-13 15:26:29 +02:00
|
|
|
if (gotWater)
|
2006-09-10 22:38:39 +02:00
|
|
|
{
|
|
|
|
// Only intersecting water plane
|
|
|
|
return waterPoint;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Not intersecting terrain or water; just return 0,0,0.
|
|
|
|
return CVector3D(0.f, 0.f, 0.f);
|
|
|
|
}
|
|
|
|
}
|
2004-08-03 01:14:54 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-08-13 15:26:29 +02:00
|
|
|
CVector3D CCamera::GetWorldCoordinates(int px, int py, float h) const
|
2005-12-02 22:08:10 +01:00
|
|
|
{
|
|
|
|
CPlane plane;
|
|
|
|
plane.Set(CVector3D(0.f, 1.f, 0.f), CVector3D(0.f, h, 0.f)); // upwards normal, passes through h
|
|
|
|
|
|
|
|
CVector3D origin, dir, delta, currentTarget;
|
|
|
|
|
|
|
|
BuildCameraRay(px, py, origin, dir);
|
|
|
|
|
|
|
|
if (plane.FindRayIntersection(origin, dir, ¤tTarget))
|
|
|
|
return currentTarget;
|
|
|
|
|
|
|
|
// No intersection with the infinite plane - nothing sensible can be returned,
|
|
|
|
// so just choose an arbitrary point on the plane
|
|
|
|
return CVector3D(0.f, h, 0.f);
|
|
|
|
}
|
|
|
|
|
2011-08-11 00:45:55 +02:00
|
|
|
CVector3D CCamera::GetFocus() const
|
2004-08-03 01:14:54 +02:00
|
|
|
{
|
|
|
|
// Basically the same as GetWorldCoordinates
|
|
|
|
|
2010-08-13 15:26:29 +02:00
|
|
|
CHFTracer tracer(g_Game->GetWorld()->GetTerrain());
|
|
|
|
int x, z;
|
2004-08-03 01:14:54 +02:00
|
|
|
|
2011-08-11 00:45:55 +02:00
|
|
|
CVector3D origin, dir, delta, terrainPoint, waterPoint;
|
2006-01-22 20:12:30 +01:00
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
origin = m_Orientation.GetTranslation();
|
|
|
|
dir = m_Orientation.GetIn();
|
|
|
|
|
2011-08-11 00:45:55 +02:00
|
|
|
bool gotTerrain = tracer.RayIntersect(origin, dir, x, z, terrainPoint);
|
|
|
|
|
|
|
|
CPlane plane;
|
|
|
|
plane.Set(CVector3D(0.f, 1.f, 0.f), // upwards normal
|
|
|
|
CVector3D(0.f, g_Renderer.GetWaterManager()->m_WaterHeight, 0.f)); // passes through water plane
|
2004-08-03 01:14:54 +02:00
|
|
|
|
2011-08-11 00:45:55 +02:00
|
|
|
bool gotWater = plane.FindRayIntersection( origin, dir, &waterPoint );
|
2004-08-03 01:14:54 +02:00
|
|
|
|
2011-08-11 00:45:55 +02:00
|
|
|
// Clamp the water intersection to within the map's bounds, so that
|
|
|
|
// we'll always return a valid position on the map
|
|
|
|
ssize_t mapSize = g_Game->GetWorld()->GetTerrain()->GetVerticesPerSide();
|
|
|
|
if (gotWater)
|
|
|
|
{
|
2019-09-18 10:34:36 +02:00
|
|
|
waterPoint.X = Clamp(waterPoint.X, 0.f, static_cast<float>((mapSize - 1) * TERRAIN_TILE_SIZE));
|
|
|
|
waterPoint.Z = Clamp(waterPoint.Z, 0.f, static_cast<float>((mapSize - 1) * TERRAIN_TILE_SIZE));
|
2011-08-11 00:45:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (gotTerrain)
|
|
|
|
{
|
|
|
|
if (gotWater)
|
|
|
|
{
|
|
|
|
// Intersecting both heightmap and water plane; choose the closest of those
|
|
|
|
if ((origin - terrainPoint).LengthSquared() < (origin - waterPoint).LengthSquared())
|
|
|
|
return terrainPoint;
|
|
|
|
else
|
|
|
|
return waterPoint;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Intersecting heightmap but parallel to water plane
|
|
|
|
return terrainPoint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (gotWater)
|
|
|
|
{
|
|
|
|
// Only intersecting water plane
|
|
|
|
return waterPoint;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Not intersecting terrain or water; just return 0,0,0.
|
|
|
|
return CVector3D(0.f, 0.f, 0.f);
|
|
|
|
}
|
|
|
|
}
|
2004-08-03 01:14:54 +02:00
|
|
|
}
|
|
|
|
|
2010-08-13 15:26:29 +02:00
|
|
|
void CCamera::LookAt(const CVector3D& camera, const CVector3D& target, const CVector3D& up)
|
2004-07-22 18:18:12 +02:00
|
|
|
{
|
|
|
|
CVector3D delta = target - camera;
|
2010-08-13 15:26:29 +02:00
|
|
|
LookAlong(camera, delta, up);
|
2004-08-03 01:14:54 +02:00
|
|
|
}
|
|
|
|
|
2017-05-08 06:04:45 +02:00
|
|
|
void CCamera::LookAlong(const CVector3D& camera, CVector3D orientation, CVector3D up)
|
2004-08-03 01:14:54 +02:00
|
|
|
{
|
|
|
|
orientation.Normalize();
|
|
|
|
up.Normalize();
|
2010-08-13 15:26:29 +02:00
|
|
|
CVector3D s = orientation.Cross(up);
|
2004-10-07 21:23:35 +02:00
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
m_Orientation._11 = -s.X; m_Orientation._12 = up.X; m_Orientation._13 = orientation.X; m_Orientation._14 = camera.X;
|
|
|
|
m_Orientation._21 = -s.Y; m_Orientation._22 = up.Y; m_Orientation._23 = orientation.Y; m_Orientation._24 = camera.Y;
|
|
|
|
m_Orientation._31 = -s.Z; m_Orientation._32 = up.Z; m_Orientation._33 = orientation.Z; m_Orientation._34 = camera.Z;
|
|
|
|
m_Orientation._41 = 0.0f; m_Orientation._42 = 0.0f; m_Orientation._43 = 0.0f; m_Orientation._44 = 1.0f;
|
2004-08-09 18:44:08 +02:00
|
|
|
}
|
2006-01-22 20:12:30 +01:00
|
|
|
|
|
|
|
// Render the camera's frustum
|
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 CCamera::Render(int intermediates) const
|
2006-01-22 20:12:30 +01:00
|
|
|
{
|
2012-02-13 15:02:14 +01:00
|
|
|
#if CONFIG2_GLES
|
|
|
|
#warning TODO: implement camera frustum for GLES
|
|
|
|
#else
|
2019-07-25 01:08:02 +02:00
|
|
|
Quad nearPoints;
|
|
|
|
Quad farPoints;
|
2006-01-22 20:12:30 +01:00
|
|
|
|
2019-07-25 01:08:02 +02:00
|
|
|
GetViewQuad(m_NearPlane, nearPoints);
|
|
|
|
GetViewQuad(m_FarPlane, farPoints);
|
2006-01-22 20:12:30 +01:00
|
|
|
for(int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
nearPoints[i] = m_Orientation.Transform(nearPoints[i]);
|
|
|
|
farPoints[i] = m_Orientation.Transform(farPoints[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// near plane
|
|
|
|
glBegin(GL_POLYGON);
|
|
|
|
glVertex3fv(&nearPoints[0].X);
|
|
|
|
glVertex3fv(&nearPoints[1].X);
|
|
|
|
glVertex3fv(&nearPoints[2].X);
|
|
|
|
glVertex3fv(&nearPoints[3].X);
|
|
|
|
glEnd();
|
|
|
|
|
|
|
|
// far plane
|
|
|
|
glBegin(GL_POLYGON);
|
|
|
|
glVertex3fv(&farPoints[0].X);
|
|
|
|
glVertex3fv(&farPoints[1].X);
|
|
|
|
glVertex3fv(&farPoints[2].X);
|
|
|
|
glVertex3fv(&farPoints[3].X);
|
|
|
|
glEnd();
|
|
|
|
|
|
|
|
// connection lines
|
|
|
|
glBegin(GL_QUAD_STRIP);
|
|
|
|
glVertex3fv(&nearPoints[0].X);
|
|
|
|
glVertex3fv(&farPoints[0].X);
|
|
|
|
glVertex3fv(&nearPoints[1].X);
|
|
|
|
glVertex3fv(&farPoints[1].X);
|
|
|
|
glVertex3fv(&nearPoints[2].X);
|
|
|
|
glVertex3fv(&farPoints[2].X);
|
|
|
|
glVertex3fv(&nearPoints[3].X);
|
|
|
|
glVertex3fv(&farPoints[3].X);
|
|
|
|
glVertex3fv(&nearPoints[0].X);
|
|
|
|
glVertex3fv(&farPoints[0].X);
|
|
|
|
glEnd();
|
|
|
|
|
|
|
|
// intermediate planes
|
|
|
|
CVector3D intermediatePoints[4];
|
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
|
|
|
for(int i = 0; i < intermediates; ++i)
|
2006-01-22 20:12:30 +01:00
|
|
|
{
|
|
|
|
float t = (i+1.0)/(intermediates+1.0);
|
|
|
|
|
|
|
|
for(int j = 0; j < 4; ++j)
|
|
|
|
intermediatePoints[j] = nearPoints[j]*t + farPoints[j]*(1.0-t);
|
|
|
|
|
|
|
|
glBegin(GL_POLYGON);
|
|
|
|
glVertex3fv(&intermediatePoints[0].X);
|
|
|
|
glVertex3fv(&intermediatePoints[1].X);
|
|
|
|
glVertex3fv(&intermediatePoints[2].X);
|
|
|
|
glVertex3fv(&intermediatePoints[3].X);
|
|
|
|
glEnd();
|
|
|
|
}
|
2012-02-13 15:02:14 +01:00
|
|
|
#endif
|
2006-01-22 20:12:30 +01:00
|
|
|
}
|