From a9971b0b1a12f4c8e39ec6cb95015f1fe416e19b Mon Sep 17 00:00:00 2001 From: Leonardo Zide Date: Sun, 27 Jun 2021 12:13:54 -0700 Subject: [PATCH] Fixed rendering issues with missing textures. --- common/lc_meshloader.cpp | 58 +++++++++++++++++++++++++--------------- common/lc_scene.cpp | 22 ++++++++++----- 2 files changed, 53 insertions(+), 27 deletions(-) diff --git a/common/lc_meshloader.cpp b/common/lc_meshloader.cpp index 676661bb..5f2b449e 100644 --- a/common/lc_meshloader.cpp +++ b/common/lc_meshloader.cpp @@ -944,29 +944,36 @@ void lcLibraryMeshData::WriteSections(lcMesh* Mesh, const lcArraymIndices.GetSize(); IndexIdx++) *Index++ = BaseVertex + SrcSection->mIndices[IndexIdx]; } - else + break; + + case LC_MESH_CONDITIONAL_LINES: { - const IndexType BaseVertex = DstSection.Texture ? 0 : BaseVertices[SrcDataType]; + const IndexType BaseVertex = BaseConditionalVertices[SrcDataType]; for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.GetSize(); IndexIdx++) *Index++ = BaseVertex + SrcSection->mIndices[IndexIdx]; } - } - else - { - IndexType BaseVertex = BaseConditionalVertices[SrcDataType]; + break; - for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.GetSize(); IndexIdx++) - *Index++ = BaseVertex + SrcSection->mIndices[IndexIdx]; + case LC_MESH_TEXTURED_TRIANGLES: + { + for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.GetSize(); IndexIdx++) + *Index++ = SrcSection->mIndices[IndexIdx]; + } + break; + + case LC_MESH_NUM_PRIMITIVE_TYPES: + break; } DstSection.NumIndices += SrcSection->mIndices.GetSize(); @@ -1050,9 +1057,10 @@ void lcLibraryMeshData::UpdateMeshSectionBoundingBox(lcMesh* Mesh, lcMeshSection { const IndexType* IndexBuffer = reinterpret_cast(static_cast(Mesh->mIndexData) + Section.IndexOffset); - if (!Section.Texture) + switch (Section.PrimitiveType) { - if (Section.PrimitiveType != LC_MESH_CONDITIONAL_LINES) + case LC_MESH_LINES: + case LC_MESH_TRIANGLES: { const lcVertex* VertexBuffer = Mesh->GetVertexData(); @@ -1063,7 +1071,9 @@ void lcLibraryMeshData::UpdateMeshSectionBoundingBox(lcMesh* Mesh, lcMeshSection SectionMax = lcMax(SectionMax, Position); } } - else + break; + + case LC_MESH_CONDITIONAL_LINES: { const lcVertexConditional* VertexBuffer = Mesh->GetConditionalVertexData(); @@ -1074,17 +1084,23 @@ void lcLibraryMeshData::UpdateMeshSectionBoundingBox(lcMesh* Mesh, lcMeshSection SectionMax = lcMax(SectionMax, Position); } } - } - else - { - const lcVertexTextured* VertexBuffer = Mesh->GetTexturedVertexData(); + break; - for (int Index = 0; Index < Section.NumIndices; Index++) + case LC_MESH_TEXTURED_TRIANGLES: { - const lcVector3& Position = VertexBuffer[IndexBuffer[Index]].Position; - SectionMin = lcMin(SectionMin, Position); - SectionMax = lcMax(SectionMax, Position); + const lcVertexTextured* VertexBuffer = Mesh->GetTexturedVertexData(); + + for (int Index = 0; Index < Section.NumIndices; Index++) + { + const lcVector3& Position = VertexBuffer[IndexBuffer[Index]].Position; + SectionMin = lcMin(SectionMin, Position); + SectionMax = lcMax(SectionMax, Position); + } } + break; + + case LC_MESH_NUM_PRIMITIVE_TYPES: + break; } } diff --git a/common/lc_scene.cpp b/common/lc_scene.cpp index 5e133880..745c3976 100644 --- a/common/lc_scene.cpp +++ b/common/lc_scene.cpp @@ -262,23 +262,33 @@ void lcScene::DrawOpaqueMeshes(lcContext* Context, bool DrawLit, int PrimitiveTy } } - lcTexture* const Texture = Section->Texture; int VertexBufferOffset = Mesh->mVertexCacheOffset != -1 ? Mesh->mVertexCacheOffset : 0; const int IndexBufferOffset = Mesh->mIndexCacheOffset != -1 ? Mesh->mIndexCacheOffset : 0; - if (!Texture) + if (Section->PrimitiveType != LC_MESH_TEXTURED_TRIANGLES) { Context->SetMaterial(FlatMaterial); Context->SetVertexFormat(VertexBufferOffset, 3, 1, 0, 0, DrawLit); } else { - if (Texture->NeedsUpload()) - Texture->Upload(Context); - Context->SetMaterial(TexturedMaterial); + lcTexture* const Texture = Section->Texture; + + if (Texture) + { + if (Texture->NeedsUpload()) + Texture->Upload(Context); + + Context->SetMaterial(TexturedMaterial); + Context->BindTexture2D(Texture->mTexture); + } + else + { + Context->SetMaterial(FlatMaterial); + } + VertexBufferOffset += Mesh->mNumVertices * sizeof(lcVertex); Context->SetVertexFormat(VertexBufferOffset, 3, 1, 2, 0, DrawLit); - Context->BindTexture2D(Texture->mTexture); } const GLenum DrawPrimitiveType = Section->PrimitiveType & (LC_MESH_TRIANGLES | LC_MESH_TEXTURED_TRIANGLES) ? GL_TRIANGLES : GL_LINES;