Lecture 11 Environment mapping, Mirroring

39
Computer Graphics Lecture 11 Environment mapping, Mirroring

Transcript of Lecture 11 Environment mapping, Mirroring

Page 1: Lecture 11 Environment mapping, Mirroring

Computer Graphics

Lecture 11Environment mapping, Mirroring

Page 2: Lecture 11 Environment mapping, Mirroring

Today• Environment Mapping

o Introductiono Cubic mappingo Sphere mappingo refractive mapping

• Mirroringo Introductiono reflection firsto stencil buffero reflection last

Page 3: Lecture 11 Environment mapping, Mirroring

Environment Mapping• Simple yet powerful method to generate reflections • Simulate reflections by using the reflection vector to

index a texture map at "infinity".

The original environment map was a sphere [by Jim Blinn ’76]

Page 4: Lecture 11 Environment mapping, Mirroring

Example

Terminator II

Page 5: Lecture 11 Environment mapping, Mirroring

Cubic Mapping• The most popular method• The map resides on the

surfaces of a cube around the objecto align the faces of the cube

with the coordinate axes

Page 6: Lecture 11 Environment mapping, Mirroring

Procedure

1. During the rasterization, for every pixel, 2. Calculate the reflection vector R using the camera (incident)

vector and the normal vector of the object N3. Select the face of the environment map and the pixel on the

face according to R4. Colour the pixel with the colour of the environment map

Look up the environment map just using RDo not take into account the 3D position ofthe reflection point

Page 7: Lecture 11 Environment mapping, Mirroring

Cubic Mapping• To generate the map:

Using methods such as ray tracingo Or, take 6 photos of a real environment with

a camera in the object’s position : much easier

Page 8: Lecture 11 Environment mapping, Mirroring

Made from the Forum Images

Page 9: Lecture 11 Environment mapping, Mirroring

Calculating the reflection vector

• Normal vector of the surface : N• Incident Ray : I• Reflection Ray: R • N,I,R all normalized        R = I -2 N ( N . I )• The texture coordinate is based

on the reflection vector• Assuming the origin of the

vector is always in the center of the cube environment map

Page 10: Lecture 11 Environment mapping, Mirroring

Indexing Cubic Maps• Assume you have R and the

cube’s faces are aligned with the coordinate axes

• How do you decide which face to use?

The reflection vector coordinate with the largest magnitude

R=(0.3, 0.2, 0.8) -> face in +z direction

Page 11: Lecture 11 Environment mapping, Mirroring

Indexing Cubic Maps• How do you decide which texture coordinates to use?

o Divide by the coordinate with the largest magnitudeo Now ranging [-1,1]o Remapped to a value between 0 and 1.

