PDA

View Full Version : [opengl 2.1]How should I move my 2d sprite?



ochi12
03-24-2015, 02:43 AM
Hi, I'm currently learning opengl basics and wanted to know what's the best approach to move a 2d sprite with opengl(2.1).
I think that the most convenient way to do it is just by doing something like this(in pseudocode):

glPushMatrix
glTranslate(some value)
draw sprite
glPopMatrix

However I can also just update the vertex data in the vbo with buffersubdata.
The first method takes more opengl function calls, but it doesnt send any data to the gpu, while the second approach may need to re-send data practically every frame(if the player moves) but takes less function calls.

What method is most efficient?

Cornix
03-24-2015, 04:36 AM
In general you want to have as few OpenGL calls as possible, and you want to do many things at once. If you have many sprites, and they are all moving, just update the VBO data for all of them at once and re-send it to the GPU. If you only have one moving sprite then you could also do with the translation, it doesnt really matter.

mahmad
03-24-2015, 05:44 AM
Keep those meshes that are not moving in one vao and those that are moving in other. Use different translation matrix for those meshes that needs to be moved.

GClements
03-24-2015, 09:33 AM
The first method takes more opengl function calls, but it doesnt send any data to the gpu
It sends the matrix. And updating the vertex data will certainly be more efficient than using one draw call per sprite.

If the number of sprites is so large that you actually have to worry about the overhead of sending new vertex data each frame, use instanced rendering (or read the offset from a texture or UBO based upon gl_VertexID).

ochi12
03-24-2015, 10:37 AM
Keep those meshes that are not moving in one vao and those that are moving in other. Use different translation matrix for those meshes that needs to be moved.


I can't create a vao, I'm using opengl 2.1(am I missing an extension?).

GClements
03-24-2015, 12:39 PM
I can't create a vao, I'm using opengl 2.1(am I missing an extension?).

VAOs require OpenGL 3.0 or ARB_vertex_array_object.

But they're really not that significant; they just reduce the number of calls required to set up the arrays prior to issuing a draw call. Any performance advantages are minimal.