Changeset View
Changeset View
Standalone View
Standalone View
source/graphics/Camera.cpp
Show First 20 Lines • Show All 194 Lines • ▼ Show 20 Lines | void CCamera::GetScreenCoordinates(const CVector3D& world, float& x, float& y) const | ||||
x = screenspace.X / screenspace.W; | x = screenspace.X / screenspace.W; | ||||
y = screenspace.Y / screenspace.W; | y = screenspace.Y / screenspace.W; | ||||
x = (x + 1) * 0.5f * g_Renderer.GetWidth(); | x = (x + 1) * 0.5f * g_Renderer.GetWidth(); | ||||
y = (1 - y) * 0.5f * g_Renderer.GetHeight(); | y = (1 - y) * 0.5f * g_Renderer.GetHeight(); | ||||
} | } | ||||
CVector3D CCamera::GetWorldCoordinates(int px, int py, bool aboveWater) const | CVector3D CCamera::GetWorldCoordinates(int px, int py, bool aboveWater) const | ||||
{ | { | ||||
CHFTracer tracer(g_Game->GetWorld()->GetTerrain()); | CHFTracer tracer(CGame::GetGame()->GetWorld()->GetTerrain()); | ||||
int x, z; | int x, z; | ||||
CVector3D origin, dir, delta, terrainPoint, waterPoint; | CVector3D origin, dir, delta, terrainPoint, waterPoint; | ||||
BuildCameraRay(px, py, origin, dir); | BuildCameraRay(px, py, origin, dir); | ||||
bool gotTerrain = tracer.RayIntersect(origin, dir, x, z, terrainPoint); | bool gotTerrain = tracer.RayIntersect(origin, dir, x, z, terrainPoint); | ||||
Show All 10 Lines | CVector3D CCamera::GetWorldCoordinates(int px, int py, bool aboveWater) const | ||||
CPlane plane; | CPlane plane; | ||||
plane.Set(CVector3D(0.f, 1.f, 0.f), // upwards normal | 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 | CVector3D(0.f, g_Renderer.GetWaterManager()->m_WaterHeight, 0.f)); // passes through water plane | ||||
bool gotWater = plane.FindRayIntersection( origin, dir, &waterPoint ); | bool gotWater = plane.FindRayIntersection( origin, dir, &waterPoint ); | ||||
// Clamp the water intersection to within the map's bounds, so that | // Clamp the water intersection to within the map's bounds, so that | ||||
// we'll always return a valid position on the map | // we'll always return a valid position on the map | ||||
ssize_t mapSize = g_Game->GetWorld()->GetTerrain()->GetVerticesPerSide(); | ssize_t mapSize = CGame::GetGame()->GetWorld()->GetTerrain()->GetVerticesPerSide(); | ||||
if (gotWater) | if (gotWater) | ||||
{ | { | ||||
waterPoint.X = clamp(waterPoint.X, 0.f, (float)((mapSize-1)*TERRAIN_TILE_SIZE)); | waterPoint.X = clamp(waterPoint.X, 0.f, (float)((mapSize-1)*TERRAIN_TILE_SIZE)); | ||||
waterPoint.Z = clamp(waterPoint.Z, 0.f, (float)((mapSize-1)*TERRAIN_TILE_SIZE)); | waterPoint.Z = clamp(waterPoint.Z, 0.f, (float)((mapSize-1)*TERRAIN_TILE_SIZE)); | ||||
} | } | ||||
if (gotTerrain) | if (gotTerrain) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | CVector3D CCamera::GetWorldCoordinates(int px, int py, float h) const | ||||
// so just choose an arbitrary point on the plane | // so just choose an arbitrary point on the plane | ||||
return CVector3D(0.f, h, 0.f); | return CVector3D(0.f, h, 0.f); | ||||
} | } | ||||
CVector3D CCamera::GetFocus() const | CVector3D CCamera::GetFocus() const | ||||
{ | { | ||||
// Basically the same as GetWorldCoordinates | // Basically the same as GetWorldCoordinates | ||||
CHFTracer tracer(g_Game->GetWorld()->GetTerrain()); | CHFTracer tracer(CGame::GetGame()->GetWorld()->GetTerrain()); | ||||
int x, z; | int x, z; | ||||
CVector3D origin, dir, delta, terrainPoint, waterPoint; | CVector3D origin, dir, delta, terrainPoint, waterPoint; | ||||
origin = m_Orientation.GetTranslation(); | origin = m_Orientation.GetTranslation(); | ||||
dir = m_Orientation.GetIn(); | dir = m_Orientation.GetIn(); | ||||
bool gotTerrain = tracer.RayIntersect(origin, dir, x, z, terrainPoint); | bool gotTerrain = tracer.RayIntersect(origin, dir, x, z, terrainPoint); | ||||
CPlane plane; | CPlane plane; | ||||
plane.Set(CVector3D(0.f, 1.f, 0.f), // upwards normal | 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 | CVector3D(0.f, g_Renderer.GetWaterManager()->m_WaterHeight, 0.f)); // passes through water plane | ||||
bool gotWater = plane.FindRayIntersection( origin, dir, &waterPoint ); | bool gotWater = plane.FindRayIntersection( origin, dir, &waterPoint ); | ||||
// Clamp the water intersection to within the map's bounds, so that | // Clamp the water intersection to within the map's bounds, so that | ||||
// we'll always return a valid position on the map | // we'll always return a valid position on the map | ||||
ssize_t mapSize = g_Game->GetWorld()->GetTerrain()->GetVerticesPerSide(); | ssize_t mapSize = CGame::GetGame()->GetWorld()->GetTerrain()->GetVerticesPerSide(); | ||||
if (gotWater) | if (gotWater) | ||||
{ | { | ||||
waterPoint.X = clamp(waterPoint.X, 0.f, (float)((mapSize-1)*TERRAIN_TILE_SIZE)); | waterPoint.X = clamp(waterPoint.X, 0.f, (float)((mapSize-1)*TERRAIN_TILE_SIZE)); | ||||
waterPoint.Z = clamp(waterPoint.Z, 0.f, (float)((mapSize-1)*TERRAIN_TILE_SIZE)); | waterPoint.Z = clamp(waterPoint.Z, 0.f, (float)((mapSize-1)*TERRAIN_TILE_SIZE)); | ||||
} | } | ||||
if (gotTerrain) | if (gotTerrain) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 114 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator