1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 13 An Introduction to Ray Tracing

Preview:

Citation preview

1GR2-00

GR2Advanced Computer

GraphicsAGR

GR2Advanced Computer

GraphicsAGR

Lecture 13

An Introduction to Ray Tracing

2GR2-00

Ray TracingRay Tracing

Ray tracing is an alternative rendering approach to the polygon projection, shading and z-buffer way of working

It is capable of high quality images through taking account of global illumination

3GR2-00

Ray Tracing ExampleRay Tracing Example

4GR2-00

Firing RaysFiring Rays

pixel positionson view plane

camera

The view planeis marked with agrid correspondingto pixel positionson screen.

A ray is traced fromthe camera througheach pixel in turn.

light

5GR2-00

Finding IntersectionsFinding Intersections

pixel positionson view plane

camera

We calculate theintersection of the ray with all objectsin the scene.

The nearest inter-section will be thevisible surface forthat ray

light

6GR2-00

Intensity CalculationIntensity Calculation

pixel positionson view plane

camera

The intensity at this nearest intersectionis found from the usual Phongreflection model.

Stopping at thispoint gives us a verysimple renderingmethod.

Often called:ray casting

light

7GR2-00

Phong Reflection ModelPhong Reflection Model

lightsourceN

LR

Veye

surface

I() = Ka()Ia() + ( Kd()( L . N ) + Ks( R . V )n ) I*() / dist

Note: R.V calculation replaced by H.N for speed - H = (L+V)/2

dist = distance attenuation factor

Here V is direction of incoming ray, N is normal, L is direction tolight source, and R is direction of perfect spec. reflection.

8GR2-00

Intensity CalculationIntensity Calculation

pixel positionson view plane

camera

Thus Phong reflectionmodel gives us intensity at point based on alocal model:

I = I local

whereI local = I ambient +

I diffuse + I specular

Intensity calculatedseparately for red, green, blue

light

9GR2-00

ShadowsShadows

pixel positionson view plane

camera

To determine ifthe point is in shadow,we can fire a ray atthe light source(s) -in direction L.

If the ray intersectsanother object, then point is in shadowfrom that light.In this case, we just useambient component:I local = I ambient

Otherwise the point isfully lit.

light

shadowray

10GR2-00

Reflected RayReflected Ray

pixel positionson view plane

camera

Ray tracing modelsreflections by lookingfor light coming inalong a secondaryray, making a perfectspecular reflection.

R1 = V - 2 (V.N) N

R1

light

11GR2-00

Reflected Ray- Intersection and Recursion

Reflected Ray- Intersection and Recursion

We calculate theintersection of thisreflected ray, with allobjects in the scene.

The intensity at thenearest intersection point is calculated, and added as a contributionattenuated by distance.

This is done recursively.pixel positionson view plane

camera

light

12GR2-00

Reflected Ray - Intensity Calculation

Reflected Ray - Intensity Calculation

pixel positionson view plane

camera

light

The intensity calculationis now:I = I local + k r * I reflected

Here I reflected

is calculated recursively

k r is a reflection coefficient (similar to k s )

13GR2-00

Ray TerminationRay Termination

pixel positionson view plane

camera

Rays terminate:- on hitting diffusesurface- on going to infinity- after severalreflections - why?

14GR2-00

Transmitted RayTransmitted Ray

If the object is semi-transparent, we alsoneed to take into account refraction

pixel positionson view plane

camera

light

Thus we follow alsotransmitted rays,eg T1.

T1

15GR2-00

RefractionRefraction

N

V

T

ir

r

i Change in directionis determined by therefractive indices ofthe materials, i and r

Snell’s Law:sin r = (i / r ) * sin i

T = (i / r ) V - ( cos r - (i / r ) cos i ) N

16GR2-00

Refraction ContributionRefraction Contribution

The contribution due to transmitted light is taken as:

kt * It( ) – where

kt is the transmission coefficient

It( ) is the intensity of transmitted light, again calculated separately for red, green, blue

17GR2-00

Intensity CalculationIntensity Calculation

pixel positionson view plane

camera

The intensity calculationis now:I = I local + k r * I reflected

+ k t * I transmitted

I reflected and I transmitted

are calculated recursively

18GR2-00

Binary Ray Tracing TreeBinary Ray Tracing Tree

S1

S2 S3

S4

pixel positionson view plane

camera

S1

S2 S3

S4

R1T1

T1R1

The intensity iscalculated bytraversing the treeand accumulatingintensities

light

19GR2-00

Calculating Ray Intersections

Calculating Ray Intersections

A major part of ray tracing calculation is the intersection of ray with objects

Two important cases are:– sphere– polygon

20GR2-00

Ray - Sphere IntersectionRay - Sphere Intersection

Let camera position be (x1, y1, z1)Let pixel position be (x2, y2, z2)

Any point on ray is:x(t) = x1 + t * (x2 - x1) = x1 + t * iy(t) = y1 + t * (y2 - y1) = y1 + t * jz(t) = z1 + t * (z2 - z1) = z1 + t * k

As t increases from zero, x(t), y(t), z(t) traces outthe line from (x1, y1, z1) through (x2, y2, z2).

Equation of sphere, centre (l, m, n) and radius r:(x - l)2 + (y - m)2 + (z - n)2 = r2

21GR2-00

Ray - Sphere IntersectionRay - Sphere Intersection

Putting the parametric equations for x(t), y(t), z(t) inthe sphere equation gives a quadratic in t:

at2 + bt + c = 0

Exercise: write down a, b, c in terms of i, j, k, l, m, n, x1, y1, z1

Solving for t gives the intersection points:

b2 - 4ac < 0 no intersectionsb2 - 4ac = 0 ray is tangentb2 - 4ac > 0 two intersections, we want smallest positive

22GR2-00

Ray - Polygon IntersectionRay - Polygon Intersection

Equation of ray:x(t) = x1 + t * iy(t) = y1 + t * jz(t) = z1 + t * k

Equation of plane:ax + by + cz + d = 0

Intersection:t = - (ax1 + by1 + cz1 + d) / (ai + bj + ck)

Need to check intersection within the extent of the polygon.

23GR2-00

Ray Tracing - LimitationsRay Tracing - Limitations

Ray tracing in its basic form is computationally intensive

Much research has gone into increasing the efficiency and this will be discussed in next lecture.

24GR2-00

Ray Tracing ExampleRay Tracing Example

25GR2-00

Depth 0Depth 0

26GR2-00

Depth 1Depth 1

27GR2-00

Depth 2Depth 2

28GR2-00

Depth 3Depth 3

29GR2-00

Depth 4Depth 4

30GR2-00

Depth 7Depth 7

31GR2-00

AcknowledgementsAcknowledgements

As ever, thanks to Alan Watt for the excellent images