PDA

View Full Version : Texture units



ei05tbe
07-05-2010, 03:20 PM
Can someone help me understand this?
I have been searching around. I found for example http://berkelium.com/OpenGL/GDC99/multitexture.html

Please tell me in other words what a texture unit is? A texture has a name(id) but what is the unit needed for? It is some kind of environment?

1. If I use single texture do I have need a texture unit?

2. I have a Scene graph. Pretend there is to geometries both with multi texture. When rendering this can I use texture units 0 and 1 for both textures or do I have to use (0 and 1) for the first geometry and (2 and 3) for the second?

Regards

Dark Photon
07-05-2010, 06:02 PM
Please tell me in other words what a texture unit is?
A texture unit is a piece of hardware in the GPU that actually performs the sampling and interpolation of the texel data in a texture.

A texture is the data. A texture unit is the thing that operates on the data.

Think of a texture unit like a light bulb socket, and a texture like a light bulb. To use the light bulb, you've got to screw it into a socket. You can swap light bulbs in a single socket, but to use any given light bulb it has to be in a socket.

ei05tbe
07-06-2010, 01:30 AM
Thank you for that. That answer was very clear. So if I only use one texture the texture unit are set by default? Because I have seen examples of single texturing that don't include any texture units.


Edit: Also, do this make any sense? This is code I found. Do I have to //enable stuff for each of the texture units?


for (unsigned int i=0; i<uList.size(); i++) {
glClientActiveTextureARB(GL_TEXTURE0_ARB+i);

//enable stuff
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);

glNormalPointer(GL_FLOAT,0,&amp;m_normals[0]);
glTexCoordPointer(2,GL_FLOAT,0,&amp;m_textureCo[0]);
glVertexPointer(3,GL_FLOAT,0,&amp;m_vertices[0]);
}

Dark Photon
07-06-2010, 05:26 AM
So if I only use one texture the texture unit are set by default?
Not necessarily. Typically you'd just use texture unit 0:

glActiveTexture( GL_TEXTURE0 );
but you don't "have" to use that one (when using shaders) if you're only using one texture. You just plug the texture unit number into the sampler (texture unit) uniform via glUniform1i, and the shaders will access the right one.

But with the fixed-function pipeline, yeah, IIRC you generally want to populate the texture units in order (0,1,2,3,...) until you run out of textures. Easier that way.


Because I have seen examples of single texturing that don't include any texture units.
Yes, they're just assuming glActiveTexture is GL_TEXTURE0 by default.


Edit: Also, do this make any sense? This is code I found. Do I have to //enable stuff for each of the texture units?
Careful. This is old legacy pipeline stuff. And this is gl<u>Client</u>ActiveTexture, which is used to set which texture coordinate GL_TEXTURE_COORD_ARRAY refers to. Not glActiveTexture, which sets which texture unit the next texture bind will bind to.

Now in the old fixed-function pipe, there was typically a direct correspondence between texture coordinate numbers and texture unit numbers, but IIRC this was gotten rid of by texture combiners, and then totally eliminated with modern shaders many GPU generations ago.

If you use glVertexAttribPointer (new-style vertex attributes), then there's no need to use glClientActiveTexture anymore.

ei05tbe
07-06-2010, 09:43 AM
Thanks!