Skip to main content
gamedev.net
🔒 Locked

Multitexturing noobie

Started by vrdhananjay Jan 19, 2007 at 11:44 AM 5 replies 700+ views
Original Post
vrdhananjay
vrdhananjay
Hey Everyone, I am pretty new to openGL and GLSL, and not much of a game programmer. I need to use subract one textures from another using the pixel shader for an image processing app. I think I am making a very basic mistake. First, I am running Linux, OpenGl 2.0, on an Nvidia platform. As you would notice, most of the code is from nehe. The following are my shaders, very basic... -------------- //frag uniform sampler2D myTexture1; uniform sampler2D myTexture2; void main (void) { vec4 texval1 = texture2D(myTexture1, vec2(gl_TexCoord[0])); vec4 texval2 = texture2D(myTexture2, vec2(gl_TexCoord[1])); gl_FragColor = abs(texval1 - texval2); } ---------------------- //vertex void main(void) { gl_TexCoord[0] = gl_MultiTexCoord0; gl_TexCoord[1] = gl_MultiTexCoord1; gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; } --------------------- Heres the drawing code... int drawGLScene(GLvoid) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); GLint p; p= glGetUniformLocation(my_program, "myTexture1"); printf("Handle for uniform sampler myTexture1 is %d\n", p); GLint q; q= glGetUniformLocation(my_program, "myTexture2"); printf("Handle for uniform sampler myTexture2 is %d\n", q); /*End shader part*/ if (!loadGLTextures()) { return False; } glActiveTexture(GL_TEXTURE0 ); glBindTexture(GL_TEXTURE_2D, texture[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, texture[1]); glUseProgramObjectARB(my_program); glUniform1iARB(p, 0 ); glUniform1iARB(q, 1); glBegin(GL_QUADS); /* front face */ glNormal3f(0.0f, 0.0f, 1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); glEnd(); glUseProgramObjectARB(0); glXSwapBuffers(GLWin.dpy, GLWin.win); return True; } My shaders complile perfectly, so do both the textures. Now the trouble is that in the above funtion, when i try to pass the two textures to my shader code, only the first one reflects in the output, the second texture is somehow magically converted to a gray image. I ll put the problematic(i think) part again ----------- glActiveTexture(GL_TEXTURE0 ); glBindTexture(GL_TEXTURE_2D, texture[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, texture[1]); glUseProgramObjectARB(my_program); glUniform1iARB(p, 0 ); // p and q are uniform locations glUniform1iARB(q, 1); --------------- p and q take values of 0 and 1 which i think is okay. Could you tell me what I am doing wrong? Thanks in advance, Dj p.s. i do have glext.h included.
jmaupay
jmaupay
Does glEnable(GL_TEXTURE_2D) on both target change the problem ?

This means:
glActiveTexture(GL_TEXTURE0 );
glEnable(GL_TEXTURE_2D) ;
glBindTexture(GL_TEXTURE_2D, texture[0]);

glActiveTexture(GL_TEXTURE1 );
glEnable(GL_TEXTURE_2D) ;
glBindTexture(GL_TEXTURE_2D, texture[1]);
jma
vrdhananjay
vrdhananjay
thanks a lot for replying. But the after including the Enable commands, it still doesnt work. :(
Would appreciate it if anyone could put in their 10 cents.

Dj
ff8
ff8
Quote:
Original post by Anonymous Poster
I think you need to specify how to blend the textures :

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture[1]);

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

the last argument can be GL_MODULATE, GL_DECAL, GL_BLEND, or GL_REPLACE.

Hope this will help you!


i think when u use FS for multitex u don't need to use
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
because u have done the MultiTexing operation in FS
gl_FragColor = abs(texval1 - texval2);

try this (like what jmaupay said):
glActiveTexture(GL_TEXTURE0 );
glBindTexture(GL_TEXTURE_2D, texture[0]);
glEnable(GL_TEXTURE_2D);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glEnable(GL_TEXTURE_2D);

second make sure from the locations p and q

oh and u need to send TexCoord for both textures
like this

glBegin(GL_QUADS);
/* front face */
glNormal3f(0.0f, 0.0f, 1.0f);
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 0.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 0.0);
glVertex3f(-1.0f, -1.0f, 1.0f);
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1.0, 0.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 0.0);
glVertex3f( 1.0f, -1.0f, 1.0f);
... etc


i hope it works ^_^;
take care.

vrdhananjay
vrdhananjay
Hi guys,

Thanks for your responses. Ya it think the Modulate thing is not required as i am using the texture to do it.

Anyway the code now works, although for a reason, that i fail to understand. I changed the vertex shader to

//vertex


void main(void)
{
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_TexCoord[1] = gl_MultiTexCoord0; //note heres the change from 1 to 0

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

}
_the_phantom_
_the_phantom_
This is because in the orignal code you only ever supply texture coordinates to the "gl_MultiTexCoord0" attribute, yet you access "gl_MultiTexCoord1" in the vertex shader, which will be set to the default value (0,0,0,0).

Later, in the fragment shader you use this value to sample the texture, but you will ALWAYS be sampling from position (0,0) of the texture for each fragment.

By changing the code you are using the coords passed to "gl_MultiTexCoord0" for both texture units.

As ff8 said, if you need to use different texture coords for each unit then you can specifiy them via the code he showed.

Also, as a general note to everyone, when using GLSL to texture you do not need to glEnable(GL_TEXTURE_2D), it is implicately enabled by sampling from it in the shader. That function call is only required when using the fixed function pipeline.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.