Advertisement
Guest User

Untitled

a guest
Nov 6th, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. Color Phong::shade(Ray ray, const IntersectionData& data)
  2. {
  3.     Color ambient;
  4.     float ambientCoefficient = 0.005;
  5.     ambient = ambientCoefficient * color * lightPower / (data.p - lightPos).lengthSqr(); //ambient
  6.     Vector lightDir = lightPos - data.p;
  7.     lightDir.normalize();
  8.  
  9.     double diffuseCoefficient = std::max(0.0, dot(lightDir, data.normal));
  10.     Color diffuse = diffuseCoefficient * color * lightPower; // diffuse
  11.    
  12.     //specular
  13.     Vector cameraPos = Vector(0, 165, 0);
  14.     Vector surfaceToCamera = cameraPos - data.p;
  15.     surfaceToCamera.normalize();
  16.  
  17.     Vector reflect = lightDir - 2.0 * dot(data.normal, lightDir) * data.normal;
  18.  
  19.     float shininess = 80.0;
  20.     double specularCoefficient = 0.0;
  21.     if (diffuseCoefficient > 0.0)
  22.     {
  23.         specularCoefficient = std::pow(std::max(0.0, dot(surfaceToCamera, reflect)), shininess);
  24.     }
  25.     Color specularColor = Color(1.0, 1.0, 1.0);
  26.     Color specular = specularCoefficient * specularColor * lightPower;
  27.  
  28.     //attenuation
  29.     float lightAttenuation = 0.2;
  30.     float distanceToLight = (lightPos - data.p).length();
  31.     float attenuation = 1.0 / (1.0 + lightAttenuation * std::pow(distanceToLight, 2));
  32.  
  33.     //linear color (before gamma correction)
  34.     Color linear = ambient + attenuation * (diffuse + specular);
  35.  
  36.     //final color (after gamma correction)
  37.     linear.operator*=(1.0/2.2);
  38.  
  39.     return linear;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement