ray tracing tutorial. ray casting one type of visibility algorithm

34
Ray Tracing Tutorial

Post on 19-Dec-2015

222 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

Ray Tracing Tutorial

Page 2: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

Ray CastingOne type of visibility algorithm

Page 3: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

Basic IdeaRayCast(Camera, Scene, img_w, img_h)

{

for (row=0; row < img_h; row++) {

for (col=0; col< img_h; col++) {

ray = constructRay(camera, row, col);

(hit,color)=getIntersection(ray, scene);

image[row][col] = color;

}

}

return image

}

Page 4: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

Constructing Primary Rays

CoP

u

nv

Xmin, Ymin

Page 5: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

Constructing Primary Rays

CoP

Page 6: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

Constructing Primary Rays

CoP

Page 7: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

Constructing Primary Rays

CoP

Page 8: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

Gathering all the info from .dat file

Camera specification

• Viewport <width> <height>

• Resolution <x_res> <y_res>

• Camera {perspective, orthographic}

<cop_x><cop_y><cop_z>

<look_x><look_y><look_z>

<up_z><up_y><up_z>

<focal_length>

<near_plane> <far_plane>

CoP point

lookat point

up vector

Page 9: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

Visualizing the components

CoP

Look at point

up vector

Width (in pixels)

Height (in pixels)

x_res

y_res

focal length

Page 10: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

Constructing the Orthogonal Basis

CoP

Look at point

up vector

u

n

v

n = normalize lookat vector u = n x upv = u x n

Page 11: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

Computing Starting Point

CoPu

n

v

X, Y

Beware of units. Pixels versus world units

focal length

Page 12: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

Constructing a Primary Ray

CoP

Page 13: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

CoP

Cast Ray

Current Pixel Color(0.2,0.8,0.2) due to ambientterm

Page 14: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

CoP

Check for Intersections

Current Pixel Color(0.2,0.8,0.2) due to ambientterm

Beware of near and farclipping planes as well

Page 15: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

Some Tips• Try the simplest test case you can think of

(eg one sphere, and simple camera location)

• Break Down into steps– Ray Cast in BW with spheres (ensure

generation of primary rays is correct)– Shade with Primary Ray contribution.– Try more complex surfaces

• Normalize, Normalize, Normalize

• Clamp color [0,1]

Page 16: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

CoP

Pick Nearest

Current Pixel Color(0.2,0.8,0.2) due to ambientterm

Page 17: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

Computing the Surface Lighting Effect

Page 18: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

CoP

ConnectLight Ray

Current Pixel Color(0.2,0.8,0.2) due to ambientterm

Page 19: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

IR = IR,ambdiff + IR,diffused + IR,specular

IG = IG,ambdiff + IG,diffused + IG,specular

IB = IB,ambdiff + IB,diffused + IB,specular

Page 20: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

I = Iambdiff + I diffused + I specular

For example (0.2, 0.8, 0.2) for this material.A constant. Independent of lightingAnd viewing direction

Page 21: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

I = Iambdiff + I diffused + I specular

N

LIdiffused = kd Il (N.L)

Compute for all 3 channels. For example (0.1, 0.1, 0.1).

Independent of Viewpoint

Page 22: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

I = Iambdiff + I diffused + I specular

N

LIspecular = ks Il (V.R)ns

Compute for all 3 channels. For example (0.2, 0.2, 0.2).

V

R

Page 23: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

I = Iambdiff + I diffused + I specular

= (0.2, 0.8, 0.2) + (0.1, 0.1, 0.1) + (0.2, 0.2, 0.2)

= (0.5, 1.0, 0.5)

Clamp to [0,1]

Page 24: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

CoP

Current Pixel Color(0.5,1.0,0.5)

ConnectLight Ray

Page 25: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

What about areas in shadow?

Page 26: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

CoP Current Pixel Color(0.2,0.8,0.2) due toAmbient term

ConnectLight Ray

Page 27: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

Ray Tracing• Physical World :

– Photons shoot out from light sources, reflect off surfaces and into the eye.

– Problem : Only small fraction reaches the eye (or image plane). Difficult to simulate.

• Alternative way to simulate :– Reverse the process!– Trace the path backwards i.e from the eye (or

pixels) back to the light sources.– Ray Tracing!

Page 28: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

We modify the Ray-Casting algorithm to trace rays bouncing

off the surfaces.

Page 29: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

Outer Loop

Select center of projection and window on view plane;

For (each scan line in image) {

For (each pixel in scan line) { 1) Construct ray from center of projection through pixel 2) pixel = trace(ray,1);

}

}

Page 30: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

color trace(RT_ray ray, int depth) {Determine closest intersection of ray with an object

if there is an intersection {

1) compute normal at intersection

2) return shade ( closest object hit, ray, intersection, normal, depth)

}

}

Shade will return the cumulative color by recursively calling tracewith depth counter decremented at each invocation.

Page 31: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

CoP

ContributionFrom PrimaryRay

(0.5,1.0,0.5)

Page 32: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

CoP

ContributionFrom SecondaryRay

(0.5,1.0,0.5)

V2

V1

L (0.8,0.8,0.0)

Page 33: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

CoP

Final Pixel Color

(0.5,1.0,0.5)

V2

V1

L (0.8,0.8,0.0)+ =ks

Page 34: Ray Tracing Tutorial. Ray Casting One type of visibility algorithm

The End