Skip to main content
gamedev.net
🔒 Locked

Precise texture positioning

Started by I-C-U-P 1 reply 8.5k views
Original Post
I-C-U-P
I-C-U-P
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?
jdz_simon
jdz_simon
the way positioning the texture precisely as follows:

glBegin(GL_QUADS);

//The tile_size variable is the size in pixels of each tile
//In this case, tile_size is 16.

glTexCoord2f( (l+1)/(float)tile_size, (i+1)/(float)tile_size );
glVertex3f(float((l*tile_size)+tile_size),float((i*tile_size)+tile_size),-1);

glTexCoord2f( (l+0)/(float)tile_size, (i+1)/(float)tile_size );
glVertex3f(float((l*tile_size)),float((i*tile_size)+tile_size),-1);

glTexCoord2f( (l+0)/(float)tile_size, (i+0)/(float)tile_size );
glVertex3f(float((l*tile_size)),float((i*tile_size)),-1);

glTexCoord2f( (l+1)/(float)tile_size, (i+0)/(float)tile_size );
glVertex3f(float((l*tile_size)+tile_size),float((i*tile_size)),-1);

glEnd();
I-C-U-P
I-C-U-P
Thanks. That helped. The tiles are in the right position now, but for some reason I have to flip the texture upside down. I'll probably be able to figure this one out on my own though.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.