CStaticModel: Make use of ranged for

This commit is contained in:
Lioncash 2020-06-22 02:23:55 -04:00
parent 2732ae8383
commit 6120e60b05

View File

@ -33,26 +33,25 @@ void CStaticModel::BufferGL()
mVBO.Clear(); mVBO.Clear();
mIBOs.clear(); mIBOs.clear();
for (uint32 iSurf = 0; iSurf < mSurfaces.size(); iSurf++) for (size_t iSurf = 0; iSurf < mSurfaces.size(); iSurf++)
{ {
SSurface *pSurf = mSurfaces[iSurf]; SSurface *pSurf = mSurfaces[iSurf];
uint16 VBOStartOffset = (uint16) mVBO.Size(); const auto VBOStartOffset = static_cast<uint16>(mVBO.Size());
mVBO.Reserve((uint16) pSurf->VertexCount); mVBO.Reserve(static_cast<uint16>(pSurf->VertexCount));
for (uint32 iPrim = 0; iPrim < pSurf->Primitives.size(); iPrim++) for (const auto& pPrim : pSurf->Primitives)
{ {
SSurface::SPrimitive *pPrim = &pSurf->Primitives[iPrim]; CIndexBuffer *pIBO = InternalGetIBO(pPrim.Type);
CIndexBuffer *pIBO = InternalGetIBO(pPrim->Type); pIBO->Reserve(pPrim.Vertices.size() + 1); // Allocate enough space for this primitive, plus the restart index
pIBO->Reserve(pPrim->Vertices.size() + 1); // Allocate enough space for this primitive, plus the restart index
// Next step: add new vertices to the VBO and create a small index buffer for the current primitive // Next step: add new vertices to the VBO and create a small index buffer for the current primitive
std::vector<uint16> Indices(pPrim->Vertices.size()); std::vector<uint16> Indices(pPrim.Vertices.size());
for (uint32 iVert = 0; iVert < pPrim->Vertices.size(); iVert++) for (size_t iVert = 0; iVert < pPrim.Vertices.size(); iVert++)
Indices[iVert] = mVBO.AddIfUnique(pPrim->Vertices[iVert], VBOStartOffset); Indices[iVert] = mVBO.AddIfUnique(pPrim.Vertices[iVert], VBOStartOffset);
// then add the indices to the IBO. We convert some primitives to strips to minimize draw calls. // then add the indices to the IBO. We convert some primitives to strips to minimize draw calls.
switch (pPrim->Type) switch (pPrim.Type)
{ {
case EPrimitiveType::Triangles: case EPrimitiveType::Triangles:
pIBO->TrianglesToStrips(Indices.data(), Indices.size()); pIBO->TrianglesToStrips(Indices.data(), Indices.size());
@ -74,14 +73,14 @@ void CStaticModel::BufferGL()
while (mIBOs.size() > mSurfaceEndOffsets.size()) while (mIBOs.size() > mSurfaceEndOffsets.size())
mSurfaceEndOffsets.emplace_back(std::vector<uint32>(mSurfaces.size())); mSurfaceEndOffsets.emplace_back(std::vector<uint32>(mSurfaces.size()));
for (uint32 iIBO = 0; iIBO < mIBOs.size(); iIBO++) for (size_t iIBO = 0; iIBO < mIBOs.size(); iIBO++)
mSurfaceEndOffsets[iIBO][iSurf] = mIBOs[iIBO].GetSize(); mSurfaceEndOffsets[iIBO][iSurf] = mIBOs[iIBO].GetSize();
} }
mVBO.Buffer(); mVBO.Buffer();
for (uint32 iIBO = 0; iIBO < mIBOs.size(); iIBO++) for (auto& ibo : mIBOs)
mIBOs[iIBO].Buffer(); ibo.Buffer();
mBuffered = true; mBuffered = true;
} }
@ -89,7 +88,9 @@ void CStaticModel::BufferGL()
void CStaticModel::GenerateMaterialShaders() void CStaticModel::GenerateMaterialShaders()
{ {
if (mpMaterial) if (mpMaterial == nullptr)
return;
mpMaterial->GenerateShader(false); mpMaterial->GenerateShader(false);
} }
@ -103,20 +104,20 @@ void CStaticModel::ClearGLBuffer()
void CStaticModel::Draw(FRenderOptions Options) void CStaticModel::Draw(FRenderOptions Options)
{ {
if (!mBuffered) BufferGL(); if (!mBuffered)
BufferGL();
mVBO.Bind(); mVBO.Bind();
glLineWidth(1.f); glLineWidth(1.f);
auto DoDraw = [this]() const auto DoDraw = [this]
{ {
// Draw IBOs // Draw IBOs
for (uint32 iIBO = 0; iIBO < mIBOs.size(); iIBO++) for (CIndexBuffer& ibo : mIBOs)
{ {
CIndexBuffer *pIBO = &mIBOs[iIBO]; ibo.Bind();
pIBO->Bind(); glDrawElements(ibo.GetPrimitiveType(), ibo.GetSize(), GL_UNSIGNED_SHORT, nullptr);
glDrawElements(pIBO->GetPrimitiveType(), pIBO->GetSize(), GL_UNSIGNED_SHORT, nullptr); ibo.Unbind();
pIBO->Unbind();
gDrawCount++; gDrawCount++;
} }
}; };
@ -124,7 +125,7 @@ void CStaticModel::Draw(FRenderOptions Options)
// Bind material // Bind material
if ((Options & ERenderOption::NoMaterialSetup) == 0) if ((Options & ERenderOption::NoMaterialSetup) == 0)
{ {
for (CMaterial* passMat = mpMaterial; passMat; passMat = passMat->GetNextDrawPass()) for (CMaterial* passMat = mpMaterial; passMat != nullptr; passMat = passMat->GetNextDrawPass())
{ {
passMat->SetCurrent(Options); passMat->SetCurrent(Options);
DoDraw(); DoDraw();
@ -140,21 +141,26 @@ void CStaticModel::Draw(FRenderOptions Options)
void CStaticModel::DrawSurface(FRenderOptions Options, uint32 Surface) void CStaticModel::DrawSurface(FRenderOptions Options, uint32 Surface)
{ {
if (!mBuffered) BufferGL(); if (!mBuffered)
BufferGL();
mVBO.Bind(); mVBO.Bind();
glLineWidth(1.f); glLineWidth(1.f);
auto DoDraw = [this, Surface]() const auto DoDraw = [this, Surface]
{ {
for (uint32 iIBO = 0; iIBO < mIBOs.size(); iIBO++) for (size_t iIBO = 0; iIBO < mIBOs.size(); iIBO++)
{ {
// Since there is a shared IBO for every mesh, we need two things to draw a single one: an offset and a size // Since there is a shared IBO for every mesh, we need two things to draw a single one: an offset and a size
uint32 Offset = 0; uint32 Offset = 0;
if (Surface > 0) Offset = mSurfaceEndOffsets[iIBO][Surface - 1]; if (Surface > 0)
uint32 Size = mSurfaceEndOffsets[iIBO][Surface] - Offset; Offset = mSurfaceEndOffsets[iIBO][Surface - 1];
if (!Size) continue; // The chosen submesh doesn't use this IBO const uint32 Size = mSurfaceEndOffsets[iIBO][Surface] - Offset;
// The chosen submesh doesn't use this IBO
if (Size == 0)
continue;
// Now we have both, so we can draw // Now we have both, so we can draw
mIBOs[iIBO].DrawElements(Offset, Size); mIBOs[iIBO].DrawElements(Offset, Size);
@ -165,7 +171,7 @@ void CStaticModel::DrawSurface(FRenderOptions Options, uint32 Surface)
// Bind material // Bind material
if ((Options & ERenderOption::NoMaterialSetup) == 0) if ((Options & ERenderOption::NoMaterialSetup) == 0)
{ {
for (CMaterial* passMat = mpMaterial; passMat; passMat = passMat->GetNextDrawPass()) for (CMaterial* passMat = mpMaterial; passMat != nullptr; passMat = passMat->GetNextDrawPass())
{ {
passMat->SetCurrent(Options); passMat->SetCurrent(Options);
DoDraw(); DoDraw();
@ -181,7 +187,8 @@ void CStaticModel::DrawSurface(FRenderOptions Options, uint32 Surface)
void CStaticModel::DrawWireframe(FRenderOptions Options, CColor WireColor /*= CColor::skWhite*/) void CStaticModel::DrawWireframe(FRenderOptions Options, CColor WireColor /*= CColor::skWhite*/)
{ {
if (!mBuffered) BufferGL(); if (!mBuffered)
BufferGL();
// Set up wireframe // Set up wireframe
WireColor.A = 0; WireColor.A = 0;
@ -221,12 +228,12 @@ bool CStaticModel::IsOccluder() const
CIndexBuffer* CStaticModel::InternalGetIBO(EPrimitiveType Primitive) CIndexBuffer* CStaticModel::InternalGetIBO(EPrimitiveType Primitive)
{ {
GLenum type = GXPrimToGLPrim(Primitive); const GLenum type = GXPrimToGLPrim(Primitive);
for (uint32 iIBO = 0; iIBO < mIBOs.size(); iIBO++) for (auto& ibo : mIBOs)
{ {
if (mIBOs[iIBO].GetPrimitiveType() == type) if (ibo.GetPrimitiveType() == type)
return &mIBOs[iIBO]; return &ibo;
} }
mIBOs.emplace_back(CIndexBuffer(type)); mIBOs.emplace_back(CIndexBuffer(type));