0
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – david van brink Mar 24 '15 at 4:20
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ great, i guess that calling more functions is more effective than just re-sending data to the gpu every frame. Thanks a lot! \$\endgroup\$ – twkmz Mar 24 '15 at 5:10
  • \$\begingroup\$ 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. \$\endgroup\$ – Yourpalal Mar 24 '15 at 20:43
0
\$\begingroup\$

ochi12 you asked this question

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.

add

in vec4 Color; to the vertex shader out vec4 frag_Color;

and then use in vec4 frag_Color; in the frag shader.

Or that worked for me.

Or that worked for me to get shader working for first time, I didn't see a way to PM you, so posting this in this other thread of yours.

\$\endgroup\$
  • \$\begingroup\$ Thanks a lot, I'll give it a try. However, I don't know if I can use "in" and "out" qualifier in glsl 1.2. \$\endgroup\$ – twkmz Apr 2 '15 at 16:27
  • \$\begingroup\$ I added a uniform to the vertex shader and passed it to the fragment shader with "varying" but it didn't work :p \$\endgroup\$ – twkmz Apr 2 '15 at 18:02
  • \$\begingroup\$ Well I could be wrong, I was presuming you wanted to send a stream of colors in (1 for each vertex), why you would pass through the vertex shader a color for every vertex like the vec is passed in?. Are you getting that uniform location correctly, is getting the uniform location returning -1(error)? \$\endgroup\$ – Robert Eastwood Apr 4 '15 at 18:48
  • \$\begingroup\$ I asked another question which wasn't closed.stackoverflow.com/questions/29420369/… , the code is a bit modified to make it easier, and no, glgetuniformlocation isn't returning -1 \$\endgroup\$ – twkmz Apr 4 '15 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.