c44966e73ba4beaf86486aa9fc40acd2c1887019
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 front if angle > 90°
38 if(normal
.dot(ray
.direction()) < 0) {
41 // direction of reflection
42 Vec3f refl_dir
= ray
.direction() - normal
* 2 * normal
.dot(ray
.direction());
46 Vec3f amb_color
= m_ambient_color
* m_ka
;
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 Vec3f view_dir
= ray
.direction()*-1; // direction from hitpoint to viewer
59 spec_sum
+= intensity
* pow(fmax(view_dir
.dot(refl_dir
),0), m_ke
);
61 Vec3f diff_color
= m_diffuse_color
* m_kd
* diff_sum
;
62 Vec3f spec_color
= m_specular_color
* m_ks
* spec_sum
;
64 return amb_color
+ diff_color
+ spec_color
;
This page took 0.046012 seconds and 3 git commands to generate.