Shaders do no give the right effect? Beginner

Hi there. Currently I’m trying to implement depth of field through GLSL. First of all let me say that the shaders I use are not mine, they are taken from a project online which implements depth of field so they are not faulty since the project where they are used works as it should.

Aside from setting up my scene the use of the shaders are done through a multipass rendering in OpenGL where I use a framebuffer object for each pass (except the last pass where I render the result). There are in total 6 pass-methods I’ve implemented and each one use different vertex and fragment shaders except one pass which shares the same vertex shader with another pass.

I’ll explain the idea as short as I can and then explain my problem:

The idea is that in your first pass you render the scene to full resolution, as well as computing a depth falloff value for each pixel which determine how much each pixel should be blurred in the post-processing stage. This pass works(!) as it should so there is nothing wrong here

Once this pass is done there are two ways to go…the first way is to actually implement the depth of field effect which means that we go through pass 2-5, the other way is to use another pass (call it testpass) which shows where the focus will lie on the scene and where the blurring will be carried out. This pass works(!) as well! So in those two there are no problems whatsoever.

The other way: In the second pass the image is downsampled to 1/4th of its size, in the third and fourth pass gaussian blur is applied in x and y-axis. The fifth pass is responsible for rendering two textures, the texture that containts the original scene (from the first pass)…and the texture that results from going through pass 2-4…in other words the one that applies the blur.

Now here’s the problem…the first texture that shows the original scene gets rendered perfectly fine however the texture which is responsible for causing the blur effect does not work at all. Instead of a blur effect we get a black color effect.

This is how the fifth fragment shader used in the fifth pass looks alike:


void main (void)
{
	vec4 Fullres = texture2D(Tex0, gl_TexCoord[0].st);
	vec4 Blurred = texture2D(Tex1, gl_TexCoord[1].st); //***
	

	// HLSL linear interpolation function
	//gl_FragColor = Fullres + Fullres.a * (Blurred - Fullres);

	//GLSL linear interpolation function.	
	gl_FragColor = mix(Fullres,Blurred,Fullres.a);
	
}

If I comment the ‘Blurred’ variable (commented with three stars) I get my original scene back which seem to be blurred (no idea why) but if I use ‘Blurred’ I get my scene + a black color effect. The point is that its supposed to use the blur…not give a black color. You can see the effects in this picture:

http://img697.imageshack.us/i/dofh.png/

I’m totally lost in what wrong I am doing. Since the shaders are already confirmed to work in another project it would make me think that the problem doesn’t lie there but I’m not so sure anymore. Just for the sake of it I’ll post the section of my OpenGL code where I perform the multipass rendering:


//WORKS!!!
void firstPass()
{
	glEnable(GL_DEPTH_TEST);
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo0);
	glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	glActiveTexture(GL_TEXTURE0);
	glUseProgram(programObj1);
	loc = glGetUniformLocation(programObj1, "focalDistance");
	if(loc == -1) fprintf( stderr, "Binding warning: Failed to locate uniform variable 'focaldistance'.
");
	glUniform1f(loc, focalDistance);
	if(loc == -1) fprintf( stderr, "Binding warning: Failed to locate uniform variable 'focalRange'.
");
	loc = glGetUniformLocation(programObj1, "focalRange");
	glUniform1f(loc, focalRange);
	
	skybox();
	teapots();
}

void secondPass()
{
	//glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT);
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo1);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glBindTexture(GL_TEXTURE_2D, img0);
	glUseProgram(programObj2);
	glPushAttrib(GL_VIEWPORT_BIT);
	glViewport(0, 0, windowWidth/2, windowHeight/2);
	drawQuad();
}

void thirdPass()
{
	//glDrawBuffer(GL_COLOR_ATTACHMENT2_EXT);
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo2);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glBindTexture(GL_TEXTURE_2D, img1);
	glUseProgram(programObj3);
	drawQuad();
}

void fourthPass()
{
	//glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT);
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo3);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glBindTexture(GL_TEXTURE_2D, img2);
	glUseProgram(programObj4);
	drawQuad();
}

void fifthPass()
{
	//glDisable(GL_LIGHTING)
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
	glDrawBuffer(GL_BACK);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, img0);
	glActiveTexture(GL_TEXTURE1);
	glBindTexture(GL_TEXTURE_2D, img3);
	glOrtho(0.0,1.0,0.0,1.0,0.0,1.0);
	glUseProgram(programObj5);
	glPopAttrib();
	glViewport(0, 0, windowWidth, windowHeight);

	glBegin(GL_QUADS);
		glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 0.0f);
		glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 0.0f);
		glVertex3f(0.0, 0.0,0.368);
		glMultiTexCoord2f(GL_TEXTURE0, 1.0f, 0.0f);
		glMultiTexCoord2f(GL_TEXTURE1, 1.0f, 0.0f);
		glVertex3f(1.0, 0.0,0.368);
		glMultiTexCoord2f(GL_TEXTURE0, 1.0f, 1.0f);
		glMultiTexCoord2f(GL_TEXTURE1, 1.0f, 1.0f);
		glVertex3f(1.0, 1.0,0.368);
		glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 1.0f);
		glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 1.0f);
		glVertex3f(0.0, 1.0,0.368);
	glEnd();
}

//WORKS!!!
void blurTestPass()
{
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
	glDrawBuffer(GL_BACK);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  	glOrtho(0.0,1.0,0.0,1.0,0.0,1.0); //Testa ta bort sen
	glBindTexture(GL_TEXTURE_2D, img0);
	glUseProgram(programObj6);
	drawQuad();
}

void display()
{
	glClearColor(0.0,0.0,0.0,0.0);
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 
	glClearDepth(1);
	glLoadIdentity();
	firstPass();
	if(focusCheck)
	{
		secondPass();
		thirdPass();
		fourthPass();
		fifthPass();	
	}
	else
	{
	blurTestPass();
	}
	glutSwapBuffers(); //Swap between the two buffers
}

I hope anyone can help me out…if anything is unclear or you need more code then let me know :slight_smile:

Nobody that has any suggestions on what I should look at?

Fifth pass :
glBindTexture(GL_TEXTURE_2D, img3);
But img3 is never referenced before ? Did you mean img2 instead ?

And what happens when in fifth pass, you change the shader to only sample the blurred texture ? Still black ?

gl_FragColor = Blurred;

It should be img3 that is used, because we use img2 in the fourth pass, drawing to an FBO linked to img3. The example we follow does the exact same thing :slight_smile:

We tried only sampling the blurred texture, what we did was to comment the linear interpolation and just putted

gl_FragColor = Blurred;

beneath it. This resulted in a black screen where we couldn’t even see our scene anymore when we changed the position of our focus.

Well then continue to debug up the chain, investigating if img2 is black too, etc, until the problem is bracketed.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.