Advertisement Jump to content
Sign in to follow this  

Setting Texture coordinate to render part of image

This topic is 2658 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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:

& the code for my tile-map rendering:

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


// 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);


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

& here is a screenshot of what i currently get:

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
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!