View Full Version : texture mapping quad issue

09-17-2009, 02:14 PM
I am trying to use a single texture and take a part (tile) from that texture and map it onto a quad. Months ago I THINK i had this working while using the torque game engine but i dont recall doing anything specific in regards to changing the orientation of the texture or a different math formula .

I have a texture that is split into 4 sections labelled TL (top left), TR(top right), BL (bottom left), and BR (bottom right).

I can individually get each or any part of a texture, but i'm drawing a 2x2 tile map and i want each tile from the top to map each part of this texture correctly. Currently i get BL at the top left, BR at top right, TL at bottom left and TR at bottom right.

I don't use GL alot but it seems this is more to do with the coordinates, in the tile map drawing loop i use this code....

int xo = x * sizex (48width/length);
int x1 = (x +1 )*sizex;
int yo= y*sizey;
int y1=(y+1)*sizey;

float txo= (float)x*1.0/2.0;
float tx1=((float)x+1)*1.0/2.0;
float tyo=(float)y* 1.0/2.0;
float ty1=((float)y+1)*1.0/2.0;


glTexCoord2f(txo,ty1); glVertex2f(xo,yo);
glTexCoord2f(tx1,ty1); glVertex2f(x1, yo);
glTexCoord2f(tx1,tyo); glVertex2f(x1, y1);
glTexCoord2f(txo,tyo); glVertex2f(xo, y1);

If you draw this on a 2d cartesian coordinate, you'd see why its drawing each texture incorrectly, so the main question really is can anyone help me with some sort of math or formula to get the textures correctly.

So in my tile map loop, it should get me TL as the first tile, then BL, then TR, then BL into the 2x2 tile map. I have screens below to clarify a bit more.

http://img196.imageshack.us/i/2x2tilemap.png/ - thats the resulting 2x2 tilemap i get.

http://img441.imageshack.us/i/tileset512.jpg/ - this is how it should come out, basically want to be able to use the texture like a spritesheet or tileset.

Or in other words, when the drawing loop's x and y values are equal to 0,0 it should get me TL or any other ideas as to what i'm trying to achieve (tileset/spritesheet,etc).

Sorry if this is kinda confusing, if you need it cleared up a bit or anything please let me know.