Bình Nguyên
Bình Nguyên

Reputation: 2352

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.

Upvotes: 32

Views: 24319

Answers (2)

datenwolf
datenwolf

Reputation: 162317

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.

Upvotes: 37

Bartek Banachewicz
Bartek Banachewicz

Reputation: 39390

OpenGL introduced multitexturing at some point (which is basically using more than one texture on one geometry pass); in order to keep older code working, they couldn't just add additional parameter to all the functions. That's why you have to explicitly specify which texture unit you are addressing.

Upvotes: 23

Related Questions