Presentation on theme: "GR2 Advanced Computer Graphics AGR"— Presentation transcript:
1GR2 Advanced Computer Graphics AGR Lecture 13An Introduction to Ray Tracing
2Ray TracingRay tracing is an alternative rendering approach to the polygon projection, shading and z-buffer way of workingIt is capable of high quality images through taking account of global illumination
4Firing Rays The view plane is marked with a grid corresponding pixel positionson view planecameralightThe view planeis marked with agrid correspondingto pixel positionson screen.A ray is traced fromthe camera througheach pixel in turn.
5Finding Intersections pixel positionson view planecameralightWe calculate theintersection of theray with all objectsin the scene.The nearest inter-section will be thevisible surface forthat ray
6Intensity Calculation pixel positionson view planecameralightThe intensity at thisnearest intersectionis found from theusual Phongreflection model.Stopping at thispoint gives us a verysimple renderingmethod.Often called:ray casting
7Phong Reflection Model lightsourceNReyeLVsurfaceI() = Ka()Ia() + ( Kd()( L . N ) + Ks( R . V )n ) I*() / distdist = distance attenuation factorHere V is direction of incoming ray, N is normal, L is direction tolight source, and R is direction of perfect spec. reflection.Note: R.V calculation replaced by H.N for speed - H = (L+V)/2
8Intensity Calculation pixel positionson view planecameralightThus Phong reflectionmodel gives us intensityat point based on alocal model:I = I localwhereI local = I ambient +I diffuse + I specularIntensity calculatedseparately for red,green, blue
9Shadows To determine if the point is in shadow, we can fire a ray at pixel positionson view planecameralightTo determine ifthe point is in shadow,we can fire a ray atthe light source(s) -in direction L.If the ray intersectsanother object, thenpoint is in shadowfrom that light.In this case, we just useambient component:I local = I ambientOtherwise the point isfully lit.shadowray
10Reflected Ray R1 R1 = V - 2 (V.N) N Ray tracing models pixel positionson view planecameralightRay tracing modelsreflections by lookingfor light coming inalong a secondaryray, making a perfectspecular reflection.R1 = V - 2 (V.N) NR1
11Reflected Ray- Intersection and Recursion pixel positionson view planecameralightWe calculate theintersection of thisreflected ray, with allobjects in the scene.The intensity at thenearest intersectionpoint is calculated, andadded as a contributionattenuated by distance.This is done recursively.
12Reflected Ray - Intensity Calculation pixel positionson view planecameralightThe intensity calculationis now:I = I local + k r * I reflectedHere I reflectedis calculated recursivelyk r is a reflectioncoefficient (similar to k s )
13Ray Termination Rays terminate: - on hitting diffuse surface pixel positionson view planecameraRays terminate:- on hitting diffusesurface- on going to infinity- after severalreflections - why?
14Transmitted Ray If the object is semi- transparent, we also pixel positionson view planecameraIf the object is semi-transparent, we alsoneed to take intoaccount refractionlightT1Thus we follow alsotransmitted rays,eg T1.
15Refraction r i r i Snell’s Law: sin r = ( i / r ) * sin i Change in directionis determined by therefractive indices ofthe materials, i and rTrNiVSnell’s Law:sin r = ( i / r ) * sin iT = ( i / r ) V - ( cos r - ( i / r ) cos i ) N
16Refraction Contribution The contribution due to transmitted light is taken as:kt * It( )wherekt is the transmission coefficientIt( ) is the intensity of transmitted light, again calculated separately for red, green, blue
17Intensity Calculation pixel positionson view planecameraThe intensity calculationis now:I = I local + k r * I reflected+ k t * I transmittedI reflected and I transmittedare calculated recursively
18Binary Ray Tracing Tree S4pixel positionson view planecameralightS1T1R1S2S3R1S2S3T1S1S4The intensity iscalculated bytraversing the treeand accumulatingintensities
19Calculating Ray Intersections A major part of ray tracing calculation is the intersection of ray with objectsTwo important cases are:spherepolygon
20Ray - 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 * kAs 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
21Ray - Sphere Intersection Putting the parametric equations for x(t), y(t), z(t) inthe sphere equation gives a quadratic in t:at2 + bt + c = 0Exercise: write down a, b, c in terms of i, j, k, l, m, n, x1, y1, z1Solving for t gives the intersection points:b2 - 4ac < 0 no intersectionsb2 - 4ac = 0 ray is tangentb2 - 4ac > 0 two intersections, we want smallest positive
22Ray - Polygon Intersection Equation of ray:x(t) = x1 + t * iy(t) = y1 + t * jz(t) = z1 + t * kEquation of plane:ax + by + cz + d = 0Intersection:t = - (ax1 + by1 + cz1 + d) / (ai + bj + ck)Need to check intersection within the extent of the polygon.
23Ray Tracing - Limitations Ray tracing in its basic form is computationally intensiveMuch research has gone into increasing the efficiency and this will be discussed in next lecture.