Sprite Sheets - Printable Version
+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: Sprite Sheets (/thread-2182.html)
Sprite Sheets - bmantzey - Nov 7, 2008 10:00 AM
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!
Sprite Sheets - OptimisticMonkey - Nov 7, 2008 10:27 AM
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);
The coordinates go from 0 to 1, thus 0.5 is in the middle of the texture.
Hope that helps...
Sprite Sheets - bmantzey - Nov 7, 2008 10:52 AM
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.
Sprite Sheets - Wowbagger - Nov 7, 2008 11:49 AM
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.
Sprite Sheets - bmantzey - Nov 7, 2008 12:34 PM
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.