Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GuiTraceRay from theater ray intersection instead of camera position. #1782

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions rts/Game/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "System/float3.h"
#include "System/Matrix44f.h"
#include "System/Config/ConfigHandler.h"
#include "Sim/Features/FeatureHandler.h"
#include "Sim/Units/UnitHandler.h"

#include "System/Misc/TracyDefs.h"
Expand Down Expand Up @@ -774,22 +775,30 @@ float3 CCamera::GetMoveVectorFromState(bool fromKeyState) const
return v;
}

float3 CCamera::PointToMaxUnitAltitude(const float3& point, const float rayLength, const float maxAltitude) const
bool CCamera::TracePointToMaxAltitude(const float3& point, const float rayLength, const float maxAltitude, float3& result) const
saurtron marked this conversation as resolved.
Show resolved Hide resolved
{
const float3 dir = (point-pos).Normalize();
float dist = CGround::LinePlaneCol(pos, dir, rayLength, maxAltitude);
if (dist > 0.0)
return pos + dir*dist;
return point;
if (dist > 0.0) {
result = pos + dir*dist;
return true;
}
return false;
}

float3 CCamera::NearTheaterIntersection(const float3& dir, const float rayLength) const
{
const float maxAltitude = std::max(unitHandler.MaxUnitAltitude(), readMap->GetCurrMaxHeight());
float maxAltitude = std::max(unitHandler.MaxUnitAltitude(), readMap->GetCurrMaxHeight());
maxAltitude = std::max(maxAltitude, featureHandler.MaxFeatureAltitude());
saurtron marked this conversation as resolved.
Show resolved Hide resolved
if (pos.y < maxAltitude)
return pos;
const float3 fv1 = PointToMaxUnitAltitude(GetFrustumVert(CCamera::FRUSTUM_POINT_FBL), rayLength, maxAltitude);
const float3 fv2 = PointToMaxUnitAltitude(GetFrustumVert(CCamera::FRUSTUM_POINT_FBR), rayLength, maxAltitude);

float3 fv1, fv2;
const bool res1 = TracePointToMaxAltitude(GetFrustumVert(CCamera::FRUSTUM_POINT_FBL), rayLength, maxAltitude, fv1);
const bool res2 = TracePointToMaxAltitude(GetFrustumVert(CCamera::FRUSTUM_POINT_FBR), rayLength, maxAltitude, fv2);
if (!res1 || !res2)
return pos;

float3 midFv = (fv1+fv2)/2.0;
midFv.y = pos.y;

Expand Down
2 changes: 1 addition & 1 deletion rts/Game/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class CCamera {
return (forward.dot(objPos - pos));
}

float3 PointToMaxUnitAltitude(const float3& point, const float rayLength, const float maxAltitude) const;
bool TracePointToMaxAltitude(const float3& point, const float rayLength, const float maxAltitude, float3& result) const;
float3 NearTheaterIntersection(const float3& dir, const float rayLength) const;

/*
Expand Down
2 changes: 2 additions & 0 deletions rts/Sim/Features/Feature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ void CFeature::ForcedMove(const float3& newPos)
UpdateTransformAndPhysState();

eventHandler.FeatureMoved(this, oldPos);
featureHandler.MovedFeature(this);

// insert into managers
quadField.AddFeature(this);
Expand Down Expand Up @@ -596,6 +597,7 @@ bool CFeature::UpdatePosition()
// use an exact comparison for the y-component (gravity is small)
if (!pos.equals(oldPos, float3(float3::cmp_eps(), 0.0f, float3::cmp_eps()))) {
eventHandler.FeatureMoved(this, oldPos);
featureHandler.MovedFeature(this);
return true;
}

Expand Down
26 changes: 26 additions & 0 deletions rts/Sim/Features/FeatureHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Sim/Ecs/Registry.h"
#include "Sim/Misc/QuadField.h"
#include "Sim/Units/CommandAI/BuilderCAI.h"
#include "Sim/Units/UnitHandler.h"
#include "System/creg/STL_Set.h"
#include "System/EventHandler.h"
#include "System/TimeProfiler.h"
Expand Down Expand Up @@ -39,6 +40,7 @@ void CFeatureHandler::Init() {
features.resize(MAX_FEATURES, nullptr);
activeFeatureIDs.reserve(MAX_FEATURES); // internal table size must be constant
featureMemPool.reserve(128);
maxFeatureAltitude = readMap->GetCurrMaxHeight();

idPool.Clear();
idPool.Expand(0, MAX_FEATURES);
Expand All @@ -58,6 +60,7 @@ void CFeatureHandler::Kill() {
deletedFeatureIDs.clear();
features.clear();
updateFeatures.clear();
maxFeatureAltitude = std::numeric_limits<float>::lowest();
}


Expand Down Expand Up @@ -141,6 +144,7 @@ bool CFeatureHandler::AddFeature(CFeature* feature)

InsertActiveFeature(feature);
SetFeatureUpdateable(feature);
MovedFeature(feature);
return true;
}

Expand Down Expand Up @@ -186,6 +190,19 @@ CFeature* CFeatureHandler::CreateWreckage(const FeatureLoadParams& cparams)
}


void CFeatureHandler::RecalculateMaxAltitude()
{
if (maxFeatureAltitude < std::max(readMap->GetCurrMaxHeight, unitHandler.MaxUnitAltitude()))
return;

maxFeatureAltitude = readMap->GetCurrMaxHeight();

for (const int featureID: activeFeatureIDs) {
CFeature* f = features[featureID];
MovedFeature(f);
}
}


void CFeatureHandler::Update()
{
Expand All @@ -203,6 +220,9 @@ void CFeatureHandler::Update()

updateFeatures.erase(iter, updateFeatures.end());
}
if ((gs->frameNum & 62) == 0) {
saurtron marked this conversation as resolved.
Show resolved Hide resolved
RecalculateMaxAltitude();
}
}


Expand Down Expand Up @@ -287,3 +307,9 @@ void CFeatureHandler::TerrainChanged(int x1, int y1, int x2, int y2)
}
}

void CFeatureHandler::MovedFeature(const CFeature* feature)
{
const CollisionVolume& cv = feature->selectionVolume;
const float top = cv.GetWorldSpacePos(feature).y + cv.GetBoundingRadius();
maxFeatureAltitude = std::max(top, maxFeatureAltitude);
}
9 changes: 9 additions & 0 deletions rts/Sim/Features/FeatureHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,15 @@ class CFeatureHandler : public spring::noncopyable
bool TryFreeFeatureID(int id);
bool AddFeature(CFeature* feature);
void DeleteFeature(CFeature* feature);
void MovedFeature(const CFeature* feature);

void LoadFeaturesFromMap();

void SetFeatureUpdateable(CFeature* feature);
void TerrainChanged(int x1, int y1, int x2, int y2);

float MaxFeatureAltitude() const { return maxFeatureAltitude; }

const spring::unordered_set<int>& GetActiveFeatureIDs() const { return activeFeatureIDs; }

private:
Expand All @@ -88,6 +91,12 @@ class CFeatureHandler : public spring::noncopyable
std::vector<int> deletedFeatureIDs;
std::vector<CFeature*> features;
std::vector<CFeature*> updateFeatures;

///< highest altitude of any feature added so far
///< (ray tracing uses this in some cases)
float maxFeatureAltitude = 0.0f;

void RecalculateMaxAltitude();
};

extern CFeatureHandler featureHandler;
Expand Down
13 changes: 8 additions & 5 deletions rts/Sim/Units/UnitHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "UnitTypes/Factory.h"

#include "CommandAI/BuilderCAI.h"
#include "Map/ReadMap.h"
#include "Sim/Ecs/Registry.h"
#include "Sim/Misc/GlobalSynced.h"
#include "Sim/Misc/ModInfo.h"
Expand Down Expand Up @@ -116,7 +117,7 @@ void CUnitHandler::Init() {
// other team in the respective allyteam
maxUnits = CalcMaxUnits();
maxUnitRadius = 0.0f;
maxUnitAltitude = 0.0f;
maxUnitAltitude = readMap->GetCurrMaxHeight();
}
{
activeSlowUpdateUnit = 0;
Expand Down Expand Up @@ -172,7 +173,7 @@ void CUnitHandler::Kill()
{
maxUnits = 0;
maxUnitRadius = 0.0f;
maxUnitAltitude = 0.0f;
maxUnitAltitude = std::numeric_limits<float>::lowest();
}
}

Expand Down Expand Up @@ -412,7 +413,7 @@ void CUnitHandler::UpdateUnits()
{
SCOPED_TIMER("Sim::Unit::Update");

maxUnitAltitude = 0.0f;
maxUnitAltitude = readMap->GetCurrMaxHeight();
size_t activeUnitCount = activeUnits.size();
for (size_t i = 0; i < activeUnitCount; ++i) {
CUnit* unit = activeUnits[i];
Expand Down Expand Up @@ -520,7 +521,9 @@ unsigned int CUnitHandler::CalcMaxUnits() const
return n;
}

void CUnitHandler::MovedUnit(CUnit* unit)
void CUnitHandler::MovedUnit(const CUnit* unit)
{
maxUnitAltitude = std::max(unit->pos.y + unit->radius, maxUnitAltitude);
const CollisionVolume& cv = unit->selectionVolume;
const float top = cv.GetWorldSpacePos(unit).y + cv.GetBoundingRadius();
maxUnitAltitude = std::max(top, maxUnitAltitude);
}
2 changes: 1 addition & 1 deletion rts/Sim/Units/UnitHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CUnitHandler

void Update();
bool AddUnit(CUnit* unit);
void MovedUnit(CUnit* unit);
void MovedUnit(const CUnit* unit);

bool CanAddUnit(int id) const {
// do we want to be assigned a random ID and are any left in pool?
Expand Down
Loading