Docsity

Raytracing Algorithm: Hit Function for Sphere and Adding Ambient, Diffuse, Specular, Slides of Computer Graphics

Partial preview of the text

Download Raytracing Algorithm: Hit Function for Sphere and Adding Ambient, Diffuse, Specular and more Slides Computer Graphics in PDF only on Docsity!

Computer Graphics Raytracing (Part 3)

hit( ) Function for Sphere

n Recall that for generic sphere, there are two hit times, t1 and t corresponding to the solutions

n which are the solutions to the quadratic equation A t^2 + 2 Bt + C = 0 where A = | c |^2 , B = S. c and C = | S|^2 – 1 n Thus the hit( ) function for a sphere is as follows:

Bool Sphere::hit(Ray &r, Intersection inter) { Ray genRay; // need to make the generic ray xfrmRay(genRay, invTransf, r); double A, B, C ………

A

B AC

A

B

th

2

Hit( ) Function for Sphere

If(t1 > 0.00001) // is hit in front of the eye? { inter.hit[0].hitTime = t1; inter.hit[0].hitObject = this; inter.hit[0].isEntering = true; inter.hit[0].surface = 0; Point3 P(rayPos(genRay, t1)); // hit spot inter.hit[0].hitPoint.set(P); inter.hit[0].hitNormal.set(P); num = 1; // have a hit }

A

B AC

A

B

th

2

Hit( ) Function for Sphere

double t2 = (-B + discRoot)/A; // the later hit

If(t2 > 0.00001) // is hit in front of the eye? { inter.hit[num].hitTime = t2; inter.hit[num].hitObject = this; inter.hit[num].isEntering = false; inter.hit[num].surface = 0; Point3 P(rayPos(genRay, t2)); // hit spot inter.hit[num].hitPoint.set(P); inter.hit[num].hitNormal.set(P); num++; // have a hit } inter.numHits = num; return (num > 0); // true of false }

A

B AC

A

B

th

2

Emissive shade( ) Function

Color3 Scene :: shade(Ray& ray) // is hit in front of the eye? { Color3 color; Intersection best; getFirstHit(ray, best); if(best.numHits == 0) return background; Shape* myObj = (Shape*)best.hit[0].hitObject; // hit object color.set(myObj->mtrl.emissive); return color; } n Need hit functions for more shapes (cube, square, cylinder, etc) n At this point, will take things out of order.. n Jump to section 12.7 to add ambient diffuse, specular n Return later to do more intersections

Adding Ambient, Diffuse, Specular to shade( ) Function

n Recall Phong’s illumination model

n Where light vector s = Light positon – hit Point n View vector v = -dir

Ph

v

s m r dir

f

I = Ia ka + Idkd × lambert + Ispks × phong

max 0 ,

s m

s m

lambert 

max 0 ,

h m

h m

phong

Finding Normal at Hit Spot

n How do we get transformed normal? n We set generic object normal at hit point n E.g. in sphere hit function, set hit point normal = hit point for generic sphere, we did

inter.hit[0].hitNormal.set(P);

n So, we have normal for generic object m’ n To get transformed object normal m , simply (see section 6.5.3)

m = M − T m '

Adding Ambient, Diffuse, Specular to shade( ) Function

n You specify ambient, diffuse and specular values of materials in your SDL file n Each cylinder, cube is a GeomObj class n GeomObj class has material mtrl as member n Your ray tracer can then access ka, kd … as mtrl.ambient , mtrl.diffuse and mtrl.specular n For more realistic look, can use carefully measure values from McReynolds and Blythe. ! first define values def Copper{ ambient 0.19125 0.0735 0. diffuse 0.7038 0.27048 0. specular 0.256777 0.137622 0.086014 exponent 12.8}

!then use defined values Use Copper sphere

Adding Ambient, Diffuse, Specular to shade( ) Function

Vector3 normal;

// transform the generic normal to the world normal xfrmNormal(normal, myObj->invTransf, h.hitNormal); normal.normalize( ); // normalize it for(each light source, L) // sum over all sources { if(isInShadow(..)) continue; // skip L if it’s in shadow Form s = L.pos – hitPoint; // vector from hit pt to src s.normalize( ); float mDotS = s.dot(normal); // Lambert term if(mDotS > 0.0){ // hit point is turned toward the light Form diffuseColor = mDotS * myObj->mtrl.diffuse * L.color color.add(diffuseColor); // add the diffuse part }

= ^ •

max 0 ,

s m

lambert s m

Adding Ambient, Diffuse, Specular to shade( ) Function

Form h = v + s; // the halfway vector h.normalize( ); float mDotH = h.dot(normal); // part of phong term if(mDotH <= 0) continue; // no specular contribution

float phong = pow(mDotH, myObj->mtrl.specularExponent); specColor = phong * myObj->mtrl.specular * L.color; color.add(specColor); } return color; }

n isInShadow( ) is function to tests if point is in shadow. Implement next!

= ^ •

max 0 ,

h m

phong h m

Adding Shadows

n Need routine isInShadow( ) which tests to see if hit point

is in shadow

n isInShadow( ) returns

n true if hit point is in shadow n false otherwise

n isInShadow( ) spawns new ray called shadow feeler

emanating from hit point at time t=0 and reaching light

source at t=

n So, parametric equation of shadow feeler is Ph + (L – Ph)t

n So, shadow feeler is built and each object in object list is

scanned for intersection (just like eye ray)

n If any valid intersection in time range t=[0,1]

isInShadow returns true, otherwise returns false

Adding Shadows

n Note : since we made hit function general, takes ray as

argument, once we build shadow feeler, reuse hit( ) functions

n One more sticky point: self-shadowing!!

n How? Since shadow feeler starts at hit point at t=0,

isInShadow always intersects with object itself (returns true)

n Can fix this by starting shadow ray slightly away from hit

point. E.g. in figure, start shadow feeler starts at Ph-ε dir

L

Ph^ Ph-ε^ dir

Note: feeler is ε

toward eye NOT light

Shade Function with Shadow Pseudocode

feeler.start = hitPoint - ε ray.dir;

feeler.recurseLevel = 1;

color = ambient part;

for(each light source, L)

feeler.dir = L.pos - hitPoint;

if(isInShadow(feeler)) continue;

color.add(diffuse light);

color.add(specular light);

isInShadow( ) Implementation

bool Scene:: isInShadow(Ray& f)

for(GeomObj* p = obj; p; p = p->next)

if(p->hit(f)) return true;

return false;

n Above, we use simplified hit( ) function

n Only tests for hit time between 0 and 1 n If valid hit, return, don’t fill hit record, hit object, etc