PDA

View Full Version : using spritesheets



junioryz
09-23-2009, 07:32 AM
Sorry if i'm repeating this but I do really need anyone to help as much as possible.

If possible could you please take a look at this image/tileset and see if its possible to draw a 2x2 tile map with each of the frames in that image correctly mapped onto the quads in the tile map.

The idea is to go on and use this as a spritesheet or/and tileset, to basically cut out ANY PORTION (or frame) of a single texture.


Thank You

The image : http://img441.imageshack.us/i/tileset512.jpg/

This is all in 2D, the formula i use gets me the frames in the wrong order (draws from BL to TL, then BR to TR) so if anyone can help i'd appreciate it and note i'm NOT ane expert with opengl lol.

So my 2x2 tile map should look EXACTLY as that image!

Also! If by some magical way the texture coordinate system can be modified to start with 0,0 at the top left then i'd be fine as i only get the correct coordinates from the texture starting at the bottom (BL).

GameQ
09-24-2009, 01:05 PM
I think there are multiple questions here, so please bare with me if i don't answer this correctly.

First off it seems like your asking how to cut up that image and render out the portions you want. This will be simply converting the X,Y pixel coordinates of the image into U,V tex coordinates. Say you want to draw a portion from (10,20) -> (30,40) (TopLeft -> BottomLeft) and the image is 100x200. you simply convert to U,V by dividing by the dimensions. (10/100,20/200) -> (30/100,40/200) = (.1,.1) -> (.3,.2). Then you render your quad, and apply the uvs to the right vertex.

Secondly, Is how you actually render a 2D image. You asked how to flip the coordinate system so that (0,0) is the top left of the screen. See this code:



void czEnterOrtho()
{
GLint viewport[4];

//get viewport
glGetIntegerv(GL_VIEWPORT, viewport);

/************************************************** **********************
(0,0,)
------------------------>
| |
| |
| |
| |
| |
V |
-------------------------
(W,H)
************************************************** **********************/
//set and save stats
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(viewport[0], //left (X)
viewport[0]+viewport[2], //right (W)
viewport[1], //bottom (Y)
viewport[1]+viewport[3], //top (H)
-1, 1); //near, far

//+Y goes down
glScalef(1, -1, 1);

//put position marker at TOP/LEFT
glTranslatef(0, -glutGet(GLUT_WINDOW_HEIGHT), 0);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

//turn off lighting
glDisable(GL_LIGHTING);

if(glGetError())
{
printf("error in starting 2D texture\n");
}
}

void czExitOrtho()
{
//pop the saved states
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();

glEnable(GL_LIGHTING);

if(glGetError())
{
printf("error in ending 2D texture\n");
assert(false);
}
}


That code is what I use to put my engine into 2D orthographic mode. It's straight forward if you know the glOrtho() command. The only strange part about it is the glScale() and glTranslate() calls. When I call glOrtho I get (0,0) on the bottom left corner. This is not how I think, I like it in the top/left corner. So the call to glScalef(1,-1,1) actually FLIPS the coordinate system's Y axis. Now +Y goes down the screen. But if I was to render something, it would still go to the bottom left, so I do a translate up (-Y now) to put my system's starting matrix in the top/right corner.

Hope this help, and gets you on your way a little.
~GameQ