# What's the correct way to move 2d sprites in opengl 2.1?

I'm getting into Opengl 2.1 and wanted to know how can I move 2d sprites. I already created my vbo and ibo, and the vertex data is already there. But, how can I move a sprite once it's already drawn?

Should I update the vertex data with glBufferSubData(I don't think that's efficient), or whould I use glTranslate3f? If I use glTranslate I move all the sprites in the screen.

Any help appreciated, thanks!

• Either way is fine. If you change the matrix (glTranslate), you need to draw each sprite with its own glDraw call. You're right that updating vertex data has a cost... but you should try it and see if it's ok. – david van brink Mar 24 '15 at 4:20

The usual way to do this in openGL 2.1 would be to draw each one like this (pseudocode):

    applyCameraTransformation();

for sprite in sprites:
glPushMatrix();
glTranslate3f(sprite.x, sprite.y, sprite.z);
sprite.draw();
glPopMatrix();


However, notice that you now have many more draw calls, and each one has to send its own 4x4 matrix (16 floats altogether). If you can just update a single VBO, send all of the sprite data at once and then render it with a single draw call, then you would probably find that this is faster. As you add more sprites, this might become unwieldy, but less data sent and fewer calls to the graphics card will probably be faster.

At any rate, it's probably best to do whatever is easiest for you now and speed it up later if you have to. No matter what, drawing a bunch of 2D sprites is going to be pretty fast.

• great, i guess that calling more functions is more effective than just re-sending data to the gpu every frame. Thanks a lot! – twkmz Mar 24 '15 at 5:10
• Well, to know what is more efficient you really need to do both and try them. GPU throughput is very high, and each draw call will send at least 16 floats for the modelview matrix. Just do whatever is easiest for you and worry about performance later if it becomes a problem. – Yourpalal Mar 24 '15 at 20:43

https://gamedev.stackexchange.com/questions/97624/opengl-my-triangle-is-always-the-same-color

they closed your thread, but I understand the question. You asked why the color does not change, you need to send the vertex and color to the vertex shader then from there to the fragment shader, you try to send the vertex to one shader and the color to the other, the color must also go through the vertex shader.

in vec4 Color; in the frag shader is not enough.