GLSL - Using multiple textures

Started by
13 comments, last by VanKurt 18 years, 3 months ago
Hi there! I'm trying to create a simple shader that uses multiple textures (using one texture worked flawlessly). Here's the code (I use the same set of coords for both textures, is that ok?):

uniform sampler2D tex1;
uniform sampler2D tex2;

void main()
{
	vec4 col1 = texture2D( tex1, gl_TexCoord[0].st );
	vec4 col2 = texture2D( tex2, gl_TexCoord[0].st );

	//gl_FragColor = col1;
	gl_FragColor = col2;
}


The strange thing is that the shader always displays the same texture, no matter which of the two lines I comment out! Here's the code of my application, where the textures are set and the quad is drawn:

glActiveTextureARB(GL_TEXTURE0_ARB);
glEnable(GL_TEXTURE_2D);
bind(texColor);

glActiveTextureARB(GL_TEXTURE1_ARB);
glEnable(GL_TEXTURE_2D);
bind(texColor);

glActiveTextureARB(GL_TEXTURE0_ARB);

glBegin(GL_QUADS);

glMultiTexCoord2fARB( GL_TEXTURE0_ARB, 0.0f, 0.0f );
glMultiTexCoord2fARB( GL_TEXTURE1_ARB, 0.0f, 0.0f );
glVertex3f( -20.0f, -20.0f, 0.0f );

glMultiTexCoord2fARB( GL_TEXTURE0_ARB, 0.0f, 1.0f );
glMultiTexCoord2fARB( GL_TEXTURE1_ARB, 0.0f, 1.0f );
glVertex3f( -20.0f,  20.0f, 0.0f );

glMultiTexCoord2fARB( GL_TEXTURE0_ARB, 1.0f, 1.0f );
glMultiTexCoord2fARB( GL_TEXTURE1_ARB, 1.0f, 1.0f );
glVertex3f(  20.0f,  20.0f, 0.0f );

glMultiTexCoord2fARB( GL_TEXTURE0_ARB, 1.0f, 0.0f );
glMultiTexCoord2fARB( GL_TEXTURE1_ARB, 1.0f, 0.0f );
glVertex3f(  20.0f, -20.0f, 0.0f );

glEnd();

IMHO the whole code is very simple and nothing can go wrong. But obviously it does. I always see the texture in unit 0, no matter what I do (of course, when I switch the two bind-lines I see the other texture). How can that be?
Advertisement
yes, using the same texture coords for both texture lookups is fine.

The reason why it uses the same texture is, although you bind the textures to different units, you never tell OpenGL that the samplers in your code should hold any value, as such they both default to 0, meaning that both texture lookups come from texture unit 0.
there could be many things (u havent posted the relevant parts) in what u have posted this is wrong

vec4 col1 = texture2D( tex1, gl_TexCoord[0].st );
vec4 col2 = texture2D( tex2, gl_TexCoord[0].st );

is not

glMultiTexCoord2fARB( GL_TEXTURE0_ARB, 0.0f, 0.0f );
glMultiTexCoord2fARB( GL_TEXTURE1_ARB, 0.0f, 0.0f );

but shouldnt make a diff
all the glEnable(GL_TEXTURE_2D); etc calls arent needed
also u seem to binding the same texture to different texture units
Need to see the code for your bind() function.
You need to bind the texture object to the texture unit and then set the uniform shader variables to the texture unit number:

GLint loc;glActiveTexture(<texture unit>);glEnable(<texture target>);loc = glGetUniformLocation(<shader program object>, <texture variable name>);glUniform1i(loc, <texture unit>);glBindTexture(<texture target>, <texture object>);

Thanks for the reply! That sounds good ;-)
I added the following code to my initialization method:

// Get the texture's OpenGL namesint tex1 = texColor->mTexID;int tex2 = texLight->mTexID;// Get the locations of the variablesint tex1Loc, tex2Loc;tex1Loc = glGetUniformLocationARB( prgmBMap, "tex1" );tex2Loc = glGetUniformLocationARB( prgmBMap, "tex2" );// Set the variablesglUniform1fARB( tex1Loc, tex1 );glUniform1fARB( tex2Loc, tex2 );


The sad thing is that this does not help :-(
What am I doing wrong?
"also u seem to binding the same texture to different texture units"

Ooopps, copy&paste bug ;-)
In my app these are two diferent textures...
you are giving it texture ids, thats wrong, its texture units you need to give it
Hum! Good to know...
So I changed

int tex1 = texColor->mTexID;
int tex2 = texLight->mTexID;

to

int tex1 = 0;
int tex2 = 1;

But it still aint working. Same problem as before.
just as a sanity check, are you sure its using your shader?
try changing the shader so it just writes out red all the time (dont touch the rest of the code).
Also, I ment to ask, which texture is it showing?
The first texture (unit 0) is shown, and yes, the shader works. [help]

This topic is closed to new replies.

Advertisement