Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::pair<int,double> const Sphere::getIntersection(Ray const& input_ray, bool shadow_ray) const
- {
- std::pair<int,double> pair;
- double t;
- Ray ray = input_ray;
- if(shadow_ray == false)
- ray = Ray(inv_t_matrix_*ray.origin,inv_t_matrix_*ray.dir);
- // double A = 1; (unused)
- double B = 2*(
- ray.dir[0]*(ray.origin[0]-center_[0])
- + ray.dir[1]*(ray.origin[1]-center_[1])
- + ray.dir[2]*(ray.origin[2]-center_[2]));
- double C = pow(ray.origin[0]-center_[0],2)
- + pow(ray.origin[1]-center_[1],2)
- + pow(ray.origin[2]-center_[2],2) - radius_pow2_;
- double discr = B*B-4*C;
- if(discr > 0)
- {
- t = (-B - sqrt(discr))*0.5;
- if(t > 0.00001)
- pair = std::make_pair(1,t);
- if(t < 0)
- {
- t = (-B + sqrt(discr))*0.5;
- if(t > 0.00001)
- pair = std::make_pair(1,t);
- }
- else
- pair = std::make_pair(1,t);
- }
- else
- pair = std::make_pair(0,0);
- return pair;
- }
- Color const Sphere::getColor(math3d::point const& intersection_point, std::shared_ptr<Scene> const& scene) const
- {
- double red, green, blue;
- double dot_normal_light, dot_normal_reflection;
- math3d::point center = t_matrix_*center_;
- math3d::vector sphere_normal = math3d::vector(
- intersection_point[0]-center[0],
- intersection_point[1]-center[1],
- intersection_point[2]-center[2]
- ).normalize();
- sphere_normal = math3d::transpose(inv_t_matrix_)*sphere_normal;
- std::vector<std::shared_ptr<SFDS>> lights = scene->shadowFeeler(intersection_point);
- Color ambiance = scene->getAmbiance();
- red = ambiance[0]*material_->ka[0];
- blue = ambiance[1]*material_->ka[1];
- green = ambiance[2]*material_->ka[2];
- for(std::vector<std::shared_ptr<SFDS>>::iterator iter = lights.begin(); iter!=lights.end(); ++iter)
- {
- if((*iter)->delta != 0)
- {
- dot_normal_light = math3d::dot(sphere_normal,(*iter)->vector);
- dot_normal_reflection = math3d::dot(2*(dot_normal_light)*sphere_normal-(*iter)->vector,sphere_normal);
- if(dot_normal_light < 0) dot_normal_light = 0;
- red += (*iter)->delta
- * (*iter)->light->dif_[0]
- * (material_->kd[0]*dot_normal_light
- + pow(material_->ks[0]*dot_normal_reflection,
- material_->m));
- green += (*iter)->delta
- * (*iter)->light->dif_[1]
- * (material_->kd[1]*dot_normal_light
- + pow(material_->ks[1]*dot_normal_reflection,
- material_->m));
- blue += (*iter)->delta
- * (*iter)->light->dif_[2]
- * (material_->kd[2]*dot_normal_light
- + pow(material_->ks[2]*dot_normal_reflection,
- material_->m));
- }
- }
- Color color = Color(red,blue,green);
- return color;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement