CBasicModel: Make use of size_t where applicable

Same behavior, but without some internal variable truncation.
This commit is contained in:
Lioncash 2020-06-15 19:47:55 -04:00
parent 4978bc9e5c
commit e18e73cca5
12 changed files with 78 additions and 60 deletions

View File

@ -24,10 +24,10 @@ void CRayCollisionTester::AddNodeModel(CSceneNode *pNode, CBasicModel *pModel)
// Check each of the model's surfaces and queue them for further testing if they hit // Check each of the model's surfaces and queue them for further testing if they hit
for (uint32 iSurf = 0; iSurf < pModel->GetSurfaceCount(); iSurf++) for (uint32 iSurf = 0; iSurf < pModel->GetSurfaceCount(); iSurf++)
{ {
std::pair<bool,float> SurfResult = pModel->GetSurfaceAABox(iSurf).Transformed(pNode->Transform()).IntersectsRay(mRay); const auto [intersects, distance] = pModel->GetSurfaceAABox(iSurf).Transformed(pNode->Transform()).IntersectsRay(mRay);
if (SurfResult.first) if (intersects)
AddNode(pNode, iSurf, SurfResult.second); AddNode(pNode, iSurf, distance);
} }
} }

View File

@ -61,18 +61,17 @@ void CGameArea::MergeTerrain()
if (mTerrainMerged) return; if (mTerrainMerged) return;
// Nothing really complicated here - iterate through every terrain submesh, add each to a static model // Nothing really complicated here - iterate through every terrain submesh, add each to a static model
for (uint32 iMdl = 0; iMdl < mWorldModels.size(); iMdl++) for (auto& pMdl : mWorldModels)
{ {
auto& pMdl = mWorldModels[iMdl]; const size_t SubmeshCount = pMdl->GetSurfaceCount();
uint32 SubmeshCount = pMdl->GetSurfaceCount();
for (uint32 iSurf = 0; iSurf < SubmeshCount; iSurf++) for (size_t iSurf = 0; iSurf < SubmeshCount; iSurf++)
{ {
SSurface *pSurf = pMdl->GetSurface(iSurf); SSurface *pSurf = pMdl->GetSurface(iSurf);
CMaterial *pMat = mpMaterialSet->MaterialByIndex(pSurf->MaterialID, false); CMaterial *pMat = mpMaterialSet->MaterialByIndex(pSurf->MaterialID, false);
bool NewMat = true; bool NewMat = true;
for (auto it = mStaticWorldModels.begin(); it != mStaticWorldModels.end(); it++) for (auto it = mStaticWorldModels.begin(); it != mStaticWorldModels.end(); ++it)
{ {
if ((*it)->GetMaterial() == pMat) if ((*it)->GetMaterial() == pMat)
{ {

View File

@ -15,7 +15,7 @@ void CAreaCooker::DetermineSectionNumbersPrime()
// Determine how many sections are taken up by geometry... // Determine how many sections are taken up by geometry...
// Each world mesh has 7-9 sections (depending on game) plus one section per surface. // Each world mesh has 7-9 sections (depending on game) plus one section per surface.
uint32 GeometrySections = 0; uint32 GeometrySections = 0;
uint32 OriginalMeshCount = mpArea->mOriginalWorldMeshCount; const uint32 OriginalMeshCount = mpArea->mOriginalWorldMeshCount;
switch (mVersion) switch (mVersion)
{ {
@ -38,8 +38,10 @@ void CAreaCooker::DetermineSectionNumbersPrime()
// Set section numbers // Set section numbers
uint32 SecNum = GeometrySections; uint32 SecNum = GeometrySections;
if (mVersion <= EGame::Prime) mAROTSecNum = SecNum++; if (mVersion <= EGame::Prime)
if (mVersion >= EGame::EchoesDemo) mFFFFSecNum = SecNum++; mAROTSecNum = SecNum++;
if (mVersion >= EGame::EchoesDemo)
mFFFFSecNum = SecNum++;
if (mVersion >= EGame::EchoesDemo) if (mVersion >= EGame::EchoesDemo)
{ {
@ -48,7 +50,9 @@ void CAreaCooker::DetermineSectionNumbersPrime()
mSCGNSecNum = SecNum++; mSCGNSecNum = SecNum++;
} }
else else
{
mSCLYSecNum = SecNum++; mSCLYSecNum = SecNum++;
}
mCollisionSecNum = SecNum++; mCollisionSecNum = SecNum++;
mUnknownSecNum = SecNum++; mUnknownSecNum = SecNum++;

View File

@ -86,9 +86,9 @@ void CAreaLoader::ReadGeometryPrime()
else // For Echoes+, load surface mesh IDs, then skip to the start of the next mesh else // For Echoes+, load surface mesh IDs, then skip to the start of the next mesh
{ {
auto& pModel = FileModels.emplace_back(CModelLoader::LoadWorldModel(*mpMREA, *mpSectionMgr, *mpArea->mpMaterialSet, mVersion)); auto& pModel = FileModels.emplace_back(CModelLoader::LoadWorldModel(*mpMREA, *mpSectionMgr, *mpArea->mpMaterialSet, mVersion));
const uint16 NumSurfaces = mpMREA->ReadShort(); const size_t NumSurfaces = mpMREA->ReadShort();
for (uint32 iSurf = 0; iSurf < NumSurfaces; iSurf++) for (size_t iSurf = 0; iSurf < NumSurfaces; iSurf++)
{ {
mpMREA->Seek(0x2, SEEK_CUR); mpMREA->Seek(0x2, SEEK_CUR);
pModel->GetSurface(iSurf)->MeshID = mpMREA->ReadShort(); pModel->GetSurface(iSurf)->MeshID = mpMREA->ReadShort();
@ -391,9 +391,9 @@ void CAreaLoader::ReadGeometryCorruption()
// Load surface mesh IDs // Load surface mesh IDs
mpSectionMgr->ToSection(CurWOBJSection - 2); mpSectionMgr->ToSection(CurWOBJSection - 2);
uint16 NumSurfaces = mpMREA->ReadShort(); const size_t NumSurfaces = mpMREA->ReadShort();
for (uint32 iSurf = 0; iSurf < NumSurfaces; iSurf++) for (size_t iSurf = 0; iSurf < NumSurfaces; iSurf++)
{ {
mpMREA->Seek(0x2, SEEK_CUR); mpMREA->Seek(0x2, SEEK_CUR);
pWorldModel->GetSurface(iSurf)->MeshID = mpMREA->ReadShort(); pWorldModel->GetSurface(iSurf)->MeshID = mpMREA->ReadShort();

View File

@ -1,6 +1,4 @@
#include "CBasicModel.h" #include "CBasicModel.h"
#include <iostream>
#include <list>
CBasicModel::CBasicModel(CResourceEntry *pEntry) CBasicModel::CBasicModel(CResourceEntry *pEntry)
: CResource(pEntry) : CResource(pEntry)
@ -14,12 +12,12 @@ CBasicModel::~CBasicModel()
delete mSurfaces[iSurf]; delete mSurfaces[iSurf];
} }
uint32 CBasicModel::GetVertexCount() const size_t CBasicModel::GetVertexCount() const
{ {
return mVertexCount; return mVertexCount;
} }
uint32 CBasicModel::GetTriangleCount() const size_t CBasicModel::GetTriangleCount() const
{ {
return mTriangleCount; return mTriangleCount;
} }
@ -34,17 +32,17 @@ bool CBasicModel::IsBuffered() const
return mBuffered; return mBuffered;
} }
uint32 CBasicModel::GetSurfaceCount() const size_t CBasicModel::GetSurfaceCount() const
{ {
return mSurfaces.size(); return mSurfaces.size();
} }
CAABox CBasicModel::GetSurfaceAABox(uint32 Surface) const CAABox CBasicModel::GetSurfaceAABox(size_t Surface) const
{ {
return mSurfaces[Surface]->AABox; return mSurfaces[Surface]->AABox;
} }
SSurface* CBasicModel::GetSurface(uint32 Surface) SSurface* CBasicModel::GetSurface(size_t Surface)
{ {
return mSurfaces[Surface]; return mSurfaces[Surface];
} }

View File

@ -24,13 +24,13 @@ public:
explicit CBasicModel(CResourceEntry *pEntry = nullptr); explicit CBasicModel(CResourceEntry *pEntry = nullptr);
~CBasicModel(); ~CBasicModel();
uint32 GetVertexCount() const; size_t GetVertexCount() const;
uint32 GetTriangleCount() const; size_t GetTriangleCount() const;
CAABox AABox() const; CAABox AABox() const;
bool IsBuffered() const; bool IsBuffered() const;
uint32 GetSurfaceCount() const; size_t GetSurfaceCount() const;
CAABox GetSurfaceAABox(uint32 Surface) const; CAABox GetSurfaceAABox(size_t Surface) const;
SSurface* GetSurface(uint32 Surface); SSurface* GetSurface(size_t Surface);
virtual void ClearGLBuffer() = 0; virtual void ClearGLBuffer() = 0;
}; };

View File

@ -152,9 +152,10 @@ SRayIntersection CModelNode::RayNodeIntersectTest(const CRay& rkRay, uint32 Asse
CVector3f WorldHitPoint = Transform() * HitPoint; CVector3f WorldHitPoint = Transform() * HitPoint;
Out.Distance = Math::Distance(rkRay.Origin(), WorldHitPoint); Out.Distance = Math::Distance(rkRay.Origin(), WorldHitPoint);
} }
else else
{
Out.Hit = false; Out.Hit = false;
}
return Out; return Out;
} }

View File

@ -135,8 +135,10 @@ SRayIntersection CScriptAttachNode::RayNodeIntersectTest(const CRay& rkRay, uint
CVector3f WorldHitPoint = Transform() * HitPoint; CVector3f WorldHitPoint = Transform() * HitPoint;
Out.Distance = rkRay.Origin().Distance(WorldHitPoint); Out.Distance = rkRay.Origin().Distance(WorldHitPoint);
} }
else
else Out.Hit = false; {
Out.Hit = false;
}
return Out; return Out;
} }

View File

@ -354,7 +354,8 @@ SRayIntersection CScriptNode::RayNodeIntersectTest(const CRay& rkRay, uint32 Ass
if (UsesModel()) if (UsesModel())
{ {
CModel *pModel = ActiveModel(); CModel *pModel = ActiveModel();
if (!pModel) pModel = CDrawUtil::GetCubeModel(); if (!pModel)
pModel = CDrawUtil::GetCubeModel();
CRay TransformedRay = rkRay.Transformed(Transform().Inverse()); CRay TransformedRay = rkRay.Transformed(Transform().Inverse());
std::pair<bool,float> Result = pModel->GetSurface(AssetID)->IntersectsRay(TransformedRay, ((Options & ERenderOption::EnableBackfaceCull) == 0)); std::pair<bool,float> Result = pModel->GetSurface(AssetID)->IntersectsRay(TransformedRay, ((Options & ERenderOption::EnableBackfaceCull) == 0));
@ -367,10 +368,11 @@ SRayIntersection CScriptNode::RayNodeIntersectTest(const CRay& rkRay, uint32 Ass
CVector3f WorldHitPoint = Transform() * HitPoint; CVector3f WorldHitPoint = Transform() * HitPoint;
Out.Distance = Math::Distance(rkRay.Origin(), WorldHitPoint); Out.Distance = Math::Distance(rkRay.Origin(), WorldHitPoint);
} }
else else
{
Out.Hit = false; Out.Hit = false;
} }
}
// Billboard test // Billboard test
// todo: come up with a better way to share this code between CScriptNode and CLightNode // todo: come up with a better way to share this code between CScriptNode and CLightNode
@ -403,8 +405,9 @@ SRayIntersection CScriptNode::RayNodeIntersectTest(const CRay& rkRay, uint32 Ass
float TexelAlpha = ActiveBillboard()->ReadTexelAlpha(TexCoord); float TexelAlpha = ActiveBillboard()->ReadTexelAlpha(TexCoord);
if (TexelAlpha < 0.25f) if (TexelAlpha < 0.25f)
{
Out.Hit = false; Out.Hit = false;
}
else else
{ {
// It's opaque... we have a hit! // It's opaque... we have a hit!
@ -412,14 +415,16 @@ SRayIntersection CScriptNode::RayNodeIntersectTest(const CRay& rkRay, uint32 Ass
Out.Distance = PlaneTest.second; Out.Distance = PlaneTest.second;
} }
} }
else else
{
Out.Hit = false; Out.Hit = false;
} }
}
else else
{
Out.Hit = false; Out.Hit = false;
} }
}
return Out; return Out;
} }

