0

I'm trying to understand how to render multiple textures for different objects in OpenGL. I decided to simply try it, as far as I'm aware glActivate is used to select the right texture, but it doesn't work as I expected it. In my code, two objects are shown (in turns) but they have the same texture. This is the texture initialization part of the code:

bool (some args, unsigned int textureUnit, bool wrap)
{
    // some code

    OpenGL->glActiveTexture (GL_TEXTURE0 + textureUnit);
    glGenTextures (1, &textureID);
    glBindTexture (GL_TEXTURE_2D, textureID);
    glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, targaImage);

    if (wrap) {
        glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    } 
    else {
        glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
        glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    }

    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    OpenGL->glGenerateMipmap (GL_TEXTURE_2D);
}

textureUnit is 0 for the first texture, and 1 for the second. Model rendering code:

{
    OpenGL->glBindVertexArray (vertexArrayId);
    glDrawElements (GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
}

Putting glActive between that part, or before that part doesn't change a thing. So my question is, how to render multiple textures? Should I put glActivate somewhere else or do I need to change something else.

1 Answers1

1

There is some misunderstanding here, the glActiveTexture calls are for managing multiple textures when drawing the same object. For different objects, you just bind a different texture. Just remove your call to ActiveTexture when setting your textures (or make sure it is always on 0 when you are drawing). Then, when drawing:

{
    OpenGL->glBindVertexArray (vertexArrayId);
    glBindTexture(GL_TEXTURE_2D, textureID);     /* this has to be set somehow */
    glDrawElements (GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
} 
user26347
  • 594
  • 3
  • 4
  • So glActivate is for multiple textures on the same object? –  Mar 22 '14 at 12:11
  • 1
    @Ruben right. Changing textures between objects is done by binding them. glActiveTexture sets which "texture location" is being affected by the glBindTexture calls, but since you are only using one texture per object, you can safely ignore it. – user26347 Mar 22 '14 at 12:18