Ray Tracing 2
Ray Tracing 2
Ray Tracing 2
• 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?
Light occluder
Shadow ray
Intersection point
Indirect illumination
Total/partial reflector
surface normal
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
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
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.
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