Game Development Reference
In-Depth Information
typedef struct stTEXVEC{
float u,v;
}TEXVEC;
typedef struct stPOLYGON{
int numverts;
double normal[3];
double nx, ny, nz;//Rotated normal
int p[4];
TEXVEC tc[4];//texture coordinates
int srf;
}POLYGON;
It is common practice to refer to texture coordinates as ( u , v ), where u
is a number from 0 to 1 that defines a position across the bitmap texture
from 0 to texture width in pixels, and v is a number between 0 and 1 that
defines a position down the bitmap texture from 0 to texture height in
pixels. The first step in creating the texture coordinates involves choosing
the mapping type. Let's look at the principal texturing methods.
Planar mapping
Planar mapping takes an image and projects it parallel down one of the
axes. If the axis is z , then the coordinates are applied with the x -axis
running left to right and the y -axis running up and down. One method of
defining a mapping is to iterate through all the polygons in a mesh that use
the current textured surface and store a vector which defines the bottom,
left, near position, and another vector that stores the top, right, distant
position. The first vector stores all the minimum values for the texture
coordinates and the second the maximum values. The two vectors define
a bounding box for the texture. A third vector is used to define the overall
size by subtracting the minimum values from the maximum.
If we are mapping down the z -axis, then we need to take each vertex in
each polygon in turn and derive a ( u , v ) value for this vertex based on the
x and y values of the vertex. Since we know the overall size of the textured
area, we can scale the current coordinates by subtracting the minimum
vector from the current vertex coordinates and dividing the result by the
texture size vector. With a z -axis mapping we only need to work with the
x and y components of the vectors. With a y -axis mapping we would work
with the x and z components, and an x -axis mapping would use the y and
z components. Here is a code snippet that deals with a single polygon.
You will need to call this function for each polygon.