diff --git a/common/lc_model.cpp b/common/lc_model.cpp index 0611262b..03a6ad86 100644 --- a/common/lc_model.cpp +++ b/common/lc_model.cpp @@ -4425,6 +4425,18 @@ void lcModel::SetMinifig(const lcMinifig& Minifig) SetSelectionAndFocus(Pieces, nullptr, 0, false); } +void lcModel::SetPreviewPieceInfo(PieceInfo* Info, int ColorIndex) +{ + DeleteModel(); + + lcPiece* Piece = new lcPiece(Info); + + Piece->Initialize(lcMatrix44Identity(), 1); + Piece->SetColorIndex(ColorIndex); + AddPiece(Piece); + Piece->UpdatePosition(1); +} + void lcModel::UpdateInterface() { if (!gMainWindow) diff --git a/common/lc_model.h b/common/lc_model.h index 80e9c2fc..da4f17cc 100644 --- a/common/lc_model.h +++ b/common/lc_model.h @@ -226,10 +226,7 @@ public: } void SetMinifig(const lcMinifig& Minifig); - void SetPreviewPiece(lcPiece* Piece) - { - AddPiece(Piece); - } + void SetPreviewPieceInfo(PieceInfo* Info, int ColorIndex); void Cut(); void Copy(); diff --git a/common/lc_partselectionwidget.cpp b/common/lc_partselectionwidget.cpp index 5b64cd6f..03de8351 100644 --- a/common/lc_partselectionwidget.cpp +++ b/common/lc_partselectionwidget.cpp @@ -8,11 +8,10 @@ #include "lc_model.h" #include "project.h" #include "pieceinf.h" +#include "camera.h" #include "lc_scene.h" #include "lc_view.h" #include "lc_glextensions.h" -#include "lc_viewwidget.h" -#include "lc_previewwidget.h" #include "lc_category.h" Q_DECLARE_METATYPE(QList) @@ -67,6 +66,9 @@ lcPartSelectionListModel::lcPartSelectionListModel(QObject* Parent) lcPartSelectionListModel::~lcPartSelectionListModel() { ClearRequests(); + + mView.reset(); + mModel.reset(); } void lcPartSelectionListModel::ClearRequests() @@ -395,10 +397,13 @@ void lcPartSelectionListModel::DrawPreview(int InfoIndex) if (!mView) { - mView = std::unique_ptr(new lcView(lcViewType::PartsList, nullptr)); + if (!mModel) + mModel = std::unique_ptr(new lcModel(QString(), nullptr, true)); + mView = std::unique_ptr(new lcView(lcViewType::PartsList, mModel.get())); mView->SetOffscreenContext(); mView->MakeCurrent(); + mView->SetSize(Width, Height); if (!mView->BeginRenderToImage(Width, Height)) { @@ -410,34 +415,20 @@ void lcPartSelectionListModel::DrawPreview(int InfoIndex) mView->MakeCurrent(); mView->BindRenderFramebuffer(); - lcContext* Context = mView->mContext; + const uint BackgroundColor = mListView->palette().color(QPalette::Base).rgba(); + mView->SetBackgroundColorOverride(LC_RGBA(qRed(BackgroundColor), qGreen(BackgroundColor), qBlue(BackgroundColor), 0)); - const float Aspect = (float)Width / (float)Height; - Context->SetViewport(0, 0, Width, Height); - - Context->SetDefaultState(); - - lcPiecesLibrary* Library = lcGetPiecesLibrary(); PieceInfo* Info = mParts[InfoIndex].first; + mModel->SetPreviewPieceInfo(Info, mColorIndex); - Context->ClearColorAndDepth(lcVector4(1.0f, 1.0f, 1.0f, 0.0f)); + const lcVector3 Center = (Info->GetBoundingBox().Min + Info->GetBoundingBox().Max) / 2.0f; + const lcVector3 Position = Center + lcVector3(100.0f, -100.0f, 75.0f); - lcMatrix44 ProjectionMatrix, ViewMatrix; + mView->GetCamera()->SetViewpoint(Position, Center, lcVector3(0, 0, 1)); + mView->GetCamera()->m_fovy = 20.0f; + mView->ZoomExtents(); - Info->ZoomExtents(20.0f, Aspect, ProjectionMatrix, ViewMatrix); - - Context->SetProjectionMatrix(ProjectionMatrix); - - lcScene Scene; - Scene.SetAllowWireframe(false); - Scene.SetAllowLOD(false); - Scene.Begin(ViewMatrix); - - Info->AddRenderMeshes(&Scene, lcMatrix44Identity(), mColorIndex, lcRenderMeshState::Default, false); - - Scene.End(); - - Scene.Draw(Context); + mView->OnDraw(); mView->UnbindRenderFramebuffer(); @@ -445,9 +436,7 @@ void lcPartSelectionListModel::DrawPreview(int InfoIndex) mParts[InfoIndex].second = QPixmap::fromImage(Image).scaled(mIconSize, mIconSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); - Library->ReleasePieceInfo(Info); - - Context->ClearResources(); + lcGetPiecesLibrary()->ReleasePieceInfo(Info); emit dataChanged(index(InfoIndex, 0), index(InfoIndex, 0), QVector() << Qt::DecorationRole); } diff --git a/common/lc_partselectionwidget.h b/common/lc_partselectionwidget.h index fcabaa87..6bba9189 100644 --- a/common/lc_partselectionwidget.h +++ b/common/lc_partselectionwidget.h @@ -137,6 +137,7 @@ protected: bool mShowPartAliases; QByteArray mFilter; std::unique_ptr mView; + std::unique_ptr mModel; }; class lcPartSelectionListView : public QListView diff --git a/common/lc_previewwidget.cpp b/common/lc_previewwidget.cpp index 3bbc5a24..4ac205a8 100644 --- a/common/lc_previewwidget.cpp +++ b/common/lc_previewwidget.cpp @@ -115,14 +115,9 @@ bool lcPreview::SetCurrentPiece(const QString& PartType, int ColorCode) Library->LoadPieceInfo(Info, false, true); Library->WaitForLoadQueue(); - lcStep CurrentStep = 1; - lcPiece* Piece = new lcPiece(nullptr); + mModel->SetPreviewPieceInfo(Info, lcGetColorIndex(ColorCode)); - Piece->SetPieceInfo(Info, PartType, false); - Piece->Initialize(lcMatrix44Identity(), CurrentStep); - Piece->SetColorCode(ColorCode); - - mModel->SetPreviewPiece(Piece); + Library->ReleasePieceInfo(Info); } else { diff --git a/common/lc_view.cpp b/common/lc_view.cpp index 64b82f3d..58824338 100644 --- a/common/lc_view.cpp +++ b/common/lc_view.cpp @@ -835,6 +835,7 @@ void lcView::OnDraw() const bool DrawOverlays = mWidget != nullptr; const bool DrawInterface = mWidget != nullptr && mViewType == lcViewType::View; + mScene->SetAllowWireframe(mWidget != nullptr); mScene->SetAllowLOD(Preferences.mAllowLOD && mWidget != nullptr); mScene->SetLODDistance(Preferences.mMeshLODDistance);