# Sprite sheets with buffer objects and shaders

What is the "best" way to handle sprite sheets with shaders and buffers? In immediate mode it is easy to modify a sprite's texcoords when it is drawn, but I don't know how to do this with modern opengl. Should I store all the texcoords in a buffer and change the pointer for every sprite, or is there a better way?

You can always make looong buffer "vertex;texcoord" repeat. And just use indices how you said. It is propably most easy and kinda similar to what you know.

If you want to save some memory. There is pretty neat solution and it is to have one sprite in buffer with unite texcoords and create UVs transformation matrix and send it to your shader and multiply texcoords with it (in vertex shader).

You dont have to create matrices by yourself if you are not fine with that. You can just use regular GL functions and than read matrix with glGetFloatvfunction. Like this:

glGetFloatv (GL_MODELVIEW_MATRIX ,&g_TexCoordMat);


I created image to describe it better. glTranslate ang glScale can be looked as that they are just moving with the UV space basis.

If you don't know how to send matrix to shader i can share some code with you.

(source for spritesheet i used: funorb.com)

• Wow, thanks! Perfect answer. I really like this solution and will be looking into it a soon as I am back from work. :-) If I get stuck, I'll bother you with some more questions. Jan 31 '11 at 5:37
• This will, as far as I can see, generate one draw call for each sprite. Is there a way to batch these sprites for a smoother implementation? Mar 4 '11 at 9:39
• @uggwar Instanced drawing? Mar 4 '11 at 9:43