ray tracing tutorial. ray casting one type of visibility algorithm
Post on 19-Dec-2015
222 views
TRANSCRIPT
Ray Tracing Tutorial
Ray CastingOne 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
}
Constructing Primary Rays
CoP
u
nv
Xmin, Ymin
Constructing Primary Rays
CoP
Constructing Primary Rays
CoP
Constructing Primary Rays
CoP
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
Visualizing the components
CoP
Look at point
up vector
Width (in pixels)
Height (in pixels)
x_res
y_res
focal length
Constructing the Orthogonal Basis
CoP
Look at point
up vector
u
n
v
n = normalize lookat vector u = n x upv = u x n
Computing Starting Point
CoPu
n
v
X, Y
Beware of units. Pixels versus world units
focal length
Constructing a Primary Ray
CoP
CoP
Cast Ray
Current Pixel Color(0.2,0.8,0.2) due to ambientterm
CoP
Check for Intersections
Current Pixel Color(0.2,0.8,0.2) due to ambientterm
Beware of near and farclipping planes as well
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]
CoP
Pick Nearest
Current Pixel Color(0.2,0.8,0.2) due to ambientterm
Computing the Surface Lighting Effect
CoP
ConnectLight Ray
Current Pixel Color(0.2,0.8,0.2) due to ambientterm
IR = IR,ambdiff + IR,diffused + IR,specular
IG = IG,ambdiff + IG,diffused + IG,specular
IB = IB,ambdiff + IB,diffused + IB,specular
I = Iambdiff + I diffused + I specular
For example (0.2, 0.8, 0.2) for this material.A constant. Independent of lightingAnd viewing direction
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
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
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]
CoP
Current Pixel Color(0.5,1.0,0.5)
ConnectLight Ray
What about areas in shadow?
CoP Current Pixel Color(0.2,0.8,0.2) due toAmbient term
ConnectLight Ray
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!
We modify the Ray-Casting algorithm to trace rays bouncing
off the surfaces.
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);
}
}
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.
CoP
ContributionFrom PrimaryRay
(0.5,1.0,0.5)
CoP
ContributionFrom SecondaryRay
(0.5,1.0,0.5)
V2
V1
L (0.8,0.8,0.0)
CoP
Final Pixel Color
(0.5,1.0,0.5)
V2
V1
L (0.8,0.8,0.0)+ =ks
The End