Pass multiple texture coordinates to my shader


I have a question that I cannot figure out on my own.
Maybe somebody could give me a hint or a link.
Any help would be appreciated!
Thanks in advance.

What I want to do:

I want to draw a little 2D sprite on the screen.
When drawing the sprite I want to take the color information from one texture (“tex”) and the alpha information from another one (“mask”).

The fragment shader right now looks like this:

uniform sampler2D tex; // providing RGB
uniform sampler2D mask; // providing alpha

void main()
	vec4 texel = texture2D(tex, gl_TexCoord[0].st);
	vec4 mask = texture2D(mask, gl_TexCoord[0].st);

	texel.a = mask.a;

	gl_FragColor = texel;

Here is my problem:

The texture coordinates in texture “tex” and the ones in “mask” are not the same.
Unfortunately I don’t know how to pass two different texture coordinates to my shader.
Right now I pass texture coordinates like this:

	gl2.glTexCoord2f(tx1, ty1);
	gl2.glVertex3f(qx1, qy1, 0.0f);

	gl2.glTexCoord2f(tx2, ty2);
	gl2.glVertex3f(qx2, qy1, 0.0f);

	gl2.glTexCoord2f(tx2, ty2);
	gl2.glVertex3f(qx2, qy2, 0.0f);

	gl2.glTexCoord2f(tx1, ty2);
	gl2.glVertex3f(qx, qy2, 0.0f);

So I guess I will need to enhance this code somehow but I cannot find out how.
I’m using JOGL (java bindings for OpenGL) if that matters.

Thanks a lot for your time and help!
Greetings Xoric


try using
glMultiTexCoord2f( GL_TEXTURE0, s1, t1);
glMultiTexCoord2f( GL_TEXTURE1, s2, t2);

and access them in the sahder as gl_TexCoord[0].st and gl_TexCoord[1].st .

Alternatively (based on the target hardware) you could use generic vertex attributes & VBOs instead of using the old immediate mode and the fixed number of texture coordinates.
(The immediate mode is still fine for simple applications where performance is not a concern or if the app has to run on hardware older than 5 years but it’s probably not the way to start learning OpenGL in 2012…).

Technically that shader looks correct, though I’d not the use the name “mask” both for the uniform and the vec4.

I once had a similar problem in Mesa GLSL, there changing to

texel = vec4(texel.rgb, mask.a);

fixed it.

If the alpha mask matches the sprite then just sample the mask texture with the same coordinates.

Otherwise use glmultitexcoord or an additional shader attribute (the new preferred way) to send a new set of coordinates.


Your code was exactly what I was looking for menzel.
I could not get it to work the way I intended yet but I will figure it out soon. Thanks a lot!

Also thanks for your hint mbentrup.
I edited the shader. Thanks!

The alpha mask does not have the same coordinates dorbie.
That’s why I will use glMultiTexCoord. Thank you!


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