PDA

View Full Version : Passing 2 Textures in to a Shader



problemChild
11-15-2012, 06:33 PM
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



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_R GB,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_R GB,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(programObjectFlo or,"Tex1");
GLuint location2=glGetUniformLocationARB(programObjectFlo or,"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



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);
}


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



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



glGenTextures(1,&textures);
glGenTextures(1,&textures1);
glBindTexture(GL_TEXTURE_2D,textures);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,256,256,0,GL_R GB,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_R GB,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(programObjectFlo or,"Tex1");
GLuint location2=glGetUniformLocationARB(programObjectFlo or,"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


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!!

Alfonse Reinheart
11-15-2012, 08:35 PM
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 (http://www.opengl.org/wiki/GLAPI/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.

problemChild
11-15-2012, 08:46 PM
Thanks for the reply Alfonse :) its fixed now, Changed it to, but there is no change in the output of the program. :(



glGenTextures(1,&textures);
glGenTextures(1,&textures1);
glBindTexture(GL_TEXTURE_2D,textures);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,256,256,0,GL_R GB,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_R GB,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!!

Dan Bartlett
11-16-2012, 02:51 AM
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:

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);