# How to sort VBO data before sending it to the GPU?

## Recommended Posts

MarkS    180
I have read two different things here, both of which are the "recommended" way to send geometry to the GPU and both are contradictory.

The first thing I read was that should put all geometry into one VBO (I'm drawing tiles) and send it in one large chunk.

The second thing is that I should sort the tiles by texture and shader and send them in batches.

So, how do I batch a single VBO? The only way I can see this being possible is to separate the different tiles into their own VBO's by tile ID and send each VBO in turn. That would be better than sending each tile separately, but still not exactly point #1.

I figure that I am misunderstanding something here.

##### Share on other sites
Erik Rufelt    5901
You don't have to draw the entire VBO at once, but can draw for example the first half, then change textures, and draw the second half. If you use indexed triangles with glDrawElements, specify the number of bytes from the start of the index VBO to the first index to use as the last parameter.

So if I remember correctly it's like this:
// Draw 3 triangles from the same VBOglBindTexture(...);glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);glBindTexture();glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, (GLvoid*)(3*sizeof(GLuint)));glBindTexture();glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, (GLvoid*)(6*sizeof(GLuint)));

However, using more than one VBO isn't necessarily bad. You don't want to have a separate VBO for every tile, but several different VBOs doesn't matter if it fits your design better.

##### Share on other sites
You can always send the tiles in as one big grid and just draw the whole grid. For each vertex you can store the color and tex coordinate in the VBO as well. If you can put all your tiles into one 3d texture where each depth is a different texture, then you can just use the z-coordinate for each tile as the texture lookup.

##### Share on other sites
MarkS    180
Quote:
 Original post by Erik RufeltHowever, using more than one VBO isn't necessarily bad. You don't want to have a separate VBO for every tile, but several different VBOs doesn't matter if it fits your design better.

I wouldn't do one for each and every tile, but one VBO for all tiles of tile ID 0, another for all tiles of ID 1, etc. I think this would fit better with my design in the long run.

Quote:
 Original post by dpadam450You can always send the tiles in as one big grid and just draw the whole grid. For each vertex you can store the color and tex coordinate in the VBO as well. If you can put all your tiles into one 3d texture where each depth is a different texture, then you can just use the z-coordinate for each tile as the texture lookup.

This was my original plan when I was planning on a basic 2D tile map. However, I am now using the tile map to specify the placement of 3D models and using a perspective projection. Each tile will consist of more than four vertices and will require textures in the 512x512 pixel range.

What I now have planned is quite an interesting effect. The very center of the screen looks like a orthogonal projection, with the 3D aspects of the tiles becoming apparent away from center. Tiles and objects will move in and out of perspective, depending on their position on screen. Of course, the true benefit is being able to use a 3D physics library for collision detection.

##### Share on other sites
Well if your tiles have physical z-depth and bumpiness, then sort them and just bind the vbo for that tile, draw all those tiles, do the same for all tiles.

##### Share on other sites
MarkS    180
Quote:
 Original post by dpadam450Well if your tiles have physical z-depth and bumpiness, then sort them and just bind the vbo for that tile, draw all those tiles, do the same for all tiles.

Well, DUH! Why didn't I think of that! [embarrass]