Original Post
I'm an OpenGL noob, so go easy on me. I am trying to build a 2d tile engine. For testing, I'm using this 16x16 Mario tileset that I found online. I have my tile editor that can export tile maps in my own file format. I can load the maps and draw the polygons on the screen just fine, but I cant seem to get the texture positioned right. Here is some of the code that draws the tiles.
void CTileMap::draw()
{
glBindTexture(GL_TEXTURE_2D,texture);
GLuint d=0; //used to keep track of the tile data
glScalef(0.02f,0.02f,0.02f); //shrink it some
//tile_vcount and tile_hcount store the number of tiles to draw
//horizontally and vertically.
for (GLuint i=0; i<tile_vcount; i++)
{
for (GLuint l=0; l<tile_hcount; l++)
{
//tile_data is an array that tells where each tile is located on
//the texture. Every two indices store the x and y tile offsets on
//the texture. 0,0 means no tile, 1,1 would mean the first
//tile in the top left corner, 2,1 would mean the second tile on the
//first row, ect.
if (tile_data[ d ]!=0) //If there is a tile here
{
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
//I have messed around with positioning the texture here, but
//I cant seem to get it in the right position. This is the
//area I need help with.
glMatrixMode(GL_MODELVIEW);
glBegin(GL_QUADS);
//The tile_size variable is the size in pixels of each tile
//In this case, tile_size is 16.
glTexCoord2f( 1, 1 );
glVertex3f(float((l*tile_size)+tile_size),float((i*tile_size)+tile_size),-1);
glTexCoord2f( 0, 1);
glVertex3f(float((l*tile_size)),float((i*tile_size)+tile_size),-1);
glTexCoord2f( 0, 0 );
glVertex3f(float((l*tile_size)),float((i*tile_size)),-1);
glTexCoord2f( 1, 0 );
glVertex3f(float((l*tile_size)+tile_size),float((i*tile_size)),-1);
glEnd();
}
d+=2;
}
l=0;
}
}
Can someone here help me out?