Jump to content
  • Advertisement
Sign in to follow this  
dan369

Setting Texture coordinate to render part of image

This topic is 2471 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:
http://imageshack.us...tilesheet2.png/

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

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



& here is a screenshot of what i currently get:
http://imageshack.us.../534/issue.png/

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!