We are talking 2D sprites here, so I have 4 vertices for each sprite (8 GLint‘s) and 4 texcoords (another 8 GLint‘s). I have a sorting routine which spits out lists of sprites that can be rendered in one pass (they have the same blending and same texture, blablabla). However, each sprite also has a translation, rotation, scale, etc.
I currently do this (pseudocode):
cdef int *vertices
cdef int *texcoords
bind_texture(texture)
set_blending_mode_and_other_blablabla(spritelist)
vertices = alloc_mem(len(sprites) * 8 * sizeof(GLint))
texcoords = alloc_mem(len(sprites) * 8 * sizeof(GLint))
glEnableClientState(GL_VERTEX_ARRAY)
glEnableClientState(GL_TEXTURE_COORD_ARRAY)
glTexCoordPointer(2, GL_INT, 0, texcoords)
glVertexPointer(2, GL_INT, 0, vertices)
load_vertices(sprites, vertices)
load_texcoords(sprites, texcoords)
for index, sprite in spritelist:
glPushMatrix()
glColor4f(sprite.red, sprite.green, sprite.blue, sprite.alpha)
glTranslatef(int(sprite.x), int(sprite.y), 0.0)
glRotatef(sprite.rotation, 0.0, 0.0, 1.0)
glScalef(sprite.scale_x, sprite.scale_y, 1.0)
glTranslatef(-int(sprite.anchor_x), -int(sprite.anchor_y), 0.0)
glDrawArrays(GL_QUADS, 4 * i, 4)
glPopMatrix()
Now, as you can guess, this isn’t terribly fast. I’d like to draw everything in one pass. I really have no idea how to pass the translation, rotation, etc data into OpenGL. If someone could point out an efficient render path or two that would be very nice.
The exact per-sprite unique data is:
- Color (red, green, blue, alpha)
- Scale (x and y factor)
- Translation (x and y)
- Rotation
- Texcoords (8
GLint‘s) - Vertices (8
GLint‘s)
Please be gentle, I’m new to OpenGL.
Stick your sprite info in a vertex attribute(s) and apply them in your vertex shader.
Alternatively you can move the scaling, translation, and rotation computation onto the CPU and just pass off the pre-transformed vertex buffer off to the GPU each frame. This will cut way down on GL API calls, at the cost of increased CPU usage. I’d recommend Eigen or
glmfor the heavy matrix lifting.EDIT: Shader solution: