Return by value from CCamera::GetScreenCoordinates

Pack the two `float`s in to a `CVector2D`.
Rename some variables to not use underscore.
This commit is contained in:
phosit 2024-11-17 10:17:06 +01:00 committed by phosit
parent 998dc30b6d
commit f8afd49ae1
8 changed files with 38 additions and 38 deletions

View file

@ -233,16 +233,17 @@ CCamera::Ray CCamera::BuildCameraRay(const int px, const int py) const
return result;
}
void CCamera::GetScreenCoordinates(const CVector3D& world, float& x, float& y) const
CVector2D CCamera::GetScreenCoordinates(const CVector3D& world) const
{
CMatrix3D transform = m_ProjMat * m_Orientation.GetInverse();
CVector4D screenspace = transform.Transform(CVector4D(world.X, world.Y, world.Z, 1.0f));
x = screenspace.X / screenspace.W;
y = screenspace.Y / screenspace.W;
float x = screenspace.X / screenspace.W;
float y = screenspace.Y / screenspace.W;
x = (x + 1) * 0.5f * m_ViewPort.m_Width;
y = (1 - y) * 0.5f * m_ViewPort.m_Height;
return {x, y};
}
CVector3D CCamera::GetWorldCoordinates(int px, int py, bool aboveWater) const

View file

@ -26,6 +26,7 @@
#include "maths/BoundingBoxAligned.h"
#include "maths/Frustum.h"
#include "maths/Matrix3D.h"
#include "maths/Vector2D.h"
#include <array>
@ -97,7 +98,7 @@ class CCamera
// General helpers that seem to fit here
// Get the screen-space coordinates corresponding to a given world-space position
void GetScreenCoordinates(const CVector3D& world, float& x, float& y) const;
CVector2D GetScreenCoordinates(const CVector3D& world) const;
// Get the point on the terrain corresponding to pixel (px,py) (or the mouse coordinates)
// The aboveWater parameter determines whether we want to stop at the water plane or also get underwater points

View file

@ -1363,10 +1363,10 @@ void CPatchRData::RenderPriorities(CTextRenderer& textRenderer)
pos.X += TERRAIN_TILE_SIZE/4.f;
pos.Z += TERRAIN_TILE_SIZE/4.f;
float x, y;
camera.GetScreenCoordinates(pos, x, y);
const CVector2D screenPos{camera.GetScreenCoordinates(pos)};
textRenderer.PrintfAt(x, y, L"%d", m_Patch->m_MiniPatches[j][i].Priority);
textRenderer.PrintfAt(screenPos.X, screenPos.Y, L"%d",
m_Patch->m_MiniPatches[j][i].Priority);
}
}
}

View file

@ -114,10 +114,9 @@ bool CheckEntityInRect(CEntityHandle handle, const CCamera& camera, int sx0, int
return false;
// Compare screen-space coordinates
float x, y;
camera.GetScreenCoordinates(position, x, y);
int ix = (int)x;
int iy = (int)y;
const CVector2D screenPos{camera.GetScreenCoordinates(position)};
int ix = static_cast<int>(screenPos.X);
int iy = static_cast<int>(screenPos.Y);
return sx0 <= ix && ix <= sx1 && sy0 <= iy && iy <= sy1;
}

View file

