Access different texture units?

How can I access the different teture unit in this shader.
addUniform(cube,“tex1”,0);
works as expected but not when I try to use texture unit 1.

I Have tried

gl_FragData[2] = texture2D(tex1, gl_TexCoord[1].st);

and

gl_TexCoord[0] = gl_MultiTexCoord0+1;

without result.

I have a multi texture shader that seems to work. Can someone please explain how to access texture unit ‘i’ in my shader for single textures.

  1 #extension GL_ARB_draw_buffers : enable
  2 
  3 varying vec3 position, normal;
  4 uniform sampler2D tex1,tex2;
  5 
  6 
  7 void main(void) {
  8     gl_FragData[0] = vec4(position.xyz, 0.0);
  9     gl_FragData[1] = vec4(normalize(normal.xyz), 0.0);
 10     gl_FragData[2] = texture2D(tex1, gl_TexCoord[0].st) +
 11                      texture2D(tex2,gl_TexCoord[0].st);
 12 }

This is code that ads the uniforms and the shader source.

 56     addProgram(cube, "./shaders/single.vert", "./shaders/single.frag");
 57     addUniform(cube,"tex1",1);
 56     addProgram(cube, "./shaders/single.vert", "./shaders/single.frag");
 57     addUniform(cube,"tex1",0);
  1 #extension GL_ARB_draw_buffers : enable
  2 
  3 varying vec3 position, normal;
  4 uniform sampler2D tex1;
  5 
  6 
  7 void main(void) {
  8     gl_FragData[0] = vec4(position.xyz, 0.0);
  9     gl_FragData[1] = vec4(normalize(normal.xyz), 0.0);
 10     gl_FragData[2] = texture2D(tex1, gl_TexCoord[0].st);
 11 }
~        
  1 varying vec3 position, normal;
  2 
  3 void main(void) {
  4     position = vec3(gl_ModelViewMatrix * gl_Vertex);
  5     normal = gl_NormalMatrix * gl_Normal;
  6     gl_Position = ftransform();
  7     gl_TexCoord[0] = gl_MultiTexCoord0;
  8 }

You need to bind the texture to that texture unit in the code.

glActiveTexture(GL_TEXTURE0);
glbindtexture(GL_TEXTURE_2D, texid);

glActiveTexture(GL_TEXTURE0+1); // or glActiveTexture(GL_TEXTURE1);
glbindtexture(GL_TEXTURE_2D, texid2);

Than the sampler will access the texture you have set depending on which texture unit you had bind it to.

addUniform(cube,“tex1”,0); //will access the texture texid
addUniform(cube,“tex2”,1); //will access the texture texid2

Thanks but I already how. I works now(same code) after a make -clean.

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