Skip to main content
GameDev.net gamedev.net
🔒 Locked

GLSL - Multiple texture passing. **Updated**

Started by Funkapotamus Dec 7, 2004 at 3:21 PM 5 replies 9.5k views
Original Post
Funkapotamus
Funkapotamus
Ugh... i'm lost. My problem is getting a shader to listen to me! Here's the code:

//FRAG
uniform sampler2D keke0;
uniform sampler2D keke1;
void main (void)
{
	vec2 src = vec2(gl_TexCoord[0]);
	vec4 dest = texture2D( keke0, src);
	vec2 src2 = vec2(gl_TexCoord[1]);
	vec4 dest2 = texture2D( keke1, src);	

// Draw texture 1
	gl_FragColor = dest;   
// Draw texture 2
        //gl_FragColor = dest2;

}

//VERT
void main(void)
{
	gl_TexCoord[0] = gl_MultiTexCoord0;
	gl_TexCoord[1] = gl_MultiTexCoord1;

	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
See, this code works great if I want to draw 1 texture. This always draws the last texture bound. Under no circumstances have I been able to get it to listen to me and utilize the second sampler2D I have there. I know how to pass uniform variables to the shader, however, when it comes to sampler2D's I don't know how to get it to work. Grr..
//uniformLocation String = "keke2"
ARBShaderObjects.glUniform1iARB(uniformLocation, textureID);
...
vec2 src2 = vec2(gl_TexCoord[1]);
vec4 dest2 = texture2D( keke1, src);	
gl_FragColor = dest2;
...Doesn't work! Grr...
GL13.glClientActiveTexture( GL13.GL_TEXTURE1);
GL11.glBindTexture( GL11.GL_TEXTURE_2D, sprite.getTextureID());
GL11.glEnable( GL11.GL_TEXTURE_2D);
ARBShaderObjects.glUniform1iARB(uniformLocation, 1);
...
vec2 src2 = vec2(gl_TexCoord[1]);
vec4 dest2 = texture2D( keke1, src);	
gl_FragColor = dest2;
...Doesn't work either! As a side note: the GL11/GL13/ARBShaderObjects is from LWJGL, an OpenGL binding for Java. Ignore that if you arn't familliar with it. If you do, great, maybe I'm doing something wrong with that! [Edited by - Funkapotamus on December 7, 2004 8:07:48 PM]
_the_phantom_
_the_phantom_
First thing which jumps out at me on reading all your code, you refer to a keke2 in a comment, but not in the shader...

Check list:
- make sure you are getting back a valid location for the uniform
- make sure you are using the correct uniform name when requesting the location
- make sure you are swapping to the right texture unit when you bind the texture
- make sure you are supplying sane texture coords for the 2nd texture unit

and when I say 'make sure' I'm talking until your eyes bleed from looking, coz I dont want to have to ask you to post all the relivent code and then spot one of those errors [wink]
Twixn
Twixn
Thats because your completely overwriting 'gl_FragColor' with 'dest2'.

Imagine in your app if you put:

int Var;
Var=2;
Var=3;

The two is not going to do anything cos its overwritten.

so, decide how you want the two samplers to combine (modulate/add etc), then put them together like this.

gl_FragColor = dest * dest2;//to modulate samplers

in one call.


Also, when passing uniform samplers to shaders, pass the TexUnit
eg,

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,Tex0);
glUniform1iARB(Sampler0,0);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D,Tex1);
glUniform1iARB(Sampler1,1);

I am Me...you cant argue with THAT can you!!!
_the_phantom_
_the_phantom_
*points out to Twixn that the 2nd write to gl_FragColor is commented out* [wink]

oh yeah, and should ARBShaderObjects.glUniform1iARB() be taking a string as its first arg? In the C world the first arg is an int which is the location of the Uniform, which you've had to get back via asking the shader object where it is. Granted, the Java wrapper could do this in one go, but I thought it worth pointing out [smile]
Funkapotamus
Funkapotamus
Ok, I guess I need to clarify a bit more.

1) I know "keke1" is edited out. It is there for reference. But it really doesn't matter. If I wanted to do something with both textures I can't because it only takes the last texture bound.

2) No, glUniform1iARB doesn't take a string. It's pseudocode.

3) Something like Twixin posted was what I am looking for. Still however, even emulating that code in Java, it does not work the way I would like it.
_the_phantom_
_the_phantom_
in which case, post the proper relivent bits of code, it would be more help in seeing whats going on.

Also, while I'm here, two points of note [smile]
- Passing vec2s to the fragment program would be better than converting down to a vec2 per fragment
- use ftransform() todo the vertex transform unless you need the result, as its going to be more precise
Funkapotamus
Funkapotamus
Alrighty then. Due to popular demand... ** Updated **


//VERTvoid main(void){	gl_TexCoord[0] = gl_MultiTexCoord0;	gl_TexCoord[1] = gl_MultiTexCoord1;		gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; }//FRAGuniform sampler2D keke;uniform sampler2D smee;void main (void){	vec2 src = vec2(gl_TexCoord[0]);	vec4 dest = texture2D(keke, src);		vec2 src2 = vec2(gl_TexCoord[1]);	vec4 dest2 = texture2D(smee, src2);			gl_FragColor = 0.5 * (dest + dest2);  }// JAVAARBShaderObjects.glUseProgramObjectARB(shader.getProgramID());		int location = shader.getUniformLocation(shader.getProgramID(), "keke");int location2 = shader.getUniformLocation(shader.getProgramID(), "smee");						ARBMultitexture.glActiveTextureARB(ARBMultitexture.GL_TEXTURE0_ARB);GL11.glBindTexture(GL11.GL_TEXTURE_2D, TestEngine.cID);GL11.glEnable(GL11.GL_TEXTURE_2D);ARBShaderObjects.glUniform1iARB(location, 0);						ARBMultitexture.glActiveTextureARB(ARBMultitexture.GL_TEXTURE1_ARB);GL11.glBindTexture(GL11.GL_TEXTURE_2D, sprite.getTextureID());GL11.glEnable(GL11.GL_TEXTURE_2D);	ARBShaderObjects.glUniform1iARB(location2, 1);						EngineFunc.translate(origin.x, origin.y);EngineFunc.drawQuads(sprite.getPoints());		ARBShaderObjects.glUseProgramObjectARB(0);		ARBMultitexture.glActiveTextureARB(ARBMultitexture.GL_TEXTURE1_ARB);GL11.glDisable(GL11.GL_TEXTURE_2D);		ARBMultitexture.glActiveTextureARB(ARBMultitexture.GL_TEXTURE0_ARB);GL11.glDisable(GL11.GL_TEXTURE_2D);


Ok... the new updated result is that things still don't work. Here's a picture of what's happening: http://www.funkapotamus.org/error.JPG I've outlined the quad in question in red. The part in blue is the "keke" image, which is a texture from a pbuffer of the whole screen before shading. Anyway, as you can see, the shader is blending properly... however, the shader is not drawing the second texture.. it's all blue (though, it's strangely part of the same blue color that the texture itself is). I'm lost, scared, and confused :/

[Edited by - Funkapotamus on December 7, 2004 8:01:56 PM]

Topic Locked

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

Sign in to reply to this topic.