jogl + glsl + multitexturing

Hi!
I’m just trying to get two (or more) textures from jogl into my glsl fragment shader.

jogle global for TMU IDs:


//-----global-------//
int[] tmu = new int[2];

//shader program handle
int p_texture;


jogle setup textures in init method:


//----- init -------//

//---SETUP TEXTURES

//---base texture
gl.glGenTextures(2, tmu, 0);

gl.glClientActiveTexture(GL.GL_TEXTURE0);
gl.glEnable(GL.GL_TEXTURE_2D);

gl.glBindTexture(GL.GL_TEXTURE_2D, tmu[0]);
		
textureTBase = null;
try {
	// TextureReader utility to read in Textures
	textureTBase = TextureReader.readTexture("data/Fieldstone.jpg");
} catch (IOException e) {
	e.printStackTrace();
	throw new RuntimeException(e);
}
//set teximage
gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB,  textureTBase.getWidth(),  textureTBase.getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE,  textureTBase.getPixels());

gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER,GL.GL_LINEAR);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER,GL.GL_LINEAR);



//---bump texture

gl.glClientActiveTexture(GL.GL_TEXTURE1);
gl.glEnable(GL.GL_TEXTURE_2D);

gl.glBindTexture(GL.GL_TEXTURE_2D, tmu[1]);
		
textureTBump = null;
try {
	// TextureReader utility to read in Textures
	textureTBump = TextureReader.readTexture("data/FieldstoneBump.jpg");
  
} catch (IOException e) {
	e.printStackTrace();
	throw new RuntimeException(e);
}
gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB,  textureTBump.getWidth(),  textureTBump.getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE,  textureTBump.getPixels());

gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER,GL.GL_LINEAR);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER,GL.GL_LINEAR);


jogle setting up cube with texture coords and calling shader:



gl.glEnable(GL.GL_TEXTURE_2D);

gl.glClientActiveTexture(GL.GL_TEXTURE0);
gl.glBindTexture(GL.GL_TEXTURE_2D, tmu[0]);
gl.glUniform1iARB(gl.glGetUniformLocationARB(p_texture, "tex"),0);
		    
gl.glClientActiveTexture(GL.GL_TEXTURE1);
gl.glBindTexture(GL.GL_TEXTURE_2D, tmu[1]);
gl.glUniform1iARB(gl.glGetUniformLocationARB(p_texture, "tex2"),1);
		    
gl.glUseProgramObjectARB(p_texture);
		    
gl.glColor3f(1.0f, 1.0f, 1.0f);
		    
// Ausgabe Seite vorne (=1)
gl.glBegin(GL.GL_QUADS);
gl.glNormal3f(0.0f, 0.0f, 1.0f);
gl.glTexCoord2f(0.0f, 0.5f); gl.glVertex3f(-0.5f, 0.0f, 0.5f);
gl.glTexCoord2f(0.333333f, 0.5f); gl.glVertex3f(0.5f, 0.0f, 0.5f);
gl.glTexCoord2f(0.333333f, 1.0f); gl.glVertex3f(0.5f, 1.0f, 0.5f);...
.....

I think the code of the fragmentshader is unimportant. I’ve implemented some functions to check if both textures are loaded.

Problem:
I get only one Texture into the fragment shader!!

Facts:
The program just takes the texture that was bound the latest time!
Uniform doesn’t have any effect.
Both textures are saved to GL_TEXTURE0. (tmu[0] and tmu[1])

Could anybody help me please?

thx pat

I had the same problem myself. If I remember correctly, the trick was not to enable TEXTURE_2D. And I used glActiveTexture instead of glClientActiveTexture, but this may be unrelated.

you should do some changes to your code:


// First set active texture unit
gl.glClientActiveTexture(GL.GL_TEXTURE0);
// Then enable it!
gl.glEnable(GL.GL_TEXTURE_2D);

gl.glBindTexture(GL.GL_TEXTURE_2D, tmu[0]);
gl.glUniform1iARB(gl.glGetUniformLocationARB(p_texture, "tex"),0);
   
gl.glClientActiveTexture(GL.GL_TEXTURE1);

// Here too, you have to enable each unit for its own
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glBindTexture(GL.GL_TEXTURE_2D, tmu[1]);

// Here first use program!
gl.glUseProgramObjectARB(p_texture);

// Then do any passUniform... calls.
gl.glUniform1iARB(gl.glGetUniformLocationARB(p_texture, "tex2"),1);
   
gl.glColor3f(1.0f, 1.0f, 1.0f);
.
.
.


hope this will help.

Hi!

Thanks for Help, but I could solve the problem on my own.

It’s quite logical (at least after I found out what was wrong):

You have to set the Uniform variables after(!!!) defining the shaderprogramm do be used!!!

:smiley:



// frist define program
gl.glUseProgramObjectARB(p_texture);

// then set uniforms!!
gl.glClientActiveTexture(GL.GL_TEXTURE0);
gl.glBindTexture(GL.GL_TEXTURE_2D, tmu[0]);
gl.glUniform1iARB(gl.glGetUniformLocationARB(p_texture, "tex"),0);
   
gl.glClientActiveTexture(GL.GL_TEXTURE1);
gl.glBindTexture(GL.GL_TEXTURE_2D, tmu[1]);
gl.glUniform1iARB(gl.glGetUniformLocationARB(p_texture, "tex2"),1);
   
gl.glColor3f(1.0f, 1.0f, 1.0f);
   
// then draw object
gl.glBegin(GL.GL_QUADS);
gl.glNormal3f(0.0f, 0.0f, 1.0f);
gl.glTexCoord2f(0.0f, 0.5f); gl.glVertex3f(-0.5f, 0.0f, 0.5f);
gl.glTexCoord2f(0.333333f, 0.5f); gl.glVertex3f(0.5f, 0.0f, 0.5f);
gl.glTexCoord2f(0.333333f, 1.0f); gl.glVertex3f(0.5f, 1.0f, 0.5f);...
.....

thx pat