Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: Problem with multiple textures in shader

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2009
    Posts
    3

    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 ( ), 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:
    Code :
    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.

    Code :
    // 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!

  2. #2
    Junior Member Newbie
    Join Date
    Nov 2008
    Posts
    13

    Re: Problem with multiple textures in shader

    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

  3. #3
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,370

    Re: Problem with multiple textures in shader

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

  4. #4
    Junior Member Newbie
    Join Date
    Mar 2009
    Posts
    3

    Re: Problem with multiple textures in shader

    Quote Originally Posted by DerTherion
    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
    Ahh, of course! Thanks!

    Quote Originally Posted by dletozeun
    And for completeness, you don't have to call glEnable( GL_TEXTURE_2D ); for any of the texture units using shaders.
    Oh, thanks for the tip!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •