Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Color Phong::shade(Ray ray, const IntersectionData& data)
- {
- Color ambient;
- float ambientCoefficient = 0.005;
- ambient = ambientCoefficient * color * lightPower / (data.p - lightPos).lengthSqr(); //ambient
- Vector lightDir = lightPos - data.p;
- lightDir.normalize();
- double diffuseCoefficient = std::max(0.0, dot(lightDir, data.normal));
- Color diffuse = diffuseCoefficient * color * lightPower; // diffuse
- //specular
- Vector cameraPos = Vector(0, 165, 0);
- Vector surfaceToCamera = cameraPos - data.p;
- surfaceToCamera.normalize();
- Vector reflect = lightDir - 2.0 * dot(data.normal, lightDir) * data.normal;
- float shininess = 80.0;
- double specularCoefficient = 0.0;
- if (diffuseCoefficient > 0.0)
- {
- specularCoefficient = std::pow(std::max(0.0, dot(surfaceToCamera, reflect)), shininess);
- }
- Color specularColor = Color(1.0, 1.0, 1.0);
- Color specular = specularCoefficient * specularColor * lightPower;
- //attenuation
- float lightAttenuation = 0.2;
- float distanceToLight = (lightPos - data.p).length();
- float attenuation = 1.0 / (1.0 + lightAttenuation * std::pow(distanceToLight, 2));
- //linear color (before gamma correction)
- Color linear = ambient + attenuation * (diffuse + specular);
- //final color (after gamma correction)
- linear.operator*=(1.0/2.2);
- return linear;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement