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
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)
AddNode(pNode, iSurf, SurfResult.second);
if (intersects)
AddNode(pNode, iSurf, distance);
}
}

View File

@ -61,18 +61,17 @@ void CGameArea::MergeTerrain()
if (mTerrainMerged) return;
// 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];
uint32 SubmeshCount = pMdl->GetSurfaceCount();
const size_t SubmeshCount = pMdl->GetSurfaceCount();
for (uint32 iSurf = 0; iSurf < SubmeshCount; iSurf++)
for (size_t iSurf = 0; iSurf < SubmeshCount; iSurf++)
{
SSurface *pSurf = pMdl->GetSurface(iSurf);
CMaterial *pMat = mpMaterialSet->MaterialByIndex(pSurf->MaterialID, false);
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)
{

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -354,7 +354,8 @@ SRayIntersection CScriptNode::RayNodeIntersectTest(const CRay& rkRay, uint32 Ass
if (UsesModel())
{
CModel *pModel = ActiveModel();
if (!pModel) pModel = CDrawUtil::GetCubeModel();
if (!pModel)
pModel = CDrawUtil::GetCubeModel();
CRay TransformedRay = rkRay.Transformed(Transform().Inverse());
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;
Out.Distance = Math::Distance(rkRay.Origin(), WorldHitPoint);
}
else
{
Out.Hit = false;
}
}
// Billboard test
// 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);
if (TexelAlpha < 0.25f)
{
Out.Hit = false;
}
else
{
// It's opaque... we have a hit!
@ -412,14 +415,16 @@ SRayIntersection CScriptNode::RayNodeIntersectTest(const CRay& rkRay, uint32 Ass
Out.Distance = PlaneTest.second;
}
}
else
{
Out.Hit = false;
}
}
else
{
Out.Hit = false;
}
}
return Out;
}

View File

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

View File

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

View File

@ -163,7 +163,7 @@ bool CGizmo::CheckSelectedAxes(const CRay& rkRay)
bool Hit = false;
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
SSurface *pSurf = pModel->GetSurface(iSurf);
@ -171,7 +171,7 @@ bool CGizmo::CheckSelectedAxes(const CRay& rkRay)
if (SurfCheck.first)
{
if ((!Hit) || (SurfCheck.second < Dist))
if (!Hit || SurfCheck.second < Dist)
Dist = SurfCheck.second;
Hit = true;