• Advertisement
Sign in to follow this  

Texture Coordinate Calculation

This topic is 2618 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

I have calculated the correct texture coordinates for sprite sheets before so i thought calculating them for a random box would be easy...

This is my attempt at trying to calculate texture coordinates given the visible rectangle of the texture(i hope that made sense)

void DrawQuad2D(Box2D coordinates, GLuint texture, Box2D visible_box)
Vert2D TexCoord1, TexCoord2, TexCoord3, TexCoord4;
float TexCoordWidth, TexCoordHeight, TexCoordStartX, TexCoordStartY, Temp1;

TexCoordWidth = (1.0/coordinates.W)*visible_box.W;
TexCoordHeight = (1.0/coordinates.H)*visible_box.H;

Temp1 = (1.0/coordinates.W)*visible_box.X;
TexCoordStartX = Temp1;

Temp1 = (1.0/coordinates.H)*visible_box.Y;
TexCoordStartY = Temp1;

TexCoord1.X = TexCoordStartX;
TexCoord1.Y = TexCoordStartY+TexCoordHeight;

TexCoord2.X = TexCoordStartX+TexCoordWidth;
TexCoord2.Y = TexCoordStartY+TexCoordHeight;

TexCoord3.X = TexCoordStartX+TexCoordWidth;
TexCoord3.Y = TexCoordStartY;

TexCoord4.X = TexCoordStartX;
TexCoord4.Y = TexCoordStartY;


This is how im drawing it:


glBindTexture(GL_TEXTURE_2D, texture);

glVertex2f( coordinates.X, coordinates.Y );

glTexCoord2i(TexCoord2.X, TexCoord2.Y);
glVertex2f( coordinates.X, coordinates.Y + coordinates.H);

glTexCoord2i(TexCoord3.X, TexCoord3.Y);
glVertex2f( coordinates.X + coordinates.W, coordinates.Y + coordinates.H );

glTexCoord2i(TexCoord4.X, TexCoord4.Y);
glVertex2f( coordinates.X + coordinates.W, coordinates.Y);


Box2D is just a structure that represents a 2d box, it has 4 floats, W, H, X, Y.

The whole function calculates and draws the textured quad.

I hope you understand what im trying to do. At the moment my code is not drawing anything. when the coordinate and visible_box paramaters are the same.

Share this post

Link to post
Share on other sites
Use glTexCoord2f, not glTexCoord2i. The i means integer, so it casts the float to 0 or 1.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement