From: Roland Hieber Date: Wed, 27 Jan 2010 01:51:31 +0000 (+0100) Subject: Phong does not work yet, but saving here X-Git-Url: https://git.rohieb.name/MicroTrace.git/commitdiff_plain/9fa235f6c621a9737be66359dc6bed473f1823d9 Phong does not work yet, but saving here --- diff --git a/PhongShader.cxx b/PhongShader.cxx index 4f4d065..67363a6 100644 --- a/PhongShader.cxx +++ b/PhongShader.cxx @@ -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::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; } diff --git a/PhongShader.hxx b/PhongShader.hxx index b83e670..3a18d6c 100644 --- a/PhongShader.hxx +++ b/PhongShader.hxx @@ -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; }; diff --git a/PointLight.cxx b/PointLight.cxx index 7b8c8d2..2efe585 100644 --- a/PointLight.cxx +++ b/PointLight.cxx @@ -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& diff --git a/Scene.hxx b/Scene.hxx index b92e9e5..74154eb 100644 --- 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 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 m_primitives; - // lights - std::vector m_lights; }; #endif