More than one texture with Vertex Arrays?

When using vertex arrays, and glDrawRangeElements(), I can only figure out how to use a single texture to cover all sides of a cube:

glBindTexture(…);
glDrawRangeElements(…);

and the effect is, of course, that all the polygons end up
with the same texture. So, how would I get a cube with a different texture on each side?

Thanks

Draw each side seperately. Bind the texture before the each side is drawn.

So your saying I would have to use glBegin() and
glEnd(), thus abandoning vertex arrays?

No, you can use vertex arrays. But you will have to bind and draw each side seperately. pseudo code:

for each side
glBindTexture(theTextureThatGoesOnThisSide);
glDrawRangeElements(start and length of current side inside array);

So you can’t draw the whole array at once if you wan’t to have different textures on different triangles.

Hope this makes sense.

You could also build a texture that is a combination of all six textures (if the original six aren’t too big), and just use texture coordinates to place the textures in the correct place.

Ok, I understand now Tom. It’s amazing that there isn’t something better…six calls to glDrawRangeElements() to draw one stupid box!

In a real scene do people do something like this?

  1. Bind woodTexture

  2. Draw all polys that use woodTexture

  3. Bind rockTexture

  4. Draw all polys that use rockTexture

etc…

But in that case, you would have to have the polygons sorted for draw order by the texture they use, and that sounds odd.

[quote]Ok, I understand now Tom. It’s amazing that there isn’t something better…six calls to glDrawRangeElements() to draw one stupid box!

In a real scene do people do something like this?

  1. Bind woodTexture

  2. Draw all polys that use woodTexture

  3. Bind rockTexture

  4. Draw all polys that use rockTexture

etc…

But in that case, you would have to have the polygons sorted for draw order by the texture they use, and that sounds odd.
[/quote]
Yup, thats how its done. Usually though, if you have a more complex model, where the texture changes very often, you rather have one texture that has all the different “paintings” and appropriate tex-coords to get the desired effect.
Current graphics accelerators couldn’t be this fast, I they couldn’t pipeline the vertex and fragment processing because the texture-loading between vertices with different textures would be way to costly.

Greetings,

Jan

Just yesterday I looked at the lwjgl-based spagetti sourcecode (it’s 2D sprite engine for opengl) for curious.


http://cvs.sourceforge.net/viewcvs.py/spgl/spgl/src/com/shavenpuppy/jglib/sprites/SpriteRenderer.java?rev=1.29&view=auto

If I can understand this right, then SpriteRenderer.build() method will do exactly what you described. It will loop all sprites and sort by textures before doing the real rendering.