## Sprite Sheets

Member
Posts: 241
Joined: 2008.07
Post: #1
Hey guys, I'm trying to create a system that will utilize sprite sheets and I'm having a tough time. In DirectX, the LPD3DXSPRITE object's draw function has a parameter that takes in a RECT. That rectangle basically crops a portion of the bound texture and draws that rectangular cropping.

Could somebody point me in the right direction so that I can achieve the same thing in OpenGL? Thanks in advance!
Member
Posts: 86
Joined: 2008.04
Post: #2
The texture coordinates control where vertexes are mapped onto the texture.

You can map a quad to a specific rectangular region using appropriate texture coordinates
e.g. this maps to a top left quadrant
glBindTexture (GL_TEXTURE_2D, 13);
glTexCoord2f (0.0, 0.0);
glVertex3f (0.0, 0.0, 0.0);
glTexCoord2f (0.25, 0.0);
glVertex3f (10.0, 0.0, 0.0);
glTexCoord2f (0.25, 0.25);
glVertex3f (10.0, 10.0, 0.0);
glTexCoord2f (0.0, 0.25);
glVertex3f (0.0, 10.0, 0.0);
glEnd ();

The coordinates go from 0 to 1, thus 0.5 is in the middle of the texture.

Hope that helps...
Member
Posts: 241
Joined: 2008.07
Post: #3
Oh nice! I should have thought about that!

I could probably use some math and make a function that will take a CGRect, use those dimensions, and convert to texture coordinates.

Thanks!
Member
Posts: 67
Joined: 2006.07
Post: #4
bmantzey Wrote:I could probably use some math and make a function that will take a CGRect, use those dimensions, and convert to texture coordinates.

That's actually what I do for my mapping function, and it is quite convenient. I hightly recommend it.

Granted, I use rectangular textures (and therefore use pixel coordinates for the vertices), so it's a little easier for me, but I'm sure that converting it to normalized coordinates for GL_TEXTURE_2D would be trivial.

Since when was "Fred" a placeholder variable?
Member
Posts: 241
Joined: 2008.07
Post: #5
I was just experimenting with that actually. Since PNG's are loaded upside down in OpenGL, U and V will be swapped and it's a little bit confusing.