mirror of
https://github.com/leozide/leocad
synced 2025-02-07 08:45:49 +01:00
Refactored duplicate code.
This commit is contained in:
parent
613bc91c4d
commit
ee5c551bc8
2 changed files with 62 additions and 115 deletions
|
@ -842,12 +842,6 @@ void lcLibraryMeshData::AddMeshDataNoDuplicateCheck(const lcLibraryMeshData& Dat
|
||||||
mHasTextures |= (Data.mHasTextures || TextureMap);
|
mHasTextures |= (Data.mHasTextures || TextureMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct lcMergeSection
|
|
||||||
{
|
|
||||||
lcMeshLoaderSection* Shared;
|
|
||||||
lcMeshLoaderSection* Lod;
|
|
||||||
};
|
|
||||||
|
|
||||||
static bool lcLibraryMeshSectionCompare(const lcMergeSection& First, const lcMergeSection& Second)
|
static bool lcLibraryMeshSectionCompare(const lcMergeSection& First, const lcMergeSection& Second)
|
||||||
{
|
{
|
||||||
lcMeshLoaderSection* a = First.Lod ? First.Lod : First.Shared;
|
lcMeshLoaderSection* a = First.Lod ? First.Lod : First.Shared;
|
||||||
|
@ -1106,13 +1100,29 @@ lcMesh* lcLibraryMeshData::CreateMesh()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
NumIndices = 0;
|
if (Mesh->mIndexType == GL_UNSIGNED_SHORT)
|
||||||
|
WriteSections<quint16>(Mesh, MergeSections, BaseVertices, BaseTexturedVertices, BaseConditionalVertices);
|
||||||
|
else
|
||||||
|
WriteSections<quint32>(Mesh, MergeSections, BaseVertices, BaseTexturedVertices, BaseConditionalVertices);
|
||||||
|
|
||||||
|
if (mHasStyleStud)
|
||||||
|
Mesh->mFlags |= lcMeshFlag::HasStyleStud;
|
||||||
|
|
||||||
|
UpdateMeshBoundingBox(Mesh);
|
||||||
|
|
||||||
|
return Mesh;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename IndexType>
|
||||||
|
void lcLibraryMeshData::WriteSections(lcMesh* Mesh, const lcArray<lcMergeSection> (&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 LodIdx = 0; LodIdx < LC_NUM_MESH_LODS; LodIdx++)
|
||||||
{
|
{
|
||||||
for (int SectionIdx = 0; SectionIdx < MergeSections[LodIdx].GetSize(); SectionIdx++)
|
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];
|
lcMeshSection& DstSection = Mesh->mLods[LodIdx].Sections[SectionIdx];
|
||||||
|
|
||||||
lcMeshLoaderSection* SetupSection = MergeSection.Shared ? MergeSection.Shared : MergeSection.Lod;
|
lcMeshLoaderSection* SetupSection = MergeSection.Shared ? MergeSection.Shared : MergeSection.Lod;
|
||||||
|
@ -1125,11 +1135,9 @@ lcMesh* lcLibraryMeshData::CreateMesh()
|
||||||
if (DstSection.Texture)
|
if (DstSection.Texture)
|
||||||
DstSection.Texture->AddRef();
|
DstSection.Texture->AddRef();
|
||||||
|
|
||||||
if (Mesh->mIndexType == GL_UNSIGNED_SHORT)
|
DstSection.IndexOffset = NumIndices * sizeof(IndexType);
|
||||||
{
|
|
||||||
DstSection.IndexOffset = NumIndices * 2;
|
|
||||||
|
|
||||||
quint16* Index = (quint16*)Mesh->mIndexData + NumIndices;
|
IndexType* Index = (IndexType*)Mesh->mIndexData + NumIndices;
|
||||||
|
|
||||||
if (MergeSection.Shared)
|
if (MergeSection.Shared)
|
||||||
{
|
{
|
||||||
|
@ -1139,7 +1147,7 @@ lcMesh* lcLibraryMeshData::CreateMesh()
|
||||||
{
|
{
|
||||||
if (!mHasTextures)
|
if (!mHasTextures)
|
||||||
{
|
{
|
||||||
quint16 BaseVertex = DstSection.Texture ? BaseTexturedVertices[LC_MESHDATA_SHARED] : BaseVertices[LC_MESHDATA_SHARED];
|
IndexType BaseVertex = DstSection.Texture ? BaseTexturedVertices[LC_MESHDATA_SHARED] : BaseVertices[LC_MESHDATA_SHARED];
|
||||||
|
|
||||||
for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.GetSize(); IndexIdx++)
|
for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.GetSize(); IndexIdx++)
|
||||||
*Index++ = BaseVertex + SrcSection->mIndices[IndexIdx];
|
*Index++ = BaseVertex + SrcSection->mIndices[IndexIdx];
|
||||||
|
@ -1150,7 +1158,7 @@ lcMesh* lcLibraryMeshData::CreateMesh()
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
quint16 BaseVertex = BaseConditionalVertices[LC_MESHDATA_SHARED];
|
IndexType BaseVertex = BaseConditionalVertices[LC_MESHDATA_SHARED];
|
||||||
|
|
||||||
for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.GetSize(); IndexIdx++)
|
for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.GetSize(); IndexIdx++)
|
||||||
*Index++ = BaseVertex + SrcSection->mIndices[IndexIdx];
|
*Index++ = BaseVertex + SrcSection->mIndices[IndexIdx];
|
||||||
|
@ -1167,7 +1175,7 @@ lcMesh* lcLibraryMeshData::CreateMesh()
|
||||||
{
|
{
|
||||||
if (!mHasTextures)
|
if (!mHasTextures)
|
||||||
{
|
{
|
||||||
quint16 BaseVertex = DstSection.Texture ? BaseTexturedVertices[LodIdx] : BaseVertices[LodIdx];
|
IndexType BaseVertex = DstSection.Texture ? BaseTexturedVertices[LodIdx] : BaseVertices[LodIdx];
|
||||||
|
|
||||||
for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.GetSize(); IndexIdx++)
|
for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.GetSize(); IndexIdx++)
|
||||||
*Index++ = BaseVertex + SrcSection->mIndices[IndexIdx];
|
*Index++ = BaseVertex + SrcSection->mIndices[IndexIdx];
|
||||||
|
@ -1178,7 +1186,7 @@ lcMesh* lcLibraryMeshData::CreateMesh()
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
quint16 BaseVertex = BaseConditionalVertices[LodIdx];
|
IndexType BaseVertex = BaseConditionalVertices[LodIdx];
|
||||||
|
|
||||||
for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.GetSize(); IndexIdx++)
|
for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.GetSize(); IndexIdx++)
|
||||||
*Index++ = BaseVertex + SrcSection->mIndices[IndexIdx];
|
*Index++ = BaseVertex + SrcSection->mIndices[IndexIdx];
|
||||||
|
@ -1186,69 +1194,6 @@ lcMesh* lcLibraryMeshData::CreateMesh()
|
||||||
|
|
||||||
DstSection.NumIndices += SrcSection->mIndices.GetSize();
|
DstSection.NumIndices += SrcSection->mIndices.GetSize();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
DstSection.IndexOffset = NumIndices * 4;
|
|
||||||
|
|
||||||
quint32* Index = (quint32*)Mesh->mIndexData + NumIndices;
|
|
||||||
|
|
||||||
if (MergeSection.Shared)
|
|
||||||
{
|
|
||||||
lcMeshLoaderSection* SrcSection = MergeSection.Shared;
|
|
||||||
|
|
||||||
if (DstSection.PrimitiveType != LC_MESH_CONDITIONAL_LINES)
|
|
||||||
{
|
|
||||||
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];
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (DstSection.PrimitiveType == LC_MESH_TRIANGLES || DstSection.PrimitiveType == LC_MESH_TEXTURED_TRIANGLES)
|
if (DstSection.PrimitiveType == LC_MESH_TRIANGLES || DstSection.PrimitiveType == LC_MESH_TEXTURED_TRIANGLES)
|
||||||
{
|
{
|
||||||
|
@ -1271,13 +1216,6 @@ lcMesh* lcLibraryMeshData::CreateMesh()
|
||||||
NumIndices += DstSection.NumIndices;
|
NumIndices += DstSection.NumIndices;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mHasStyleStud)
|
|
||||||
Mesh->mFlags |= lcMeshFlag::HasStyleStud;
|
|
||||||
|
|
||||||
UpdateMeshBoundingBox(Mesh);
|
|
||||||
|
|
||||||
return Mesh;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void lcLibraryMeshData::UpdateMeshBoundingBox(lcMesh* Mesh)
|
void lcLibraryMeshData::UpdateMeshBoundingBox(lcMesh* Mesh)
|
||||||
|
|
|
@ -46,6 +46,12 @@ public:
|
||||||
lcArray<quint32> mIndices;
|
lcArray<quint32> mIndices;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct lcMergeSection
|
||||||
|
{
|
||||||
|
lcMeshLoaderSection* Shared;
|
||||||
|
lcMeshLoaderSection* Lod;
|
||||||
|
};
|
||||||
|
|
||||||
enum class lcMeshLoaderTextureMapType
|
enum class lcMeshLoaderTextureMapType
|
||||||
{
|
{
|
||||||
Planar,
|
Planar,
|
||||||
|
@ -177,6 +183,9 @@ public:
|
||||||
bool mHasStyleStud;
|
bool mHasStyleStud;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
template<typename IndexType>
|
||||||
|
void WriteSections(lcMesh* Mesh, const lcArray<lcMergeSection> (&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);
|
static void UpdateMeshBoundingBox(lcMesh* Mesh);
|
||||||
template<typename IndexType>
|
template<typename IndexType>
|
||||||
static void UpdateMeshSectionBoundingBox(lcMesh* Mesh, lcMeshSection& Section, lcVector3& SectionMin, lcVector3& SectionMax);
|
static void UpdateMeshSectionBoundingBox(lcMesh* Mesh, lcMeshSection& Section, lcVector3& SectionMin, lcVector3& SectionMax);
|
||||||
|
|
Loading…
Add table
Reference in a new issue