From e7b8fbf43043c5c046e8fdaa1e1cc0acaa165a6d Mon Sep 17 00:00:00 2001 From: Paolo Cignoni cignoni Date: Wed, 8 Mar 2006 17:26:13 +0000 Subject: [PATCH] added texture tab --- src/meshlabplugins/meshrender/meshrender.cpp | 42 ++--- .../meshrender/shaderDialog.cpp | 175 +++++++++++++----- src/meshlabplugins/meshrender/shaderDialog.h | 8 +- src/meshlabplugins/meshrender/shaderDialog.ui | 147 ++++++++------- 4 files changed, 233 insertions(+), 139 deletions(-) diff --git a/src/meshlabplugins/meshrender/meshrender.cpp b/src/meshlabplugins/meshrender/meshrender.cpp index 0af9593e0..fd3cf7784 100644 --- a/src/meshlabplugins/meshrender/meshrender.cpp +++ b/src/meshlabplugins/meshrender/meshrender.cpp @@ -23,6 +23,9 @@ /**************************************************************************** History $Log$ +Revision 1.16 2006/03/08 17:26:13 ggangemi +added texture tab + Revision 1.15 2006/02/27 05:02:01 ggangemi Added texture support @@ -112,7 +115,7 @@ void MeshShaderRenderPlugin::initActionList() { if (!child.isNull()) { //first child of "Filenames" is "Filename0" child = child.firstChild(); - si.vpFile = (child.toElement()).attribute("VertexProgram", ""); + si.vpFile = shadersDir.absoluteFilePath((child.toElement()).attribute("VertexProgram", "")); } } @@ -124,7 +127,7 @@ void MeshShaderRenderPlugin::initActionList() { if (!child.isNull()) { //first child of "Filenames" is "Filename0" child = child.firstChild(); - si.fpFile = (child.toElement()).attribute("FragmentProgram", ""); + si.fpFile = shadersDir.absoluteFilePath((child.toElement()).attribute("FragmentProgram", "")); } } @@ -215,6 +218,9 @@ void MeshShaderRenderPlugin::initActionList() { //Textures + + shadersDir.cdUp(); + shadersDir.cd("textures"); elem = root.firstChildElement("TexturedUsed"); if (!elem.isNull()) { QDomNode unif = elem.firstChild(); @@ -222,7 +228,7 @@ void MeshShaderRenderPlugin::initActionList() { QDomElement unifElem = unif.toElement(); TextureInfo tInfo; - tInfo.path = (unifElem.attribute("Filename", "")); + tInfo.path = shadersDir.absoluteFilePath((unifElem.attribute("Filename", ""))); tInfo.MinFilter = (unifElem.attribute("MinFilter", 0)).toInt(); tInfo.MagFilter = (unifElem.attribute("MagFilter", 0)).toInt(); tInfo.Target = (unifElem.attribute("Target", 0)).toInt(); @@ -234,6 +240,10 @@ void MeshShaderRenderPlugin::initActionList() { unif = unif.nextSibling(); } } + shadersDir.cdUp(); + shadersDir.cd("shaders"); + + //End Textures shaders[fileName] = si; @@ -262,22 +272,8 @@ void MeshShaderRenderPlugin::Init(QAction *a, MeshModel &m, RenderMode &rm, GLAr v = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); f = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); - - QDir shadersDir = QDir(qApp->applicationDirPath()); -#if defined(Q_OS_WIN) - if (shadersDir.dirName() == "debug" || shadersDir.dirName() == "release") - shadersDir.cdUp(); -#elif defined(Q_OS_MAC) - if (shadersDir.dirName() == "MacOS") { - shadersDir.cdUp(); - shadersDir.cdUp(); - shadersDir.cdUp(); - } -#endif - shadersDir.cd("shaders"); - - char *fs = textFileRead(shadersDir.absoluteFilePath(shaders[a->text()].fpFile).toLocal8Bit().data()); - char *vs = textFileRead(shadersDir.absoluteFilePath(shaders[a->text()].vpFile).toLocal8Bit().data()); + char *fs = textFileRead((shaders[a->text()].fpFile).toLocal8Bit().data()); + char *vs = textFileRead((shaders[a->text()].vpFile).toLocal8Bit().data()); const char * vv = vs; const char * ff = fs; @@ -320,14 +316,13 @@ void MeshShaderRenderPlugin::Init(QAction *a, MeshModel &m, RenderMode &rm, GLAr } //Textures - shadersDir.cdUp(); - shadersDir.cd("textures"); + std::vector::iterator tIter = shaders[a->text()].textureInfo.begin(); while (tIter != shaders[a->text()].textureInfo.end()) { glEnable(tIter->Target); QImage img, imgScaled, imgGL; - img.load(shadersDir.absoluteFilePath(tIter->path)); + img.load(tIter->path); // image has to be scaled to a 2^n size. We choose the first 2^N <= picture size. int bestW=pow(2.0,floor(::log(double(img.width() ))/::log(2.0))); int bestH=pow(2.0,floor(::log(double(img.height()))/::log(2.0))); @@ -424,11 +419,12 @@ void MeshShaderRenderPlugin::Render(QAction *a, MeshModel &m, RenderMode &rm, GL std::vector::iterator tIter = shaders[a->text()].textureInfo.begin(); while (tIter != shaders[a->text()].textureInfo.end()) { glActiveTexture(n); + glEnable(tIter->Target); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glBindTexture( tIter->Target, tIter->tId ); rm.textureMode = GLW::TMPerVert; - glEnable(tIter->Target); + ++tIter; ++n; } diff --git a/src/meshlabplugins/meshrender/shaderDialog.cpp b/src/meshlabplugins/meshrender/shaderDialog.cpp index 0ea5a4261..221a99a0c 100644 --- a/src/meshlabplugins/meshrender/shaderDialog.cpp +++ b/src/meshlabplugins/meshrender/shaderDialog.cpp @@ -108,8 +108,47 @@ ShaderDialog::ShaderDialog(ShaderInfo *sInfo, GLArea* gla, RenderMode &rm, QWidg connect(colorSignalMapper, SIGNAL(mapped(const QString &)), this, SLOT(setColorValue(const QString &))); connect(valueSignalMapper, SIGNAL(mapped(const QString &)), this, SLOT(valuesChanged(const QString &))); + //Texture Tab Section + + if (shaderInfo->textureInfo.size() > 0) { + textLineSignalMapper = new QSignalMapper(this); + textButtonSignalMapper = new QSignalMapper(this); + + + QGridLayout * qgridTexTab = new QGridLayout(ui.textureTab); + qgridTexTab->setColumnMinimumWidth(0, 45); + qgridTexTab->setColumnMinimumWidth(1, 40); + + row = 0; + std::vector::iterator textIter; + for (textIter = shaderInfo->textureInfo.begin(); textIter != shaderInfo->textureInfo.end(); ++textIter) { + QLabel *textNameLabel = new QLabel(this); + QLineEdit *textValueEdit = new QLineEdit(this); + QPushButton * textButton = new QPushButton(this); + textButton->setText("Browse"); + textNameLabel->setText(tr("Texture Unit %1:").arg(row)); + textValueEdit->setText(textIter->path); + qgridTexTab->addWidget(textNameLabel, row, 0); + qgridTexTab->addWidget(textValueEdit, row, 1); + qgridTexTab->addWidget(textButton, row, 2); + + + connect(textValueEdit, SIGNAL(editingFinished()), textLineSignalMapper, SLOT(map())); + textLineSignalMapper->setMapping(textValueEdit,row); + + connect(textButton, SIGNAL(clicked()), textButtonSignalMapper, SLOT(map())); + textButtonSignalMapper->setMapping(textButton,row); + + textLineEdits.push_back(textValueEdit); + ++row; + } + + connect(textLineSignalMapper, SIGNAL(mapped(int)), this, SLOT(changeTexturePath(int))); + connect(textButtonSignalMapper, SIGNAL(mapped(int)), this, SLOT(browseTexturePath(int))); + } + //OpenGL Status Tab Section - + QGridLayout * qgridGlStatus = new QGridLayout(ui.glTab); qgridGlStatus->setColumnMinimumWidth(0, 45); qgridGlStatus->setColumnMinimumWidth(1, 40); @@ -119,64 +158,53 @@ ShaderDialog::ShaderDialog(ShaderInfo *sInfo, GLArea* gla, RenderMode &rm, QWidg for (glIterator = shaderInfo->glStatus.begin(); glIterator != shaderInfo->glStatus.end(); ++glIterator) { QLabel *glVarLabel = new QLabel(this); QLabel *glValueLabel = new QLabel(this); - - + + switch (glIterator->first) { case SHADE: glVarLabel->setText("glShadeModel"); glValueLabel->setText(glIterator->second); ++row; break; case ALPHA_TEST: glVarLabel->setText("GL_ALPHA_TEST"); glValueLabel->setText(glIterator->second); ++row; break; case ALPHA_FUNC: glVarLabel->setText("glAlphaFunc"); glValueLabel->setText(glIterator->second + ", " + shaderInfo->glStatus[ALPHA_CLAMP]); ++row; break; - //case ALPHA_CLAMP: used in ALPHA_FUNC + //case ALPHA_CLAMP: used in ALPHA_FUNC case BLENDING: glVarLabel->setText("GL_BLEND"); glValueLabel->setText(glIterator->second); ++row; break; case BLEND_FUNC_SRC: glVarLabel->setText("glBlendFunc"); glValueLabel->setText(glIterator->second + ", " + shaderInfo->glStatus[BLEND_FUNC_SRC]); ++row; break; - //case BLEND_FUNC_DST: used in BLEND_FUNC_SRC + //case BLEND_FUNC_DST: used in BLEND_FUNC_SRC case BLEND_EQUATION: glVarLabel->setText("glBlendEquation"); glValueLabel->setText(glIterator->second); ++row; break; case DEPTH_TEST: glVarLabel->setText("GL_DEPTH_TEST"); glValueLabel->setText(glIterator->second); ++row; break; case DEPTH_FUNC: glVarLabel->setText("glDepthFunc"); glValueLabel->setText(glIterator->second); ++row; break; - //case CLAMP_NEAR: - //case CLAMP_FAR: + //case CLAMP_NEAR: + //case CLAMP_FAR: case CLEAR_COLOR_R: glVarLabel->setText("glClearColor"); glValueLabel->setText(glIterator->second + ", " + - shaderInfo->glStatus[CLEAR_COLOR_G] + ", " + - shaderInfo->glStatus[CLEAR_COLOR_B] + ", " + - shaderInfo->glStatus[CLEAR_COLOR_A]); ++row; break; - //case CLEAR_COLOR_G: used in CLEAR_COLOR_R - //case CLEAR_COLOR_B: used in CLEAR_COLOR_R - //case CLEAR_COLOR_A: used in CLEAR_COLOR_R + shaderInfo->glStatus[CLEAR_COLOR_G] + ", " + + shaderInfo->glStatus[CLEAR_COLOR_B] + ", " + + shaderInfo->glStatus[CLEAR_COLOR_A]); ++row; break; + //case CLEAR_COLOR_G: used in CLEAR_COLOR_R + //case CLEAR_COLOR_B: used in CLEAR_COLOR_R + //case CLEAR_COLOR_A: used in CLEAR_COLOR_R } qgridGlStatus->addWidget(glVarLabel, row, 0); qgridGlStatus->addWidget(glValueLabel, row, 1); - - } - - //Vertex and Fragment Program Tabs Section - QDir shadersDir = QDir(qApp->applicationDirPath()); -#if defined(Q_OS_WIN) - if (shadersDir.dirName() == "debug" || shadersDir.dirName() == "release") - shadersDir.cdUp(); -#elif defined(Q_OS_MAC) - if (shadersDir.dirName() == "MacOS") { - shadersDir.cdUp(); - shadersDir.cdUp(); - shadersDir.cdUp(); - } -#endif - shadersDir.cd("shaders"); - QFile qf; - QTextStream ts(&qf); - - qf.setFileName(shadersDir.path()+QString("/")+shaderInfo->vpFile); - if (!qf.open(QIODevice::ReadOnly | QIODevice::Text) ) - QMessageBox::critical(this,"Opengl Shader" ,"unable to open file"); - ui.vpTextBrowser->insertPlainText(ts.readAll()); - qf.close(); - - qf.setFileName(shadersDir.path()+QString("/")+shaderInfo->fpFile); - if (!qf.open(QIODevice::ReadOnly | QIODevice::Text) ) - QMessageBox::critical(this,"Opengl Shader" ,"unable to open file"); - ui.fpTextBrowser->insertPlainText(ts.readAll()); - qf.close(); - //End of Vertex and Fragment Program Tabs Section + } + + //Vertex and Fragment Program Tabs Section + + QFile qf; + QTextStream ts(&qf); + + qf.setFileName(shaderInfo->vpFile); + if (!qf.open(QIODevice::ReadOnly | QIODevice::Text) ) + QMessageBox::critical(this,"Opengl Shader" ,"unable to open file"); + ui.vpTextBrowser->insertPlainText(ts.readAll()); + qf.close(); + + qf.setFileName(shaderInfo->fpFile); + if (!qf.open(QIODevice::ReadOnly | QIODevice::Text) ) + QMessageBox::critical(this,"Opengl Shader" ,"unable to open file"); + ui.fpTextBrowser->insertPlainText(ts.readAll()); + qf.close(); + + //End of Vertex and Fragment Program Tabs Section this->setWindowFlags(Qt::WindowStaysOnTopHint); @@ -256,5 +284,64 @@ void ShaderDialog::setColorMode(int state) { } else { rendMode->colorMode = GLW::CMNone; } + glarea->updateGL(); +} + +void ShaderDialog::changeTexturePath(int i) { + shaderInfo->textureInfo[i].path = textLineEdits[i]->text(); + reloadTexture(i); +} + +void ShaderDialog::browseTexturePath(int i) { + QFileDialog fd(0,"Choose new texture"); + + QDir shadersDir = QDir(qApp->applicationDirPath()); +#if defined(Q_OS_WIN) + if (shadersDir.dirName() == "debug" || shadersDir.dirName() == "release") + shadersDir.cdUp(); +#elif defined(Q_OS_MAC) + if (shadersDir.dirName() == "MacOS") { + shadersDir.cdUp(); + shadersDir.cdUp(); + shadersDir.cdUp(); + } +#endif + shadersDir.cd("textures"); + + fd.setDirectory(shadersDir); + fd.move(500, 100); + + QStringList newPath; + if (fd.exec()) + { + newPath = fd.selectedFiles(); + textLineEdits[i]->setText(newPath.at(0)); + shaderInfo->textureInfo[i].path = newPath.at(0); + reloadTexture(i); + } + +} + +void ShaderDialog::reloadTexture(int i) { + glDeleteTextures( 1, &shaderInfo->textureInfo[i].tId); + + glEnable(shaderInfo->textureInfo[i].Target); + QImage img, imgScaled, imgGL; + img.load(shaderInfo->textureInfo[i].path); + // image has to be scaled to a 2^n size. We choose the first 2^N <= picture size. + int bestW=pow(2.0,floor(::log(double(img.width() ))/::log(2.0))); + int bestH=pow(2.0,floor(::log(double(img.height()))/::log(2.0))); + imgScaled=img.scaled(bestW,bestH,Qt::IgnoreAspectRatio,Qt::SmoothTransformation); + imgGL=QGLWidget::convertToGLFormat(imgScaled); + + glGenTextures( 1, &(shaderInfo->textureInfo[i].tId) ); + glBindTexture( shaderInfo->textureInfo[i].Target, shaderInfo->textureInfo[i].tId ); + glTexImage2D( shaderInfo->textureInfo[i].Target, 0, 3, imgGL.width(), imgGL.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, imgGL.bits() ); + glTexParameteri( shaderInfo->textureInfo[i].Target, GL_TEXTURE_MIN_FILTER, shaderInfo->textureInfo[i].MinFilter ); + glTexParameteri( shaderInfo->textureInfo[i].Target, GL_TEXTURE_MAG_FILTER, shaderInfo->textureInfo[i].MagFilter ); + glTexParameteri( shaderInfo->textureInfo[i].Target, GL_TEXTURE_WRAP_S, shaderInfo->textureInfo[i].WrapS ); + glTexParameteri( shaderInfo->textureInfo[i].Target, GL_TEXTURE_WRAP_T, shaderInfo->textureInfo[i].WrapT ); + glTexParameteri( shaderInfo->textureInfo[i].Target, GL_TEXTURE_WRAP_R, shaderInfo->textureInfo[i].WrapR ); + glarea->updateGL(); } \ No newline at end of file diff --git a/src/meshlabplugins/meshrender/shaderDialog.h b/src/meshlabplugins/meshrender/shaderDialog.h index 97cd34a98..5b9a55016 100644 --- a/src/meshlabplugins/meshrender/shaderDialog.h +++ b/src/meshlabplugins/meshrender/shaderDialog.h @@ -29,8 +29,11 @@ private: ShaderInfo * shaderInfo; QSignalMapper *colorSignalMapper; QSignalMapper *valueSignalMapper; - std::map labels; + QSignalMapper *textLineSignalMapper; + QSignalMapper *textButtonSignalMapper; + std::map labels; std::map sliders; + std::vector textLineEdits; std::map lineEdits; Ui::ShaderDialogClass ui; @@ -38,6 +41,9 @@ private slots: void valuesChanged(const QString &); void setColorValue(const QString &); void setColorMode(int); + void changeTexturePath(int); + void browseTexturePath(int); + void reloadTexture(int i); }; diff --git a/src/meshlabplugins/meshrender/shaderDialog.ui b/src/meshlabplugins/meshrender/shaderDialog.ui index f658f39ff..edf19a3cb 100644 --- a/src/meshlabplugins/meshrender/shaderDialog.ui +++ b/src/meshlabplugins/meshrender/shaderDialog.ui @@ -41,77 +41,6 @@ ..\..\..\..\..\meshlab\src\meshlab\images\eye16.png - - - - 10 - 10 - 421 - 301 - - - - 0 - - - - Uniform Variables - - - - - 10 - 20 - 171 - 20 - - - - - 0 - - - 6 - - - - - - - OpenGL Status - - - - - Vertex Program - - - - - 0 - 0 - 421 - 281 - - - - - - - Fragment Program - - - - - 0 - 0 - 421 - 281 - - - - - @@ -150,6 +79,82 @@ + + + + 10 + 10 + 421 + 301 + + + + 0 + + + + Uniform Variables + + + + + 10 + 20 + 171 + 20 + + + + + 0 + + + 6 + + + + + + + Textures + + + + + OpenGL Status + + + + + Vertex Program + + + + + 0 + 0 + 421 + 281 + + + + + + + Fragment Program + + + + + 0 + 0 + 421 + 281 + + + + +