Jump to content
  • Advertisement
Sign in to follow this  
jdindia

Batching 2d sprites

This topic is 3545 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 currently doing:
sort by texture
for each sprite
{
  if texture is new
    glBind()
  glPushMatrix()
  glTranslate()
  glRotate()
  glBegin(GL_QUADS)
  glTexCoord()
  glVertex()
  ...
  glEnd()
  glPopMatrix()
}
It seems like I ought to be able to improve this, but it's not obvious exactly how to do so. Display lists, vertex arrays, vertex buffers? Animation is done by changing the texture coordinates, and each sprite is a different size so it's hard to factor to a generic rectangle. I could use scaling and the texture matrix to address that.
sort by texture
for each sprite
{
  if texture is new
    glBind()
  glPushMatrix()
  <set texture matrix> // a few lines of code here
  glTranslate()
  glRotate()
  glScale()
  display list generic unit quad
  glPopMatrix()
}
Is that even worthwhile? What about using vertex arrays? (my memory of the exact syntax isn't perfect)
vertex[]
texCoord[]
sort by texture
for each sprite
{
  if texture is new
    glBind()
  glPushMatrix()
  glTranslate()
  glRotate()
  vertex[] = sprite.vertex;
  texCoord[] = sprite.texCoord;
  glDrawArray ( GL_QUADS, vertex, texcoord )
  glPopMatrix()
}
Do I want to be copying vertex/texcoord data to one local vertex/texcoord buffer, or do I want to reset the vertex/texcoord pointer to my sprite's local buffer? It would be nice to push the Begin()/End() outside the loop, but translate and rotate calls make that impossible. Worth making a shader for something like this or is the performance gain negligible (how does translate/rotate info pass to the vertex shader without having to be passed per vertex?)? I actually have polygons as well as quads, but I assume the discussion is very similar.

Share this post


Link to post
Share on other sites
Advertisement
all methods would run about the same speed, immediate may even be the quickest.

the first thing is how many quads? anything less than 10,000 then its prolly not worth bothering about.

personally Ild also ditch the
glPushMatrix()
glTranslate()
glRotate() etc stuff + just do the calculations yourslef on the cpu + pass the vertices to the gpu that way

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!