Strangely dark textures

Hello there. I had a quick look around and I’m not sure this question has been asked. Im writing an application in Openframeworks based on the CameraGrabber example and the VSM shader example found here: http://www.fabiensanglard.net/shadowmappingVSM/

So far it works quite well but I’m looking at the blur stage. Essentially, the lightmap is rendered to fill the entire offscreen buffer. This buffer has a blur shader applied to make it all nice. However, there is also a scale factor that is applied. This scale factor seems to make the texture very dark; the higher the factor, the darker it gets.

I played around some more. It turns out that glOrtho() doesnt like OpenFrameworks (not sure why) so I turned that off and attempted to render the polygon with the texture on it. Oddly enough, i didnt see very much. I then figured out that the world space was different thanks to calling glLoadIdentity(). Rather than draw a polygon from 0,0 to the extents of the screen, I had to draw a polygon from -1 to 1 on X and Y axes

THe point of the second bit is that when the texture was draw from 0,0 to extents the texture was fine in brightness if a little too zoomed in. when i got the coordinate mapping right, the texture is too dark. the same occurs when the blur_coefficient is set to 1; i.e when the blur texture is the same size as the unblurred texture buffer.

This seems to me to suggest that resizing textures is causing darkness to occur somehere. I cant really figure it out. Will try to post code later.

lighting with opengl can be a bit of a nightmare. there may be a scaling thing going on between the light source and your surface… try disabling lighting and see if that makes any difference. if it does, you might have to think about scaling up the light coordinates as well (the angles between the surface normal and the light-to-surface vector can become to great to cast enough light if the light is too close to the surface, but if attenuation is not enabled on the light it’s hard to intuitively see this because there’s no falloff in the intensity…)

I shall post up some code. I’m not using any lighting at the point where the texture is generated at all. Lighting is all per pixel and performed much later in the pipeline. Managed to get glOrtho to work finally but its certainly proving rather tricky.

This particular method is taken from the VSM code I mentioned in the previous post. It should create a blured shadowmap and the code seems identical to the original which works ok. I display this texture on a quad in the corner of my main screen as part of a debug. With that in mind i can see the texture becoming too dark. The more that BLUR_COEF decreases, the darker it gets.

  
  
        glDisable(GL_CULL_FACE);  
	  
	// Bluring the shadow map  horinzontaly  
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,blurFboId);	  
	glViewport(0,0,(float)ofGetWidth() * SHADOW_MAP_COEF * BLUR_COEF,(float)ofGetHeight() * SHADOW_MAP_COEF * BLUR_COEF );  
	blurShader.setShaderActive(true);  
	  
	glUniform2fARB(scaleUniform,1.0/ (float(ofGetWidth()) * SHADOW_MAP_COEF * BLUR_COEF),0.0);		  
  
       // Bluring horinzontaly  
	  
	glUniform1iARB(textureSourceUniform,0);  
	glActiveTextureARB(GL_TEXTURE0);  
	glBindTexture(GL_TEXTURE_2D,colorTextureId);  
	  
	//Preparing to draw quad	  
	glMatrixMode(GL_PROJECTION);  
	glPushMatrix();  
	glLoadIdentity();  
	glOrtho(-ofGetWidth()/2,ofGetWidth()/2,-ofGetHeight()/2,ofGetHeight()/2,1,20);  
	  
	  
	glMatrixMode(GL_MODELVIEW);  
	glPushMatrix();  
	glLoadIdentity();  
	glTranslated(0,0,-5);  
	//Drawing quad   
	glColor3f(1.0,1.0,1.0);  
	glBegin(GL_QUADS);  
	  
	  
	glTexCoord2d(0,0);glVertex3f(-ofGetWidth()/2,-ofGetHeight()/2,0);  
	glTexCoord2d(1,0);glVertex3f(ofGetWidth()/2,-ofGetHeight()/2,0);  
	glTexCoord2d(1,1);glVertex3f(ofGetWidth()/2,ofGetHeight()/2,0);  
	glTexCoord2d(0,1);glVertex3f(-ofGetWidth()/2,ofGetHeight()/2,0);  
	  
	  
	glEnd();  
  
	// Bluring vertically  
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,fboId);	  
	glViewport(0,0,(float)ofGetWidth() * SHADOW_MAP_COEF ,(float)ofGetHeight()* SHADOW_MAP_COEF);  
	glUniform2fARB(scaleUniform,0.0, 1.0/ ((float)ofGetHeight() * SHADOW_MAP_COEF ));		  
	glBindTexture(GL_TEXTURE_2D,blurFboIdColorTextureId);  
	glBegin(GL_QUADS);  
	  
	glTexCoord2d(0,0);glVertex3f(0,0,0);  
	glTexCoord2d(1,0);glVertex3f((float)ofGetWidth(),0,0);  
	glTexCoord2d(1,1);glVertex3f((float)ofGetWidth(),(float)ofGetHeight(),0);  
	glTexCoord2d(0,1);glVertex3f(0,(float)ofGetHeight(),0);  
	  
	  
	glEnd();  
	  
	blurShader.setShaderActive(false);  
	  
	glMatrixMode(GL_PROJECTION);  
	glPopMatrix();  
	glMatrixMode(GL_MODELVIEW);  
	glPopMatrix();  
	  
	glEnable(GL_CULL_FACE);  
	  
}  

it’s hard to tell what’s going on without also seeing the shader source, but:

  
  
   // Bluring horizontally  
 glViewport(0,0,(float)ofGetWidth() * SHADOW_MAP_COEF * BLUR_COEF,(float)ofGetHeight() * SHADOW_MAP_COEF * BLUR_COEF );  
   blurShader.setShaderActive(true);  
     
   glUniform2fARB(scaleUniform,1.0/ (float(ofGetWidth()) * SHADOW_MAP_COEF * BLUR_COEF),0.0);        

vs

  
  
   // Bluring vertically  
   glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,fboId);     
   glViewport(0,0,(float)ofGetWidth() * SHADOW_MAP_COEF ,(float)ofGetHeight()* SHADOW_MAP_COEF);  
   glUniform2fARB(scaleUniform,0.0, 1.0/ ((float)ofGetHeight() * SHADOW_MAP_COEF ));        
   glBindTexture(GL_TEXTURE_2D,blurFboIdColorTextureId);  
   glBegin(GL_QUADS);  
  

now i could be wrong, but it looks like you’re confusing where the BLUR_COEF is multiplied…? seems to be present in the bottom, and not in the top? and why are you multiplying the viewport size by BLUR_COEFF also?

Yes, the blur shader applies what I believe to be a gaussian blur but I suspect I’ve fallen into a classic programmer trap of learning by dumping in working code that I don’t fully understand. What I believe this is trying to do is render a texture within a small viewport and then rendering it again on a larger viewport to bring in aliasing effects that blur the texture “on the cheap”. Again, the link at the top of the thread works fine so my only conclusions is that I have introduced something within the code with OpenFrameworks. I suspected it was the model 3DS code as that has some texture calls in it, but removing this seemed not to work.

I shall have a look over the code asap and see whats what. Thanks again! :slight_smile:

Finally, I managed to solve it

  
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);  
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);  

NeHe is still the daddy when it comes to OpenGL!

As I see it, the VSM code I was using rendered to a small view port on one pass to a texture. This is then re-rendered at the original size giving an aliasing and “stretch” effect to do blurring on the cheap. Im sure we’ve all seen this with shadow-mapping algorithms in the past.

It turns out though that it wasn’t using a linear filtering for some reason. I wonder if this is embedded somewhere within the OF code or something I’ve missed? I suspect it might be as I’m using some code for loading 3DS models.

All is well :slight_smile: