Game Development Reference
In-Depth Information
Figure 8-10. Bounding shapes around Bob in model space
This process may seem a little cumbersome, but the steps involved are not all that hard. The first
thing we have to remember is how texture mapping works. We specify the texture coordinates
for each vertex of Bob's rectangle (which is composed of two triangles) in texture space. The
upper-left corner of the texture image in texture space is at (0,0), and the lower-left corner is
at (1,1), no matter the actual width and height of the image in pixels. To convert from the pixel
space of our image to texture space, we can use this simple transformation:
u
=
x / imageWidth
v
=
y / imageHeight
where u and v are the texture coordinates of the pixel given by x and y in image space. The
imageWidth and imageHeight are set to the image's dimensions in pixels (32×32 in Bob's case).
Figure 8-11 shows how the center of Bob's image maps to texture space.
Figure 8-11. Mapping a pixel from image space to texture space
The texture is applied to a rectangle that you define in model space. In the example in Figure 8-10 ,
the upper-left corner is at (-1,1) and the lower-right corner is at (1,-1). We can use meters as the
units in our world, so the rectangle has a width and height of 2 m. Additionally, we know that
the upper-left corner has the texture coordinates (0,0) and the lower-right corner has the texture
coordinates (1,1), so we can map the complete texture to Bob. This won't always be the case, as
you'll see later in the texture atlas section.
Now we need a generic way to map from texture space to model space. We can make our life
a little easier by constraining our mapping to only axis-aligned rectangles in texture space and
model space. Assume that an axis-aligned rectangular region in texture space is mapped to
an axis-aligned rectangle in model space. For the transformation, we need to know the width
and height of the rectangle in model space and the width and height of the rectangle in texture
space. In our Bob example, we have a 2×2 rectangle in model space and a 1×1 rectangle in
Search WWH ::




Custom Search