• # Environment Mapping

Graphics and GPU Programming

[size="5"] Introduction

Ever seen those effects in demos where an object seems to reflect its background? Wish you could do it too? Well, now I'll explain how to do it.

[size="5"]The Original Method

Environment mapping was originally developed by Blinn and Newell. Blinn is one seriously devoted guy! You can't pick up a single book on 3D graphics without finding some of his work in it! You can find the exact workings of his original algorithm in CGPP. Back to the point. Their basic method was to reflect V (viewing vector) about N (normal) to generate a vector which pointed to an environment map. The environment map is basically a big textured sphere, which surrounds the object. It's generated by rendering the scene from different views. Now, as you might guess, to index into the sphere, we will need spherical coordinates. Spherical coordinates are obtained with the following equations:

theta = arctan (y/x).
rho = arccos (z/R).
R = sqrt (x^2 + y^2 + z^2)

If you use unit vectors, you lose R, which is handy. A good variation on this system is to modulate the co-ordinates in the Y axis by a sinewave.

Another method suggests using a cube. Here, you have 6 texture maps, surrounding your object, forming a cube. To chose which texture map to use, you would check the normalized reflection vector: if the X co-ordinate is the greatest, index the texture map to your right. If the Z is greatest, index into the texture map to your front, and so on. Negative co-ordinates mean to index the opposite: eg left instead of right. Again, the problem here is speed. Generating all those environment maps takes time. Not quick enough for real-time applications.

[size="5"] Demo Style Environment Mapping

The way demos do environment mapping is very simple. Take the X and Y components of your vertices, and use that to index your texture map!. Very simple indeed. Your formula would be:

U = N.x*128 + 127 V = N.y*128 + 127 Or in general U = N.x * (width / 2) + ((width / 2) - 1) V = N.y * (height / 2) + ((height / 2) - 1)
Assuming 256x256 texture maps. Some even normalize their vectors to length 128, to avoid the multiplication. Very clever, however can get in the way. I've also heard of people storing them as spherical co-ordinates, meaning only addition/subtraction is needed, when you rotate. I haven't tried this one myself - it sounds too much of a pain in the neck.

A useful side-effect here is that you get your 'phong' highlight texture co-ordinates for free. A useful speedup technique.

Report Article

## User Feedback

There are no comments to display.

## Create an account

Register a new account