Search code examples
openglopengl-es

What is the function of glActiveTexture and GL_TEXTURE0 in OpenGL?


I'm finding a way to understand why glActiveTexture is needed. I have the code below:

glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);

If I imagine that the GL_TEXTURE_2D is a picture's frame hanging on the wall and textureId is the real picture, the glBindTexture is the only command for assigning a real picture to the frame, so, what are GL_TEXTURE0 and glActiveTexture?

Will my code below work fine?

glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);

glActiveTexture(GL_TEXTURE1);
glTexImage2D(GL_TEXTURE_2D, .....)
glTexParameteri(GL_TEXTURE_2D, ...)

I'm currently working with OpenGL2.1.


Solution

  • If I imagine that the GL_TEXTURE_2D is a picture's frame hangging on the wall and textureId is the real picture,

    Actually a very good analogy :)

    so, what GL_TEXTURE0 and glActiveTexture are?

    Think about a wall with multiple picture frames, the first frame being labeled GL_TEXTURE0, the second GL_TEXTURE1 and so on.