Ray Tracing Basics I Computer Graphics as Virtual Photography real - - PDF document

ray tracing basics i
SMART_READER_LITE
LIVE PREVIEW

Ray Tracing Basics I Computer Graphics as Virtual Photography real - - PDF document

Ray Tracing Basics I Computer Graphics as Virtual Photography real camera photo Photographic Photography: scene (captures processing print light) processing camera Computer 3D synthetic tone model Graphics: models image


slide-1
SLIDE 1

1

Ray Tracing Basics I

Computer Graphics as Virtual Photography

camera (captures light) synthetic image camera model (focuses simulated lighting)

processing

photo processing tone reproduction real scene 3D models Photography: Computer Graphics: Photographic print

slide-2
SLIDE 2

2

Ray Tracing in the real world

 Light is emitted from light source  Bounces off of the environment  Assumptions

 Light travels in straight rays  Path of light changes based on object interaction.  Can simulate using basic geometry.

 Some light will reach and be focused by

camera onto film plane.

 Lots of light will not!  In image synthesis, we are only interested in the

light that does

Backwards Ray Tracing

 Light rays are traced backward from the

eye (center of projection), through a viewing plane, into scene to see what it hits.

 The pixel is then set to the color values

returned by the ray.

 This color is a result of the object hit by

the ray.

Turner Whitted

slide-3
SLIDE 3

3

 Insert Cheesy Ray Tracing Movie Here

Ray Tracing - Basics

Sometimes you don’t hit an object

slide-4
SLIDE 4

4

Ray Tracing - Basics

Sometimes you do

Ray Tracing - Basics

 If you do hit an object, additional rays

are spawned and sent into world to determine color at intersection point

 Shadow ray  Reflected ray  Transmitted ray

slide-5
SLIDE 5

5

Ray Tracing - Basics

 Shadow ray

 Ray spawned toward each light source to see

if point is in shadow.

Ray Tracing - Basics

 Shadow ray

slide-6
SLIDE 6

6

Ray Tracing

 Reflective Ray

Ray Tracing

 Transmitted ray

slide-7
SLIDE 7

7

Recursive Ray Tracing Ray Tracing

 Ray Tracing incorporates into a single

framework:

 Hidden surface removal  Shadow computation  Reflection of light  Refraction of light  Global Specular Interaction

 Extremely elegant and compact

slide-8
SLIDE 8

8

Ray Tracing Basics

 Basic Ray Tracing -- Example Whitted

Ray Tracing Assignment

 For Checkpoint 2:

 Trace rays through camera model  Using ray tracing for visible surface

determination.

 Questions -- Break

slide-9
SLIDE 9

9

Ray Tracing through the Camera

 Issues

 Ray Geometry  Object-Ray Intersection  Projection

Introducing Ray

 Use mathematical description of a ray

and objects to determine intersection.

 Parametric representation of a ray:

 Origin of ray, Po = (xo,yo,zo)  Direction D = (dx, dy, dz)  Ray (ω) = Po + ω D

 If D is normalized, then ω will be the

distance from origin of the ray.

slide-10
SLIDE 10

10

Ray-Object Intersection

 Most of the computation in ray tracing

is determining ray object-intersection

 When a ray intersects an object, we

need to know:

 Point of intersection  Normal of surface at point of intersection

Ray-Sphere Intersection

 The Sphere

 A sphere can be defined by:

 Center (xc, yc, zc)  Radius r

 Equation of a point (xs, ys, zs) on a sphere:

2 2 2 2

) ( ) ( ) ( r z z y y x x

c s c s c s

=

  • +
  • +
slide-11
SLIDE 11

11

Ray-Sphere Intersection

 Ray - Sphere Intersection

 Substituting ray equation for (xs, ys, zs)  We get:

A ω2 + B ω + C = 0

 where

))

