Color Phong::getPhongColor(const IntersectionInfo &info, const Ray &ray) { Color res=COLOR_BLACK; for (int i=0;i<(int)light.size();i++) { Light *lt=light[i]; Vector directionToLight=lt->position-info.point; //without being normalized Object *block=getNearestObject( Ray(info.point+directionToLight*EPS,directionToLight,0,ray.side),directionToLight.mod()); //move a little to the light if (block!=NULL) continue; Color now=COLOR_BLACK; directionToLight.normalize(); //diffuse real nowDot=dot(directionToLight,info.norm); //attention!!! if (nowDot>EPS) now+=info.material->getColorAt(info.point)*lt->color*lt->intensity*nowDot*info.material->diffuse; //specular Vector directionToEye=(ray.origin-info.point).getNormalize(); real half=info.norm.dot((directionToLight+directionToEye)/2); if (half>0) now+=lt->color*lt->intensity*info.material->specular*pow(half,info.material->shininess); //Beer-Lambert's Law real distanceToLight=(lt->position-info.point).mod(); res+=now*pow(M_E,-distanceToLight*AIR_BEER_DENSITY); } return res; }
void rayTrace( Scene* scene, double *eye, double *ray, int depth, float *out_color ) { Object* object; double distance; double point[VECTOR], normal[VECTOR]; /* Calcula o primeiro objeto a ser atingido pelo raio */ getNearestObject( scene, eye, ray, &object, &distance ); /* Se o raio n�o interceptou nenhum objeto... */ if( distance == DBL_MAX ) { sceGetBackgroundColor( scene, eye, ray, out_color ); return; } /* Calcula o ponto de interse��o do raio com o objeto */ algScale( distance, ray, point ); algAdd( eye, point, point ); /* Obt�m o vetor normal ao objeto no ponto de interse��o */ objNormalAt( object, point, normal ); shade( scene, eye, ray, object, point, normal, depth, out_color ); }
Color Phong::rayTracing(Ray &ray,int depth) { if (depth>MAX_TRACE_DEPTH) return COLOR_BLACK; Object *obj=getNearestObject(ray); if (obj==NULL) return COLOR_BLACK; IntersectionInfo info=obj->getIntersecionInfo(ray); //ambient Color nowAmbient=ambient*(info.material->getColorAt(info.point))*info.material->ambient; //diffuse + specular Color nowColor=getPhongColor(info,ray); //Beer-Lambert's Law ray.distance+=info.distance; nowColor*=pow(M_E,-ray.distance*AIR_BEER_DENSITY); //reflection Color nowReflection=getReflectionColor(info,ray,depth); //refraction Color nowRefraction=getRefractionColor(info,ray,depth); Color res=nowAmbient+nowColor+nowReflection+nowRefraction; res.normalize(); return res; }