Color3 Raytracer::shadeDirectBSDF(const SurfaceSample& intersector, const Ray& ray) const { Color3 lightContribution(0,0,0); for (int i = 0; i < _currentScene->lighting()->lightArray.length(); ++i) { GLight& light = _currentScene->lighting()->lightArray[i]; const Point3& X = intersector.shadingLocation; const Point3& n = intersector.shadingNormal; const Vector3& diff = light.position.xyz() - X; const Vector3 w_i = diff.direction(); const float distance = diff.length(); const Vector3 w_eye = -ray.direction(); const Power3& Phi = light.color.rgb(); if (!isInSpotlight(light, w_i)) { continue; } if (isInShadow(intersector, w_i, distance)) { continue; } // power area const Color3& E_i = w_i.dot(n) * Phi / (4 * pif() * (distance*distance)); const SuperBSDF::Ref bsdf = intersector.material->bsdf(); const Color3& bsdfColor = bsdf->evaluate(intersector.shadingNormal, intersector.texCoord, w_i, w_eye).rgb(); lightContribution += bsdfColor * E_i + intersector.emit; } return lightContribution; }
color_fixed raytracer ( ray *r, kdTreeNode* currentPacket, scene *myScene ) { // Initialing variables color_fixed c1 = {0,0,0}; //where to keep the color values node rayNodes[MAXRAYS]; node* freeRayNodes; freeRayNodes = initFreeNodeList( rayNodes ); //GENERATE TREE node* root = requestNode( &freeRayNodes ); if ( root != NULL ) { //cout << "root full" << endl; initNode( root, NULL, currentPacket, *r, NULL, 0, ROOT_RAY, OUTSIDE ); node * courant = root; //adding and inserting nodes while ( courant != NULL ) { // cout<< "ray: x " << courant->v.start.x // << " y"<< courant->v.start.y // <<" z " << courant->v.start.z <<endl; // cout << " Not arrived to the root yet" << endl; // cout << "ray start x"<< r.start.x // << " y " << r.start.y // << " z " << r.start.z <<endl; // cout << "ray dir x"<< r.dir.x // << " y " << r.dir.y // << " z " << r.dir.z <<endl; // cout << "r start x"<< courant->v.start.x // << " y " << courant->v.start.y // << " z " << courant->v.start.z <<endl; // cout << "r dir x"<< courant->v.dir.x // << " y " << courant->v.dir.y // << " z " << courant->v.dir.z <<endl; // cout << "intersection ray" << endl; courant->lastIntercepted = figureIntersection( courant, myScene, &courant->t ); c1 += lightContribution ( courant, myScene ); //cout << "intersection surface" << endl; courant = treeGenerator( &freeRayNodes, MAXDEPTH, courant, myScene ); } } else { //cout << "no node available" << endl; } //the tree will be destroyed when exiting the function return c1; }
Color3 Raytracer::myShadePixel(const Tri::Intersector& intersector, const Ray& ray, int backwardBouncesLeft) const { ray; backwardBouncesLeft; Vector3 position, normal; Vector2 texCoord; intersector.getResult(position, normal, texCoord); SurfaceSample surfaceSample(intersector); Color3 lightContribution(0,0,0); for (int i = 0; i < _currentScene->lighting()->lightArray.length(); ++i) { GLight& light = _currentScene->lighting()->lightArray[i]; const float lightDistance = light.distance(position); const Power3 power = light.power().rgb(); check(power.average()); const float lightArea = 4 * pif() * lightDistance * lightDistance; const Color3 kx = surfaceSample.lambertianReflect; const Vector3 light_in = (light.position.xyz() - position).unit(); const float lightIn_dot_normal = light_in.dot(normal); // Spotlight if (!isInSpotlight(light, light_in)) { continue; } if (isInShadow(surfaceSample, light_in, lightDistance)) { continue; } Color3 fx(0, 0, 0); if (lightIn_dot_normal > 0) { fx = kx / pif(); } lightContribution += (power / lightArea) * lightIn_dot_normal * fx; } return lightContribution; }