1 #include "PhongShader.hxx"
2 #include "Primitive.hxx"
4 PhongShader::PhongShader(Scene
* scene
,
13 m_ambient_color(am_c
),
14 m_diffuse_color(di_c
),
15 m_specular_color(sp_c
),
23 PhongShader::PhongShader()
28 PhongShader::~PhongShader()
33 PhongShader::Shade(Ray
& ray
)
35 // surface normal at hit point of object
36 Vec3f normal
= ray
.hit()->GetNormal(ray
);
37 // turn to fron if angle < 90°
38 if(normal
.dot(ray
.direction()) < 0) {
41 // direction of reflection
42 Vec3f refl_dir
= ray
.direction() - normal
* 2 * (normal
% ray
.direction());
45 // ambient color term - radiance is 1 since we have point light
46 Vec3f amb_color
= m_ambient_color
* m_ka
* 1.0f
;
48 Vec3f intensity
, light_dir
;
50 // diffuse and specular color for each light source
53 for(std::vector
<Light
*>::iterator it
= m_scene
->m_lights
.begin();
54 it
!= m_scene
->m_lights
.end(); it
++) {
56 (*it
)->Illuminate(light_ray
, intensity
);
57 diff_sum
+= intensity
* light_ray
.direction().dot(normal
);
58 spec_sum
+= intensity
* light_ray
.direction().dot(refl_dir
);
60 Vec3f diff_color
= m_diffuse_color
* m_kd
* diff_sum
;
61 Vec3f spec_color
= m_specular_color
* m_ks
* spec_sum
;
63 return amb_color
+ diff_color
+ spec_sum
;
This page took 0.053383 seconds and 5 git commands to generate.