Game Development Reference
In-Depth Information
texture space (since we map the complete texture to the rectangle). We also need to know the
coordinates of the upper-left corner of each rectangle in its respective space. For the model
space rectangle, that's (-1,1); for the texture space rectangle, it's (0,0) (again, since we map the
complete texture, not just a portion). With this information, and the u and v coordinates of the
pixel we want to map to model space, we can do the transformation with these two equations:
(
) ( )
mx
=−
u minU /
tWidth
×
mWidth minX
+
(
)
(
) ( )
my
=− −
(1
v minV /
tHeight
×
mHeight minY
The variables u and v are the coordinates calculated in the previous transformation from pixel
space to texture space. The variables minU and minV are the coordinates of the top-left corner
of the region you map from texture space. The variables tWidth and tHeight are the width
and height of your texture space region. The variables mWidth and mHeight are the width and
height of your model space rectangle. The variables minX and minY are—you guessed it—the
coordinates of the top-left corner of the rectangle in model space. Finally, mx and my are the
transformed coordinates in model space.
These equations take the u and v coordinates, map them to the range 0 to 1, and then scale and
position them in model space. Figure 8-12 shows a texel in texture space and how it is mapped
to a rectangle in model space. On the sides, you see tWidth and tHeight , and mWidth and
mHeight . The top-left corner of each rectangle corresponds to ( minU , minV) in texture space and
( minX , minY) in model space.
Figure 8-12. Mapping from texture space to model space
Substituting the first two equations, we can go directly from pixel space to model space:
(
) ( )
(
)
*
mx
=
x / imageWidth
minU /
tWidth mWidth minX
+
(
(
) ( )
)
(
)
*
my
=−
(1
y / imageHeight
minV /
tHeight
mH
eight minY
We can use these two equations to calculate the bounding shapes of our objects based on the
image we map to their rectangles via texture mapping. In the case of the triangle mesh, this can
get a little tedious; the bounding rectangle and bounding circle cases are a lot easier. Usually,
you won't need to take this hard route, but rather create your textures so that the bounding
rectangles at least have the same aspect ratio as the rectangle you render for the object via
OpenGL ES. This way, you can construct the bounding rectangle from the object's image
dimension directly. The same is true for the bounding circle.
Search WWH ::




Custom Search