From 4a69332c0bd43eec15cd0bdd6bc7f9262148e176 Mon Sep 17 00:00:00 2001 From: Paolo Cignoni cignoni Date: Thu, 7 Oct 2010 16:26:22 +0000 Subject: [PATCH] Cleaned up the creation/destruction of shaders: crashed after changing too many times the parameters for leaking. --- .../decorate_shadow/decorate_shader.h | 34 ++++++++++--------- .../decorate_shadow/decorate_shadow.cpp | 27 +++++---------- .../decorate_shadow/decorate_shadow.h | 9 ++++- .../decorate_shadow/shadow_mapping.h | 4 +++ src/meshlabplugins/decorate_shadow/ssao.cpp | 2 +- src/meshlabplugins/decorate_shadow/ssao.h | 9 +++++ .../variance_shadow_mapping_blur.cpp | 2 +- 7 files changed, 49 insertions(+), 38 deletions(-) diff --git a/src/meshlabplugins/decorate_shadow/decorate_shader.h b/src/meshlabplugins/decorate_shadow/decorate_shader.h index ed701abeb..abd0a7556 100644 --- a/src/meshlabplugins/decorate_shadow/decorate_shader.h +++ b/src/meshlabplugins/decorate_shadow/decorate_shader.h @@ -40,8 +40,9 @@ public: this->_initOk = false; //default texture size - this->_texSize = 1024; - } + this->_texW = 1024; + this->_texH = 1024; + } //virtual ~DecorateShader(); @@ -58,10 +59,11 @@ public: * @param gla GLArea reference. */ virtual void runShader(MeshDocument&, GLArea*) = 0; + virtual void setShadowIntensity(float f) =0; protected: bool _initOk; - int _texSize; + int _texW,_texH; /** The FrameBufferObject handler */ GLuint _fbo; @@ -82,7 +84,7 @@ protected: glClearDepth(1.0); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _fbo); glPushAttrib(GL_VIEWPORT_BIT); - glViewport(0, 0, this->_texSize, this->_texSize); + glViewport(0, 0, this->_texW, this->_texH); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); } @@ -143,15 +145,15 @@ protected: if (!this->_initOk) return; - QImage img(this->_texSize, this->_texSize, QImage::Format_RGB32); + QImage img(this->_texW, this->_texH, QImage::Format_RGB32); - float *tempFBuf = new float[this->_texSize * this->_texSize *1 ]; + float *tempFBuf = new float[this->_texW * this->_texH *1 ]; float *tempFBufPtr = tempFBuf; glBindTexture(GL_TEXTURE_2D, map); glGetTexImage(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, GL_FLOAT, tempFBufPtr); - for (int i = 0; i < this->_texSize; ++i) { + for (int i = 0; i < this->_texH; ++i) { QRgb *scanLine = (QRgb*)img.scanLine(i); - for (int j = 0; j < this->_texSize; ++j) { + for (int j = 0; j < this->_texW; ++j) { const unsigned char val = (unsigned char) (tempFBufPtr[0] * 255.0f); scanLine[j] = qRgb(val, val, val); tempFBufPtr ++; @@ -171,15 +173,15 @@ protected: if (!this->_initOk) return; - QImage img(this->_texSize, this->_texSize, QImage::Format_RGB32); + QImage img(this->_texW, this->_texH, QImage::Format_RGB32); - unsigned char *tempBuf = new unsigned char[this->_texSize * this->_texSize * 3]; + unsigned char *tempBuf = new unsigned char[this->_texW * this->_texH * 3]; unsigned char *tempBufPtr = tempBuf; glBindTexture(GL_TEXTURE_2D, map); glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, tempBufPtr); - for (int i = 0; i < this->_texSize; ++i) { + for (int i = 0; i < this->_texH; ++i) { QRgb *scanLine = (QRgb*)img.scanLine(i); - for (int j = 0; j < this->_texSize; ++j) { + for (int j = 0; j < this->_texW; ++j) { scanLine[j] = qRgb(tempBufPtr[0], tempBufPtr[1], tempBufPtr[2]); tempBufPtr += 3; } @@ -320,7 +322,7 @@ protected: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, this->_texSize, this->_texSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, this->_texW, this->_texH, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attachment, GL_TEXTURE_2D, tex, 0); } @@ -333,7 +335,7 @@ protected: void genDepthRenderBufferEXT(GLuint& tex){ glGenRenderbuffersEXT(1, &tex); glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, tex); - glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, this->_texSize, this->_texSize); + glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, this->_texW, this->_texH); glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, tex); } @@ -358,7 +360,7 @@ protected: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL); } - glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, this->_texSize, this->_texSize, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, this->_texW, this->_texH, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, tex, 0); return; } @@ -385,7 +387,7 @@ protected: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL); } - glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, this->_texSize, this->_texSize, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, this->_texW, this->_texH, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, tex, 0); return; } diff --git a/src/meshlabplugins/decorate_shadow/decorate_shadow.cpp b/src/meshlabplugins/decorate_shadow/decorate_shadow.cpp index 857d7c539..e57aaf254 100644 --- a/src/meshlabplugins/decorate_shadow/decorate_shadow.cpp +++ b/src/meshlabplugins/decorate_shadow/decorate_shadow.cpp @@ -78,36 +78,25 @@ void DecorateShadowPlugin::initGlobalParameterSet(QAction *action, RichParameter bool DecorateShadowPlugin::startDecorate(QAction* action, MeshDocument& /*m*/, RichParameterSet* parset, GLArea* /*gla*/){ bool result; + switch(ID(action)){ case DP_SHOW_SHADOW : if(!parset->hasParameter(DecorateShadowMethod())){ qDebug("Unable to find Shadow mapping method"); assert(0); } - switch (parset->getEnum(DecorateShadowMethod())){ - case SH_MAP: - this->_decoratorSH = new ShadowMapping(parset->getDynamicFloat(this->DecorateShadowIntensity())); - break; - - case SH_MAP_VSM: - this->_decoratorSH = new VarianceShadowMapping(parset->getDynamicFloat(this->DecorateShadowIntensity())); - break; - - case SH_MAP_VSM_BLUR: - this->_decoratorSH = new VarianceShadowMappingBlur(parset->getDynamicFloat(this->DecorateShadowIntensity())); - break; - - default: assert(0); + switch (parset->getEnum(DecorateShadowMethod())) + { + case SH_MAP: this->_decoratorSH = smShader; break; + case SH_MAP_VSM: this->_decoratorSH = vsmShader; break; + case SH_MAP_VSM_BLUR: this->_decoratorSH = vsmbShader; break; } + this->_decoratorSH->setShadowIntensity(parset->getDynamicFloat(this->DecorateShadowIntensity())); result = this->_decoratorSH->init(); return result; case DP_SHOW_SSAO: - if(!parset->hasParameter(DecorateShadowMethod())){ - qDebug("Unable to find uniform variable radius for SSAO shader"); - assert(0); - } - this->_decoratorSSAO = new SSAO(parset->getFloat(DecorateShadowSSAORadius())); + this->_decoratorSSAO->setRadius(parset->getFloat(DecorateShadowSSAORadius())); result = this->_decoratorSSAO->init(); return result; diff --git a/src/meshlabplugins/decorate_shadow/decorate_shadow.h b/src/meshlabplugins/decorate_shadow/decorate_shadow.h index e7c7d0f35..136e28368 100644 --- a/src/meshlabplugins/decorate_shadow/decorate_shadow.h +++ b/src/meshlabplugins/decorate_shadow/decorate_shadow.h @@ -74,14 +74,21 @@ public: foreach(ap,actionList){ ap->setCheckable(true); } + smShader= new ShadowMapping(0.1f); + vsmShader= new VarianceShadowMapping(0.1f); + vsmbShader= new VarianceShadowMappingBlur(0.1f); + _decoratorSSAO = new SSAO(0.1); } + QList actions () const {return actionList;} virtual bool startDecorate(QAction * /*mode*/, MeshDocument &/*m*/, RichParameterSet * /*parent*/ par, GLArea * /*parent*/); virtual void decorate(QAction *a, MeshDocument &m, RichParameterSet *, GLArea *gla, QPainter *p); virtual void initGlobalParameterSet(QAction *, RichParameterSet & globalparam); private: - DecorateShader* _decoratorSH, *_decoratorSSAO; + DecorateShader* smShader, *vsmShader, *vsmbShader; + DecorateShader* _decoratorSH; + SSAO *_decoratorSSAO; inline const QString DecorateShadowSSAORadius() { return "MeshLab::Decoration::SSAORadius" ; } inline const QString DecorateShadowMethod() { return "MeshLab::Decoration::ShadowMethod" ; } inline const QString DecorateShadowIntensity() { return "MeshLab::Decoration::ShadowIntensityVal" ; } diff --git a/src/meshlabplugins/decorate_shadow/shadow_mapping.h b/src/meshlabplugins/decorate_shadow/shadow_mapping.h index bbbf708fc..017b9254a 100644 --- a/src/meshlabplugins/decorate_shadow/shadow_mapping.h +++ b/src/meshlabplugins/decorate_shadow/shadow_mapping.h @@ -47,6 +47,10 @@ public: * @param gla GLArea reference. */ void runShader(MeshDocument&, GLArea*); + virtual void setShadowIntensity(float f) + { + _intensity=f; + } protected: /** The darkness of the shadow. diff --git a/src/meshlabplugins/decorate_shadow/ssao.cpp b/src/meshlabplugins/decorate_shadow/ssao.cpp index 1e93c48b3..605576add 100644 --- a/src/meshlabplugins/decorate_shadow/ssao.cpp +++ b/src/meshlabplugins/decorate_shadow/ssao.cpp @@ -180,7 +180,7 @@ void SSAO::runShader(MeshDocument& md, GLArea* gla){ glUseProgram(this->_blurShaderProgram); float blur_coef = 0.8; - GLfloat scale = 1/(this->_texSize * blur_coef); + GLfloat scale = 1/(this->_texW * blur_coef); GLuint scaleLoc = glGetUniformLocation(this->_blurShaderProgram, "scale"); glUniform2f(scaleLoc, scale, 0.0); diff --git a/src/meshlabplugins/decorate_shadow/ssao.h b/src/meshlabplugins/decorate_shadow/ssao.h index 9a0eedab5..42f5cb81d 100644 --- a/src/meshlabplugins/decorate_shadow/ssao.h +++ b/src/meshlabplugins/decorate_shadow/ssao.h @@ -48,6 +48,14 @@ public: * @param gla GLArea reference. */ void runShader(MeshDocument&, GLArea*); + virtual void setShadowIntensity(float f) + { + _intensity=f; + } + void setRadius(float rad) + { + this->_radius=rad; + } private: @@ -70,6 +78,7 @@ private: void printNoiseTxt(); float _radius; + float _intensity; int noiseWidth; int noiseHeight; diff --git a/src/meshlabplugins/decorate_shadow/variance_shadow_mapping_blur.cpp b/src/meshlabplugins/decorate_shadow/variance_shadow_mapping_blur.cpp index 7554a0ecb..3a1bfaa89 100644 --- a/src/meshlabplugins/decorate_shadow/variance_shadow_mapping_blur.cpp +++ b/src/meshlabplugins/decorate_shadow/variance_shadow_mapping_blur.cpp @@ -105,7 +105,7 @@ void VarianceShadowMappingBlur::runShader(MeshDocument& md, GLArea* gla){ /***********************************************************/ glUseProgram(this->_blurShaderProgram); - GLfloat scale = 1/(this->_texSize * BLUR_COEF); + GLfloat scale = 1/(this->_texW * BLUR_COEF); GLuint scaleLoc = glGetUniformLocation(this->_blurShaderProgram, "scale"); glUniform2f(scaleLoc, scale, 0.0);