diff --git a/common/lc_application.cpp b/common/lc_application.cpp index 4d71b965..a8a4056b 100644 --- a/common/lc_application.cpp +++ b/common/lc_application.cpp @@ -171,7 +171,7 @@ bool lcApplication::Initialize(int argc, char* argv[], const char* LibraryInstal // Image output options. bool SaveImage = false; - bool ImageHighlight = false; +// bool ImageHighlight = false; int ImageWidth = lcGetProfileInt(LC_PROFILE_IMAGE_WIDTH); int ImageHeight = lcGetProfileInt(LC_PROFILE_IMAGE_HEIGHT); lcStep ImageStart = 0; @@ -222,8 +222,8 @@ bool lcApplication::Initialize(int argc, char* argv[], const char* LibraryInstal ParseIntegerArgument(&i, argc, argv, &Step); ImageEnd = Step; } - else if (strcmp(Param, "--highlight") == 0) - ImageHighlight = true; +// else if (strcmp(Param, "--highlight") == 0) +// ImageHighlight = true; else if ((strcmp(Param, "-v") == 0) || (strcmp(Param, "--version") == 0)) { printf("LeoCAD Version " LC_VERSION_TEXT "\n"); diff --git a/common/lc_context.cpp b/common/lc_context.cpp index 255ca65c..4f35b707 100644 --- a/common/lc_context.cpp +++ b/common/lc_context.cpp @@ -62,6 +62,19 @@ void lcContext::SetDefaultState() mMatrixMode = GL_MODELVIEW; } +void lcContext::SetViewport(int x, int y, int Width, int Height) +{ + if (mViewportX == x && mViewportY == y && mViewportWidth == Width && mViewportHeight == Height) + return; + + glViewport(x, y, Width, Height); + + mViewportX = x; + mViewportY = y; + mViewportWidth = Width; + mViewportHeight = Height; +} + void lcContext::SetWorldViewMatrix(const lcMatrix44& WorldViewMatrix) { if (mMatrixMode != GL_MODELVIEW) diff --git a/common/lc_context.h b/common/lc_context.h index bc919f75..82f7eba1 100644 --- a/common/lc_context.h +++ b/common/lc_context.h @@ -9,8 +9,19 @@ public: lcContext(); ~lcContext(); + int GetViewportWidth() const + { + return mViewportWidth; + } + + int GetViewportHeight() const + { + return mViewportHeight; + } + void SetDefaultState(); + void SetViewport(int x, int y, int Width, int Height); void SetWorldViewMatrix(const lcMatrix44& WorldViewMatrix); void SetProjectionMatrix(const lcMatrix44& ProjectionMatrix); // void SetColor(const lcVector4& Color); @@ -37,6 +48,11 @@ protected: float mLineWidth; int mMatrixMode; + int mViewportX; + int mViewportY; + int mViewportWidth; + int mViewportHeight; + GLuint mFramebufferObject; GLuint mFramebufferTexture; GLuint mDepthRenderbufferObject; diff --git a/common/lc_model.cpp b/common/lc_model.cpp index 1b6b3d0f..fd40a0d8 100644 --- a/common/lc_model.cpp +++ b/common/lc_model.cpp @@ -429,6 +429,91 @@ void lcModel::LoadLDraw(QTextStream& Stream) delete Light; } +void lcModel::DrawBackground(lcContext* Context) +{ + if (mProperties.mBackgroundType == LC_BACKGROUND_SOLID) + { + glClearColor(mProperties.mBackgroundSolidColor[0], mProperties.mBackgroundSolidColor[1], mProperties.mBackgroundSolidColor[2], 0.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + return; + } + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glDepthMask(GL_FALSE); + glDisable(GL_DEPTH_TEST); + glDisable(GL_LIGHTING); + + float ViewWidth = (float)Context->GetViewportWidth(); + float ViewHeight = (float)Context->GetViewportHeight(); + + Context->SetProjectionMatrix(lcMatrix44Ortho(0.0f, ViewWidth, 0.0f, ViewHeight, -1.0f, 1.0f)); + Context->SetWorldViewMatrix(lcMatrix44Translation(lcVector3(0.375f, 0.375f, 0.0f))); + + if (mProperties.mBackgroundType == LC_BACKGROUND_GRADIENT) + { + glShadeModel(GL_SMOOTH); + + const lcVector3& Color1 = mProperties.mBackgroundGradientColor1; + const lcVector3& Color2 = mProperties.mBackgroundGradientColor2; + + float Verts[] = + { + ViewWidth, ViewHeight, Color1[0], Color1[1], Color1[2], 1.0f, + 0.0f, ViewHeight, Color1[0], Color1[1], Color1[2], 1.0f, + 0.0f, 0.0f, Color2[0], Color2[1], Color2[2], 1.0f, + ViewWidth, 0.0f, Color2[0], Color2[1], Color2[2], 1.0f + }; + + glVertexPointer(2, GL_FLOAT, 6 * sizeof(float), Verts); + glEnableClientState(GL_COLOR_ARRAY); + glColorPointer(4, GL_FLOAT, 6 * sizeof(float), Verts + 2); + + glDrawArrays(GL_TRIANGLE_FAN, 0, 4); + + glDisableClientState(GL_COLOR_ARRAY); + + glShadeModel(GL_FLAT); + } + + if (mProperties.mBackgroundType == LC_BACKGROUND_IMAGE) + { + glEnable(GL_TEXTURE_2D); + + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); + glBindTexture(GL_TEXTURE_2D, mBackgroundTexture->mTexture); + + float TileWidth = 1.0f, TileHeight = 1.0f; + + if (mProperties.mBackgroundImageTile) + { + TileWidth = ViewWidth / mBackgroundTexture->mWidth; + TileHeight = ViewHeight / mBackgroundTexture->mHeight; + } + + float Verts[] = + { + 0.0f, ViewHeight, 0.0f, 0.0f, + ViewWidth, ViewHeight, TileWidth, 0.0f, + ViewWidth, 0.0f, TileWidth, TileHeight, + 0.0f, 0.0f, 0.0f, TileHeight + }; + + glVertexPointer(2, GL_FLOAT, 4 * sizeof(float), Verts); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glTexCoordPointer(2, GL_FLOAT, 4 * sizeof(float), Verts + 2); + + glDrawArrays(GL_TRIANGLE_FAN, 0, 4); + + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + + glDisable(GL_TEXTURE_2D); + } + + glEnable(GL_DEPTH_TEST); + glDepthMask(GL_TRUE); +} + void lcModel::UpdateBackgroundTexture() { lcReleaseTexture(mBackgroundTexture); diff --git a/common/lc_model.h b/common/lc_model.h index 036b8479..42ffc203 100644 --- a/common/lc_model.h +++ b/common/lc_model.h @@ -150,6 +150,8 @@ public: void SaveLDraw(QTextStream& Stream) const; void LoadLDraw(QTextStream& Stream); + void DrawBackground(lcContext* Context); + void RayTest(lcObjectRayTest& ObjectRayTest) const; void BoxTest(lcObjectBoxTest& ObjectBoxTest) const; diff --git a/common/minifig.cpp b/common/minifig.cpp index a35e1d8b..f3cf47f1 100644 --- a/common/minifig.cpp +++ b/common/minifig.cpp @@ -964,7 +964,9 @@ void MinifigWizard::ParseSettings(lcFile& Settings) void MinifigWizard::OnDraw() { float Aspect = (float)mWidth/(float)mHeight; - glViewport(0, 0, mWidth, mHeight); + mContext->SetViewport(0, 0, mWidth, mHeight); + + lcGetActiveProject()->DrawBackground(mContext); float Box[6] = { 10000, 10000, 10000, -10000, -10000, -10000 }; @@ -1042,12 +1044,6 @@ void MinifigWizard::OnDraw() ViewMatrix = lcMatrix44LookAt(Eye * m_Distance, Center, lcVector3(0, 0, 1)); } - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - float *bg = lcGetActiveProject()->GetBackgroundColor(); - glClearColor(bg[0], bg[1], bg[2], bg[3]); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - Calculate(); lcArray OpaqueMeshes(LC_MFW_NUMITEMS); diff --git a/common/preview.cpp b/common/preview.cpp index 63f0dabd..66034b8b 100644 --- a/common/preview.cpp +++ b/common/preview.cpp @@ -32,9 +32,11 @@ void PiecePreview::OnDraw() glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); float aspect = (float)mWidth/(float)mHeight; - glViewport(0, 0, mWidth, mHeight); + mContext->SetViewport(0, 0, mWidth, mHeight); glEnableClientState(GL_VERTEX_ARRAY); + lcGetActiveProject()->DrawBackground(mContext); + lcVector3 Eye(0.0f, 0.0f, 1.0f); Eye = lcMul30(Eye, lcMatrix44RotationX(-m_RotateX * LC_DTOR)); @@ -57,9 +59,6 @@ void PiecePreview::OnDraw() glLoadMatrixf(lcMatrix44LookAt(Eye * m_Distance, m_PieceInfo->GetCenter(), lcVector3(0, 0, 1))); } - float *bg = lcGetActiveProject()->GetBackgroundColor(); - glClearColor(bg[0], bg[1], bg[2], bg[3]); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); m_PieceInfo->RenderPiece(gMainWindow->mColorIndex); glDisableClientState(GL_VERTEX_ARRAY); diff --git a/common/project.cpp b/common/project.cpp index 9c6828e2..6ce0c0af 100644 --- a/common/project.cpp +++ b/common/project.cpp @@ -722,6 +722,7 @@ bool Project::SaveIfModified() switch (QMessageBox::question(gMainWindow->mHandle, tr("Save Project"), tr("Save changes to '%1'?").arg(GetTitle()), QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel)) { + default: case QMessageBox::Cancel: return false; @@ -878,10 +879,12 @@ void Project::CheckPoint(const char* Description) void Project::Render(View* View, bool ToMemory) { - View->mContext->SetDefaultState(); - glViewport(0, 0, View->mWidth, View->mHeight); + lcContext* Context = View->mContext; - RenderBackground(View); + Context->SetDefaultState(); + Context->SetViewport(0, 0, View->mWidth, View->mHeight); + + DrawBackground(Context); RenderScenePieces(View, !ToMemory); @@ -891,93 +894,6 @@ void Project::Render(View* View, bool ToMemory) } } -void Project::RenderBackground(View* View) -{ - lcContext* Context = View->mContext; - - if (mProperties.mBackgroundType == LC_BACKGROUND_SOLID) - { - glClearColor(mProperties.mBackgroundSolidColor[0], mProperties.mBackgroundSolidColor[1], mProperties.mBackgroundSolidColor[2], 0.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - return; - } - - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - glDepthMask(GL_FALSE); - glDisable(GL_DEPTH_TEST); - glDisable(GL_LIGHTING); - - float ViewWidth = (float)View->mWidth; - float ViewHeight = (float)View->mHeight; - - Context->SetProjectionMatrix(lcMatrix44Ortho(0.0f, ViewWidth, 0.0f, ViewHeight, -1.0f, 1.0f)); - Context->SetWorldViewMatrix(lcMatrix44Translation(lcVector3(0.375f, 0.375f, 0.0f))); - - if (mProperties.mBackgroundType == LC_BACKGROUND_GRADIENT) - { - glShadeModel(GL_SMOOTH); - - const lcVector3& Color1 = mProperties.mBackgroundGradientColor1; - const lcVector3& Color2 = mProperties.mBackgroundGradientColor2; - - float Verts[] = - { - ViewWidth, ViewHeight, Color1[0], Color1[1], Color1[2], 1.0f, - 0.0f, ViewHeight, Color1[0], Color1[1], Color1[2], 1.0f, - 0.0f, 0.0f, Color2[0], Color2[1], Color2[2], 1.0f, - ViewWidth, 0.0f, Color2[0], Color2[1], Color2[2], 1.0f - }; - - glVertexPointer(2, GL_FLOAT, 6 * sizeof(float), Verts); - glEnableClientState(GL_COLOR_ARRAY); - glColorPointer(4, GL_FLOAT, 6 * sizeof(float), Verts + 2); - - glDrawArrays(GL_TRIANGLE_FAN, 0, 4); - - glDisableClientState(GL_COLOR_ARRAY); - - glShadeModel(GL_FLAT); - } - - if (mProperties.mBackgroundType == LC_BACKGROUND_IMAGE) - { - glEnable(GL_TEXTURE_2D); - - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); - glBindTexture(GL_TEXTURE_2D, mBackgroundTexture->mTexture); - - float TileWidth = 1.0f, TileHeight = 1.0f; - - if (mProperties.mBackgroundImageTile) - { - TileWidth = ViewWidth / mBackgroundTexture->mWidth; - TileHeight = ViewHeight / mBackgroundTexture->mHeight; - } - - float Verts[] = - { - 0.0f, ViewHeight, 0.0f, 0.0f, - ViewWidth, ViewHeight, TileWidth, 0.0f, - ViewWidth, 0.0f, TileWidth, TileHeight, - 0.0f, 0.0f, 0.0f, TileHeight - }; - - glVertexPointer(2, GL_FLOAT, 4 * sizeof(float), Verts); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glTexCoordPointer(2, GL_FLOAT, 4 * sizeof(float), Verts + 2); - - glDrawArrays(GL_TRIANGLE_FAN, 0, 4); - - glDisableClientState(GL_TEXTURE_COORD_ARRAY); - - glDisable(GL_TEXTURE_2D); - } - - glEnable(GL_DEPTH_TEST); - glDepthMask(GL_TRUE); -} - void Project::RenderScenePieces(View* view, bool DrawInterface) { const lcPreferences& Preferences = lcGetPreferences(); @@ -1585,7 +1501,7 @@ void Project::ExportHTML() } float aspect = (float)Width/(float)Height; - glViewport(0, 0, Width, Height); + Context->SetViewport(0, 0, Width, Height); for (int PieceIdx = 0; PieceIdx < mPieces.GetSize(); PieceIdx++) { diff --git a/common/project.h b/common/project.h index 982e84d7..1951a440 100644 --- a/common/project.h +++ b/common/project.h @@ -73,19 +73,12 @@ public: CalculateStep(); } - float* GetBackgroundColor() // todo: remove - { - return mProperties.mBackgroundSolidColor; - } - int GetGroupIndex(lcGroup* Group) const { return mGroups.FindIndex(Group); } void UpdateInterface(); - -public: void LoadDefaults(); void SaveImage(); void SaveStepImages(const QString& BaseName, int Width, int Height, lcStep Start, lcStep End); @@ -104,7 +97,6 @@ protected: static int InstanceOfName(const String& existingString, const String& candidateString, String& baseNameOut); - void RenderBackground(View* view); void RenderScenePieces(View* view, bool DrawInterface); void RenderSceneObjects(View* view);