Access multitexture in fragment shader

Hi all,
I got problems when I tried to access multitexture with glsl. I wrote two shaders:
/////// first ////////////////////////
uniform sampler2D tex; // texture0
uniform sampler2D ctex; // texture1

void main ()
{
gl_FragColor = texture2D (ctex, gl_TexCoord[1].xy);
}
/////// scoend ////////////////////////
uniform sampler2D tex;
uniform sampler2D ctex;

void main ()
{
vec4 temp = texture2D (tex, gl_TexCoord[0].xy);
gl_FragColor = texture2D (ctex, gl_TexCoord[1].xy);
}
//////////////c program///////////////
location = glGetUniformLocationARB (prg, “tex”);
glUniform1iARB (location, 0);
location = glGetUniformLocationARB (prg, “ctex”);
glUniform1iARB (location, 1);
//////////////////////////////////////
The first shader worked well. But when I tried to access texture0 in the shader (like the second shader), texture2D (tex1,…) returned color of texture0.
However, both shaders worked well in shader designer. So I think there must be something wrong with my c code. But I failed to find it.
Any suggestion will be appreciated.

Off the top of my head:
Make sure you have called glUseProgramObjectARB(prg) before setting the uniforms.

You need to use glActiveTexture().
At least, that’s what i’ve done and it worked :smiley:

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, id1);
location = glGetUniformLocationARB(prg, "tex1");
glUniform1iARB(location, 0);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, id2);
location = glGetUniformLocationARB(prg, "tex2");
glUniform1iARB(location, 1);

Good Luck.

Thank you.
But it still not work.

Each of the textures can be looked up from the shader. But when I tried to use both of them, only texture0 was accessible.

Try this

void main ()
{
gl_FragColor = texture2D (ctex, gl_TexCoord[1].xy);
}

If that still gives you texture 0, then make sure you are binding the textures properly and that the texture targets are correct.
And you might want to post your driver version just to know this is not an old bug.

void main ()
{
gl_FragColor = texture2D (ctex, gl_TexCoord[1].xy);
}
returned texture1. But the following shader returned texture0.

void main ()
{
vec4 temp = texture2D (tex, gl_TexCoord[0].xy);
gl_FragColor = texture2D (ctex, gl_TexCoord[1].xy);
}

My card is READON 9800 Pro, with CATALYST 4.7.

Please, paste you texture setup code in your host application, by this way we can see where are the error

This is my code.

GLhandleARB prg; //shader program
GLhandleARB vs, fs; //vertex shader and fragment shader
VBitmap *nmap, *cmap; //my texture loader
GLuint ntex, ctex; //gl texture handle

int init ()
{
… //initialize shaders

glLinkProgramARB (prg);
glGetObjectParameterivARB (prg, GL_OBJECT_LINK_STATUS_ARB, &linked);
glGetInfoLogARB (prg, 10000, &loglength, shaderlog);  //linked successfully

glActiveTextureARB (GL_TEXTURE0_ARB);
glEnable (GL_TEXTURE_2D);
glActiveTextureARB (GL_TEXTURE1_ARB);
glEnable (GL_TEXTURE_2D);

nmap = new_map (0, 0);
load_map (nmap, "maps/normal.bmp");
glGenTextures (1, &ntex);
glBindTexture (GL_TEXTURE_2D, ntex);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, nmap->size_x, nmap->size_y, 0, GL_BGRA, GL_UNSIGNED_BYTE, nmap->data);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

cmap = new_map (0, 0);
load_map (cmap, "maps/color.bmp");
glGenTextures (1, &ctex);
glBindTexture (GL_TEXTURE_2D, ctex);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, cmap->size_x, cmap->size_y, 0, GL_BGRA, GL_UNSIGNED_BYTE, cmap->data);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glUseProgramObjectARB (prg);

    //check gl error
    //no error reported
GLERR ();
return 0;

}

int draw ()
{
glActiveTextureARB (GL_TEXTURE0_ARB);
glBindTexture (GL_TEXTURE_2D, ntex);
location = glGetUniformLocationARB (prg, “tex”);
glUniform1iARB (location, 0);

glActiveTextureARB (GL_TEXTURE1_ARB);
glBindTexture (GL_TEXTURE_2D, ctex);
location = glGetUniformLocationARB (prg, "ctex");
glUniform1iARB (location, 1);

    //draw a quad
glTranslatef (0, 0, -5);
glBegin (GL_QUADS);
	glNormal3f (0, 0, 1);

	glTexCoord3f (0, 0, 0);		glMultiTexCoord2f (GL_TEXTURE1_ARB, 0, 0);	glVertex3f (-1, -1, 0);
	glTexCoord3f (1, 0, 0);		glMultiTexCoord2f (GL_TEXTURE1_ARB, 1, 0);	glVertex3f (1, -1, 0);
	glTexCoord3f (1, 1, 0);		glMultiTexCoord2f (GL_TEXTURE1_ARB, 1, 1);	glVertex3f (1, 1, 0);
	glTexCoord3f (0, 1, 0);		glMultiTexCoord2f (GL_TEXTURE1_ARB, 0, 1);	glVertex3f (-1, 1, 0);
glEnd ();

    //check gl error
    //no error reported
GLERR ();
return 0;

}

I changed some c code and it works now. But it is still confusing. If glUseProgramObject was called during initializing (after the initialization of RC), the shader takes effect but the result is incorrect. For example, program 1 works but program 2 doesn’t. Is this a feature of program object?

//program 1//
init ()
{

}

draw ()
{
glUseProgramObject (prg);

glUseProgramObject (0);
}

//program 2//
init ()
{

glUseProgramObject (prg);
}

draw ()
{

}

Both ways should work fine, granted that you don’t call glUseProgramObject() anywhere else your code in your second example.

I’m sure about that.

Ok, if you got a sample app showing the problem, send it to me at epersson ‘at’ ati.com and I’ll take a look at it.

FYI all, this issue occurs on Cat 4.7 but have been solved already and the fix is available in the beta Cat 4.9 drivers if anyone needs the fix.
http://www.ati.com/support/infobase/4547.html

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.