Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: bound textures?

  1. #1
    Junior Member Regular Contributor
    Join Date
    Feb 2004
    Location
    Aus
    Posts
    141

    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....

    cheers

  2. #2
    Member Regular Contributor
    Join Date
    Apr 2002
    Location
    Austria
    Posts
    328

    Re: bound textures?

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

    Code :
    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:

    Code :
    uniform sampler2D My2DSampler;
    EDIT:
    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.
    There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.

    There is another theory which states that this has already happened...

  3. #3
    Junior Member Regular Contributor
    Join Date
    Feb 2004
    Location
    Aus
    Posts
    141

    Re: bound textures?

    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?

  4. #4
    Member Regular Contributor
    Join Date
    Apr 2002
    Location
    Austria
    Posts
    328

    Re: bound textures?

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

    Code :
    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.
    There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.

    There is another theory which states that this has already happened...

  5. #5
    Member Regular Contributor
    Join Date
    Apr 2004
    Location
    UK
    Posts
    420

    Re: bound textures?

    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)

  6. #6
    Junior Member Regular Contributor
    Join Date
    Feb 2004
    Location
    Aus
    Posts
    141

    Re: bound textures?

    um both would be good

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •