Cleaned up the creation/destruction of shaders: crashed after changing too many times the parameters for leaking.

This commit is contained in:
Paolo Cignoni cignoni 2010-10-07 16:26:22 +00:00
parent f652f5f284
commit 4a69332c0b
7 changed files with 49 additions and 38 deletions

View File

@ -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;
}

View File

@ -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;

View File

@ -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<QAction *> 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" ; }

View File

@ -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.

View File

@ -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);

View File

@ -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;

View File

@ -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);