user1502256
user1502256

Reputation:

How to render multiple textures?

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.

Upvotes: 0

Views: 150

Answers (1)

user26347
user26347

Reputation: 604

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);
} 

Upvotes: 1

Related Questions