glBegin - glEnd + textures problem.

Hi. I know that if I render several quads, it’s better (in optimisation point of view) to put them in one glBegin - glEnd.
But I’ve got a problem with texturing quads this way - I use a tile based map, and render quads in two loops, like this:

for(int i = 0; i < SX; i++) {
for(int j = 0; j < SY; j++) {
glBindTexture(GL_TEXTURE_2D, appropriate texture id);
quad on appropriate position

The problem is that I cannot put glBegin and glEnd outside the two loops becouse it doesn’t bind textures then. What’s the solution to do that really quick ?

that the best way i know of doing it. And if i remember correctly
, the time it takes to do a new glBegin/End block is insignificant to binding a texture.

you should sort your quads by texture, so that you have to bind each texture only once.

If I would sort my quads, then I ought to go via my loops n-times, where n == number of different textures. Are you sure that this method will be faster? I must calculate this. Thanks for your answers :wink:

Just do something like this:

myList = glGenLists(1);
glNewList(myList, GL_COMPILE);
FOR every texture
  select texture
  FOR every quad
    draw quad

You do that only once during initialization. When you need to draw your quads, then you just call:


As long as you do not need to modify your quads, then display list will be very good solutoin.
Otherwise you would have to use vertex arrays. Using immediate mode (glBegin/glEnd) without display list will always be a bit slow.

Of course, the pure CPU time of your application will be n times as much as without sorting, but usually the texture binding uses most of the time, so it’s better to bind textures outside the loop.

Assume you have n textures. Your algorithm does SXSY texture binds and quads. If you sort it per texture, you’ll have only n texture binds, and you still draw only SXSY quads. Your complexity is increased to nSXSY, but that’s not what is limiting your rendering speed.

If you are using a tile-based map, can’t you create a “uber-texture” with all your textures?

In this way you only need to make a glBindTexture and take care with the texcoords.