Bound textures?

ive just started glsl in my engine, and looking att he glsl specs, and see some great stuff automatically bound such as light info, matrices, and the like, but i see nothing like this for textures?

did i just miss it is there a reason they have not done this? because this means i need to parse my shader, and search for some “sampler*” keyword and do it myself…


Sampler are passed to GLSL via uniforms. You have to do:

glActiveTexture(GL_TEXTURE0 + my_tex_unit);
glBindTexture(GL_TEXTURE_2D, my_tex_obj);
glUniform1i(glGetUniformLocation("My2DSampler"), my_tex_unit);

Where you have defined a 2D sampler in a shader:

uniform sampler2D My2DSampler;

Right now there’s no possibility to get all samplers out of a program object by calling glActiveUniform because samplers are threated as ints and so you can’t say if an uniform is a sampler or an int. But there should be a GLSL specification update in the next time where this problem is solved.

hrmm thinkg something like gl_Position would have been specified
or like gl_LightSourceParameters

i would expect something like gl_Texture0

any idea why they have left this out? :mad:

Don’t know but I think this is a good way of binding textures. In your fragment shader you can simple write:

uniform sampler2D My2DSampler;
varying vec2 TexCoord;

void main()
  gl_FragColor = texture2D(My2DSampler, TexCoord);

Without bordering at which this texture unit the sampler is bound.

Then in you app you can choose where to bind your texture. If your texture has to be bound at a specific texture unit then simple pass that texture unit using glUniform1i to tell the shader where to sample.

indeed, this method does seem better as it decouples the texture unit used and the shader instead of forcing you to always use a certain texture unit for a certain effect (and as the number of texture units increase this will help with resource wastage)

um both would be good :slight_smile:

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