Scene::CalcBounds(), including Box::Extend(Box&), Box::Clear()
[MicroTrace.git] / PhongShader.cxx
index 4f4d065..f6bc870 100644 (file)
@@ -1,4 +1,8 @@
+#include <vector>
+
 #include "PhongShader.hxx"
+#include "Primitive.hxx"
+#include "Scene.hxx"
 
 PhongShader::PhongShader(Scene* scene,
                         const Vec3f& am_c,
@@ -31,5 +35,65 @@ PhongShader::~PhongShader()
 Vec3f
 PhongShader::Shade(Ray& ray)
 {
-  return Vec3f();
+ // surface normal at hit point
+  Vec3f N = ray.hit()->GetNormal(ray);
+  // turn normal to front
+  if(N.dot(ray.direction()) > 0)
+    {
+      N *= -1;
+    }
+  // reflection vector
+  Vec3f R = ray.direction() - N*2*N.dot(ray.direction());
+  R.normalize();
+
+  // ambient term
+  Vec3f color = m_ambient_color * 1.0 * m_ka;
+  
+  // construct shadow ray 
+  Vec3f shadow_org = ray.origin()+ray.direction()*ray.t();
+  Ray shadow_ray;
+  shadow_ray.setOrigin(shadow_org);
+  
+
+  int n_area_rays = 1000;
+  std::vector<Light*> lights = m_scene->lights();
+  for(unsigned int i = 0; i < lights.size(); ++i)
+    {
+      Vec3f intensity;
+
+      int max_s = (lights[i]->IsArea()) ? n_area_rays : 1;
+      Vec3f color_l(0.0f,0.0f,0.0f);
+      for(int s = 0; s < max_s; ++s)
+       {
+         
+         if(lights[i]->Illuminate(shadow_ray, intensity))
+           {
+             // check for occluders
+             if(m_scene->Occluded(shadow_ray))
+               {
+                 continue;
+               }
+             
+             float IdotN = shadow_ray.direction().dot(N);
+             if(IdotN > 0)
+               {
+                 // diffuse term
+                 color_l += m_diffuse_color * intensity * IdotN * m_kd;
+               }
+             
+             // specular term
+             float IdotR = shadow_ray.direction().dot(R);
+             if(IdotR > 0)
+               {
+                 color_l += m_specular_color * intensity * pow(IdotR,m_ke) * m_ks;
+               }
+           }
+       }
+      color_l /= static_cast<float>(max_s);
+      color += color_l;
+    }
+  color.clamp();
+  
+  return color;
 }
This page took 0.021982 seconds and 4 git commands to generate.