1

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

    GLuint textures;
    GLuint textures1;</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=glGetUniformLocation(programObjectFloor,"Tex1");
    GLuint location2=glGetUniformLocation(programObjectFloor,"Tex2");

    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D,textures1);
    glUniform1i(location2, 1);

    glActiveTexture(GL_TEXTURE0);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D,textures);
    glUniform1i(location1, 0);

The Vertex Shader is

attribute vec4 position; 
attribute vec4 texture;

uniform mat4 MVP;


varying vec4 ptexture; 

void main()
{
    ptexture = texture;     
    gl_Position = MVP * position; 
}

The Fragment Shader is

varying vec4 ptexture; 

uniform sampler2D Tex1;
uniform sampler2D Tex2;
uniform bool swit;

void main() 
{
        gl_FragColor = texture2D(Tex1,ptexture.st);
} 

When I change the line

gl_FragColor = texture2D(Tex2,ptexture.st);

Still only the first texture is shown, but when I comment out the below lines, the second texture is shown, can you please explain why this is happening?

//        glActiveTexture(GL_TEXTURE0);
//        glEnable(GL_TEXTURE_2D);
//        glBindTexture(GL_TEXTURE_2D,textures);
//        glUniform1i(location1, 0);
genpfault
  • 51,148
  • 11
  • 85
  • 139
Rohan Patil
  • 277
  • 1
  • 4
  • 10

1 Answers1

0

You must call glUseProgram before querying for the uniform locations and assinging values to them. Most likely you're switching the program later, leaving whatever you set those uniforms to in a undefined state.

Also when using shaders, there's no need for glEnable(GL_TEXTURE_2D);, if the texture is used or not is determined by the shader's code.

Not related to your problem, just pointing out, you have a redundant call to glBindTexture just before you query the uniform locations.

datenwolf
  • 159,371
  • 13
  • 185
  • 298