Vector_3D<double> Sphere:: Normal(const Vector_3D<double>& location) const { Vector_3D<double> normal; normal = (location - center); normal.Normalize(); return normal; }
Vector_3D<double> Sphere:: Normal(const Vector_3D<double>& location) const { Vector_3D<double> normal; normal.x = (location.x - center.x); normal.y = (location.y - center.y); normal.z = (location.z - center.z); normal.Normalize(); return normal; }
//-------------------------------------------------------------------------------- // Shader //-------------------------------------------------------------------------------- Vector_3D<double> Phong_Shader:: Shade_Surface(const Ray& ray,const Object& intersection_object,const Vector_3D<double>& intersection_point,const Vector_3D<double>& same_side_normal) const { Vector_3D<double> color; Vector_3D<double> ambientCoefficient = color_ambient; Vector_3D<double> diffuseReflectance = color_diffuse; // TODO: determine the color //compute each R G B value seperately //ambient ambientCoefficient.x *= world.lights.at(0)->Emitted_Light(ray).x; ambientCoefficient.y *= world.lights.at(0)->Emitted_Light(ray).y; ambientCoefficient.z *= world.lights.at(0)->Emitted_Light(ray).z; color += ambientCoefficient; for (unsigned i = 0; i < world.lights.size(); ++i) { //test for shadows Vector_3D<double> lightVector = (((world.lights.at(i))->position)-intersection_point); //tempray Ray tempRay; tempRay.endpoint = intersection_point; tempRay.direction = lightVector; tempRay.direction.Normalize(); tempRay.t_max = 999999; tempRay.semi_infinite = true; bool shadowDetected = false; for (unsigned j = 0; j < world.objects.size(); ++j) { ////calculate if object intersects if (world.objects.at(j)->Intersection(tempRay)) { if (!(tempRay.current_object == &intersection_object)) { shadowDetected = true; } } } if (!(world.enable_shadows && shadowDetected)) { Vector_3D<double> normal = intersection_object.Normal(intersection_point); //diffuse shading Vector_3D<double> diffuseIntensity = world.lights.at(i)->Emitted_Light(ray); lightVector.Normalize(); double dotProduct = lightVector.x*normal.x + lightVector.y*normal.y + lightVector.z*normal.z; if (dotProduct < 0) { dotProduct = 0; } // cout << "RED DIFFUSE INTENSITY: " << diffuseReflectance.x*(diffuseIntensity) << endl; color.x += dotProduct*diffuseReflectance.x*(diffuseIntensity.x); color.y += dotProduct*diffuseReflectance.y*(diffuseIntensity.y); color.z += dotProduct*diffuseReflectance.z*(diffuseIntensity.z); //specular shading Vector_3D<double> eyeVector = ray.endpoint - intersection_point; eyeVector.Normalize(); Vector_3D<double> tempH = lightVector+eyeVector; tempH.Normalize(); double max = (tempH.x*same_side_normal.x+tempH.y*same_side_normal.y+tempH.z*same_side_normal.z); if (max < 0) { max = 0; } color.x += color_specular.x*(world.lights.at(i)->Emitted_Light(ray).x)*pow(max,specular_power); color.y += color_specular.y*(world.lights.at(i)->Emitted_Light(ray).y)*pow(max,specular_power); color.z += color_specular.z*(world.lights.at(i)->Emitted_Light(ray).z)*pow(max,specular_power); } } return color; }