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: Passing 2 Textures in to a Shader

  1. #1
    Newbie Newbie
    Join Date
    Nov 2012
    Posts
    2

    Exclamation Passing 2 Textures in to a Shader

    Hi I am trying to pass 2 textures to a shader and I am a little confused about the working here

    This is the opengl code

    Code :
    GLuint textures;
    GLuint textures1;
            glGenTextures(1,&textures);
            glGenTextures(1,&textures1);
            glBindTexture(GL_TEXTURE_2D,textures);
            glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,256,256,0,GL_RGB,GL_UNSIGNED_BYTE,texImg);
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
     
            glBindTexture(GL_TEXTURE_2D,textures1);
            glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,256,256,0,GL_RGB,GL_UNSIGNED_BYTE,texImg1);
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
            glBindTexture(GL_TEXTURE_2D,textures1);
     
            GLuint location1=glGetUniformLocationARB(programObjectFloor,"Tex1");
            GLuint location2=glGetUniformLocationARB(programObjectFloor,"Tex2");
     
     
            glActiveTexture(GL_TEXTURE0);
            glBindTexture(GL_TEXTURE_2D,textures);
            glUniform1i(location1, 0);
     
            glActiveTexture(GL_TEXTURE1);
            glBindTexture(GL_TEXTURE_2D,textures1);
            glUniform1i(location2, 1);

    the vertex shader is

    Code :
    attribute vec4 position; 
    attribute vec4 texture;
     
    uniform mat4 MVP;
     
     
    varying vec4 ptexture; 
     
    void main()
    {
    	ptexture = texture; 	
        gl_Position = MVP * position; 
    }

    the fragment shader is

    Code :
    varying vec4 ptexture; 
     
    uniform sampler2D Tex1;
    uniform sampler2D Tex2;
    uniform bool swit;
     
    void main() 
    {
    		gl_FragColor = texture2D(Tex1,ptexture.st);
    }

    In my fragment shader, when I change my code to the below, still only the first texture is displayed.

    Code :
    varying vec4 ptexture; 
     
    uniform sampler2D Tex1;
    uniform sampler2D Tex2;
    uniform bool swit;
     
    void main() 
    {
    		gl_FragColor = texture2D(Tex2,ptexture.st);
    }

    But when I comment out the opengl code as shown below, the second texture is shown

    Code :
            glGenTextures(1,&textures);
            glGenTextures(1,&textures1);
            glBindTexture(GL_TEXTURE_2D,textures);
            glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,256,256,0,GL_RGB,GL_UNSIGNED_BYTE,texImg);
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
     
            glBindTexture(GL_TEXTURE_2D,textures1);
            glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,256,256,0,GL_RGB,GL_UNSIGNED_BYTE,texImg1);
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
            glBindTexture(GL_TEXTURE_2D,textures1);
     
            GLuint location1=glGetUniformLocationARB(programObjectFloor,"Tex1");
            GLuint location2=glGetUniformLocationARB(programObjectFloor,"Tex2");
     
     
    //        glActiveTexture(GL_TEXTURE0);
    //        glBindTexture(GL_TEXTURE_2D,textures);
    //        glUniform1i(location1, 0);
     
            glActiveTexture(GL_TEXTURE1);
            glBindTexture(GL_TEXTURE_2D,textures1);
            glUniform1i(location2, 1);

    this line below in the fragment shader does not affect the rendering in anyway
    Code :
    gl_FragColor = texture2D(Tex2,ptexture.st);
    i can mention Tex1 or Tex2 and it still displays some texture, I am totally confused, it would be great if someone could explain what is happening.

    Thanks!!

  2. #2
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,948
    You mixed ARB_shader_objects code (glGetUniformLocationARB) with actual core GLSL code (glUniform1i). That's never a good thing. Pick one: you're either using core GLSL (in which case you should be using glGetUniformLocation) or you're using ARB_shader_objects (in which case you should be using glUniform1iARB).

    More importantly, you store the return value in a GLuint. That's broken. Uniform locations can be -1, which means that the uniform is not present or active in the shader. Your conversion of -1 into an unsigned integer yields... unknown results, especially when you shove that back into a GLint later.

    Always use GLint for uniform locations.

    If you fix these things, you might get better results.

  3. #3
    Newbie Newbie
    Join Date
    Nov 2012
    Posts
    2
    Thanks for the reply Alfonse its fixed now, Changed it to, but there is no change in the output of the program.

    Code :
    glGenTextures(1,&textures);
            glGenTextures(1,&textures1);
            glBindTexture(GL_TEXTURE_2D,textures);
            glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,256,256,0,GL_RGB,GL_UNSIGNED_BYTE,texImg);
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
     
            glBindTexture(GL_TEXTURE_2D,textures1);
            glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,256,256,0,GL_RGB,GL_UNSIGNED_BYTE,texImg1);
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
            glBindTexture(GL_TEXTURE_2D,textures1);
     
            GLint location1=glGetUniformLocation(programObjectFloor,"Tex1");
            GLint location2=glGetUniformLocation(programObjectFloor,"Tex2");
            glEnable(GL_TEXTURE_2D);
            glActiveTexture(GL_TEXTURE1);
            glBindTexture(GL_TEXTURE_2D,textures1);
            glUniform1i(location2, 1);
     
            glActiveTexture(GL_TEXTURE0);
            glBindTexture(GL_TEXTURE_2D,textures);
            glUniform1i(location1, 0);

    I think I am missing some explanation or a concept of sampler2d and the binding, when I change the glActiveTexture(GL_TEXTURE1); then the second texture is shown, but why cant I change it in the fragment shader ?

    Thanks!!

  4. #4
    Member Regular Contributor
    Join Date
    Aug 2008
    Posts
    455
    Do you still have "textures" bound to texture unit 0 and "textures1" bound to unit 1 when you make the draw call?

    Unlike the way glVertexAttribPointer operates (with latched state), the texture isn't set when you make the glUniform1i call, you only tell it what texture unit will be used when the draw call is made. This means you could have this code:
    Code :
    glUseProgram(my_prog); // did you remember to use the program when setting uniforms?
    glUniform1i(location1, 0); // Tex1 will use texture unit 0
    glUniform1i(location2, 1); // Tex2 will use texture unit 1
     
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, textures);
     
    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, textures1);

Posting Permissions

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