Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: when to use glActiveTexture?

  1. #1
    Intern Contributor
    Join Date
    Jun 2011
    Posts
    89

    when to use glActiveTexture?

    In the book OpenGL programming guide, Example 9-7 uses two texture objects and it does not call glActiveTexture before calling glBindTexture, while in Example 9-10, it calls glActiveTexture before glBindTexture. What's the difference?

    what's the difference between glActiveTexture and glBindTexture? It seems they both can activate a texture.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,948

    Re: when to use glActiveTexture?

    Imagine that OpenGL internally looks like this:

    Code :
    struct TextureUnit
    {
        GLuint targetTexture1D;
        GLuint targetTexture2D;
        GLuint targetTexture3D;
        GLuint targetTextureCube;
        ...
    };
     
    TextureUnit textureUnits[GL_MAX_TEXTURE_IMAGE_UNITS]
    GLuint currentTextureUnit = 0;

    glActiveTexture means this:

    Code :
    void glActiveTexture(GLenum textureUnit)
    {
        currentTextureUnit = GL_TEXTURE0 - textureUnit;
    }

    And glBindTexture does this:

    Code :
    void glBindTexture(GLenum textureTarget, GLuint textureObject)
    {
        TextureUnit *texUnit = &textureUnits[currentTextureUnit];
        switch(textureTarget)
        {
        case GL_TEXTURE_1D: texUnit->targetTexture1D = textureObject; break;
        case GL_TEXTURE_2D: texUnit->targetTexture2D = textureObject; break;
        case GL_TEXTURE_3D: texUnit->targetTexture3D = textureObject; break;
        case GL_TEXTURE_CUBEMAP: texUnit->targetTextureCube = textureObject; break;
        }
    }

    Obviously there would be error testing, but that's the idea. All of the functions that modify the texture use the "currentTextureUnit" set by glActiveTexture. The target parameter they take tells which bound texture location within that texture unit to use.

    So if you have:

    Code :
    glActiveTexture(GL_TEXTURE0 + 5);
    glBindTexture(GL_TEXTURE_2D, object);
    glTexImage2D(GL_TEXTURE_2D, ...);

    The texture being uploaded to is the one stored in textureUnits[5]->targetTexture2D. Each texture that you bind has a texture target and a texture unit; this specifies its unique location in the context.

    I don't have that particular book, but one often binds a texture to the context just to upload some data or to modify it. It doesn't matter at that point which texture unit you bind it to, so there's no need to set the current texture unit. glTexImage2D doesn't care if the current active texture is 0, 1, 40, or whatever.

    However, texture units have special meaning to the rendering of objects. When you bind textures for the purpose of rendering with them, you need to bind them to a particular texture unit. Therefore, you need to set the current texture unit before you do the bind.

  3. #3
    Senior Member OpenGL Pro Aleksandar's Avatar
    Join Date
    Jul 2009
    Posts
    1,136

    Re: when to use glActiveTexture?

    Great explanation, Alfonse!

    Quote Originally Posted by shapeare
    In the book OpenGL programming guide, Example 9-7 uses two texture objects and it does not call glActiveTexture before calling glBindTexture, while in Example 9-10, it calls glActiveTexture before glBindTexture. What's the difference?
    Example 9-10 is not complete; maybe that why you are confused. Think in this way: If there is a need for several textures used simultaneously, then glActiveTexture() must be applied in order to setup each texture unit (see previous Alfonse's post). If you need just one texture at a time, only one unit (usually 0) is used.

    Be aware that the number of texture units is limited. On mobile devices there are usually only two. On desktop systems it should be at least 8.

  4. #4
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,190

    Re: when to use glActiveTexture?

    Quote Originally Posted by shapeare
    what's the difference between glActiveTexture and glBindTexture? It seems they both can activate a texture.
    You got some good answers already. I'll just add one thing to that. glActiveTexture selects the active texture unit, and glBindTexture binds a texture to the active texture unit. So you see they work together hand-in-hand.

    Rather than use a behind-the-scenes "active texture unit" selector to help define what BindTexture does, GL could have put the arguments to both APIs in one single stand-alone GL call. And this in-fact is what's done in the EXT_direct_state_access function glBindMultiTextureEXT:
    Code :
    void BindMultiTextureEXT(enum texunit, enum target, uint texture);

    which does exactly what we just discussed. You tell it which "texture" you want to bind, what its texture type is (target), and which "texture unit" you want to bind it to. It's all right there without any behind-the-scenes selectors.

    Similar APIs are defined by this extension for many other texture calls, and many other calls besides.

  5. #5
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,264

    Re: when to use glActiveTexture?

    Quote Originally Posted by shapeare
    In the book OpenGL programming guide, Example 9-7 uses two texture objects and it does not call glActiveTexture before calling glBindTexture, while in Example 9-10, it calls glActiveTexture before glBindTexture. What's the difference?

    what's the difference between glActiveTexture and glBindTexture? It seems they both can activate a texture.
    If you don't call glActiveTexture, then it uses the default, which is 0 (GL_TEXTURE0).

    If you need to bind a texture to another tex unit, then you need glActiveTexture.

    Despite the name, glActiveTexture does not activate a texture unit. I really don't know why they didn't just create that glBindMultiTexture function in GL 1.2.1

    There was this "let's not modify old GL functions and just introduce another one" attitude back then. Got to live with it now.
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

  6. #6
    Advanced Member Frequent Contributor
    Join Date
    Apr 2009
    Posts
    590

    Re: when to use glActiveTexture?

    Be aware that the number of texture units is limited. On mobile devices there are usually only two. On desktop systems it should be at least 8.
    The GLES2 standard requires atleast 8 texture units, not too sure what GLES1 requires though. As a side note, the N900 which uses a PowerVR SGX supports just that: 8 texture units. The SGX in the N900 is by today's standards pretty low on the totem pole, and newer devices are typically faster than the SGX in the N900 (bit often enough SGX's as well, but higher clock and higher "model number").

  7. #7
    Intern Contributor
    Join Date
    Jun 2011
    Posts
    89

    Re: when to use glActiveTexture?

    Be aware that the number of texture units is limited. On mobile devices there are usually only two. On desktop systems it should be at least 8.
    I have a NVIDIA GeForce GTS 450 graphics card. I use glGetIntegerv(GL_MAX_TEXTURE_UNITS, ...) in my c++ program and only get 4 texture units. It is really strange.

  8. #8
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,264

    Re: when to use glActiveTexture?

    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

  9. #9
    Intern Contributor
    Join Date
    Jun 2011
    Posts
    89

    Re: when to use glActiveTexture?

    Thank you very much.

    The OpenGL Programming Guide is a little misleading.

  10. #10
    Senior Member OpenGL Pro Aleksandar's Avatar
    Join Date
    Jul 2009
    Posts
    1,136

    Re: when to use glActiveTexture?

    The Red book is excellent for the fixed functionality, but gives little to no clues about what is going on in the programmable pipeline. On the other hand, Wiki is oversimplified.

    You have six different constants that define how many sampler object you can use:
    - GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS,
    - GL_MAX_GEOMETRY_TEXTURE_IMAGE_UNITS,
    - GL_MAX_TESS_CONTROL_TEXTURE_IMAGE_UNITS,
    - GL_MAX_TESS_EVALUATION_TEXTURE_IMAGE_UNITS,
    - GL_MAX_TEXTURE_IMAGE_UNITS and
    - GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS.

    Each of the first 5 defines the maximal number of texture image units in particular programmable pipeline stage. The last one defines the total number of texture image units that can be used simultaneously in all stages. Theoretically MAX_COMBINED can be the sum of all others, but in practice that number could be smaller since texture units might be shared.

Posting Permissions

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