Environment Mapping
Describes how to make objects in your game reflect the environment around them.
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)A useful side-effect here is that you get your 'phong' highlight texture co-ordinates for free. A useful speedup technique.
Related Tutorials
Procedural Generation of 2D Tile Maps in Games
The article explains how to build a procedural 2D cave map generator using cellular automata. It covers map representat…
Localizing A Game Into French — Which Variant Should You Choose?
So, you’re making an awesome indie game, and now you’re thinking about localizing it into French? Great idea! There ar…
How Long Does It Take To Localize An Indie Game?
So you’re planning on localizing your indie game, but you’re not sure how much time to schedule in. While the timing o…
Creating game videos: best practices and pitfalls to avoid
Game video production: practical tips on how to create a game trailer or teaser that you can be proud of. Give your aud…
Adopting CI/CD: How Midwinter Entertainment iterates at speed with the help of IMS
Game development was once like one long sprint: a huge effort until you reached the finish line — at which point you co…
GameDev.net
5 Things to Consider When Making a Video to Promote Your App or Game
How can you show an app or game in your video in a way that attracts new users? Let’s take a look at what to go by when…
Discussion
More from Tom Hammersley
Comparing Shadow Mapping Techniques with Shadow Explorer
New Incentives and a Whole New Platform From The Intel AppUp developer program
The final installment in this series on Intel's AppUp program details additional incentives and opportunities for devel…
The Game Maker
This book excerpt contains the first chapter of the book that introduces beginning game developers to the Game Maker pr…
Discussion