Support gradient and texture backgrounds in the preview widget.

This commit is contained in:
leo 2014-10-23 22:03:50 +00:00
parent 55e7cad5e1
commit 5834587204
9 changed files with 132 additions and 113 deletions

View file

@ -171,7 +171,7 @@ bool lcApplication::Initialize(int argc, char* argv[], const char* LibraryInstal
// Image output options. // Image output options.
bool SaveImage = false; bool SaveImage = false;
bool ImageHighlight = false; // bool ImageHighlight = false;
int ImageWidth = lcGetProfileInt(LC_PROFILE_IMAGE_WIDTH); int ImageWidth = lcGetProfileInt(LC_PROFILE_IMAGE_WIDTH);
int ImageHeight = lcGetProfileInt(LC_PROFILE_IMAGE_HEIGHT); int ImageHeight = lcGetProfileInt(LC_PROFILE_IMAGE_HEIGHT);
lcStep ImageStart = 0; lcStep ImageStart = 0;
@ -222,8 +222,8 @@ bool lcApplication::Initialize(int argc, char* argv[], const char* LibraryInstal
ParseIntegerArgument(&i, argc, argv, &Step); ParseIntegerArgument(&i, argc, argv, &Step);
ImageEnd = Step; ImageEnd = Step;
} }
else if (strcmp(Param, "--highlight") == 0) // else if (strcmp(Param, "--highlight") == 0)
ImageHighlight = true; // ImageHighlight = true;
else if ((strcmp(Param, "-v") == 0) || (strcmp(Param, "--version") == 0)) else if ((strcmp(Param, "-v") == 0) || (strcmp(Param, "--version") == 0))
{ {
printf("LeoCAD Version " LC_VERSION_TEXT "\n"); printf("LeoCAD Version " LC_VERSION_TEXT "\n");

View file

@ -62,6 +62,19 @@ void lcContext::SetDefaultState()
mMatrixMode = GL_MODELVIEW; 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) void lcContext::SetWorldViewMatrix(const lcMatrix44& WorldViewMatrix)
{ {
if (mMatrixMode != GL_MODELVIEW) if (mMatrixMode != GL_MODELVIEW)

View file

@ -9,8 +9,19 @@ public:
lcContext(); lcContext();
~lcContext(); ~lcContext();
int GetViewportWidth() const
{
return mViewportWidth;
}
int GetViewportHeight() const
{
return mViewportHeight;
}
void SetDefaultState(); void SetDefaultState();
void SetViewport(int x, int y, int Width, int Height);
void SetWorldViewMatrix(const lcMatrix44& WorldViewMatrix); void SetWorldViewMatrix(const lcMatrix44& WorldViewMatrix);
void SetProjectionMatrix(const lcMatrix44& ProjectionMatrix); void SetProjectionMatrix(const lcMatrix44& ProjectionMatrix);
// void SetColor(const lcVector4& Color); // void SetColor(const lcVector4& Color);
@ -37,6 +48,11 @@ protected:
float mLineWidth; float mLineWidth;
int mMatrixMode; int mMatrixMode;
int mViewportX;
int mViewportY;
int mViewportWidth;
int mViewportHeight;
GLuint mFramebufferObject; GLuint mFramebufferObject;
GLuint mFramebufferTexture; GLuint mFramebufferTexture;
GLuint mDepthRenderbufferObject; GLuint mDepthRenderbufferObject;

View file

@ -429,6 +429,91 @@ void lcModel::LoadLDraw(QTextStream& Stream)
delete Light; 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() void lcModel::UpdateBackgroundTexture()
{ {
lcReleaseTexture(mBackgroundTexture); lcReleaseTexture(mBackgroundTexture);

View file

@ -150,6 +150,8 @@ public:
void SaveLDraw(QTextStream& Stream) const; void SaveLDraw(QTextStream& Stream) const;
void LoadLDraw(QTextStream& Stream); void LoadLDraw(QTextStream& Stream);
void DrawBackground(lcContext* Context);
void RayTest(lcObjectRayTest& ObjectRayTest) const; void RayTest(lcObjectRayTest& ObjectRayTest) const;
void BoxTest(lcObjectBoxTest& ObjectBoxTest) const; void BoxTest(lcObjectBoxTest& ObjectBoxTest) const;

View file

@ -964,7 +964,9 @@ void MinifigWizard::ParseSettings(lcFile& Settings)
void MinifigWizard::OnDraw() void MinifigWizard::OnDraw()
{ {
float Aspect = (float)mWidth/(float)mHeight; 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 }; 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)); 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(); Calculate();
lcArray<lcRenderMesh> OpaqueMeshes(LC_MFW_NUMITEMS); lcArray<lcRenderMesh> OpaqueMeshes(LC_MFW_NUMITEMS);

View file

@ -32,9 +32,11 @@ void PiecePreview::OnDraw()
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
float aspect = (float)mWidth/(float)mHeight; float aspect = (float)mWidth/(float)mHeight;
glViewport(0, 0, mWidth, mHeight); mContext->SetViewport(0, 0, mWidth, mHeight);
glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_VERTEX_ARRAY);
lcGetActiveProject()->DrawBackground(mContext);
lcVector3 Eye(0.0f, 0.0f, 1.0f); lcVector3 Eye(0.0f, 0.0f, 1.0f);
Eye = lcMul30(Eye, lcMatrix44RotationX(-m_RotateX * LC_DTOR)); 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))); 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); m_PieceInfo->RenderPiece(gMainWindow->mColorIndex);
glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_VERTEX_ARRAY);

View file

@ -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)) 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: case QMessageBox::Cancel:
return false; return false;
@ -878,10 +879,12 @@ void Project::CheckPoint(const char* Description)
void Project::Render(View* View, bool ToMemory) void Project::Render(View* View, bool ToMemory)
{ {
View->mContext->SetDefaultState(); lcContext* Context = View->mContext;
glViewport(0, 0, View->mWidth, View->mHeight);
RenderBackground(View); Context->SetDefaultState();
Context->SetViewport(0, 0, View->mWidth, View->mHeight);
DrawBackground(Context);
RenderScenePieces(View, !ToMemory); 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) void Project::RenderScenePieces(View* view, bool DrawInterface)
{ {
const lcPreferences& Preferences = lcGetPreferences(); const lcPreferences& Preferences = lcGetPreferences();
@ -1585,7 +1501,7 @@ void Project::ExportHTML()
} }
float aspect = (float)Width/(float)Height; 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++) for (int PieceIdx = 0; PieceIdx < mPieces.GetSize(); PieceIdx++)
{ {

View file

@ -73,19 +73,12 @@ public:
CalculateStep(); CalculateStep();
} }
float* GetBackgroundColor() // todo: remove
{
return mProperties.mBackgroundSolidColor;
}
int GetGroupIndex(lcGroup* Group) const int GetGroupIndex(lcGroup* Group) const
{ {
return mGroups.FindIndex(Group); return mGroups.FindIndex(Group);
} }
void UpdateInterface(); void UpdateInterface();
public:
void LoadDefaults(); void LoadDefaults();
void SaveImage(); void SaveImage();
void SaveStepImages(const QString& BaseName, int Width, int Height, lcStep Start, lcStep End); 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); static int InstanceOfName(const String& existingString, const String& candidateString, String& baseNameOut);
void RenderBackground(View* view);
void RenderScenePieces(View* view, bool DrawInterface); void RenderScenePieces(View* view, bool DrawInterface);
void RenderSceneObjects(View* view); void RenderSceneObjects(View* view);