Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 10 down vote accepted

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)

share|improve this answer
    
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. –  uggwar 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? –  uggwar Mar 4 '11 at 9:39
    
@uggwar Instanced drawing? –  Notabene Mar 4 '11 at 9:43
    
Wow, I have a lot to learn :-) Thanks again! –  uggwar Mar 4 '11 at 10:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.