Problem with multiple textures in shader

Hi!

I’m having some trouble getting multitexturing to work in my shader… I have two textures that I render to in an initial pass, one holds the scene depth, and the other holds the scene normals. This works fine, I can later bind any of these two, and render a fullscreen quad, everything looks as it should. But now I want to bind both the textures, and sample each in a shader to do some magic ( :slight_smile: ), but this I just can’t get to work.

What happens is that it seems I get one texture, as both textures?

This is how I render my fullscreen quad:

void draw_postproc()
{
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0, window_width, 0, window_hight,-1,1);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity(); 
	
	glDrawBuffer(GL_BACK);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glColor3f(1,1,1);

	// Pass 2 of Deferred Shading
	shader_df_pass2->bind();

	glActiveTextureARB(GL_TEXTURE0_ARB);
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, render_buffers[0]);
	 
	glActiveTextureARB(GL_TEXTURE1_ARB);
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, render_buffers[1]); 
	
	glBegin(GL_QUADS);
		//glTexCoord2f(0.0, 0.0);
		glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 0.0);
		glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 0.0);
		glVertex2i(0, 0);

		//glTexCoord2f(1.0, 0.0);
		glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1.0, 0.0);
		glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 0.0);
		glVertex2i(window_width, 0);

		//glTexCoord2f(1.0, 1.0);
		glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1.0, 1.0);
		glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 1.0);
		glVertex2i(window_width, window_hight);

		//glTexCoord2f(0.0, 1.0);
		glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 1.0);
		glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 1.0);
		glVertex2i(0, window_hight);
	glEnd();

	glDisable(GL_TEXTURE_2D);
	
	shader_df_pass2->unbind();
}

render_buffers[0] and render_buffers[1] are the two textures, the first one holds the depth-data, and the second one normal-data. Both belong to a FBO I render the scene to earlier.

// vertex shader
void main()
{	
	gl_TexCoord[0] = gl_MultiTexCoord0;
	gl_TexCoord[1] = gl_MultiTexCoord1;
	gl_Position = ftransform();
}

// fragment shader
uniform sampler2D tex_depth, tex_norm;

void main (void)
{
	vec4 depth = texture2D(tex_depth, gl_TexCoord[0].st);
	vec4 norm = texture2D(tex_norm, gl_TexCoord[1].st);
	gl_FragColor = vec4(depth.r, 0.0, norm.b, 1.0);
}

To clarify; both norm and depth seems to return the same data!

Thanks in advance!

you need to have something like this in your code

glUseProgramObjectARB(program);

Tell the Fragment shader (uniform) the id of your textures

int tex_depth = gl.glGetUniformLocationARB(program, “tex_depth”);
gl.glUniform1iARB(tex_depth, 0);//tex_depth will use first texture stage

int tex_norm = gl.glGetUniformLocationARB(program, “tex_norm”);
gl.glUniform1iARB(tex_norm, 1); //tex_norm will use second texture stage

And for completeness, you don’t have to call glEnable( GL_TEXTURE_2D ); for any of the texture units using shaders.

Ahh, of course! Thanks! :smiley:

Oh, thanks for the tip!