













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!














Computer Graphics Raytracing (Part 3)
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 ………
2
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 }
2
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 }
2
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
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
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)
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
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 }
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!
n true if hit point is in shadow n false otherwise
Ph^ Ph-ε^ dir
n Only tests for hit time between 0 and 1 n If valid hit, return, don’t fill hit record, hit object, etc