(0.3,0.2,0.8) --> ((0.3/0.8 +1)*0.5, ((0.2/0.8 +1)*0.5) = (0.6875, 0.625)

Page 12: Lecture 11 Environment mapping, Mirroring

What are the potential problems? How is it different from rendering the scene more accurately?

What will we miss by environment mapping?

Page 13: Lecture 11 Environment mapping, Mirroring

A Sphere Map

• A mapping between the reflection vector and a circular texture

• Prepare an image/texture in which the environment is mapped onto a sphere

Page 14: Lecture 11 Environment mapping, Mirroring

Sphere Mapping• To generate the map:

o Take a photograph of a shiny sphere o Mapping a cubic environment map onto a

sphereo For synthetic scenes, you can use ray tracing

Page 15: Lecture 11 Environment mapping, Mirroring

Sphere Map : Procedure

• Compute the reflection vector at the surface of the object

• Find the corresponding texture on the sphere map

• Use the texture to color the surface of the object

Page 16: Lecture 11 Environment mapping, Mirroring

Indexing Sphere Maps• Given the reflection vector R (Rx,Ry,Rz)

• (u,v) on the spherical map

Page 17: Lecture 11 Environment mapping, Mirroring

Indexing Sphere Maps

Page 18: Lecture 11 Environment mapping, Mirroring

Non-linear Mapping• Problems:

o Highly non-uniform samplingo Highly non-linear mapping

• Linear interpolation of texture coordinates picks up the wrong texture pixelso Do per-pixel sampling or use high resolution polygons Can only view from one direction

Correct Linear

Page 19: Lecture 11 Environment mapping, Mirroring

Cons and Pros

• How do you compare cube mapping and sphere mapping? o Advantages of cube mapping?o Problem of sphere mapping?

Page 20: Lecture 11 Environment mapping, Mirroring

How can you make the right image from the left image?Where does middle point on the right image corresponds to? by Mark VandeWettering

Page 21: Lecture 11 Environment mapping, Mirroring

Refractive Environment Mapping

• When simulating effects mapping the refracted environment onto translucent materials such as ice or glass, we must use Refractive Environment Mapping

Page 22: Lecture 11 Environment mapping, Mirroring

Refractive Environment MappingJust use the refraction vector after the first hit

Page 23: Lecture 11 Environment mapping, Mirroring

Snell’s Law• When light passes through a

boundary between two materials of different density (air and water, for example), the light’s direction changes.

• The direction follows Snell’s Law• We can do environment mapping

using the refracted vector t

Page 24: Lecture 11 Environment mapping, Mirroring
Page 25: Lecture 11 Environment mapping, Mirroring

Today• Environment Mapping

o Introductiono Cubic mappingo Sphere mappingo refractive mapping

• Mirroringo Introductiono reflection firsto stencil buffero reflection last

Page 26: Lecture 11 Environment mapping, Mirroring

Planar Reflections (Flat Mirrors)• Basic idea: Drawing a scene with mirrors!

o We need to draw all the stuff around the mirror

o We need to draw the stuff in the mirror, reflected, without drawing over the things around the mirror

Page 27: Lecture 11 Environment mapping, Mirroring

Reflecting Objects

• If the mirror passes through the origin, and is aligned with a coordinate axis, then just negate appropriate coordinate

• For example, if a reflection plane has a normal n=(0,1,0) and passes the origin, the reflected vertices can be obtained by scaling matrix S(1,-1,1)

MirrorWall

Page 28: Lecture 11 Environment mapping, Mirroring

Reflecting Objects• If the reflection plane

passes a point p and has a normal vector n, you translate and rotate the coordinate system, negate, and move back to the original coordinate system

MirrorWall Mirror

p

Page 29: Lecture 11 Environment mapping, Mirroring

Drawing the mirrored worldTwo ways to do it:

1. Draw the mirrored world first, then the real world• Only using the depth (Z) buffer • Does not work in some cases

2. Draw the real-world first, and then the mirrored world• Requires using a stencil buffer

Page 30: Lecture 11 Environment mapping, Mirroring

Rendering Reflected First(Using the depth buffer(Z-buffer))

• First pass: Render the reflected scene without mirror, depth test on

• Second pass:o Disable the color buffer, and render the

mirror polygon (to not draw over the reflected scene)

o Now the depth buffer of the mirror region is set to the mirror’s surface

• Third Pass:o Enable the color buffer againo Render the original scene, without the

mirroro Depth buffer stops from writing over things

in mirror

Page 31: Lecture 11 Environment mapping, Mirroring

Reflection Example

The color buffer after

the final pass

Page 32: Lecture 11 Environment mapping, Mirroring

Reflected Scene First (issues)

• Objects behind the mirror cause problems:o The reflected area outside

the mirror region is just overwritten by the objects in the front

o unless there is a wall, they will remain visible

• Doesn’t do:o Reflections of mirrors in

mirrors (recursive reflections)o Multiple mirrors in one scene

(that aren’t seen in each other)

Page 33: Lecture 11 Environment mapping, Mirroring

We need to use the “Stencil Buffer”

• The stencil buffer acts like a paint stencil - it lets some fragments through but not others

• It stores multi-bit values• You specify two things:

o The test that controls which fragments get through

o The operations to perform on the buffer when the test passes or fails

Page 34: Lecture 11 Environment mapping, Mirroring

Stencil Tests• You give an operation, a

reference value, and a mask

• Operations:o Always let the fragment

througho Never let the fragment

througho Logical operations between

the reference value and the value in the buffer: <, <=, =, !=, >, >=

Page 35: Lecture 11 Environment mapping, Mirroring

Stencil Operations• Specify three different operations

o If the stencil test failso If the stencil passes but the depth test failso If the stencil passes and the depth test passes

• Operations are:o Keep the current stencil valueo Zero the stencilo Replace the stencil with the reference valueo Increment the stencilo Decrement the stencilo Invert the stencil (bitwise)

Page 36: Lecture 11 Environment mapping, Mirroring

mirror

Reflection Example

Page 37: Lecture 11 Environment mapping, Mirroring

Normal first, reflected area next• First pass:

o Render the scene without the mirror• For each mirror

o Second pass: Clear the stencil, disable the write to

the colour buffer, render the mirror, setting the stencil to 1 if the depth test passes

o Third pass: Clear the depth buffer with the stencil

active, passing things inside the mirror only

Reflect the world and draw using the stencil test. Only things seen in the mirror will be drawn

Combine it with the scene made during the first pass

The stencil buffer after the second pass

Rendering the mirrored scene into the stencil active area

Page 38: Lecture 11 Environment mapping, Mirroring

Multiple mirrors

• Can manage multiple mirrorso Render normal view, then do other

passes for each mirror• A recursive formulation exists

for mirrors that see other mirrorso After rendering the reflected area

inside the mirror surface, render the mirrors inside the mirror surface, and so on

Page 39: Lecture 11 Environment mapping, Mirroring

Readings

• Foley 16.5-6• Real-time Rendering 2, Chapter 5.7, 6.10

Reference

http://brainwagon.org/2002/12/05/fun-with-environment-maps/