Phong does not work yet, but saving here
authorRoland Hieber <rohieb@rohieb.name>
Wed, 27 Jan 2010 01:51:31 +0000 (02:51 +0100)
committerRoland Hieber <rohieb@rohieb.name>
Wed, 27 Jan 2010 01:51:31 +0000 (02:51 +0100)
PhongShader.cxx
PhongShader.hxx
PointLight.cxx
Scene.hxx

index 4f4d065..67363a6 100644 (file)
@@ -1,4 +1,5 @@
 #include "PhongShader.hxx"
+#include "Primitive.hxx"
 
 PhongShader::PhongShader(Scene* scene,
                         const Vec3f& am_c,
@@ -31,5 +32,33 @@ PhongShader::~PhongShader()
 Vec3f
 PhongShader::Shade(Ray& ray)
 {
-  return Vec3f();
+  // surface normal at hit point of object
+  Vec3f normal = ray.hit()->GetNormal(ray);
+  // turn to fron if angle < 90°
+  if(normal.dot(ray.direction()) < 0) {
+    normal = normal * -1;
+  }
+  // direction of reflection
+  Vec3f refl_dir = ray.direction() - normal * 2 * (normal % ray.direction());
+  refl_dir.normalize();
+
+  // ambient color term - radiance is 1 since we have point light
+  Vec3f amb_color = m_ambient_color * m_ka * 1.0f;
+
+  Vec3f intensity, light_dir;
+
+  // diffuse and specular color for each light source
+  Vec3f diff_sum;
+  Vec3f spec_sum;
+  for(std::vector<Light*>::iterator it = m_scene->m_lights.begin();
+    it != m_scene->m_lights.end(); it++) {
+    Ray light_ray;
+    (*it)->Illuminate(light_ray, intensity);
+    diff_sum += intensity * light_ray.direction().dot(normal);
+    spec_sum += intensity * light_ray.direction().dot(refl_dir);
+  }
+  Vec3f diff_color = m_diffuse_color * m_kd * diff_sum;
+  Vec3f spec_color = m_specular_color * m_ks * spec_sum;
+
+  return amb_color + diff_color + spec_sum;
 }
index b83e670..3a18d6c 100644 (file)
@@ -2,6 +2,7 @@
 #define PHONGSHADER_HXX
 
 #include "Shader.hxx"
+#include "Scene.hxx"
 
 class PhongShader : public Shader
 {
@@ -15,11 +16,11 @@ public:
              float ks,
              float ke);
   virtual ~PhongShader();
-  
+
   virtual Vec3f Shade(Ray& ray);
 private:
   PhongShader();
-  
+
   Vec3f m_ambient_color, m_diffuse_color, m_specular_color;
   float m_ka, m_kd, m_ks, m_ke;
 };
index 7b8c8d2..2efe585 100644 (file)
@@ -24,10 +24,13 @@ PointLight::Illuminate(Ray& ray, Vec3f& intensity)
   float dist = ((ray.origin() + ray.direction() * ray.t()) - m_pos).norm();
 
   float c1 = 1, c2 = 0.5, c3 = 0;
-  float in = 1 / (c1 + c2*dist + c3*dist*dist);
+  float f_att = 1 / (c1 + c2*dist + c3*dist*dist);
 
-  intensity = Vec3f(in, in, in);
-  return false;
+  intensity = m_intensity * f_att;
+
+  // store direction from light to hitpoint
+  ray.setDir(ray.origin() + ray.direction() * (ray.t()-Epsilon) - m_pos);
+  return true;
 }
 
 const Vec3f&
index b92e9e5..74154eb 100644 (file)
--- a/Scene.hxx
+++ b/Scene.hxx
@@ -13,7 +13,7 @@ class Scene
 public:
   Scene();
   virtual ~Scene();
-  
+
   // add another primitive to the scene
   void Add(Primitive* p);
   // add another light source to the scene
@@ -23,11 +23,13 @@ public:
   virtual bool Intersect(Ray& ray);
   // find occluder
   virtual bool Occluded(Ray& ray);
-  
+
   // trace the given ray and shade it, returnt the color of the shaded ray
   Vec3f RayTrace(Ray& ray);
-    
-  const Camera* camera() const; 
+
+  const Camera* camera() const;
+  // lights
+  std::vector<Light*> m_lights;
 private:
   Scene(const Scene& );
   Scene& operator=(const Scene& );
@@ -35,10 +37,8 @@ private:
   Camera* m_camera;
   // background color
   Vec3f m_bgColor;
-  
+
   // primitives
   std::vector<Primitive*> m_primitives;
-  // lights
-  std::vector<Light*> m_lights;
 };
 #endif
This page took 0.030151 seconds and 4 git commands to generate.