Jump to content
  • Advertisement
Sign in to follow this  
ProgrammerZ

OpenGL Drawing dynamic sprites with OpenGL ES 1.1

This topic is 2754 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to make a simple 2D sprite engine with OpenGL ES 1.1 on the iPhone. And, I'm feeling kind of stuck. From what I've read, the best way to draw 2D sprites in OpenGL ES is to draw textured quads (or pairs of triangles) in an orthographic projection. I've come across sample code like this:


glColor4f(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

static const Vertex3D vertices[] = {
{-1.0, 1.0, -0.0},
{ 1.0, 1.0, -0.0},
{-1.0, -1.0, -0.0},
{ 1.0, -1.0, -0.0}
};
static const Vector3D normals[] = {
{0.0, 0.0, 1.0},
{0.0, 0.0, 1.0},
{0.0, 0.0, 1.0},
{0.0, 0.0, 1.0}
};
static const GLfloat texCoords[] = {
0.0, 1.0,
1.0, 1.0,
0.0, 0.0,
1.0, 0.0
};

glLoadIdentity();
glTranslatef(x, y, z);
glRotatef(rot, 1.0, 1.0, 1.0);

glBindTexture(GL_TEXTURE_2D, fooTexture[0]);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glNormalPointer(GL_FLOAT, 0, normals);
glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);



This works well for drawing a single sprite. But what do I do when I want to draw multiple dynamic sprites on the screen at a time? I'm planning on rebuilding my sprite "geometry" from scratch each frame, so storing all the geometry and just moving it around each frame wouldn't work for me.
So should I just call the above code each time I need to draw a sprite? I've heard that calls to glDrawArrays() and glDrawElements() should be minimized as much as possible. However, I've also heard that geometry can sometimes be batched and sent all with one call. However, I've also heard that you have to use a separate glDraw*() call when you switch textures or other OpenGL state stuff, so I don't know how much batching I could actually do.
On a side note, should I be looking into display lists or VBOs? I've heard that they're sometimes good alternatives to vertex arrays, but I don't know if they'd be helpful to me or not.

Thanks for reading, and any help would be much appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Sounds like just continuing with vertex arrays may be the way to go for you. Display lists are old and deprecated, so I'd just forget about them (are they even in opengl es? I'm not sure).

VBOs are another alternative, but they don't really offer an advantage when you're streaming new geometry each frame. VBOs are awesome when you're drawing static 50k-polygon meshes. For simple quads with dynamic coordinates, not as much.

Yes it's always good to minimize your draw calls, but that doesn't mean you have to reduce it to only one call, just find the median that makes the most sense. For a sprite engine, I would consider just batching all the sprites that share the same texture and render state in a single vertex array. Even that might not be worth the cost of rearranging all the vertices into a contiguous array each frame, assuming that they are non-contiguous to start with.

I'd just go with VAs for now, you can consider some tweaks if you're not satisfied with the performance. Its hard to believe that just drawing sprites is going to put much strain on a gpu anyway.

Share this post


Link to post
Share on other sites
You could always transform the vertices for your quads on the CPU and then store it all in one VA and render that each frame.

However, this just moves a lot of computation from the GPU to the CPU. However in OpenGL ES and the iPhone I don't know which would be better - it might be worth trying it and seeing. 2D transformation is a doddle.

Typically speaking minimising draw calls shouldn't be done at the expense of increased CPU calls.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!