Advertisement
alex_dot

sphere.cpp

Aug 24th, 2013
535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. std::pair<int,double> const Sphere::getIntersection(Ray const& input_ray, bool shadow_ray) const
  2. {
  3.   std::pair<int,double> pair;
  4.     double t;
  5.     Ray ray = input_ray;
  6.  
  7.   if(shadow_ray == false)
  8.     ray = Ray(inv_t_matrix_*ray.origin,inv_t_matrix_*ray.dir);
  9.  
  10.     // double A = 1; (unused)
  11.     double B = 2*(
  12.           ray.dir[0]*(ray.origin[0]-center_[0])
  13.            + ray.dir[1]*(ray.origin[1]-center_[1])
  14.            + ray.dir[2]*(ray.origin[2]-center_[2]));
  15.     double C = pow(ray.origin[0]-center_[0],2)
  16.            + pow(ray.origin[1]-center_[1],2)
  17.            + pow(ray.origin[2]-center_[2],2) - radius_pow2_;
  18.     double discr = B*B-4*C;
  19.  
  20.     if(discr > 0)
  21.     {
  22.         t = (-B - sqrt(discr))*0.5;
  23.         if(t > 0.00001)
  24.           pair = std::make_pair(1,t);
  25.         if(t < 0)
  26.         {
  27.             t = (-B + sqrt(discr))*0.5;
  28.             if(t > 0.00001)
  29.               pair = std::make_pair(1,t);
  30.         }
  31.         else
  32.           pair = std::make_pair(1,t);
  33.     }
  34.     else
  35.         pair = std::make_pair(0,0);
  36.  
  37.     return pair;
  38. }
  39. Color const Sphere::getColor(math3d::point const& intersection_point, std::shared_ptr<Scene> const& scene) const
  40. {
  41.   double red, green, blue;
  42.   double dot_normal_light, dot_normal_reflection;
  43.   math3d::point center = t_matrix_*center_;
  44.   math3d::vector sphere_normal = math3d::vector(
  45.                             intersection_point[0]-center[0],
  46.                             intersection_point[1]-center[1],
  47.                             intersection_point[2]-center[2]
  48.                           ).normalize();
  49.   sphere_normal = math3d::transpose(inv_t_matrix_)*sphere_normal;
  50.   std::vector<std::shared_ptr<SFDS>> lights = scene->shadowFeeler(intersection_point);
  51.   Color ambiance = scene->getAmbiance();
  52.   red = ambiance[0]*material_->ka[0];
  53.   blue = ambiance[1]*material_->ka[1];
  54.   green = ambiance[2]*material_->ka[2];
  55.  
  56.   for(std::vector<std::shared_ptr<SFDS>>::iterator iter = lights.begin(); iter!=lights.end(); ++iter)
  57.   {
  58.     if((*iter)->delta != 0)
  59.     {
  60.       dot_normal_light = math3d::dot(sphere_normal,(*iter)->vector);
  61.       dot_normal_reflection = math3d::dot(2*(dot_normal_light)*sphere_normal-(*iter)->vector,sphere_normal);
  62.       if(dot_normal_light < 0) dot_normal_light = 0;
  63.       red +=   (*iter)->delta
  64.                 * (*iter)->light->dif_[0]
  65.                 * (material_->kd[0]*dot_normal_light
  66.                  + pow(material_->ks[0]*dot_normal_reflection,
  67.                        material_->m));
  68.       green += (*iter)->delta
  69.                 * (*iter)->light->dif_[1]
  70.                 * (material_->kd[1]*dot_normal_light
  71.                  + pow(material_->ks[1]*dot_normal_reflection,
  72.                        material_->m));
  73.       blue +=  (*iter)->delta
  74.                 * (*iter)->light->dif_[2]
  75.                 * (material_->kd[2]*dot_normal_light
  76.                  + pow(material_->ks[2]*dot_normal_reflection,
  77.                        material_->m));
  78.     }
  79.   }
  80.   Color color = Color(red,blue,green);
  81.   return color;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement