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 3 of 3

Thread: textures unit bind uniform sampler2D from learopengl

  1. #1
    Newbie Newbie
    Join Date
    Jan 2019
    Posts
    2

    Post textures unit bind uniform sampler2D from learopengl

    Hi Everyone,
    I rencently learn OpenGL from https://learnopengl.com/Getting-started/Textures.
    When I get to the chapter about Texture. I got a confused problem.

    This is the example I checked. As you can see from github, I picked out the line from 146 to 149. I think it will bind the Texture unit to correct sampler in fragment script.
    If I make "texture1" in glUniform1i(glGetUniformLocation(ourShader.ID, "texture1"), 0); to "texture1" or something else which is not correct with fragment script. I will get a pure “awesomeface.png”.
    But the fact is not, I still got a box with a smile. I don't know, seems I have no need to call the function glUniform1i(glGetUniformLocation(ourShader.ID, "texture1"), 0);
    So, I remove that. I still got a box with a smile. That confused me. Can someone help me out? Thanks

    Plus with the post, If I didn't replace GL_TEXTURE0 with GL_TEXTURE0. That's correct with my expectations. Why ???

    https://github.com/JoeyDeVries/Learn...s_combined.cpp


    Code :
        // either set it manually like so:
        glUniform1i(glGetUniformLocation(ourShader.ID, "texture1"), 0);
        // or set it via the texture class
        ourShader.setInt("texture2", 1);

  2. #2
    Senior Member OpenGL Lord
    Join Date
    May 2009
    Posts
    6,087
    All uniform values which are not initialized in the shader text itself are initialized by the system to be 0 (or false for `bool`s). That includes sampler uniforms.

    Since you just so happen to be setting that uniform value to 0, the default just so happens to be fine. In this specific case.

    But you should never rely on this. Always explicitly set stuff your code depends on.

  3. #3
    Newbie Newbie
    Join Date
    Jan 2019
    Posts
    2
    Thanks, your answer can explain this phenomenon. And I found a reference at https://www.khronos.org/registry/Ope...fer_object.txt. Thanks again.



    Quote Originally Posted by Alfonse Reinheart View Post
    All uniform values which are not initialized in the shader text itself are initialized by the system to be 0 (or false for `bool`s). That includes sampler uniforms.

    Since you just so happen to be setting that uniform value to 0, the default just so happens to be fine. In this specific case.

    But you should never rely on this. Always explicitly set stuff your code depends on.

Posting Permissions

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