Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Ray Tracing 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Recursive ray tracing Surface Normals

• A surface normal at a
point is a vector from
that point
perpendicular to the
tangent plane at that
point
• Images from
Wikipedia

Reflections Shadows
Transparency Recursive Ray Tracing
• Each reflected or refracted ray is treated like a
Snell’s Law
primary ray – i.e. can spawn shadow rays,
reflected rays and refracted rays
• Recursive cut-off (e.g. what if a ray bounces
between two mirrors – code does not terminate)
• How does that affect things?

Recursive Depth Recursive Depth


• Primary • Depth=
rays (1) 2
Recursive Depth Recursive Depth
• Depth= • Depth=
3 5

Recursive Depth Computational Expense


• Depth= • No reflections, shadows or transparency
10 • Chess board – 32 pieces with 100 triangles each,
extra 20 triangles for board, plane, sky etc. =
3220 triangles
• Image, 1000x1000=1 million pixels
• Algorithm – for each pixel, solve ray with each
triangle. Select the closest hit
• 1000x1000x3220=3.22 billion intersection
calculations!!
Bounding Volumes Example
• Artificially surround each chess piece by a • Find closest hit point
cylinder (easy to calculate ray-cylinder • If the closest hit point is a cylinder,
intersection) then intersect ray with 100 triangles
• What does a ray hit? within.
• First trace it against the general scene geometry • Many rays will require just these 152
and 32 cylinders = 52 intersections intersections
• Some will “miss” the chess piece
although they hit the cylinder

Example Hierarchical bounding volumes


• Assume 60% of rays miss cylinders, 30% hit a triangle • 1 box around the whole of the chess board and pieces (=
in the first cylinder and 10% hit a triangle in the 6 intersection tests)
second cylinder. • 3 other planes for the sky and two ground textures (=3
intersection tests)
• Therefore intersections ≈ • Inside the chess box, place 2 boxes – one over the white
0.6x1000x1000x52 pieces, and one over the black (each 6 tests)
0.3x1000x1000x152+ • Inside those have the 32 cylinders (1 test)
0.1x1000x1000x252=102 million (about 30 times • Inside each cylinder have 3 cylinders (3 tests)
faster in this case) • Inside each of those have 30-40 triangles (30-40 tests)
• Why stop there? – hierarchical bounding volumes
Hierarchical bounding volumes Example
• At the first level of our hierarchy, 9 tests are used to see • Using same assumptions as before: 60% of rays miss
if we have hit sky, ground or are in the region of the board, 30% hit a triangle in the first cylinder and 10%
board. hit a triangle in the second cylinder.
• If we hit board region, another 6 tests see if we have hit • Therefore intersections ≈
white or black chess pieces 0.6x1000x1000x9
• If we have hit white, another 16 tests see if we have hit 0.3x1000x1000x74+
a chess piece
0.1x1000x1000x117=39.3 million (about 100 times
• Another 3 determine which part
faster in this case)
• And a maximum of 40 to determine the triangle
• Total for a hit chess piece (previously 152) = 74

Hierarchical Bounding Volumes Hierarchical Bounding Volumes


• Advantages • Andreas Dietrich
– Fast ray tracing times
– User can decide on tight fitting objects
– User can decide logical splitting of scene
• Disadvantages
– User interaction required
– Could take a lot more time than is saved
– Not all scene modellers will understand BVs
Octrees Octrees
• Image
from
Sylvian
Lefebvre
• (max
depth=
12)

Others Surface Lighting


• kd-trees (cut is not • Ambient
necessarily in the • Diffuse
centre)
• bsp-trees (cut
is a plane –
not a cube)
• Specular
Indirect Illumination
Point light

Light occluder
Shadow ray

Intersection point
Indirect illumination

Total/partial reflector

We’ve seen ray tracing using DIRECT ILLUMINATION


INDIRECT illumination is VERY expensive to calculate

Ambient Reflection Light Sources


• Ambient reflection • To create a realistic rendering (during rasterization or ray
tracing), we need to calculate the light incident upon the
• Pixel intensity, I=IaKa object
• A point (x,y,z) can be the spatial location of a point light
• Ia=ambient light intensity source
• Ka=ambient reflection coefficient of the surface – • A vector (dx, dy, dz) can be the direction of a directional
light source
how much light it reflects (0-1) • As we shall see, many of the following calculations require
• Usually r,g,b triples a vector
• Either a directional light source is used, or the direction
• e.g. White light (1,1,1) x red object can be calculated by subtracting the intersection point
(1,0,0)=(1,0,0) (red) from the point light
Reflection of light Incident Light
Same shape at 90 and 45 degrees to the
incoming light vector surface
reflected light (specular)

surface normal
Light vector

Diffuse / Lambertian reflection

Light can be perfectly reflected. This is known as specular


reflection and is often rendered using Phong’s method. Light energy per unit area is higher when at 90o, compared to 45o.
Reflected light can be evenly spread. This is known as diffuse or Example: The sun striking at the equator compared to higher latitudes.
Lambertian reflection.

Incident Light Lambertian / Diffuse reflection


The incoming light at an intersection point (ray tracing) is I Light cos 
Light vector

w The outgoing light at an intersection point, that is, what we see at the
pixel is I out  I Light cos  , although, if cos<0, use 0* see lecture

cos  is the angle between the surface normal n and the direction to the light l.
A Both n and l are vectors.
w
A If we assume the vectors are normalised, cos  is the dot product of the two
cos  vectors
I out  I Light n.l

Light energy per unit area is higher when at 90o, compared to 45o. Remember, a vector (dx, dy, dz) can be normalised by finding its length, l:
Light Energy in box is I/w. Energy hitting the ground is I I cos 
 l dx 2  dy 2  dz 2
A w
and dividing each component by the length to get a new vector (dx/l, dy/l, dz/l)
Lambertian relection in colour cos
L N
• Assign each object a diffuse reflection coefficient kd in each • N = surface normal
colour channel (kd,r, kd,g, kd,b) where each is a value between 0
(absorb all light) and 1 (reflect all light)
 • L = direction to light
source
• Example, a red object will be (1,0,0) – absorbs blue and green
light, but reflects all red light Light directly above • cos= N.L/|N||L|
• The colour of a pixel Iout is Light at 90 degrees • N and L are usually
I out , r  K d , r I Light , r n.l Light behind (use zero) normalised during
I out , g  K d , g I Light , g n.l calculation, so
I out , b  K d , b I Light , b n.l • cos= N.L
• Note, we apply the equation in each colour channel (r,g,b)
• if cos<0, use 0

Specular Reflection Phong Reflection


Phong’s equation for specular reflection
incoming light vector, l reflected light, r
I out , r  K s , r I Light , r ( r .e ) n
surface normal, n

eye/pixel direction, e I out , g  K s , g I Light , g ( r .e ) n
I out , b  K s , b I Light , b ( r .e ) n
cos is calculated using the dot product of r and e
Ks is the specular reflection coeffiecient for the surface.

Simple Reflection Model


If the object is a perfect mirror, then the light will only be seen if =0
I out , c  K a , c I Light , c  K d , c I Light , c n .l  K s , c I Light , c ( r .e ) n
Less perfect mirrors will reflect light in a cone
Phong proposed that the reflected light is approximately cos n, where where c takes on the values of the colour channels: r, g, and b
n is chosen to be a shininess coefficient for the object Note the equation adds together the Ambient, Diffuse and Specular terms
1 Ray Tracing
1.1 Scene Definition
Objects are defined within world coordinates. For example, a Centre of
sphere at (2, 4, 6) with radius 10 could be defined using the equa- projection Scene domain
tion (x − 2)2 + (y − 4)2 + (z − 6)2 = 102 . Image plane
The ’camera’ is also defined in world coordinates. Its position,
orientation (known as view up vector) and its direction (known Figure 3: Perspective ray tracing, rays emit from the centre of
as view plane normal) must all be given (figure 1). Once these projection, through each pixel, and on to intersect (or not) objects
factors are known (along with the size of the image) rays can be within the scene.
fired through each pixel into the scene (the origin of the pixel is
~ and therefore the line representing
known, o, and the direction, d, 1.4 Ray / Object Intersection
the ray can be defined:
Ray tracing is the process of sending imaginary rays out from the
L = o + td~ (1) viewer’s eye into the scene of objects which we wish to visualise.
The object each ray hits is determined, and the colour for the
pixel that ray corresponds to is accordingly found. Naively we
where 0 ≤ t ≤ ∞.
have to determine if each ray intersects any of the objects in the
scene. We then have to find which object was the closest, and
obtain the colour of the object.
Typical objects used in ray tracing are those that can be inter-
sected with a 3D line (ray) easily. These include spheres, boxes,
cones, cylinders, swept surfaces and surfaces of revolution.
Object (defined using polygons) The equation of a 3D line is solved with this equation of the
World coordinate
system
object in question. One of the simplest equations to solve is that
of the line and a sphere which accounts for why we see so many
Image plane
spheres in ray tracing.
For example, all points, p, on a sphere can be defined using
Camera position and orientation its centre, c, and radius, r as (p − c)2 = r2 . We can substitute
the equation for the line (equation 1), into the equation for the
sphere as p:

Figure 1: Object defined within world coordinates, with camera


(o + td~ − c)2 = r2 (2)
position, orientation and image plane.
(o − c)2 + d~2 t2 + 2dt(o
~ − c) = r2 (3)
d~2 t2 + 2d(o
~ − c)t + (o − c)2 − r2 = 0 (4)

−b± b2 −4ac
1.2 Orthographic Projection Solving for t in at2 + bt + c = 0 gives t = 2a ,
and we can apply it in this case with a = d~2 , b = 2d(o ~ − c),
A ray is traced that originates at the pixel and travels parallel to c = (o − c) − r
2 2

all other pixels and orthogonal to the plane (Figures 2). Once the intersection has been found; the exact point of con-
tact is known; the surface normal is known; and the object hit is
known. The pixel can be given the appropriate colour by calcu-
lating the lighting equations at that point.
Disadvantage: sheer computational expense! For example; A
1000x1000 image requires 1 million rays to be intersected with
the scene domain. If the scene involves 1000 primitives (which
is quite low) then 1 billion calculations must be done for 1 frame.
Scene domain
Image plane 2 Recursive Ray Tracing
Figure 2: Orthographic projection for ray tracing. Why do we use ray tracing? Why not use a faster method? Ray
tracing is the most accurate rendering model so far (when ad-
vanced global illumination techniques are included). It closely
models the real physics of light, and therefore known properties
1.3 Perspective Projection of light such as frequency, different speeds of travel in a different
medium (refraction), intersurface absorption, colour, reflection,
A ray is traced that originates at the eye and travels though a etc. can all be simulated. Ray tracing research has progressed on
pixel in the image (Figures 3). two fronts - better realism and quicker ray tracing.
Recursive ray tracing is the process of continuing rays (or ini- Object

tiating new ones) at object intersection points so that we can de- p


2nd Object
θ
termine reflections, shadows and refractions. S
1 S
2

2.1 Reflection Figure 6: A ray refracted through a transparent object.


If an object is partially or totally reflective we can use the inter-
section point as the origin of a ray to find out what is reflected
θ = angle of refraction (physics of light moving from one
(Figure 4).
medium to another.)
p = primary ray.
Reflected S1 = first secondary ray.
S2 = second secondary ray.
Object
n
If the object is n% transparent the pixel will take on (100-n)%
of the objects colour and n% of the second object.
s
2.4 Putting it all together : Recursive ray tracing
θ θ
p
Each ray can spawn several rays (Figure 7) - each of which can
Object spawn other rays depending upon what they hit. If we are not
careful there could be a recursive explosion of rays which may
not terminate - e.g. a ray bouncing between two mirrors. We can
Figure 4: A ray being reflected. end this by counting the levels of recursion and stopping at some
cut-off point.
n = surface normal.
θ = angle of incidence (physics of reflection). n
p = primary ray (from pixel).
s = secondary ray (from object).
S
r
If the object is a total reflector (100%), the colour of the pixel p θ θ
will come from the secondary ray. If the object is a partial reflec-
tor of x%, the colour of the pixel is (100- x)% of the object, and
S Object S
x% of the secondary ray. Light l t

2.2 Shadows Figure 7: Recursive ray tracing.

If a ray hits an object, the object may or may not be lit. To de- p = primary ray.
termine this we shoot a secondary ray towards each light source n = surface normal.
and determine if it hits anything in-between. If it hits something Sl = secondary ray to light.
that point on the object is not lit, otherwise it is (Figure 5). St = secondary ray due to transparency (refracted).
Sr = secondary ray due to reflection.
Light 2 Light 1
Through the use of recursive ray tracing we can simulate re-
ality - partial mirrors, partially transparent objects, light sources.
When this is combined with conventional shading techniques we
have a very powerful way of producing accurate images. More
S
2 S1
advanced techniques simulate how a camera works. By doing
this we can access features such as depth of field, focus and
p
shutter speed (hence motion blurring). The only drawback is
Object the computational expense involved. We can go some way to ad-
dressing this problem by using the bounding boxes and spatial
subdivision.
Figure 5: Checking if a point can see the light sources.

In this case the object is only lit by one light (2). 3 Bounding Volumes
We shall look at the best algorithms for speeding up ray-tracing
2.3 Transparency
- bounding volumes and spatial subdivision.
If an object is partially or totally transparent then the pixel will Example: Consider a complex scene such as a chess board.
take on some of the colour of the object and some of the colour Each piece could consist of say 100 primitives. We also have the
of objects behind the partially transparent object (Figure 6). board itself. Altogether there would be around 3000 primitives
(by primitive we mean triangle, sphere, plane etc.) Using our y and z). Each box can further be divided into 2 in each axis and
1000x1000 image that would be 3 billion intersection calcula- so on. We regard this as an octree (8 splits).
tions. The easiest acceleration technique to use is the bounding We cover the scene with a box - this is our top node.
box. Each object is defined - and then has a box or sphere put Recursive step If the box contains some primitive in the scene,
around it which contains the object totally. Our scene can now we divide it into eight. We then test each primitive in the scene
be viewed as a board and 32 bounding boxes for the chess pieces. to see which box it is in. For each box it is in (it may be in more
Each ray is intersected with this scene and for each pixel that in- than one), we place a pointer from that box’s object list to this
tersects a bounding box, it is then intersected with the primitives object. We continue until we have located all parts of the scene.
in that box. We now have the case that many pixels will just be We then take each of the eight boxes in turn and repeat the
intersected with 33 objects, and those that intersect a bounding recursive step until we reach a certain predetermined tree-depth.
box will be intersected with the 100 primitives that comprise it. Usually if a box only contains a few primitives in its list (e.g. 10),
Our 1000x1000 image now requires around 100 million calcula- we would not split it any further, as we would be replacing a test
tions - 30 times faster. against 10 primitives with a test against 8 boxes plus whatever
primitives are in each box intersected (i.e. no benefit).
This is done once for our static scene. We can then use this
octree data structure to ray trace many views of the scene.

Figure 8: A ray traced chessboard (from POV-Ray).

If a ray hits a bounding volume, it will have to be tested with


every primitive inside that bounding volume. Obviously we re-
quire bounding volumes to fit the object as tightly as possible. In
Figure 9 we use both a cylinder and a sphere to bound the pawn.
The cylinder is the tighter fit, and correspondingly less rays will
hit the bounding volume than in the case of using a sphere.
Figure 10: An octree around a complex model. Note how sparse
regions are encoded using a large box, and detailed regions have
lots of small bounding boxes (recursion is high in these regions).
This image was produced by Sylvian Lefebvre

Referring to figure 10, the ray is tested with the top bounding
box, and if it is found to intersect, it will be tested with each of
the 8 children. If not, we have saved testing with the complex
Figure 9: Using a cylinder and a sphere as bounding volumes. model. If it intersects any children, the closest one is checked
by checking the ray against each of its 8 children and so on re-
cursively. This recursive search ends with leaf nodes. Each leaf
node is either empty (in which case we return, and the recursion
4 Spatial Subdivision ensures the next box is checked), or has a pointer to a list of ob-
jects with which the ray is checked. If the ray hits any of the
It should be obvious that using bounding boxes is a good idea. objects, we know it is the closest intersection because we have
We can take this one step further and say that; of those 100 prim- traversed the tree in order of proximity. We have therefore found
itives can we fit several boxes around groups of those primitives? the closest intersection by only testing boxes along the recursive
When testing against an actual chess piece we now check against descent and the few objects in the node. In the case of complex
say 4 bounding boxes, and then whichever the ray intersects we models we may have found our closest object with just a few
check against those 25 primitives. intersection test rather than with millions of intersection tests.
This should remind you of the binary tree - we can eliminate
up to half the search space at each stage by just one decision,
thus enabling us to get down to the item we require quickly.
We can extend the binary tree to 3D. Given a box surrounding
the whole scene we can split it into 2 in each of the three axis (x,
Pa
ge563f
rom Hea
rna
ndBa
ker

You might also like