Batching 2d sprites

Started by
0 comments, last by zedz 15 years, 3 months ago
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.
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

This topic is closed to new replies.

Advertisement