@ -1,4 +1,4 @@
/* Copyright (C) 2021 Wildfire Games.
/* Copyright (C) 2024 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -147,38 +147,37 @@ float CSoundGroup::RadiansOffCenter(const CVector3D& position, bool& onScreen, f
const float yBufferSize = 15.f;
const float radianCap = m_MaxStereoAngle;
float x, y;
g_Game->GetView()->GetCamera()->GetScreenCoordinates(position, x, y);
const CVector2D screenPos{g_Game->GetView()->GetCamera()->GetScreenCoordinates(position)};
onScreen = true;
float answer = 0.f;
if (x < -xBufferSize)
if (screenPos.X < -xBufferSize)
{
onScreen = false;
answer = -radianCap;
}
else if (x > screenWidth + xBufferSize)
else if (screenPos.X > screenWidth + xBufferSize)
{
onScreen = false;
answer = radianCap;
}
else
{
if (x < 0 || x > screenWidth)
if (screenPos.X < 0 || screenPos.X > screenWidth)
itemRollOff = MAX_ROLLOFF;
answer = radianCap * (x * 2 / screenWidth - 1);
answer = radianCap * (screenPos.X * 2 / screenWidth - 1);
}
if (y < -yBufferSize)
if (screenPos.X < -yBufferSize)
{
onScreen = false;
}
else if (y > screenHeight + yBufferSize)
else if (screenPos.Y > screenHeight + yBufferSize)
{
onScreen = false;
}
else if (y < 0 || y > screenHeight)
else if (screenPos.Y < 0 || screenPos.Y > screenHeight)
{
itemRollOff = MAX_ROLLOFF;
}

View file

@ -1,4 +1,4 @@
/* Copyright (C) 2021 Wildfire Games.
/* Copyright (C) 2024 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -292,9 +292,8 @@ static CVector3D GetNearestPointToScreenCoords(const CVector3D& base, const CVec
float delta = (upper - lower) / 3.0;
float middle1 = lower + delta, middle2 = lower + 2.0f * delta;
CVector3D p1 = base + dir * middle1, p2 = base + dir * middle2;
CVector2D s1, s2;
g_Game->GetView()->GetCamera()->GetScreenCoordinates(p1, s1.X, s1.Y);
g_Game->GetView()->GetCamera()->GetScreenCoordinates(p2, s2.X, s2.Y);
const CVector2D s1{g_Game->GetView()->GetCamera()->GetScreenCoordinates(p1)};
const CVector2D s2{g_Game->GetView()->GetCamera()->GetScreenCoordinates(p2)};
if ((s1 - screen).Length() < (s2 - screen).Length())
upper = middle2;
else
@ -341,7 +340,7 @@ BEGIN_COMMAND(AddPathNode)
fixed::FromFloat(focus.Z)
);
spline.InsertNode(index + 1, target, CFixedVector3D(), fixed::FromInt(1));
spline.BuildSpline();
cmpCinemaManager->DeletePath(name);
cmpCinemaManager->AddPath(CCinemaPath(data, positionSpline, targetSpline));
@ -462,9 +461,8 @@ static bool isPathNodePicked(const TNSpline& spline, const CVector2D& cursor, At
data.Position.Y.ToFloat(),
data.Position.Z.ToFloat()
);
CVector2D screen_pos;
g_Game->GetView()->GetCamera()->GetScreenCoordinates(position, screen_pos.X, screen_pos.Y);
if ((screen_pos - cursor).Length() < MINIMAL_SCREEN_DISTANCE)
const CVector2D screenPos{g_Game->GetView()->GetCamera()->GetScreenCoordinates(position)};
if ((screenPos - cursor).Length() < MINIMAL_SCREEN_DISTANCE)
{
node.index = i;
node.targetNode = targetNode;
@ -508,9 +506,8 @@ QUERYHANDLER(PickPathNode)
static bool isAxisPicked(const CVector3D& base, const CVector3D& direction, float length, const CVector2D& cursor)
{
CVector3D position = GetNearestPointToScreenCoords(base, direction, cursor, 0, length);
CVector2D screen_position;
g_Game->GetView()->GetCamera()->GetScreenCoordinates(position, screen_position.X, screen_position.Y);
return (cursor - screen_position).Length() < MINIMAL_SCREEN_DISTANCE;
const CVector2D screenPos{g_Game->GetView()->GetCamera()->GetScreenCoordinates(position)};
return (cursor - screenPos).Length() < MINIMAL_SCREEN_DISTANCE;
}
QUERYHANDLER(PickAxis)

View file

@ -667,11 +667,10 @@ QUERYHANDLER(PickObject)
CFixedVector3D fixed = cmpPosition->GetPosition();
CVector3D centre = CVector3D(fixed.X.ToFloat(), fixed.Y.ToFloat(), fixed.Z.ToFloat());
float cx, cy;
g_Game->GetView()->GetCamera()->GetScreenCoordinates(centre, cx, cy);
const CVector2D screenPos{g_Game->GetView()->GetCamera()->GetScreenCoordinates(centre)};
msg->offsetx = (int)(cx - x);
msg->offsety = (int)(cy - y);
msg->offsetx = static_cast<int>(screenPos.X - x);
msg->offsety = static_cast<int>(screenPos.Y - y);
}
}
}

View file

@ -78,9 +78,13 @@ void AtlasMessage::Position::GetScreenSpace(float& x, float& y) const
switch (type)
{
case 0:
g_Game->GetView()->GetCamera()->GetScreenCoordinates(CVector3D(type0.x, type0.y, type0.x), x, y);
{
const CVector2D screenPos{g_Game->GetView()->GetCamera()->GetScreenCoordinates(
CVector3D{type0.x, type0.y, type0.x})};
x = screenPos.X;
y = screenPos.Y;
break;
}
case 1:
x = type1.x;
y = type1.y;