View File

@ -29,16 +29,22 @@ void CStaticNode::PostLoad()
void CStaticNode::AddToRenderer(CRenderer *pRenderer, const SViewInfo& rkViewInfo) void CStaticNode::AddToRenderer(CRenderer *pRenderer, const SViewInfo& rkViewInfo)
{ {
if (!mpModel) return; if (!mpModel)
if (mpModel->IsOccluder()) return; return;
if (!rkViewInfo.ViewFrustum.BoxInFrustum(AABox())) return;
if (mpModel->IsOccluder())
return;
if (!rkViewInfo.ViewFrustum.BoxInFrustum(AABox()))
return;
if (!mpModel->IsTransparent()) if (!mpModel->IsTransparent())
{
pRenderer->AddMesh(this, -1, AABox(), false, ERenderCommand::DrawMesh); pRenderer->AddMesh(this, -1, AABox(), false, ERenderCommand::DrawMesh);
}
else else
{ {
uint32 NumSurfaces = mpModel->GetSurfaceCount(); const size_t NumSurfaces = mpModel->GetSurfaceCount();
for (uint32 iSurf = 0; iSurf < NumSurfaces; iSurf++) for (uint32 iSurf = 0; iSurf < NumSurfaces; iSurf++)
{ {
CAABox TransformedBox = mpModel->GetSurfaceAABox(iSurf).Transformed(Transform()); CAABox TransformedBox = mpModel->GetSurfaceAABox(iSurf).Transformed(Transform());
@ -96,21 +102,21 @@ void CStaticNode::DrawSelection()
void CStaticNode::RayAABoxIntersectTest(CRayCollisionTester& rTester, const SViewInfo& /*rkViewInfo*/) void CStaticNode::RayAABoxIntersectTest(CRayCollisionTester& rTester, const SViewInfo& /*rkViewInfo*/)
{ {
if ((!mpModel) || (mpModel->IsOccluder())) if (!mpModel || mpModel->IsOccluder())
return; return;
const CRay& rkRay = rTester.Ray(); const CRay& rkRay = rTester.Ray();
std::pair<bool,float> BoxResult = AABox().IntersectsRay(rkRay); const std::pair<bool, float> BoxResult = AABox().IntersectsRay(rkRay);
if (!BoxResult.first)
return;
if (BoxResult.first)
{
for (uint32 iSurf = 0; iSurf < mpModel->GetSurfaceCount(); iSurf++) for (uint32 iSurf = 0; iSurf < mpModel->GetSurfaceCount(); iSurf++)
{ {
std::pair<bool,float> SurfResult = mpModel->GetSurfaceAABox(iSurf).Transformed(Transform()).IntersectsRay(rkRay); const auto [intersects, distance] = mpModel->GetSurfaceAABox(iSurf).Transformed(Transform()).IntersectsRay(rkRay);
if (SurfResult.first) if (intersects)
rTester.AddNode(this, iSurf, SurfResult.second); rTester.AddNode(this, iSurf, distance);
}
} }
} }
@ -120,21 +126,22 @@ SRayIntersection CStaticNode::RayNodeIntersectTest(const CRay& rkRay, uint32 Ass
Out.pNode = this; Out.pNode = this;
Out.ComponentIndex = AssetID; Out.ComponentIndex = AssetID;
CRay TransformedRay = rkRay.Transformed(Transform().Inverse()); const CRay TransformedRay = rkRay.Transformed(Transform().Inverse());
FRenderOptions Options = rkViewInfo.pRenderer->RenderOptions(); const FRenderOptions Options = rkViewInfo.pRenderer->RenderOptions();
std::pair<bool,float> Result = mpModel->GetSurface(AssetID)->IntersectsRay(TransformedRay, ((Options & ERenderOption::EnableBackfaceCull) == 0)); const auto [intersects, distance] = mpModel->GetSurface(AssetID)->IntersectsRay(TransformedRay, ((Options & ERenderOption::EnableBackfaceCull) == 0));
if (Result.first) if (intersects)
{ {
Out.Hit = true; Out.Hit = true;
CVector3f HitPoint = TransformedRay.PointOnRay(Result.second); const CVector3f HitPoint = TransformedRay.PointOnRay(distance);
CVector3f WorldHitPoint = Transform() * HitPoint; const CVector3f WorldHitPoint = Transform() * HitPoint;
Out.Distance = Math::Distance(rkRay.Origin(), WorldHitPoint); Out.Distance = Math::Distance(rkRay.Origin(), WorldHitPoint);
} }
else else
{
Out.Hit = false; Out.Hit = false;
}
return Out; return Out;
} }

View File

@ -121,8 +121,10 @@ SRayIntersection CDoorExtra::RayNodeIntersectTest(const CRay& rkRay, uint32 Asse
CVector3f WorldHitPoint = Transform() * HitPoint; CVector3f WorldHitPoint = Transform() * HitPoint;
Out.Distance = rkRay.Origin().Distance(WorldHitPoint); Out.Distance = rkRay.Origin().Distance(WorldHitPoint);
} }
else
else Out.Hit = false; {
Out.Hit = false;
}
return Out; return Out;
} }

View File

@ -163,7 +163,7 @@ bool CGizmo::CheckSelectedAxes(const CRay& rkRay)
bool Hit = false; bool Hit = false;
float Dist; float Dist;
for (uint32 iSurf = 0; iSurf < pModel->GetSurfaceCount(); iSurf++) for (size_t iSurf = 0; iSurf < pModel->GetSurfaceCount(); iSurf++)
{ {
// Skip surface/box check - since we use lines the boxes might be too small // Skip surface/box check - since we use lines the boxes might be too small
SSurface *pSurf = pModel->GetSurface(iSurf); SSurface *pSurf = pModel->GetSurface(iSurf);
@ -171,7 +171,7 @@ bool CGizmo::CheckSelectedAxes(const CRay& rkRay)
if (SurfCheck.first) if (SurfCheck.first)
{ {
if ((!Hit) || (SurfCheck.second < Dist)) if (!Hit || SurfCheck.second < Dist)
Dist = SurfCheck.second; Dist = SurfCheck.second;
Hit = true; Hit = true;