2 2 2 2 2 2 2

) ( ) ( ) ( ( ) ( ) ( ( 2 r z z y y x x C z z dz y y dy x x dx B dz dy dx A

c

  • c
  • c
  • c
  • c
  • c

− + − + − = − + − + − = + + =

Ray-Sphere Intersection

 Using the Quadratic Formula  Note: ω must be positive, otherwise the

intersection is BEHIND the origin of the ray

A AC B B

ω

2 4

2 −

± − =

slide-12
SLIDE 12

12

Ray-Sphere Intersection

Note: If D is normalized A = dx2 + dy2 + dz2 = 1 and 2 4

2

C B B

ω

− ± − =

Ray-Sphere Intersection

 If B2 – 4C is:

< 0 – no real root, no intersection = 0 – one root, ray intersects at sphere’s surface > 0 – two roots, ray goes through sphere. Use least positive root

slide-13
SLIDE 13

13

Ray-Sphere Intersection

 Once we found a ωi for the point of

intersection, the actual point is:

 (xi, yi, zi) = (x0 + dx * ωi , y0 + dy * ωi , z0 + dz * ωi )

 The normal at the point of intersection is:

 (xn, yn, zn) = ((xi - xc)/r, (yi - yc)/r, (zi - zc)/r)  (We divide by r to normalize!)

Ray-Plane Intersection

 A plane can be defined by:

 A normal vector and a point on the plane

 It has the equation

 where Pn = (A, B, C) gives the normal and if

normalized (A2 + B2 + C2 = 1), F will the shortest distance to the plane from the origin of world.

= + + + F Cz By Ax

slide-14
SLIDE 14

14

Ray-Plane Intersection

 Ray - Plane Intersection

 For plane with equation:  Plug in equation for ray and we get

) /( ) ( ) (

n n

  • D

P F P P Cdz Bdy Adx F Cz By Ax

ω

  • +

= + + + + + − =

= + + + F Cz By Ax

Ray-Plane Intesection

 If (Pn • D) is

 0 – then ray is parallel to plane, no

intersection

 If ω is

 < 0 – then the ray intersects behind the

  • rigin of the ray…ignore!

 > 0 – calculate the point of intersection

slide-15
SLIDE 15

15

Ray-Plane Intersection

 Once we found a ωi for the point of

intersection, the actual point is:

 (xi, yi, zi) = (x0 + dx * ωi , y0 + dy * ωi , z0 + dz * ωi )

 And we already have the normal at the

point of intersection is:

 Pn = (A, B, C)

Ray-Polygon Intesection

  Find the plane in which the polygon

sits

  Find the point of intersection between

the ray and the plane

  If point of intersection is found, see if

it lies within the boundaries of the polygon.

slide-16
SLIDE 16

16

Ray-Polygon Intersection

 Find the plane in which the polygon sits

 A plane can be defined by:

 A normal vector and a point

 And has the equation

 where Pn = (A, B, C) gives the normal and if normalized

(A2 + B2 + C2 = 1), F will give the shortest distance to the plane from the origin of the world.

= + + + F Cz By Ax

Ray-Polygon Intersection

 Find the point of intersection between the ray

and the plane

 Done previously

 See if point of intersection lies within the

boundaries of the polygon.

 One algorithm:

 Draw line from Pi to each polygon vertex  Measure angles between lines  Recall: (A • B) = |A||B| cos θ  If sum of angles between lines is 360°,

polygon contains Pi

slide-17
SLIDE 17

17

Other Intersections

 To add other geometric primitives to

your ray tracer

 Must mathematically derive the point of

intersection between a ray and geometric primitive.

 Questions?

Ray Tracing through the Camera

 Issues

 Ray Geometry  Object-Ray Intersection  Projection

slide-18
SLIDE 18

18

Projection Ray Tracing through a camera

  Set up your scene

  Determine position / orientation of objects in

scene.

 Spawn a ray and send into scene

  Define ray direction (remember to normalize)   Check for closest intersection   Calculate and return color

 Display or save final image

slide-19
SLIDE 19

19

Introducing Ray

 Use mathematical description of a ray

and objects to determine intersection.

 Parametric representation of a ray:

 Origin of ray, Po = (xo,yo,zo)  Direction D = (dx, dy, dz)  Ray (ω) = Po + ω D

 If D is normalized, then ω will be the

distance from origin of the ray.

Graphics Pipeline

3D Object Coordinates 3D World Coordinates 3D Eye Coordinates 3D Eye Coordinates 2D Eye Coordinates 2D Screen Coordinates Object Transformation Viewing Transformation 3D Clipping Projection Window to Viewport Mapping

slide-20
SLIDE 20

20

Camera Transformations

 (ux,uy,uz) are coordinates of unit u vector w.r.t. world space

 Similar for v, n,

 (eye) is the origin of view space w.r.t world space

 If ups are aligned, simply

use negative eye location values in the fourth column

            = 1

z y x z y x z y x

n n n v v v u u u M

  • eye• u
  • eye• v
  • eye• n

Graphics Pipeline

3D Object Coordinates 3D World Coordinates 3D Eye Coordinates 3D Eye Coordinates 2D Eye Coordinates 2D Screen Coordinates Object Transformation Viewing Transformation 3D Clipping Projection Window to Viewport Mapping

slide-21
SLIDE 21

21

Projection

 Note: Projection not required as this will be

done as part of the ray tracing process

  • =
  • 1

1

z y x n v u

p p p M P P P

Spawning rays through camera

 Coordinate spaces

 Can do in camera space or world space  Camera space

 Must transform all objects/lights to camera

space

 World space

 Must transform initial rays to world space

slide-22
SLIDE 22

22

Projection in Camera Space

 The role of cameras can be described as

projecting a 3D scene onto a 2D plane

Converting to World Space

  • =
  • 1

1

1 n v u z y x

p p p M P P P

slide-23
SLIDE 23

23

Inverting a 4x4 Matrix

 Code samples from

 Graphics Gems  Ken Perlin  Available on Web

 Will link on DIARY

Tips – World Space

 Need only transform the location of 1st “pixel”

location on image plane and dx, dy, and dz as you move across and down the plane

slide-24
SLIDE 24

24

Tips – Calculating Color

 Find point of intersection

 Good Safety tip – only consider

intersections if they occur past the image plane.

 If intersection

 Return color  Of object

Displaying your image

 You don’t really need the full power of a

3D API to do ray tracing

 Just need the ability to write color values

to pixels

 Some of the matrix operation routines may

be helpful.

slide-25
SLIDE 25

25

Displaying your image

 OpenGL

 glDrawPixels();  Chapter 10, Hill  Chapter 8, OpenGL, red book

Displaying your image

 C library

 Netppm

 Netpbm is a freeware toolkit/library for

m anipulation of graphic im ages, including conversion

  • f im ages between a variety of different form ats.

 http://netpbm.sourceforge.net/

 Java

 Java2D

 java.awt.im age  javax.im ageio

 Questions?

slide-26
SLIDE 26

26