Sign in to follow this  
dan369

Setting Texture coordinate to render part of image

Recommended Posts

Hi, me again :P.
Basically, i'm just a bit confused on why my tile-map which i'm rendering isn't working correctly, and i'm pretty sure that i might be my texture coordinates. In my head, they look fine but i must be missing something in the formula.
My tile-sheet:
[url="http://imageshack.us/photo/my-images/202/tilesheet2.png/"]http://imageshack.us...tilesheet2.png/[/url]

& the code for my tile-map rendering:
[code]

void DrawMap(int x, int y, int sourceX, int sourceY, int imageWidth, int imageHeight, GLuint texture)
{
glBindTexture(GL_TEXTURE_2D, texture);

glBegin(GL_QUADS);

// Top-left vertex (corner)
glTexCoord2i( sourceX / tileWidth, 0);
glVertex2i(x, y);

// Bottom-left vertex (corner)
glTexCoord2i( sourceX / tileWidth, 1);
glVertex2i( x + imageWidth, y);

// Bottom-right vertex (corner)
glTexCoord2i((sourceX+1) / tileWidth, 1);
glVertex2i( x + imageWidth, y + imageHeight);

// Top-right vertex (corner)
glTexCoord2i((sourceX+1) / tileWidth, 0);
glVertex2i(x, y + imageHeight);

glEnd();
glLoadIdentity();
}


void DrawTileMap(Map& cMap)
{
for (int n = 0; n <= cMap.NumOfLayers-1; n++)
{
for (int y = 0; y < cMap.HeightOfMap; y++)
{
for (int x = 0; x < cMap.WidthOfMap; x++)
{
int tile = cMap.layers[n].layer[y][x];

if (tile > 0)
{
DrawMap(x * tileWidth, y * tileHeight, tile % numberOfTiles, tile / numberOfTiles, tileWidth, tileHeight, tileSheet);
}
}
}
}
}
[/code]


& here is a screenshot of what i currently get:
[url="http://imageshack.us/photo/my-images/534/issue.png/"]http://imageshack.us.../534/issue.png/[/url]

What i should be getting (were the sort of gold tiles are), are a green tile (tile 1) & a red tile (tile 2). I'm pretty sure that my texture coordinates must be wrong, but can anyway point me in the right direction of what i should be doing?

Thanks for any help :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this