0

I'm using PyOpenGL to implement a small 2D game engine. I'm hesitating on the way I implement a Sprite class.

I'm already keeping all the scene (tiled map) in a VBO, and all textures are kept in the same big texture. All the sprite's images are also in this texture. So I suppose that, for performance, I should include the sprite in the VBO, let say from position sprite_start_position.

The first question is : since a sprite can have several stances (images), is it better to :

  • setting only one entry in the VBO for the sprite, and modifying the texture coords in this entry accordingly to the stance, using glBufferSubData
  • setting as many entries in the VBO as there are stances, but drawing only the current one with glDrawArrays
  • other ?

The second is the similar with sprite position. Must I :

  • change the position of the right entry in the VBO with glBufferSubData
  • use some glTranslate before glDrawArrays(GL_QUADS, sprite_start_position, 1)
  • other ?

I'm relatively new to OpenGL and I still feel a little lost in this API...

Fred
  • 901
  • 1
  • 8
  • 16
  • Fastest way would be to use an existing game library xD – user2246674 Sep 29 '13 at 22:22
  • It's mainly for learning purpose, so I don't want to. – Fred Sep 29 '13 at 22:28
  • 1
    Believe it or not, you can solve all of these problems using matrices. You can transform the texture matrix `glMatrixMode (GL_TEXTURE)` the same way you do position `glMatrixMode (GL_MODELVIEW)`. This is how I would approach the problem using shaders as well, instead of changing the sprite's vertex or texture coordinate data inside the vertex buffer every frame, just supply an appropriate transform matrix for texture coordinates and position. – Andon M. Coleman Sep 29 '13 at 23:18
  • Thanks for the answer. I'll check that. – Fred Oct 02 '13 at 12:58
  • I don't really understand why this post is on hold as opinion-based : had I asked for 'the best way', it'd have been subjective. I asked for 'fastest way' on purpose, because it's objective (you can mesure it with benchmarks if necessary). Moreover, I asked between 2 techniques, precisely for avoiding too open discussions. – Fred Oct 02 '13 at 13:01

1 Answers1

1

You really shouldn't have to worry about performance for simple sprite based 2D game. Your graphics card is capable of rendering tens or hundreds of thousands of triangles per second (or more!) which is way more than you are likely to need.

Thus, it really doesn't matter much which method you choose to use for updating sprites. If you want to have animated sprites though, I recommend you look into 3D textures. They allow you to interpolate between animation frames, which makes your animations look even better!

As a side note, you mentioned using glTranslate which is a function from OpenGL 1.x/2.x that has been removed from later versions of the API. I recommend that you try to use "modern OpenGL" (ie OpenGL 3.x/4.x). You can find plenty of information about the differences online. That said, it is still OK to use the older versions of the API if you have a specific reason for doing so, and if you do functions like glTranslate will continue to work.

fintelia
  • 1,201
  • 6
  • 17
  • Thanks for the answer. I can ensure you though there are wrong ways of doing that, especially when using Python... I add too that finding good informations on Modern OpenGL is not so easy : either you find deprecated techniques, or the articles supposes you already master basic OpenGL. I've found Learning Modern 3D Graphics Programming, by Jason L. McKesson, quite enlightening, even if it covers far more than I actually need, but doesn't deal with 3D textures (or I didn't have reached it yet). – Fred Oct 02 '13 at 12:57
  • A 3D texture acts like a stack of 2D textures. Each one could be a frame in your animation for instance. The third dimension would then represent time. Zero would be the first frame and 1.0 would be the last. Linear interpolation then provides a smooth transition between frames. – fintelia Oct 02 '13 at 19:36
  • Feel free to ask more questions if there is something you can't find online! – fintelia Oct 02 '13 at 19:40