diff --git a/common/lc_meshloader.cpp b/common/lc_meshloader.cpp index 3dec271b..6c75f07f 100644 --- a/common/lc_meshloader.cpp +++ b/common/lc_meshloader.cpp @@ -842,12 +842,6 @@ void lcLibraryMeshData::AddMeshDataNoDuplicateCheck(const lcLibraryMeshData& Dat mHasTextures |= (Data.mHasTextures || TextureMap); } -struct lcMergeSection -{ - lcMeshLoaderSection* Shared; - lcMeshLoaderSection* Lod; -}; - static bool lcLibraryMeshSectionCompare(const lcMergeSection& First, const lcMergeSection& Second) { lcMeshLoaderSection* a = First.Lod ? First.Lod : First.Shared; @@ -1106,13 +1100,29 @@ lcMesh* lcLibraryMeshData::CreateMesh() } } - NumIndices = 0; + if (Mesh->mIndexType == GL_UNSIGNED_SHORT) + WriteSections(Mesh, MergeSections, BaseVertices, BaseTexturedVertices, BaseConditionalVertices); + else + WriteSections(Mesh, MergeSections, BaseVertices, BaseTexturedVertices, BaseConditionalVertices); + + if (mHasStyleStud) + Mesh->mFlags |= lcMeshFlag::HasStyleStud; + + UpdateMeshBoundingBox(Mesh); + + return Mesh; +} + +template +void lcLibraryMeshData::WriteSections(lcMesh* Mesh, const lcArray (&MergeSections)[LC_NUM_MESH_LODS], int(&BaseVertices)[LC_NUM_MESHDATA_TYPES], int(&BaseTexturedVertices)[LC_NUM_MESHDATA_TYPES], int(&BaseConditionalVertices)[LC_NUM_MESHDATA_TYPES]) +{ + int NumIndices = 0; for (int LodIdx = 0; LodIdx < LC_NUM_MESH_LODS; LodIdx++) { for (int SectionIdx = 0; SectionIdx < MergeSections[LodIdx].GetSize(); SectionIdx++) { - lcMergeSection& MergeSection = MergeSections[LodIdx][SectionIdx]; + const lcMergeSection& MergeSection = MergeSections[LodIdx][SectionIdx]; lcMeshSection& DstSection = Mesh->mLods[LodIdx].Sections[SectionIdx]; lcMeshLoaderSection* SetupSection = MergeSection.Shared ? MergeSection.Shared : MergeSection.Lod; @@ -1125,129 +1135,64 @@ lcMesh* lcLibraryMeshData::CreateMesh() if (DstSection.Texture) DstSection.Texture->AddRef(); - if (Mesh->mIndexType == GL_UNSIGNED_SHORT) + DstSection.IndexOffset = NumIndices * sizeof(IndexType); + + IndexType* Index = (IndexType*)Mesh->mIndexData + NumIndices; + + if (MergeSection.Shared) { - DstSection.IndexOffset = NumIndices * 2; + lcMeshLoaderSection* SrcSection = MergeSection.Shared; - quint16* Index = (quint16*)Mesh->mIndexData + NumIndices; - - if (MergeSection.Shared) + if (DstSection.PrimitiveType != LC_MESH_CONDITIONAL_LINES) { - lcMeshLoaderSection* SrcSection = MergeSection.Shared; - - if (DstSection.PrimitiveType != LC_MESH_CONDITIONAL_LINES) + if (!mHasTextures) { - if (!mHasTextures) - { - quint16 BaseVertex = DstSection.Texture ? BaseTexturedVertices[LC_MESHDATA_SHARED] : BaseVertices[LC_MESHDATA_SHARED]; - - for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.GetSize(); IndexIdx++) - *Index++ = BaseVertex + SrcSection->mIndices[IndexIdx]; - } - else - for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.GetSize(); IndexIdx++) - *Index++ = SrcSection->mIndices[IndexIdx]; - } - else - { - quint16 BaseVertex = BaseConditionalVertices[LC_MESHDATA_SHARED]; + IndexType BaseVertex = DstSection.Texture ? BaseTexturedVertices[LC_MESHDATA_SHARED] : BaseVertices[LC_MESHDATA_SHARED]; for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.GetSize(); IndexIdx++) *Index++ = BaseVertex + SrcSection->mIndices[IndexIdx]; } - - DstSection.NumIndices += SrcSection->mIndices.GetSize(); - } - - if (MergeSection.Lod) - { - lcMeshLoaderSection* SrcSection = MergeSection.Lod; - - if (DstSection.PrimitiveType != LC_MESH_CONDITIONAL_LINES) - { - if (!mHasTextures) - { - quint16 BaseVertex = DstSection.Texture ? BaseTexturedVertices[LodIdx] : BaseVertices[LodIdx]; - - for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.GetSize(); IndexIdx++) - *Index++ = BaseVertex + SrcSection->mIndices[IndexIdx]; - } - else - for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.GetSize(); IndexIdx++) - *Index++ = SrcSection->mIndices[IndexIdx]; - } else - { - quint16 BaseVertex = BaseConditionalVertices[LodIdx]; - for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.GetSize(); IndexIdx++) - *Index++ = BaseVertex + SrcSection->mIndices[IndexIdx]; - } - - DstSection.NumIndices += SrcSection->mIndices.GetSize(); + *Index++ = SrcSection->mIndices[IndexIdx]; } + else + { + IndexType BaseVertex = BaseConditionalVertices[LC_MESHDATA_SHARED]; + + for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.GetSize(); IndexIdx++) + *Index++ = BaseVertex + SrcSection->mIndices[IndexIdx]; + } + + DstSection.NumIndices += SrcSection->mIndices.GetSize(); } - else + + if (MergeSection.Lod) { - DstSection.IndexOffset = NumIndices * 4; + lcMeshLoaderSection* SrcSection = MergeSection.Lod; - quint32* Index = (quint32*)Mesh->mIndexData + NumIndices; - - if (MergeSection.Shared) + if (DstSection.PrimitiveType != LC_MESH_CONDITIONAL_LINES) { - lcMeshLoaderSection* SrcSection = MergeSection.Shared; - - if (DstSection.PrimitiveType != LC_MESH_CONDITIONAL_LINES) + if (!mHasTextures) { - if (!mHasTextures) - { - quint32 BaseVertex = DstSection.Texture ? BaseTexturedVertices[LC_MESHDATA_SHARED] : BaseVertices[LC_MESHDATA_SHARED]; - - for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.GetSize(); IndexIdx++) - *Index++ = BaseVertex + SrcSection->mIndices[IndexIdx]; - } - else - for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.GetSize(); IndexIdx++) - *Index++ = SrcSection->mIndices[IndexIdx]; - } - else - { - quint32 BaseVertex = BaseConditionalVertices[LC_MESHDATA_SHARED]; + IndexType BaseVertex = DstSection.Texture ? BaseTexturedVertices[LodIdx] : BaseVertices[LodIdx]; for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.GetSize(); IndexIdx++) *Index++ = BaseVertex + SrcSection->mIndices[IndexIdx]; } - - DstSection.NumIndices += SrcSection->mIndices.GetSize(); - } - - if (MergeSection.Lod) - { - lcMeshLoaderSection* SrcSection = MergeSection.Lod; - - if (DstSection.PrimitiveType != LC_MESH_CONDITIONAL_LINES) - { - if (!mHasTextures) - { - quint32 BaseVertex = DstSection.Texture ? BaseTexturedVertices[LodIdx] : BaseVertices[LodIdx]; - - for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.GetSize(); IndexIdx++) - *Index++ = BaseVertex + SrcSection->mIndices[IndexIdx]; - } - else - for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.GetSize(); IndexIdx++) - *Index++ = SrcSection->mIndices[IndexIdx]; - } else - { - quint32 BaseVertex = BaseConditionalVertices[LodIdx]; - for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.GetSize(); IndexIdx++) - *Index++ = BaseVertex + SrcSection->mIndices[IndexIdx]; - } - - DstSection.NumIndices += SrcSection->mIndices.GetSize(); + *Index++ = SrcSection->mIndices[IndexIdx]; } + else + { + IndexType BaseVertex = BaseConditionalVertices[LodIdx]; + + for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.GetSize(); IndexIdx++) + *Index++ = BaseVertex + SrcSection->mIndices[IndexIdx]; + } + + DstSection.NumIndices += SrcSection->mIndices.GetSize(); } if (DstSection.PrimitiveType == LC_MESH_TRIANGLES || DstSection.PrimitiveType == LC_MESH_TEXTURED_TRIANGLES) @@ -1271,13 +1216,6 @@ lcMesh* lcLibraryMeshData::CreateMesh() NumIndices += DstSection.NumIndices; } } - - if (mHasStyleStud) - Mesh->mFlags |= lcMeshFlag::HasStyleStud; - - UpdateMeshBoundingBox(Mesh); - - return Mesh; } void lcLibraryMeshData::UpdateMeshBoundingBox(lcMesh* Mesh) diff --git a/common/lc_meshloader.h b/common/lc_meshloader.h index 5da75a78..a03414b1 100644 --- a/common/lc_meshloader.h +++ b/common/lc_meshloader.h @@ -46,6 +46,12 @@ public: lcArray mIndices; }; +struct lcMergeSection +{ + lcMeshLoaderSection* Shared; + lcMeshLoaderSection* Lod; +}; + enum class lcMeshLoaderTextureMapType { Planar, @@ -177,6 +183,9 @@ public: bool mHasStyleStud; protected: + template + void WriteSections(lcMesh* Mesh, const lcArray (&MergeSections)[LC_NUM_MESH_LODS], int (&BaseVertices)[LC_NUM_MESHDATA_TYPES], int (&BaseTexturedVertices)[LC_NUM_MESHDATA_TYPES], int (&BaseConditionalVertices)[LC_NUM_MESHDATA_TYPES]); + static void UpdateMeshBoundingBox(lcMesh* Mesh); template static void UpdateMeshSectionBoundingBox(lcMesh* Mesh, lcMeshSection& Section, lcVector3& SectionMin, lcVector3& SectionMax);