# Ray Casting

Document Sample

```					Ray Tracing

• In a single
framework
incorporates:
– Hidden Surface
Removal
Computation
– Reflection of Light
– Refraction of Light
– Global Specular
Interaction           I(P) = Ilocal(P) + krgI(Pr) + ktg(Pt)

1
Recursive Ray Tracing

I(P) = Ilocal(P) + krgI(Pr) + ktg(Pt)
Local term   Reflected   Transmitted

• Treats the same surface differently
depending on which term is being evaluated
• In the direct Specular Term Ilocal spread is
calculated empirically (see Phong)
• In the global raytraced terms, spreading of
light due to surface imperfections is totally
ignored as this would spawn a large number
of reflected and refracted rays
2
Ray Tracing
• We are only interested in light
rays that reach the viewplane
from the light source
• If we begin at the light source
only a small proportion of the
rays we trace will reach the
viewer                            Tracing from light source
• More efficient to start at
viewer and trace in reverse –
• Some implementations start at
the light source and trace the
actually light path –
backwards raytracing
3
Ray Casting
• For each pixel in the viewport, we cast a ray from the eye (called
the eye ray) into the scene, through the pixel and determine the
first object hit by the ray  ray casting.
• We then shade this using an extended form of the Phong Model.
Ray Tracing

5
Ray Tracing
• The eye ray will typically intersect a number of
objects, some more than once  sort intersections to
find the closest one.
• The Phong illumination model is then evaluated BUT:
– we trace a reflected ray if the surface is specular
– we trace a refracted ray if the surface is
transparent
– we trace shadow rays towards the light sources to
determine which sources are visible to the point
• The reflected/refracted rays themselves will hit
surfaces and we will recursively evaluate the
illumination at these points.
 a very large number of rays must be traced to
illuminate a single pixel.
Recursive Ray Tracing
• The ray tracing algorithm is recursive, just as the radiance
equation is recursive.
– At each intersection we trace a specularly reflected, a
shadow ray and transmitted ray (if the surface is specular) or
terminate the ray if diffuse.
• Thus we trace a ray back in time to determine its history,
beginning with the eye ray: this leads to a binary tree
Recursive Ray Tracing
• Theoretically, this recursive process could continue
indefinitely.
• In practice, at each intersection the ray loses some of its
contribution to the pixel (i.e. its importance decreases).
– if the eye ray hits a specularly reflecting surface with
reflectivity of 50%, then only 50% of the energy hitting
the surface from the reflected direction is reflected
towards the pixel.
– if the next surface hit is of the same material, the
reflected ray will have its contribution reduced to 25%.
• We terminate the recursion if:
– the current recursive depth > a pre-determined
maximum depth or
– if the ray’s contribution to the pixel < some pre-
determined threshold 
The Ray Tracing Algorithm

for each pixel in viewport
{
determine eye ray for pixel
intersection = trace(ray, objects)
}

Intersection trace(ray, objects)
{
for each object in scene
intersect(ray, object)
sort intersections
return closest intersection
}

9
The Ray Tracing Algorithm
{
if no intersection
return background colour

for each light source
if(visible)
colour += Phong contribution

if(recursion level < maxlevel)
{
ray = reflected ray
intersection = trace(ray, objects)
ray = transmitted ray
intersection = trace(ray, objects)
}
return colour
}
Ray Tracing Issues

I(P) = Ilocal(P) + krgI(Pr) + ktg(Pt)

1) Cast a ray
2) Determine Intersections
3) For closest Intersection:
– Extend light(shadow feeler) ray +
calculate local term
– Spawn Transmitted Ray (step 2)
– Spawn Reflected Ray (step 2)

11
The Ray

• Using a parameterized representation of the
ray
t<0
x  x1  ( x2  x1 ) t  x1  it            Ray Start   (x1, y1, z1)
y  y1  ( y2  y1 ) t  y1  jt      t=0
0 <= t <= 1
z  z1  ( z2  z1 ) t  z1  kt
• Or
R(t )  O  D  t
Ray End    (x2, y2, z2)     t>1
Where                                                         t=1
R is a set of points on the line
O is the origin of the ray
D is the direction vector of the ray

12
Eye Ray Construction

• We need to construct rays originating at the
eye and passing through a point on the
viewport.
we need to relate the viewport and window
co-ordinate systems.
– Construct vectors u and v which span the viewing
plane
– Determine origin of viewport (usually defined as
the center)
– all points P on the viewport are linear
combinations of u and v with scalars  and 
defining the co-ordinate of the point:
13
Eye Ray Construction

=1

v
=0
    
P  u  v                    u
P= ( u+ v)
=-1

=-1   =0            =1

14
Eye Ray Construction

=1    (0,0)

v
=0
u
P= ( u+ v)
=-1
(xres –1, yres -1)
=-1         =0           =1
window
viewport

2 x  0  5            2 y  0  5
               1   1                      Pixel center used for ray direction.
xres                     yres            Y value is inverted

15
Eye Ray Construction
• Given pixel location (x,y) we can readily determine
the associated viewport co-ordinates (,).
• To construct basis vectors u and v for the viewport
we first construct a camera co-ordinate system using
the camera parameters.
 LE
• Construct the gaze vector:    g
LE
• The viewport origin is defined using the view plane
distance, i.e. move a distance d along g and we
reach the viewport center:

O  Ed g
• Normally, the z axis of the camera system is chosen
to be g

16
Eye Ray Construction
• Construct the x axis by taking the cross product of
 
U and z           Uz
x  
Uz
• Finally we construct y by taking the cross product
of x and z:         
y  zx
Eye Ray Construction
• To construct u and v, note that they are
simple scaled values of x and y respectively
 we need only determine the length of u and v given
the field of view angles  and :

             
u  d tan , v  d tan 
             
 u  d tan x
             
v  d tan  y
Eye Ray Construction
• Now we’re in a position to construct an eye ray:
– the origin of the ray is simple the eye point E.
– the ray direction (normalised) is given by the vector taking
us from E to the point P on the viewport associated with a
particular pixel:

Oray  E                                 dray
                                         P=O+ u+ v
       O  u   v  E
d ray            
O  u   v  E

19
Ray Object Intersections

• Once we’ve constructed the eye rays
we need to determine the intersections
of these rays and the objects in the
scene.
• Upon intersection we need the normal
to the object at the point of intersection
in order to perform shading calculations.

20
Ray Object Intersection

• Usually objects are defined either implicitly or
explicitly (parametrically).         
 0 if v inside surface
• Implicit:                   
f v   f v0 , v1 ,, vn   0 if v on surface


 0 if v outside surface

– the set of points on the surface are the zeros of
the function f.
• Explicit:
– n are the generating parameters and usually
have infinite ranges      S n  f  0 , 1 ,,  n 
– for surfaces in 3-space, S2, there will be 2
generating parameters.
– we iterate over all parameters to generate the set
of all points on the surface.
21
The Sphere

• A sphere object is defined by its center C and
• Implicit Form:
     
f v   v  C  r 2  0
2

f x, y, z   x  Cx  2  y  C y  2  z  Cz  2  r 2  0
• Explicit Form:
x  f x  ,    C x  r sin  cos
y  f y  ,    C y  r cos
z  f z  ,    Cz  r sin  sin 

• We can use either form to determine the
intersection; we will choose the implicit form.
22
Ray Sphere Intersection
• All points on the ray are of the form:

r  O  td t  0
• All points on the sphere satisfy:
x  Cx   y  Cy   z  Cz 
2           2            2
 r2  0
• any intersection points (= points shared by
both) must satisfy both, so substitute the ray
equation into the sphere equation and solve
for t:
O  t d   C   O
x    x        x
2
y    t d  C   O
y    y
2
y    t d  C   r
y    z
2     2
0

ray equation
23
Ray Sphere Intersection

• Rearrange and solving for t leads to a
quadratic form (which is to be expected as
the sphere is a quadratic surface):
At2  Bt  C  0
A  d x  d y  d z2   1
2     2

B  2d x Ox  C x   2d y O y  C y   2d z Oz  Cz 
C  Ox  C x   O y  C y   Oz  Cz   r 2
2              2              2

• We employ the classic quadratic formula to
determine the 2 possible values of t:
 B  B2  4 AC  B  B2  4C
t                
2A              2
24
Ray Sphere Intersection
• Depending on the number of real roots we
have a number of outcomes which have nice
geometric interpretations:
– we use the discriminant d:
d  B 2  4C                                     no roots

d  0  1 root (ray is tangent to sphere)
d 0      no real roots (ray misses)
1 root
d 0      2 real roots:
Both positive  sphere in front of ray                         2 roots
One negative  ray origin inside sphere
1 root negative
25
Ray Sphere Intersection Test
• If we have 2 positive values of t we use the
smallest (i.e. the nearest to the origin of the
ray).
• t is substituted back into the ray equation
yielding the point of intersection:
Pint  Oray  tint d ray
• We then evaluate the Phong illumination
model at this point. To do so we need the
normal to the surface of the sphere at the
point of intersection.
• The normal and the original ray direction are
then used to determine the directions of
reflected and refracted rays.
Normals to Explicit Surfaces
• Given an explicitly defined surface S with
parameters u and v we can find the normal to
the surface at a point x:
– first we determine the tangents to the u and v
curves at the point
f u, v 
N   f u, v 
v                   u
f f
                                x
N  v u                                        v
f f                          u

v u
f u, v 

27
Normals to Implicit Surfaces
• With implicit surfaces, the function represents
a density field the gradient of which is the
normal we require:
f  x, y , z 
f  x, y , z 
y
z                 N
f  f  f
N i  j k
x  y  z
 f   f   f                                           f  x, y , z 

 x   y   z 

x

 f                                   f  x, y , z 

28
Normal to Sphere

• We can therefore compute the normal
to a sphere at a point x:
f           f            f
x2  y2  z2  r2  0                2x          2y           2z
x           y            z
 2 x 2 y 2 z 
i.e. the normal to the sphere is the normalised vector associated
with the point. For a sphere with center C we compute the normal as:

x C
N 
x C

29
Ray Plane Intersections

• A plane may be defined implicitly as:
 
v  nP  d p   or   nx x  n y y  nz z  d P  0

Plane normal

perpendicular distance to the origin

30
Ray Plane Intersections

• To intersect a ray with a plane we use the
same approach as with the sphere i.e.
substitute in the ray equation and solve for t:
nx Ox  td x   n y Oy  td y   nz Oz  td z   d P  0

n x Ox  n y O y  n z Oz  d P     nP  Or
t                                         
nx d x  n y d y  nz d z       nP  d r
 
• If nP  d r  0 then the ray is parallel to the
plane and no intersection occurs.
• Note there can only be one intersection. The
normal to the plane at each point is the same,
i.e. the plane normal nP
31
Ray Polygon Intersections
• First intersect the ray with the polygon plane: given
this intersection point (assuming ray is not parallel to
the plane) determine if it is within the polygon interior
using the Jordan Curve Theorem:
– construct any ray with the intersection point as an origin
– count the number of polygon edges this ray crosses ( =
winding number)
– if w is odd then the point is in the interior
Ray Polygon Intersections
• Note that this is essentially a 2
dimensional problem:
principal axis
– after intersecting the ray and the
plane, reduce the problem to 2D
by projecting parallel to the
polygon’s principal axis onto the
plane formed by the other 2 axes
– the principal axis is given by the
largest ordinate of the polygon
normal
– e.g. if n = [0.2 0.3 0.93] then z is
the principal axis and we project
onto the xy plane.
– This projection preserves the
topology (assuming the
projection direction is not parallel projected polygon and intersection point
to the polygon)
33
Ray Polygon Intersections
• Note that this projection is constant for each polygon,
therefore we can pre-compute the polygon projection
and principal axis.
• Once projected, the winding number is determined.
– translate the intersection point to the origin (apply same
translation to all projected polygon vertices).
– use an axis (not the principal axis) as the direction to try
– determine the number of edge crossings along the positive
axis.
Ray Polygon Intersections

• We can speed up the process of
determining edge intersection with an
axis. There are 3 redundant cases of
edge axis topology:

Both x values negative
 no intersection

Both z values positive
Both z values negative
 no intersection
 no intersection
35
Ray Polygon Intersections
• If none of these cases are satisfied then we must
compute the intersection with the axis and determine
if it lies on the positive half of the axis:

Remember the general equation of a line:
y  mx  c
intersection with the y axis
z2

xint  x1  z1
 x2  x1 
x1
 z2  z1 
x2            If xint > 0 then the edge crosses the
z1
axis and the winding number is
xint         incremented.
36

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 11 posted: 12/4/2011 language: English pages: 36