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; }
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; }
static void shade( Scene* scene, double *eye, double *ray, Object* object, double *point, double *normal, int depth, float *out_color ) { Material *material = NULL; Light *light = NULL; int nlights, i; double reflectionFactor, specularExponent, refractedIndex, opacity, cos, sin; double V[VECTOR], Rr[VECTOR], Rt[VECTOR], vt[VECTOR], T[VECTOR], aux[VECTOR], lightpos[VECTOR], L[VECTOR], unitnormal[VECTOR], aux2[VECTOR], r[4]; float colorRr[COLOR], colorRt[COLOR], color [COLOR], ambient[COLOR], diffuse[COLOR], specular[COLOR], lightcolor[COLOR], reflecColor[COLOR]; sceGetMaterial( scene, objGetMaterial(object), &material ); matGetReflectionFactor( material, &reflectionFactor ); matGetSpecularExponent( material, &specularExponent ); matGetRefractionIndex ( material, &refractedIndex); matGetOpacity( material, &opacity ); sceGetAmbientLight( scene, ambient ); objTextureCoordinateAt( object, point, aux ); matGetDiffuse( material, aux, diffuse ); matGetSpecular( material, specular ); /* Come�a com a cor ambiente */ colorMultiplication( diffuse, ambient, color ); algUnit( normal, unitnormal ); /* Adiciona a componente difusa */ sceGetLightCount(scene, &nlights); /* numero de luzes na cena */ for( i = 0; i < nlights; i++ ) { sceGetLight( scene, i, &light ); /* luz i da cena */ lightGetColor( light, lightcolor ); /* cor da luz i */ lightGetPosition( light, lightpos ); /* posicao da luz i */ algSub( lightpos, point, L ); algUnit( L, L ); /* vetor do ponto para a luz i */ algDot( L, unitnormal, &cos ); /* cosseno com a normal */ if( cos > 0 && isInShadow( scene, point, L, lightpos ) == 0) /* se for visivel para a luz */ { colorReflection( cos, lightcolor, diffuse, reflecColor ); colorAddition( color, reflecColor, color ); } } /*Componente especular*/ algMinus( ray, V ); algUnit( V, V ); for( i = 0; i < nlights; i++ ) { sceGetLight( scene, i, &light ); /* ponteiro da luz i */ lightGetColor( light, lightcolor ); /* cor da luz i */ lightGetPosition( light, lightpos ); /* posicao da luz i */ algSub(lightpos,point, L); algUnit( L, L ); /* aponta para a Luz i */ algReflect( L, unitnormal, r ); /* reflex�o da luz i na normal */ algDot( r, V, &cos ); if( cos > 0 && isInShadow( scene, point, L, lightpos) == 0 ) { colorReflection( pow(cos, specularExponent), lightcolor, specular, reflecColor ); colorAddition( color, reflecColor, color); } } depth ++; /*Reflex�o*/ algReflect( V,unitnormal, Rr ); if( (reflectionFactor > 0.001) && (depth < MAX_DEPTH) ) { rayTrace( scene, point, Rr, depth, colorRr ); colorScale( reflectionFactor, colorRr, colorRr); colorAddition( color, colorRr, color ); } /*Transpar�ncia */ if( ((1-opacity) > 0.001) && (depth < MAX_DEPTH) ) { algProj( V, unitnormal, vt ); algSub( vt, V, vt ); algNorm( vt, &sin ); sin = (1.0/refractedIndex) * sin; cos = sqrt(1.-sin*sin); algUnit( vt, T ); algScale( sin, T, aux ); algScale(-cos, unitnormal, aux2 ); algAdd( aux, aux2, Rt ); //Rt=algMinus(V); rayTrace( scene, point, Rt, depth, colorRt ); colorScale( 1-opacity, colorRt, colorRt ); colorAddition(color, colorRt, color); } colorCopy( color, out_color ); }