From e0751a6f75eecbd1200dd299c4c87bfee7fa3196 Mon Sep 17 00:00:00 2001 From: leo Date: Tue, 16 Oct 2012 00:43:52 +0000 Subject: [PATCH] Sort mesh sections. --- common/lc_global.h | 2 ++ common/lc_library.cpp | 58 +++++++++++++++++++++++++++++++++++------- common/lc_library.h | 10 ++++---- common/lc_mesh.h | 2 -- common/lc_texture.h | 5 +++- common/object.cpp | 1 - common/project.h | 1 - common/terrain.h | 1 - win/propertiespane.cpp | 1 - 9 files changed, 60 insertions(+), 21 deletions(-) diff --git a/common/lc_global.h b/common/lc_global.h index 43ac6b8d..3bb47ace 100644 --- a/common/lc_global.h +++ b/common/lc_global.h @@ -15,11 +15,13 @@ #endif // Forward declarations. +class lcVector2; class lcVector3; class lcVector4; class lcMatrix44; class lcMesh; +class lcTexture; class lcFile; class lcMemFile; diff --git a/common/lc_library.cpp b/common/lc_library.cpp index 2f2fc8b2..bc20f037 100644 --- a/common/lc_library.cpp +++ b/common/lc_library.cpp @@ -482,6 +482,39 @@ bool lcPiecesLibrary::OpenDirectory(const char* Path) return true; } +int LibraryMeshSectionCompare(const lcLibraryMeshSection* a, const lcLibraryMeshSection* b, void* Data) +{ + if (a->mPrimitiveType != b->mPrimitiveType) + { + int PrimitiveOrder[LC_MESH_NUM_PRIMITIVE_TYPES] = + { + LC_MESH_TRIANGLES, + LC_MESH_TEXTURED_TRIANGLES, + LC_MESH_LINES, + LC_MESH_TEXTURED_LINES, + }; + + for (int PrimitiveType = 0; PrimitiveType < LC_MESH_NUM_PRIMITIVE_TYPES; PrimitiveType++) + { + int Primitive = PrimitiveOrder[PrimitiveType]; + + if (a->mPrimitiveType == Primitive) + return -1; + + if (b->mPrimitiveType == Primitive) + return 1; + } + } + + bool TranslucentA = lcIsColorTranslucent(a->mColor); + bool TranslucentB = lcIsColorTranslucent(b->mColor); + + if (TranslucentA != TranslucentB) + return TranslucentA ? 1 : -1; + + return a->mColor > b->mColor ? -1 : 1; +} + bool lcPiecesLibrary::LoadPiece(PieceInfo* Info) { lcLibraryMeshData MeshData; @@ -520,7 +553,14 @@ bool lcPiecesLibrary::LoadPiece(PieceInfo* Info) int NumIndices = 0; for (int SectionIdx = 0; SectionIdx < MeshData.mSections.GetSize(); SectionIdx++) - NumIndices += MeshData.mSections[SectionIdx]->mIndices.GetSize(); + { + lcLibraryMeshSection* Section = MeshData.mSections[SectionIdx]; + + Section->mColor = lcGetColorIndex(Section->mColor); + NumIndices += Section->mIndices.GetSize(); + } + + MeshData.mSections.Sort(LibraryMeshSectionCompare, NULL); Mesh->Create(MeshData.mSections.GetSize(), MeshData.mVertices.GetSize(), MeshData.mTexturedVertices.GetSize(), NumIndices); @@ -579,7 +619,7 @@ bool lcPiecesLibrary::LoadPiece(PieceInfo* Info) lcMeshSection& DstSection = Mesh->mSections[SectionIdx]; lcLibraryMeshSection* SrcSection = MeshData.mSections[SectionIdx]; - DstSection.ColorIndex = lcGetColorIndex(SrcSection->mColorCode); + DstSection.ColorIndex = SrcSection->mColor; DstSection.PrimitiveType = (SrcSection->mPrimitiveType == LC_MESH_TRIANGLES || SrcSection->mPrimitiveType == LC_MESH_TEXTURED_TRIANGLES) ? GL_TRIANGLES : GL_LINES; DstSection.NumIndices = SrcSection->mIndices.GetSize(); DstSection.Texture = SrcSection->mTexture; @@ -608,7 +648,7 @@ bool lcPiecesLibrary::LoadPiece(PieceInfo* Info) if (DstSection.PrimitiveType == GL_TRIANGLES) { - if (SrcSection->mColorCode == 16) + if (SrcSection->mColor == 16) Info->mFlags |= LC_PIECE_HAS_DEFAULT; else { @@ -1072,7 +1112,7 @@ void lcLibraryMeshData::AddLine(int LineType, lcuint32 ColorCode, const lcVector { Section = mSections[SectionIdx]; - if (Section->mColorCode == ColorCode && Section->mPrimitiveType == PrimitiveType) + if (Section->mColor == ColorCode && Section->mPrimitiveType == PrimitiveType) break; } @@ -1145,7 +1185,7 @@ void lcLibraryMeshData::AddTexturedLine(int LineType, lcuint32 ColorCode, const { Section = mSections[SectionIdx]; - if (Section->mColorCode == ColorCode && Section->mPrimitiveType == PrimitiveType) + if (Section->mColor == ColorCode && Section->mPrimitiveType == PrimitiveType) break; } @@ -1326,7 +1366,7 @@ void lcLibraryMeshData::AddMeshData(const lcLibraryMeshData& Data, const lcMatri { lcLibraryMeshSection* SrcSection = Data.mSections[SrcSectionIdx]; lcLibraryMeshSection* DstSection = NULL; - lcuint32 ColorCode = SrcSection->mColorCode == 16 ? CurrentColorCode : SrcSection->mColorCode; + lcuint32 ColorCode = SrcSection->mColor == 16 ? CurrentColorCode : SrcSection->mColor; lcTexture* Texture; if (SrcSection->mTexture) @@ -1340,7 +1380,7 @@ void lcLibraryMeshData::AddMeshData(const lcLibraryMeshData& Data, const lcMatri { lcLibraryMeshSection* Section = mSections[DstSectionIdx]; - if (Section->mColorCode == ColorCode && Section->mPrimitiveType == SrcSection->mPrimitiveType && Section->mTexture == Texture) + if (Section->mColor == ColorCode && Section->mPrimitiveType == SrcSection->mPrimitiveType && Section->mTexture == Texture) { DstSection = Section; break; @@ -1425,7 +1465,7 @@ void lcLibraryMeshData::AddMeshDataNoDuplicateCheck(const lcLibraryMeshData& Dat { lcLibraryMeshSection* SrcSection = Data.mSections[SrcSectionIdx]; lcLibraryMeshSection* DstSection = NULL; - lcuint32 ColorCode = SrcSection->mColorCode == 16 ? CurrentColorCode : SrcSection->mColorCode; + lcuint32 ColorCode = SrcSection->mColor == 16 ? CurrentColorCode : SrcSection->mColor; lcTexture* Texture; if (SrcSection->mTexture) @@ -1439,7 +1479,7 @@ void lcLibraryMeshData::AddMeshDataNoDuplicateCheck(const lcLibraryMeshData& Dat { lcLibraryMeshSection* Section = mSections[DstSectionIdx]; - if (Section->mColorCode == ColorCode && Section->mPrimitiveType == SrcSection->mPrimitiveType && Section->mTexture == Texture) + if (Section->mColor == ColorCode && Section->mPrimitiveType == SrcSection->mPrimitiveType && Section->mTexture == Texture) { DstSection = Section; break; diff --git a/common/lc_library.h b/common/lc_library.h index 5b494182..ef11ba5a 100644 --- a/common/lc_library.h +++ b/common/lc_library.h @@ -7,7 +7,6 @@ #include "str.h" class PieceInfo; -class lcTexture; class lcZipFile; #define LC_CATEGORY_FILE_ID LC_FOURCC('C', 'A', 'T', 0) @@ -18,17 +17,18 @@ enum LC_MESH_PRIMITIVE_TYPE LC_MESH_LINES, LC_MESH_TRIANGLES, LC_MESH_TEXTURED_LINES, - LC_MESH_TEXTURED_TRIANGLES + LC_MESH_TEXTURED_TRIANGLES, + LC_MESH_NUM_PRIMITIVE_TYPES }; class lcLibraryMeshSection { public: - lcLibraryMeshSection(LC_MESH_PRIMITIVE_TYPE PrimitiveType, lcuint32 ColorCode, lcTexture* Texture) + lcLibraryMeshSection(LC_MESH_PRIMITIVE_TYPE PrimitiveType, lcuint32 Color, lcTexture* Texture) : mIndices(1024, 1024) { mPrimitiveType = PrimitiveType; - mColorCode = ColorCode; + mColor = Color; mTexture = Texture; } @@ -37,7 +37,7 @@ public: } LC_MESH_PRIMITIVE_TYPE mPrimitiveType; - lcuint32 mColorCode; + lcuint32 mColor; lcTexture* mTexture; ObjArray mIndices; }; diff --git a/common/lc_mesh.h b/common/lc_mesh.h index 19dce336..d0a73e1a 100644 --- a/common/lc_mesh.h +++ b/common/lc_mesh.h @@ -5,8 +5,6 @@ #include "opengl.h" #include "lc_math.h" -class lcTexture; - struct lcVertex { lcVector3 Position; diff --git a/common/lc_texture.h b/common/lc_texture.h index 30f5bd1a..075ba326 100644 --- a/common/lc_texture.h +++ b/common/lc_texture.h @@ -13,7 +13,6 @@ public: lcTexture(); ~lcTexture(); - bool Load(); bool Load(const char* FileName, int Flags = 0); bool Load(lcFile& File, int Flags = 0); void Unload(); @@ -42,6 +41,10 @@ public: int mHeight; char mName[LC_MAXPATH]; GLuint mTexture; + +protected: + bool Load(); + int mRefCount; }; diff --git a/common/object.cpp b/common/object.cpp index 3c149392..c7e2eb0f 100755 --- a/common/object.cpp +++ b/common/object.cpp @@ -9,7 +9,6 @@ #include "globals.h" #include "project.h" #include "object.h" -#include "matrix.h" #include "lc_file.h" #include "lc_application.h" diff --git a/common/project.h b/common/project.h index 422af8fc..2d99f4d9 100644 --- a/common/project.h +++ b/common/project.h @@ -46,7 +46,6 @@ class Piece; class Camera; class Light; class Group; -class lcTexture; class Terrain; class PieceInfo; class Matrix; diff --git a/common/terrain.h b/common/terrain.h index f4c11d9b..e1d29de4 100644 --- a/common/terrain.h +++ b/common/terrain.h @@ -2,7 +2,6 @@ #define _TERRAIN_H_ class Camera; -class lcTexture; class TerrainPatch { diff --git a/win/propertiespane.cpp b/win/propertiespane.cpp index 927a5431..dbd10f66 100644 --- a/win/propertiespane.cpp +++ b/win/propertiespane.cpp @@ -5,7 +5,6 @@ #include "piece.h" #include "camera.h" #include "light.h" -#include "matrix.h" #include "lc_application.h" BEGIN_MESSAGE_MAP(CPropertiesPane, CDockablePane)