From 635d408101e15559bdabd892088bee9b6e3bdeb5 Mon Sep 17 00:00:00 2001 From: leo Date: Sat, 28 Feb 2015 20:31:57 +0000 Subject: [PATCH] Support drawing primitives included directly by a model. --- common/lc_library.cpp | 2 +- common/lc_library.h | 9 ++++-- common/lc_model.cpp | 75 +++++++++++++++++-------------------------- common/lc_model.h | 11 ++----- common/pieceinf.cpp | 24 ++++++++++---- 5 files changed, 56 insertions(+), 65 deletions(-) diff --git a/common/lc_library.cpp b/common/lc_library.cpp index d5212740..d03d73ba 100644 --- a/common/lc_library.cpp +++ b/common/lc_library.cpp @@ -1054,7 +1054,7 @@ bool lcPiecesLibrary::LoadTexture(lcTexture* Texture) return true; } -int lcPiecesLibrary::FindPrimitiveIndex(const char* Name) +int lcPiecesLibrary::FindPrimitiveIndex(const char* Name) const { for (int PrimitiveIndex = 0; PrimitiveIndex < mPrimitives.GetSize(); PrimitiveIndex++) if (!strcmp(mPrimitives[PrimitiveIndex]->mName, Name)) diff --git a/common/lc_library.h b/common/lc_library.h index 2ea8284c..2d0e653d 100644 --- a/common/lc_library.h +++ b/common/lc_library.h @@ -137,12 +137,18 @@ public: void GetCategoryEntries(const String& CategoryKeywords, bool GroupPieces, lcArray& SinglePieces, lcArray& GroupedPieces); void GetPatternedPieces(PieceInfo* Parent, lcArray& Pieces) const; + bool IsPrimitive(const char* Name) const + { + return FindPrimitiveIndex(Name) != -1; + } + void SetOfficialPieces() { if (mZipFiles[LC_ZIPFILE_OFFICIAL]) mNumOfficialPieces = mPieces.GetSize(); } + bool ReadMeshData(lcFile& File, const lcMatrix44& CurrentTransform, lcuint32 CurrentColorCode, lcArray& TextureStack, lcLibraryMeshData& MeshData); void CreateMesh(PieceInfo* Info, lcLibraryMeshData& MeshData); lcArray mPieces; @@ -163,9 +169,8 @@ protected: bool LoadCachePiece(PieceInfo* Info); void SaveCacheFile(); - int FindPrimitiveIndex(const char* Name); + int FindPrimitiveIndex(const char* Name) const; bool LoadPrimitive(int PrimitiveIndex); - bool ReadMeshData(lcFile& File, const lcMatrix44& CurrentTransform, lcuint32 CurrentColorCode, lcArray& TextureStack, lcLibraryMeshData& MeshData); char mCacheFileName[LC_MAXPATH]; lcuint64 mCacheFileModifiedTime; diff --git a/common/lc_model.cpp b/common/lc_model.cpp index a87aeea2..cdb430be 100644 --- a/common/lc_model.cpp +++ b/common/lc_model.cpp @@ -205,7 +205,7 @@ void lcModel::DeleteModel() mCameras.DeleteAll(); mLights.DeleteAll(); mGroups.DeleteAll(); - mMeshLines.RemoveAll(); + mMeshLines.clear(); } void lcModel::CreatePieceInfo(Project* Project) @@ -382,6 +382,7 @@ void lcModel::LoadLDraw(QIODevice& Device, Project* Project) qint64 Pos = Device.pos(); QString Line = Device.readLine().trimmed(); QTextStream LineStream(&Line, QIODevice::ReadOnly); + bool MeshLine = false; QString Token; LineStream >> Token; @@ -480,62 +481,44 @@ void lcModel::LoadLDraw(QIODevice& Device, Project* Project) QString File = LineStream.readAll().trimmed().toUpper(); QString PartID = File; + PartID.replace('\\', '/'); if (PartID.endsWith(QLatin1String(".DAT"))) PartID = PartID.left(PartID.size() - 4); - if (!Piece) - Piece = new lcPiece(NULL); + lcPiecesLibrary* Library = lcGetPiecesLibrary(); - if (!CurrentGroups.IsEmpty()) - Piece->SetGroup(CurrentGroups[CurrentGroups.GetSize() - 1]); + if (Library->IsPrimitive(PartID.toLatin1().constData())) + MeshLine = true; + else + { + if (!Piece) + Piece = new lcPiece(NULL); - PieceInfo* Info = lcGetPiecesLibrary()->FindPiece(PartID.toLatin1().constData(), Project, false); + if (!CurrentGroups.IsEmpty()) + Piece->SetGroup(CurrentGroups[CurrentGroups.GetSize() - 1]); - if (!Info) - Info = lcGetPiecesLibrary()->FindPiece(File.toLatin1().constData(), Project, true); + PieceInfo* Info = Library->FindPiece(PartID.toLatin1().constData(), Project, false); - float* Matrix = IncludeTransform; - lcMatrix44 Transform(lcVector4(Matrix[0], Matrix[2], -Matrix[1], 0.0f), lcVector4(Matrix[8], Matrix[10], -Matrix[9], 0.0f), - lcVector4(-Matrix[4], -Matrix[6], Matrix[5], 0.0f), lcVector4(Matrix[12], Matrix[14], -Matrix[13], 1.0f)); + if (!Info) + Info = Library->FindPiece(File.toLatin1().constData(), Project, true); - Piece->SetPieceInfo(Info); - Piece->Initialize(Transform, CurrentStep); - Piece->SetColorCode(ColorCode); - mPieces.Add(Piece); - Piece = NULL; + float* Matrix = IncludeTransform; + lcMatrix44 Transform(lcVector4(Matrix[0], Matrix[2], -Matrix[1], 0.0f), lcVector4(Matrix[8], Matrix[10], -Matrix[9], 0.0f), + lcVector4(-Matrix[4], -Matrix[6], Matrix[5], 0.0f), lcVector4(Matrix[12], Matrix[14], -Matrix[13], 1.0f)); + + Piece->SetPieceInfo(Info); + Piece->Initialize(Transform, CurrentStep); + Piece->SetColorCode(ColorCode); + mPieces.Add(Piece); + Piece = NULL; + } } - else if (Token == QLatin1String("2")) - { - lcModelMeshLine& Line = mMeshLines.Add(); + else if (Token == QLatin1String("2") || Token == QLatin1String("3") || Token == QLatin1String("4")) + MeshLine = true; - Line.LineType = 2; - LineStream >> Line.ColorCode; - - for (int TokenIdx = 0; TokenIdx < 6; TokenIdx++) - LineStream >> ((float*)Line.Vertices)[TokenIdx]; - - } - else if (Token == QLatin1String("3")) - { - lcModelMeshLine& Line = mMeshLines.Add(); - - Line.LineType = 3; - LineStream >> Line.ColorCode; - - for (int TokenIdx = 0; TokenIdx < 9; TokenIdx++) - LineStream >> ((float*)Line.Vertices)[TokenIdx]; - } - else if (Token == QLatin1String("4")) - { - lcModelMeshLine& Line = mMeshLines.Add(); - - Line.LineType = 4; - LineStream >> Line.ColorCode; - - for (int TokenIdx = 0; TokenIdx < 12; TokenIdx++) - LineStream >> ((float*)Line.Vertices)[TokenIdx]; - } + if (MeshLine) + mMeshLines.append(Line); } mCurrentStep = CurrentStep; diff --git a/common/lc_model.h b/common/lc_model.h index c1bc717f..553c1c43 100644 --- a/common/lc_model.h +++ b/common/lc_model.h @@ -113,13 +113,6 @@ struct lcModelPartsEntry int ColorIndex; }; -struct lcModelMeshLine -{ - int LineType; - int ColorCode; - lcVector3 Vertices[4]; -}; - class lcModel { public: @@ -170,7 +163,7 @@ public: mProperties.mName = Name; } - const lcArray& GetMeshLines() const + const QStringList& GetMeshLines() const { return mMeshLines; } @@ -345,7 +338,7 @@ protected: lcArray mCameras; lcArray mLights; lcArray mGroups; - lcArray mMeshLines; + QStringList mMeshLines; lcModelHistoryEntry* mSavedHistory; lcArray mUndoHistory; diff --git a/common/pieceinf.cpp b/common/pieceinf.cpp index fcf3d8fc..8a439c8b 100644 --- a/common/pieceinf.cpp +++ b/common/pieceinf.cpp @@ -64,19 +64,29 @@ void PieceInfo::SetModel(lcModel* Model, bool UpdateMesh) strncpy(m_strDescription, Model->GetProperties().mName.toLatin1().data(), sizeof(m_strDescription)); m_strDescription[sizeof(m_strDescription)-1] = 0; - const lcArray& MeshLines = Model->GetMeshLines(); + const QStringList& MeshLines = Model->GetMeshLines(); - if (UpdateMesh && !MeshLines.IsEmpty()) + if (UpdateMesh && !MeshLines.isEmpty()) { - lcLibraryMeshData MeshData; + lcMemFile PieceFile; - for (int LineIdx = 0; LineIdx < MeshLines.GetSize(); LineIdx++) + foreach (const QString& Line, MeshLines) { - const lcModelMeshLine& Line = MeshLines[LineIdx]; - MeshData.AddLine(Line.LineType, Line.ColorCode, Line.Vertices); + QByteArray Buffer = Line.toLatin1(); + PieceFile.WriteBuffer(Buffer.constData(), Buffer.size()); + PieceFile.WriteBuffer("\r\n", 2); } - lcGetPiecesLibrary()->CreateMesh(this, MeshData); + lcLibraryMeshData MeshData; + lcArray TextureStack; + PieceFile.Seek(0, SEEK_SET); + + const char* OldLocale = setlocale(LC_NUMERIC, "C"); + bool Ret = lcGetPiecesLibrary()->ReadMeshData(PieceFile, lcMatrix44Identity(), 16, TextureStack, MeshData); + setlocale(LC_NUMERIC, OldLocale); + + if (Ret) + lcGetPiecesLibrary()->CreateMesh(this, MeshData); } }