Texture Coordinate Calculation

Started by
0 comments, last by karwosts 13 years, 3 months ago
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.
Advertisement
Use glTexCoord2f, not glTexCoord2i. The i means integer, so it casts the float to 0 or 1.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game

This topic is closed to new replies.

Advertisement