Vec radiance(const Ray &r, int depth, unsigned short *Xi) { double t; // distance to intersection int id = 0; // id of intersected object if(!intersect(r, t, id)) return Vec(); // if miss, return black const Sphere &obj = spheres[id]; // the hit object Vec x = r.o + r.d*t, n = (x - obj.p).norm(), nl = n.dot(r.d) < 0 ? n : n*-1, f = obj.c; double p = f.x > f.y && f.x>f.z ? f.x : f.y > f.z ? f.y : f.z; // max refl if(depth > 255) return obj.e; if(++depth > 5) if(erand48(Xi) < p) f = f*(1 / p); else return obj.e; //R.R. if(obj.refl == DIFF) { // Ideal DIFFUSE reflection double r1 = 2 * M_PI*erand48(Xi), r2 = erand48(Xi), r2s = sqrt(r2); Vec w = nl, u = ((fabs(w.x) > .1 ? Vec(0, 1) : Vec(1)) % w).norm(), v = w%u; Vec d = (u*cos(r1)*r2s + v*sin(r1)*r2s + w*sqrt(1 - r2)).norm(); return obj.e + f.mult(radiance(Ray(x, d), depth, Xi)); } else if(obj.refl == SPEC) // Ideal SPECULAR reflection return obj.e + f.mult(radiance(Ray(x, r.d - n * 2 * n.dot(r.d)), depth, Xi)); Ray reflRay(x, r.d - n * 2 * n.dot(r.d)); // Ideal dielectric REFRACTION bool into = n.dot(nl) > 0; // Ray from outside going in? double nc = 1, nt = 1.5, nnt = into ? nc / nt : nt / nc, ddn = r.d.dot(nl), cos2t; if((cos2t = 1 - nnt*nnt*(1 - ddn*ddn)) < 0) // Total internal reflection return obj.e + f.mult(radiance(reflRay, depth, Xi)); Vec tdir = (r.d*nnt - n*((into ? 1 : -1)*(ddn*nnt + sqrt(cos2t)))).norm(); double a = nt - nc, b = nt + nc, R0 = a*a / (b*b), c = 1 - (into ? -ddn : tdir.dot(n)); double Re = R0 + (1 - R0)*c*c*c*c*c, Tr = 1 - Re, P = .25 + .5*Re, RP = Re / P, TP = Tr / (1 - P); return obj.e + f.mult(depth > 2 ? (erand48(Xi) < P ? // Russian roulette radiance(reflRay, depth, Xi)*RP : radiance(Ray(x, tdir), depth, Xi)*TP) : radiance(reflRay, depth, Xi)*Re + radiance(Ray(x, tdir), depth, Xi)*Tr); }
Vec radiance(const Ray &r, int depth, unsigned short *Xi,int E=1){ double t; // distance to intersection int id=0; // id of intersected object if (!intersect(r, t, id)) return Vec(); // if miss, return black const Sphere &obj = spheres[id]; // the hit object Vec x=r.o+r.d*t, n=(x-obj.p).norm(), nl=n.dot(r.d)<0?n:n*-1, f=obj.c; double p = f.x>f.y && f.x>f.z ? f.x : f.y>f.z ? f.y : f.z; // max refl if (++depth>5||!p) if (erand48(Xi)<p) f=f*(1/p); else return obj.e*E; if (obj.refl == DIFF){ // Ideal DIFFUSE reflection double r1=2*M_PI*erand48(Xi), r2=erand48(Xi), r2s=sqrt(r2); Vec w=nl, u=((fabs(w.x)>.1?Vec(0,1):Vec(1))%w).norm(), v=w%u; Vec d = (u*cos(r1)*r2s + v*sin(r1)*r2s + w*sqrt(1-r2)).norm(); // Loop over any lights Vec e; for (int i=0; i<numSpheres; i++){ const Sphere &s = spheres[i]; if (s.e.x<=0 && s.e.y<=0 && s.e.z<=0) continue; // skip non-lights Vec sw=s.p-x, su=((fabs(sw.x)>.1?Vec(0,1):Vec(1))%sw).norm(), sv=sw%su; double cos_a_max = sqrt(1-s.rad*s.rad/(x-s.p).dot(x-s.p)); double eps1 = erand48(Xi), eps2 = erand48(Xi); double cos_a = 1-eps1+eps1*cos_a_max; double sin_a = sqrt(1-cos_a*cos_a); double phi = 2*M_PI*eps2; Vec l = su*cos(phi)*sin_a + sv*sin(phi)*sin_a + sw*cos_a; l.norm(); if (intersect(Ray(x,l), t, id) && id==i){ // shadow ray double omega = 2*M_PI*(1-cos_a_max); e = e + f.mult(s.e*l.dot(nl)*omega)*M_1_PI; // 1/pi for brdf } } return obj.e*E+e+f.mult(radiance(Ray(x,d),depth,Xi,0)); } else if (obj.refl == SPEC) // Ideal SPECULAR reflection return obj.e + f.mult(radiance(Ray(x,r.d-n*2*n.dot(r.d)),depth,Xi)); Ray reflRay(x, r.d-n*2*n.dot(r.d)); // Ideal dielectric REFRACTION bool into = n.dot(nl)>0; // Ray from outside going in? double nc=1, nt=1.5, nnt=into?nc/nt:nt/nc, ddn=r.d.dot(nl), cos2t; if ((cos2t=1-nnt*nnt*(1-ddn*ddn))<0) // Total internal reflection return obj.e + f.mult(radiance(reflRay,depth,Xi)); Vec tdir = (r.d*nnt - n*((into?1:-1)*(ddn*nnt+sqrt(cos2t)))).norm(); double a=nt-nc, b=nt+nc, R0=a*a/(b*b), c = 1-(into?-ddn:tdir.dot(n)); double Re=R0+(1-R0)*c*c*c*c*c,Tr=1-Re,P=.25+.5*Re,RP=Re/P,TP=Tr/(1-P); return obj.e + f.mult(depth>2 ? (erand48(Xi)<P ? // Russian roulette radiance(reflRay,depth,Xi)*RP:radiance(Ray(x,tdir),depth,Xi)*TP) : radiance(reflRay,depth,Xi)*Re+radiance(Ray(x,tdir),depth,Xi)*Tr); }
Vec radiance(const Ray &ray, int depth, unsigned short *Xi) { float t; // distance to intersection int id; // id of intersected object Ray r=ray; // L0 = Le0 + f0*(L1) // = Le0 + f0*(Le1 + f1*L2) // = Le0 + f0*(Le1 + f1*(Le2 + f2*(L3)) // = Le0 + f0*(Le1 + f1*(Le2 + f2*(Le3 + f3*(L4))) // = ... // = Le0 + f0*Le1 + f0*f1*Le2 + f0*f1*f2*Le3 + f0*f1*f2*f3*Le4 + ... // // So: // F = 1 // while (1) { // L += F*Lei // F *= fi // } // accumulated color Vec cl(0,0,0); // accumulated reflectance Vec cf(1,1,1); while (1) { // if miss, return accumulated color (black) if (!intersect(r, t, id)) return cl; // the hit object const Solid &obj = *scene[id]; // calculate intersection point Vec x=r.o+r.d*t; // calculate surface normal vector in point x Vec n=obj.normal(x); Vec nl=n*r.d<0 ? n : n*-1; // object base color Vec f=obj.c; float p = f.x>f.y && f.x>f.z ? f.x : f.y>f.z ? f.y : f.z; // max refl cl = cl + cf.mult(obj.e); if (++depth>5) { if (erand48(Xi)<p) f=f*(1/p); else return cl; } // R.R. cf = cf.mult(f); if (obj.refl == DIFF) { // Ideal DIFFUSE reflection float r1=2*M_PI*erand48(Xi), r2=erand48(Xi), r2s=sqrt(r2); Vec w=nl, u=((fabs(w.x)>.1 ? Vec(0,1) : Vec(1))%w).norm(), v=w%u; Vec d = (u*cos(r1)*r2s+v*sin(r1)*r2s+w*sqrt(1-r2)).norm(); //return obj.e + f.mult(radiance(Ray(x,d), depth, Xi)); r = Ray(x, d); continue; } else if (obj.refl == SPEC) { // Ideal SPECULAR reflection //return obj.e + f.mult(radiance(Ray(x,r.d-n*2*(n*r.d)), depth, Xi)); r = Ray(x, r.d-n*2*(n*r.d)); continue; } // Ideal dielectric REFRACTION Ray reflRay(x, r.d-n*2*(n*r.d)); bool into = n*nl>0; // Ray from outside going in? float nc=1, nt=1.5, nnt=into ? nc/nt : nt/nc, ddn=r.d*nl, cos2t; if ((cos2t=1-nnt*nnt*(1-ddn*ddn))<0) { // Total internal reflection //return obj.e + f.mult(radiance(reflRay, depth, Xi)); r = reflRay; continue; } Vec tdir = (r.d*nnt-n*((into?1:-1)*(ddn*nnt+sqrt(cos2t)))).norm(); float a=nt-nc, b=nt+nc, R0=a*a/(b*b), c=1-(into?-ddn:tdir*n); float Re=R0+(1-R0)*c*c*c*c*c, Tr=1-Re, P=.25+.5*Re, RP=Re/P, TP=Tr/(1-P); //return obj.e + f.mult(depth>2 ? // Russian roulette // (erand48(Xi)<P ? radiance(reflRay, depth, Xi)*RP : radiance(Ray(x,tdir), depth, Xi)*TP) : // (radiance(reflRay, depth, Xi)*Re + radiance(Ray(x,tdir), depth, Xi)*Tr) ); if (erand48(Xi)<P) { cf = cf*RP; r = reflRay; } else { cf = cf*TP; r = Ray(x,tdir); } continue; } /* float t; // distance to intersection int id; // id of intersected object //fprintf(stderr, "r=(%f,%f,%f):(%f,%f,%f)\n", r.o.x, r.o.y, r.o.z, r.d.x, r.d.y, r.d.z); // if miss, return black if (!intersect(r, t, id)) return Vec(); // fprintf(stderr, "id=%d, t=%25.25f\n", id, t); // the hit object Solid& obj = *scene[id]; // calculate intersection point Vec x=r.o+r.d*t; // calculate surface normal vector in point x Vec n=obj.normal(x); Vec nl=n*r.d<0 ? n : n*-1; // object base color Vec f=obj.c; float p = f.x>f.y && f.x>f.z ? f.x : f.y>f.z ? f.y : f.z; // max refl if (++depth>5) { if (erand48(Xi)<p) f=f*(1/p); else return obj.e; } // R.R. if (obj.refl == DIFF) { // Ideal DIFFUSE reflection float r1=2*M_PI*erand48(Xi), r2=erand48(Xi), r2s=sqrt(r2); Vec w=nl, u=((fabs(w.x)>.1 ? Vec(0,1) : Vec(1))%w).norm(), v=w%u; Vec d = (u*cos(r1)*r2s+v*sin(r1)*r2s+w*sqrt(1-r2)).norm(); return obj.e + f.mult(radiance(Ray(x,d), depth, Xi)); } else if (obj.refl == SPEC) { // Ideal SPECULAR reflection return obj.e + f.mult(radiance(Ray(x,r.d-n*2*(n*r.d)), depth, Xi)); } // Ideal dielectric REFRACTION Ray reflRay(x, r.d-n*2*(n*r.d)); bool into = n*nl>0; // Ray from outside going in? float nc=1, nt=1.5, nnt=into ? nc/nt : nt/nc, ddn=r.d*nl, cos2t; if ((cos2t=1-nnt*nnt*(1-ddn*ddn))<0) // Total internal reflection return obj.e + f.mult(radiance(reflRay, depth, Xi)); Vec tdir = (r.d*nnt-n*((into?1:-1)*(ddn*nnt+sqrt(cos2t)))).norm(); float a=nt-nc, b=nt+nc, R0=a*a/(b*b), c=1-(into?-ddn:tdir*n); float Re=R0+(1-R0)*c*c*c*c*c, Tr=1-Re, P=.25+.5*Re, RP=Re/P, TP=Tr/(1-P); return obj.e + f.mult(depth>2 ? // Russian roulette (erand48(Xi)<P ? radiance(reflRay, depth, Xi)*RP : radiance(Ray(x,tdir), depth, Xi)*TP) : (radiance(reflRay, depth, Xi)*Re + radiance(Ray(x,tdir), depth, Xi)*Tr) ); */ }