View Full Version : glBegin - glEnd + textures problem.

10-28-2006, 11:06 AM
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 ?

10-28-2006, 11:38 AM
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.

10-28-2006, 12:05 PM
you should sort your quads by texture, so that you have to bind each texture only once.

10-29-2006, 05:06 AM
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 ;)

10-29-2006, 06:21 AM
Just do something like this:

myList = glGenLists(1);
glNewList(myList, GL_COMPILE);
FOR every texture
select texture
FOR every quad
draw quad
glEndList()You do that only once during initialization. When you need to draw your quads, then you just call:

glCallList(myList);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.

10-29-2006, 08:49 AM
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 SX*SY texture binds and quads. If you sort it per texture, you'll have only n texture binds, and you still draw only SX*SY quads. Your complexity is increased to n*SX*SY, but that's not what is limiting your rendering speed.

10-30-2006, 12:37 AM
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.