# Texture Coordinate Calculation

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

## 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) { /* CALCULATE TEXTURE COORDINATES */ 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); glBegin(GL_QUADS); glTexCoord2i(TexCoord1.X,TexCoord1.Y); 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); glEnd(); } 
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 on other sites
Use glTexCoord2f, not glTexCoord2i. The i means integer, so it casts the float to 0 or 1.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
633710
• Total Posts
3013486
×