Presentation is loading. Please wait.

Presentation is loading. Please wait.

More on Environment Mapping Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Wednesday, December 10, 2003.

Similar presentations


Presentation on theme: "More on Environment Mapping Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Wednesday, December 10, 2003."— Presentation transcript:

1 More on Environment Mapping Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 381 Lecture Notes Wednesday, December 10, 2003

2 10 Dec 2003CS 3812 Review: More on Procedural Texture [1/2] One way to convert a noise function into an image: Choose colors for specified values in [–1,1]. Then determine the color associated with a value by lirping between the two nearest specified values. Create the image by mapping noise values to colors. –1.01.0–0.30.4 –1.01.0–0.30.4–0.1

3 10 Dec 2003CS 3813 Review: More on Procedural Texture [2/2] Code for color generation will often look something like this: // mapcolor - map value in [-1,1] to an RGB color. void mapcolor(double value, GLubyte color[3]) { if (value < -0.3) { t = (value – (-1.)) / ((-0.3) – (-1.)); color[0] = (1.-t)*1.0*255 + t*0.8*255; // lirp between white (1.0, 1.0, 1.0) color[1] = (1.-t)*1.0*255 + t*0.6*255; // and brown (0.8, 0.6, 0.4) color[2] = (1.-t)*1.0*255 + t*0.4*255; } else if (value < 0.4) { t = (value – (-0.3)) / ((0.4) – (-0.3)); color[0] = (1.-t)*0.8*255 + t*0.9*255; // lirp between brown (0.8, 0.6, 0.4) color[1] = (1.-t)*0.6*255 + t*0.1*255; // and magenta (0.9, 0.1, 0.9) color[2] = (1.-t)*0.4*255 + t*0.9*255; } else …

4 10 Dec 2003CS 3814 Review: Environment Mapping [1/4] In environment mapping, we simulate mirror-like reflection using texturing. Generate texture coordinates based on the direction that light, originating from the viewer, would reflect off the surface. Need: Viewing location (always (0,0,0) in OpenGL). Vertex coordinates (transformed by model/view). Surface normal (transformed by model/view).

5 10 Dec 2003CS 3815 Review: Environment Mapping [2/4] A simple environment-mapping technique is to use a sphere map. Take the reflected light direction, add 1 to z, normalize. Use the resulting x, y as texture coordinates. The texture is a fish-eye-lens picture of the environment. E.g., see plate 21 in the red book.

6 10 Dec 2003CS 3816 Review: Environment Mapping [3/4] Chrome mapping is a type of environment mapping in which we do not attempt to produce a realistic picture of the environment. Just make mirror-like reflections of something. Then objects look metallic, regardless of the incorrect reflections. Note: Some people would not call chrome mapping a type of environment mapping. I would. In particular, it is legal on assignment #11.

7 10 Dec 2003CS 3817 Review: Environment Mapping [4/4] OpenGL includes automatic texture-coordinate generation. One of the options does a sphere map. In initialization (or whereever): glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); To enable: glEnable(GL_TEXTURE_GEN_S); glEnable(GL_TEXTURE_GEN_T); Disabling is as usual. Normals must be specified. glTexCoord * is not necessary. In particular, all the glutSolid … functions work!

8 10 Dec 2003CS 3818 More on Environment Mapping: Introduction We have seen how to create mirror-like reflections cheaply using a texture. Disadvantages No object-object reflections. The reflected scene is considered to lie at infinity. Often get errors at the edge of the silhouette. With sphere mapping, anyway. Creating the texture can be tricky. We look at this now.

9 10 Dec 2003CS 3819 More on Environment Mapping: Creating the Texture [1/4] The hard part of environment mapping is creating the texture itself. When to Create A sphere-map texture depends on: The environment. The viewing position. Thus, the texture needs to be recomputed whenever these change. It does not need to be computed when an environment-mapped object moves. Another way to think about it: Environment mapping is good for a stationary viewer viewing moving objects in a static environment. Next: How to Create

10 10 Dec 2003CS 38110 More on Environment Mapping: Creating the Texture [2/4] In environment mapping, given a reflected-ray direction in eye coordinates, texture coordinates are computed. To make the texture do this backwards: Look at each texel in turn. For each, determine what reflected-ray direction it corresponds to. Use the color of the environment, seen in that direction, for the color of the texel. Procedure for reflected-ray determination: Given texture coordinates a, b. Scale a, b so they are in [–1,1]. Find c  0 so that (a, b, c) is a unit vector. Reflected-ray direction is (2ca, 2cb, 2c 2 –1).

11 10 Dec 2003CS 38111 More on Environment Mapping: Creating the Texture [3/4] We want to write some code to generate the sphere-map texture. Suppose that the image array is declared as usual: const int img_width = … ; const int img_height = … ; GLubyte the_image[img_height][img_width][3]; Now suppose we have a function void envgetcolor(double x, double y, double z, GLubyte color[3]) Given a unit vector (x, y, z). Returns a color in the given array (RGB are 0..255). Color is the color of the environment, see when looking in direction (x, y, z).

12 10 Dec 2003CS 38112 More on Environment Mapping: Creating the Texture [4/4] Then here is code to make the sphere-map texture: void envmakeimage() { for (int i=0; i<img_width; ++i) for (int j=0; j<img_height; ++j) { double a = 2.*i/img_width-1.; double b = 2.*j/img_height-1.; double k = 1.-a*a-b*b; double c = ((k <= 0.) ? 0. : sqrt(k)); double x = 2.*c*a; double y = 2.*c*b; double z = 2.*c*c-1.; envgetcolor(x, y, z, the_image[j][i]); }

13 10 Dec 2003CS 38113 More on Environment Mapping: The Environment Looks Like … So, how do we determine the color of the environment, seen in a particular direction? Answer 1: Render the environment in a buffer, and read this to determine colors. Tricky … Answer 2: Ray tracing. Yes, this is slow, but it only needs to be done when the texture is computed. Answer 3: Do it backwards. Decide: The color in this direction is … Then draw the environment as a large sphere, colored according to the above decisions.

14 10 Dec 2003CS 38114 More on Environment Mapping: EXAMPLE We put together an example environment- mapped object (which actually reflected its environment) using the following pieces from envmapfuncs.cpp (on the web page): Function envgetcolor was written to make an interesting background. Function envmakeimage created a sphere-map texture using envgetcolor. Function envdrawsphere drew the environment: a sphere colored using envgetcolor. The sphere was drawn with a large radius, to simulate an environment at infinity.

15 10 Dec 2003CS 38115 More on Environment Mapping: Some Hints When using environment mapping: Disable lighting for environment-mapped objects. Environment mapping is lighting … sort of. Other objects may still need lighting. Perfect reflectors are unrealistic; do not draw them. Set GL_MODULATE mode, and give your reflecting objects a color other than full white. Color (0.9, 0.9, 0.9) is not bad for silvery metal. Lower B (and maybe lower G) gives gold/coppery colors. Make your environment-mapped objects move. This adds realism. It is also cheap, since the texture does not need to be recomputed.

16 10 Dec 2003CS 38116 More on Environment Mapping: Cube Maps We have discussed how to store a picture of the environment as a sphere map. Another way to store it is as a cube map. Instead of a single, warped texture (as in a sphere map), we use 6 textures, forming the sides of a cube. The 6 textures form a picture of the environment, as seen from the center of the cube. Computations are simpler. Generating the images is much easier. Not supported by standard OpenGL.  Although it is available in extensions.


Download ppt "More on Environment Mapping Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Wednesday, December 10, 2003."

Similar presentations


Ads by Google