0
votes

I know there can be GL_MAX_TEXTURE_IMAGE_UNITS texture units in an OpenGL Program. But lets assume I am using only one. Suppose I have 5 2D textures. Can I "map" them to a single texture unit (TEXTURE0) to render all of them. I mean I will do this:

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex1);
//specify tex data and tex params for tex1

glBindTexture(GL_TEXTURE_2D, tex2);
//specify tex data and tex params for tex1

.. and so on for all 5 textures

Render Loop:

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex1);
//draw

glBindTexture(GL_TEXTURE_2D, tex2);
//draw

..and so on for all 5 textures

In the fragment shader I am using only 1 sampler2d variable which is initialized to zero (for texture unit 0)

If this will not work correctly, then should I use 5 different texture units(TEXTURE0, TEXTURE0+1, ....TEXTURE0+5) and "map" each texture to a different texture unit.

Most important question: If my first approach is correct, how many 2D textures(lets just only talk about 2D textures for now) can I "map" to a Texture unit?

2
Huh? You mean use multitexturing without using multitexturing? What really is it that you want to achieve and what does "map" actually mean for you? If your fragment shader just has a single sampler2D, how could it ever use more than one texture at a time?Christian Rau

2 Answers

5
votes

As long as you don't want to use several textures on a single piece of geometry simultanously, this is perfectly possible. Actually it's the usual thing to do, because the purpose of multitexturing and texture units is not to hold different textures for different objects, but different layers of textures used on the same object.

Think of texture units as something like layers in Photoshop.

2
votes

Well, as I understand OpenGL texture units:

Text units are a way for shaders to point texture for sampling. If your shader must access more than one texture, you must separate them into differents texture units.

In your case, as you seem to need only 1 texture at the same time, glActiveTexture is useless, just let's Opengl use the default GL_TEXTURE0 for all.

So, yes, your render loop is correct.

How many is the limit of texture units? 48+, more than enough!

Note: Remember that OpenGL is a state machine, so to configure more than 1 texture to work at the same time, you need a way to select them, that is exactly what glActiveTexture do.

I hope it will help you.