10
\$\begingroup\$

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?

\$\endgroup\$

1 Answer 1

12
\$\begingroup\$

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.

spritesheet uvs

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

Edit: Added picture
(source for spritesheet i used: funorb.com)

\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$
    – uggwar
    Jan 31, 2011 at 5:37
  • \$\begingroup\$ 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? \$\endgroup\$
    – uggwar
    Mar 4, 2011 at 9:39
  • \$\begingroup\$ @uggwar Instanced drawing? \$\endgroup\$
    – Notabene
    Mar 4, 2011 at 9:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .