Re-Order Elements in a Vector

Started by
2 comments, last by JohnnyCode 10 years, 1 month ago

This may be more of a general programming question. I'm currently batching all of my sprites' vertices in one giant, interleaved vertex array. I don't use VBOs for this right now since I am supporting OpenGL ES 2.0, and data is likely to change frequently bit over time. Since I'm drawing everything in one draw call, I'll need to have my vertices sorted within the array in a back-to-forward fashion. All of my vertices are in stored in an STL vector. I'm trying to figure out an efficient way of doing this, though. Whenever I add a sprite to my batch, I can just insert the sprites based on depth, but what if I had to move 4 vertices making up one of my sprites back 8 vertices? Should I just insert a copy of my sprite's vertices, then erase the original copy from the vector?

Advertisement

the best I can think of is to scan unordered buffer of sprites, finding ouy their indexes order upon many sort alogrithms available , and than build an index buffer to render the unordered buffer by. This way you can have the buffer static, keeping its footprint in CPU, as in many sprite/particles implementations the count of particle elements do not vary. If in your very case a once ordered buffer does not change unless being added a sprite, what seems unlikely, this given approach is valid as well. One drawback of this approach is that the generated indicies will not be adjency friendly. If you want to have unindexed buffer instead, use this approach and by ordered indexes recreate the buffer instead to be ordered upon those indexes. Once added a sprite, you will have to move sprites behind it and expand the buffer, but you would not have to do it if you index the buffer, but it will be just adjency unfriendly.

The above is correct in using and sorting an index buffer, however you will still not be able to use a static vertex buffer if you manually update the vertex locations with matrix multiplies (since you are not using instancing and keeping everything in one buffer).
Frankly, by this point you might be better off just making a single draw call for each sprite after sorting the order of calls locally. You should at least benchmark it with a static VBO.


In either case you should be using VBO’s and triple-buffering them. Most mobile devices supporting OpenGL ES 2.0 have built-in triple-buffering on the back-buffer and forced v-sync, so triple-buffering your vertex/index buffers should result in little-to-no lag.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

to use a static vertex buffer if you manually update the vertex locations with matrix multiplies

true, but one can move the tranformation to vertex function, which outperforms CPU tranforming, since paralel pipelines eat it up, and yet, it allows one to have the buffer static. This is yet possible only if Vincent_M will not expand amount of verticies/sprites, keeping them in constant amount. I myself am using a very tripy aproach. I transform sprite quads by the help of their object space z - what allows mi to have any procedural animation, transforming all verticies by the uniformal matrix. Drawing sprites/particles by an indexed buffer is rather vital too, since buffer can contain 100 of them, but with indicies you can specify how many to draw, with what uniform transformation. For example uniform buffer limits you to 128 matricies, so you draw first 128, and then the next ones, in few batches. But I rather use procedural aproach, in case I am drawing fire, not light flies of course.

This topic is closed to new replies.

Advertisement