forked from mirrors/0ad
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:
@@ -233,16 +233,17 @@ CCamera::Ray CCamera::BuildCameraRay(const int px, const int py) const
|
|||||||
return result;
|
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();
|
CMatrix3D transform = m_ProjMat * m_Orientation.GetInverse();
|
||||||
|
|
||||||
CVector4D screenspace = transform.Transform(CVector4D(world.X, world.Y, world.Z, 1.0f));
|
CVector4D screenspace = transform.Transform(CVector4D(world.X, world.Y, world.Z, 1.0f));
|
||||||
|
|
||||||
x = screenspace.X / screenspace.W;
|
float x = screenspace.X / screenspace.W;
|
||||||
y = screenspace.Y / screenspace.W;
|
float y = screenspace.Y / screenspace.W;
|
||||||
x = (x + 1) * 0.5f * m_ViewPort.m_Width;
|
x = (x + 1) * 0.5f * m_ViewPort.m_Width;
|
||||||
y = (1 - y) * 0.5f * m_ViewPort.m_Height;
|
y = (1 - y) * 0.5f * m_ViewPort.m_Height;
|
||||||
|
return {x, y};
|
||||||
}
|
}
|
||||||
|
|
||||||
CVector3D CCamera::GetWorldCoordinates(int px, int py, bool aboveWater) const
|
CVector3D CCamera::GetWorldCoordinates(int px, int py, bool aboveWater) const
|
||||||
|
|||||||
@@ -26,6 +26,7 @@
|
|||||||
#include "maths/BoundingBoxAligned.h"
|
#include "maths/BoundingBoxAligned.h"
|
||||||
#include "maths/Frustum.h"
|
#include "maths/Frustum.h"
|
||||||
#include "maths/Matrix3D.h"
|
#include "maths/Matrix3D.h"
|
||||||
|
#include "maths/Vector2D.h"
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
|
||||||
@@ -97,7 +98,7 @@ class CCamera
|
|||||||
// General helpers that seem to fit here
|
// General helpers that seem to fit here
|
||||||
|
|
||||||
// Get the screen-space coordinates corresponding to a given world-space position
|
// 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)
|
// 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
|
// The aboveWater parameter determines whether we want to stop at the water plane or also get underwater points
|
||||||
|
|||||||
@@ -1363,10 +1363,10 @@ void CPatchRData::RenderPriorities(CTextRenderer& textRenderer)
|
|||||||
pos.X += TERRAIN_TILE_SIZE/4.f;
|
pos.X += TERRAIN_TILE_SIZE/4.f;
|
||||||
pos.Z += TERRAIN_TILE_SIZE/4.f;
|
pos.Z += TERRAIN_TILE_SIZE/4.f;
|
||||||
|
|
||||||
float x, y;
|
const CVector2D screenPos{camera.GetScreenCoordinates(pos)};
|
||||||
camera.GetScreenCoordinates(pos, x, y);
|
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -114,10 +114,9 @@ bool CheckEntityInRect(CEntityHandle handle, const CCamera& camera, int sx0, int
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Compare screen-space coordinates
|
// Compare screen-space coordinates
|
||||||
float x, y;
|
const CVector2D screenPos{camera.GetScreenCoordinates(position)};
|
||||||
camera.GetScreenCoordinates(position, x, y);
|
int ix = static_cast<int>(screenPos.X);
|
||||||
int ix = (int)x;
|
int iy = static_cast<int>(screenPos.Y);
|
||||||
int iy = (int)y;
|
|
||||||
return sx0 <= ix && ix <= sx1 && sy0 <= iy && iy <= sy1;
|
return sx0 <= ix && ix <= sx1 && sy0 <= iy && iy <= sy1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (C) 2021 Wildfire Games.
|
/* Copyright (C) 2024 Wildfire Games.
|
||||||
* This file is part of 0 A.D.
|
* This file is part of 0 A.D.
|
||||||
*
|
*
|
||||||
* 0 A.D. is free software: you can redistribute it and/or modify
|
* 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 yBufferSize = 15.f;
|
||||||
const float radianCap = m_MaxStereoAngle;
|
const float radianCap = m_MaxStereoAngle;
|
||||||
|
|
||||||
float x, y;
|
const CVector2D screenPos{g_Game->GetView()->GetCamera()->GetScreenCoordinates(position)};
|
||||||
g_Game->GetView()->GetCamera()->GetScreenCoordinates(position, x, y);
|
|
||||||
|
|
||||||
onScreen = true;
|
onScreen = true;
|
||||||
float answer = 0.f;
|
float answer = 0.f;
|
||||||
if (x < -xBufferSize)
|
if (screenPos.X < -xBufferSize)
|
||||||
{
|
{
|
||||||
onScreen = false;
|
onScreen = false;
|
||||||
answer = -radianCap;
|
answer = -radianCap;
|
||||||
}
|
}
|
||||||
else if (x > screenWidth + xBufferSize)
|
else if (screenPos.X > screenWidth + xBufferSize)
|
||||||
{
|
{
|
||||||
onScreen = false;
|
onScreen = false;
|
||||||
answer = radianCap;
|
answer = radianCap;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (x < 0 || x > screenWidth)
|
if (screenPos.X < 0 || screenPos.X > screenWidth)
|
||||||
itemRollOff = MAX_ROLLOFF;
|
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;
|
onScreen = false;
|
||||||
}
|
}
|
||||||
else if (y > screenHeight + yBufferSize)
|
else if (screenPos.Y > screenHeight + yBufferSize)
|
||||||
{
|
{
|
||||||
onScreen = false;
|
onScreen = false;
|
||||||
}
|
}
|
||||||
else if (y < 0 || y > screenHeight)
|
else if (screenPos.Y < 0 || screenPos.Y > screenHeight)
|
||||||
{
|
{
|
||||||
itemRollOff = MAX_ROLLOFF;
|
itemRollOff = MAX_ROLLOFF;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (C) 2021 Wildfire Games.
|
/* Copyright (C) 2024 Wildfire Games.
|
||||||
* This file is part of 0 A.D.
|
* This file is part of 0 A.D.
|
||||||
*
|
*
|
||||||
* 0 A.D. is free software: you can redistribute it and/or modify
|
* 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 delta = (upper - lower) / 3.0;
|
||||||
float middle1 = lower + delta, middle2 = lower + 2.0f * delta;
|
float middle1 = lower + delta, middle2 = lower + 2.0f * delta;
|
||||||
CVector3D p1 = base + dir * middle1, p2 = base + dir * middle2;
|
CVector3D p1 = base + dir * middle1, p2 = base + dir * middle2;
|
||||||
CVector2D s1, s2;
|
const CVector2D s1{g_Game->GetView()->GetCamera()->GetScreenCoordinates(p1)};
|
||||||
g_Game->GetView()->GetCamera()->GetScreenCoordinates(p1, s1.X, s1.Y);
|
const CVector2D s2{g_Game->GetView()->GetCamera()->GetScreenCoordinates(p2)};
|
||||||
g_Game->GetView()->GetCamera()->GetScreenCoordinates(p2, s2.X, s2.Y);
|
|
||||||
if ((s1 - screen).Length() < (s2 - screen).Length())
|
if ((s1 - screen).Length() < (s2 - screen).Length())
|
||||||
upper = middle2;
|
upper = middle2;
|
||||||
else
|
else
|
||||||
@@ -341,7 +340,7 @@ BEGIN_COMMAND(AddPathNode)
|
|||||||
fixed::FromFloat(focus.Z)
|
fixed::FromFloat(focus.Z)
|
||||||
);
|
);
|
||||||
spline.InsertNode(index + 1, target, CFixedVector3D(), fixed::FromInt(1));
|
spline.InsertNode(index + 1, target, CFixedVector3D(), fixed::FromInt(1));
|
||||||
|
|
||||||
spline.BuildSpline();
|
spline.BuildSpline();
|
||||||
cmpCinemaManager->DeletePath(name);
|
cmpCinemaManager->DeletePath(name);
|
||||||
cmpCinemaManager->AddPath(CCinemaPath(data, positionSpline, targetSpline));
|
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.Y.ToFloat(),
|
||||||
data.Position.Z.ToFloat()
|
data.Position.Z.ToFloat()
|
||||||
);
|
);
|
||||||
CVector2D screen_pos;
|
const CVector2D screenPos{g_Game->GetView()->GetCamera()->GetScreenCoordinates(position)};
|
||||||
g_Game->GetView()->GetCamera()->GetScreenCoordinates(position, screen_pos.X, screen_pos.Y);
|
if ((screenPos - cursor).Length() < MINIMAL_SCREEN_DISTANCE)
|
||||||
if ((screen_pos - cursor).Length() < MINIMAL_SCREEN_DISTANCE)
|
|
||||||
{
|
{
|
||||||
node.index = i;
|
node.index = i;
|
||||||
node.targetNode = targetNode;
|
node.targetNode = targetNode;
|
||||||
@@ -508,9 +506,8 @@ QUERYHANDLER(PickPathNode)
|
|||||||
static bool isAxisPicked(const CVector3D& base, const CVector3D& direction, float length, const CVector2D& cursor)
|
static bool isAxisPicked(const CVector3D& base, const CVector3D& direction, float length, const CVector2D& cursor)
|
||||||
{
|
{
|
||||||
CVector3D position = GetNearestPointToScreenCoords(base, direction, cursor, 0, length);
|
CVector3D position = GetNearestPointToScreenCoords(base, direction, cursor, 0, length);
|
||||||
CVector2D screen_position;
|
const CVector2D screenPos{g_Game->GetView()->GetCamera()->GetScreenCoordinates(position)};
|
||||||
g_Game->GetView()->GetCamera()->GetScreenCoordinates(position, screen_position.X, screen_position.Y);
|
return (cursor - screenPos).Length() < MINIMAL_SCREEN_DISTANCE;
|
||||||
return (cursor - screen_position).Length() < MINIMAL_SCREEN_DISTANCE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QUERYHANDLER(PickAxis)
|
QUERYHANDLER(PickAxis)
|
||||||
|
|||||||
@@ -667,11 +667,10 @@ QUERYHANDLER(PickObject)
|
|||||||
CFixedVector3D fixed = cmpPosition->GetPosition();
|
CFixedVector3D fixed = cmpPosition->GetPosition();
|
||||||
CVector3D centre = CVector3D(fixed.X.ToFloat(), fixed.Y.ToFloat(), fixed.Z.ToFloat());
|
CVector3D centre = CVector3D(fixed.X.ToFloat(), fixed.Y.ToFloat(), fixed.Z.ToFloat());
|
||||||
|
|
||||||
float cx, cy;
|
const CVector2D screenPos{g_Game->GetView()->GetCamera()->GetScreenCoordinates(centre)};
|
||||||
g_Game->GetView()->GetCamera()->GetScreenCoordinates(centre, cx, cy);
|
|
||||||
|
|
||||||
msg->offsetx = (int)(cx - x);
|
msg->offsetx = static_cast<int>(screenPos.X - x);
|
||||||
msg->offsety = (int)(cy - y);
|
msg->offsety = static_cast<int>(screenPos.Y - y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,9 +78,13 @@ void AtlasMessage::Position::GetScreenSpace(float& x, float& y) const
|
|||||||
switch (type)
|
switch (type)
|
||||||
{
|
{
|
||||||
case 0:
|
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;
|
break;
|
||||||
|
}
|
||||||
case 1:
|
case 1:
|
||||||
x = type1.x;
|
x = type1.x;
|
||||||
y = type1.y;
|
y = type1.y;
|
||||||
|
|||||||
Reference in New Issue
Block a user