2
\$\begingroup\$

So what do I mean by layered sprites? Layered Sprites are Sprites that consist of multiple layers, e.g. you have sprite sheets for the basic Body, the Head, Clothes, Weapons, etc.

Well now I wanted to know how you would draw this in the most efficient way; right now the idea is to use vertex buffer objects (VBOs), but I heard that it is expensive to change the textures in a VBO...

So what would be the correct way to draw them? My idea right now would be to draw the Map tiles first (with a texture atlas), then I would draw all the basic bodys, then the heads, then clothes, and then the weapons. Would that be the best way or is there a more efficient way?

I also thought of using vertex arrays instead of VBOs, but as far as I know they are deprecated...

\$\endgroup\$
6
  • 3
    \$\begingroup\$ "doing X is expensive" shouldn't stop you from trying it to see how expensive actually is in your application \$\endgroup\$ Feb 6, 2014 at 9:37
  • \$\begingroup\$ That isn't what I asked. If you have a Sprite with e.g. 10 layers und you want to draw it on a Map with let's say 500x500 Tiles and 250 Sprites that would be nearly 250000 draws per frame. I just want to know what the best way would be to draw something like that. \$\endgroup\$
    – user41845
    Feb 6, 2014 at 10:44
  • 1
    \$\begingroup\$ what I'm saying is that you should at least try and see how it affects performance, high end 3D games use dozens of different textures and still get 100 FPS \$\endgroup\$ Feb 6, 2014 at 11:24
  • \$\begingroup\$ I'm planning how to best do it before I start implementing, that's why I asked. \$\endgroup\$
    – user41845
    Feb 6, 2014 at 12:09
  • \$\begingroup\$ Beyond asymptotic complexity analysis, computer science still cannot offer a reliable way of predicting your code's performance. If you can't quickly write the code to test it, you've still got a lot of programming to learn -- and hence should write a test with all the more reason. \$\endgroup\$ Feb 6, 2014 at 13:45

2 Answers 2

0
\$\begingroup\$

Pack sprites into large texture atlases, and have a quad (VBO) per sprite (for each individual frame). When you want to draw a certain sprite, you draw the appropriate quad on screen, rather than changing the texture for a quad. Sort the draw calls by layer, and that should be all you need.

And to clarify:

Rather than having, say, headQuad, for which you change texture depending on what head and what frame you want, you instead have monsterHeadQuad1, monsterHeadQuad2, monsterHeadQuad3, heroHeadQuad1, heroHeadQuad2, and so on. The numbers represent the animation frame.

If you have lots of the same sprite on screen, look into instanced rendering.

There are plenty of hacks for speeding up the process, but I'd worry more about getting it working than getting it running at 1000+ frames per second.

\$\endgroup\$
3
  • \$\begingroup\$ Wouldn't it be better if you change the texture region in the headQuad instead of having multiple quads for each animation frame? \$\endgroup\$
    – user41845
    Feb 6, 2014 at 13:05
  • \$\begingroup\$ You can store millions of quads in video memory; use it to your advantage! There's going to be a small performance cost every time you modify a quad (update the texture coordinates for each vertex), as opposed to just drawing an existing quad. \$\endgroup\$
    – Fault
    Feb 6, 2014 at 13:33
  • \$\begingroup\$ I see, I thought that it would be limited. \$\endgroup\$
    – user41845
    Feb 6, 2014 at 13:43
0
\$\begingroup\$

Probably the most efficient method would be to draw all the sprites using the same texture at once. You can also save some time transferring memory to the GPU by using a geometry shader or instancing that only takes inputs like position, scale, and orientation, and builds the sprite triangles/quads from that.

You can preserve sprite layering by using depth buffering, which is arguably a better method all-around than temporal layering.

\$\endgroup\$
2
  • \$\begingroup\$ So you say it would be better to have a big texture atlas with everything in it? And by depth buffering you mean Z-buffering? \$\endgroup\$
    – user41845
    Feb 6, 2014 at 12:24
  • \$\begingroup\$ Not necessarily. As long as you draw all the sprites using the same texture at once, you will see huge performance gains as binding a new texture is quite slow. \$\endgroup\$
    – jmegaffin
    Feb 6, 2014 at 13:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .