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
");
}
}
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
");
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