#include "PhongShader.hxx"
+#include "Primitive.hxx"
PhongShader::PhongShader(Scene* scene,
const Vec3f& am_c,
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;
}
#define PHONGSHADER_HXX
#include "Shader.hxx"
+#include "Scene.hxx"
class PhongShader : public Shader
{
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;
};
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&
public:
Scene();
virtual ~Scene();
-
+
// add another primitive to the scene
void Add(Primitive* p);
// add another light source to the scene
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& );
Camera* m_camera;
// background color
Vec3f m_bgColor;
-
+
// primitives
std::vector<Primitive*> m_primitives;
- // lights
- std::vector<Light*> m_lights;
};
#endif