3 #include "PhongShader.hxx"
4 #include "Primitive.hxx"
7 PhongShader::PhongShader(Scene
* scene
,
16 m_ambient_color(am_c
),
17 m_diffuse_color(di_c
),
18 m_specular_color(sp_c
),
26 PhongShader::PhongShader()
31 PhongShader::~PhongShader()
36 PhongShader::Shade(Ray
& ray
)
38 // surface normal at hit point
39 Vec3f N
= ray
.hit()->GetNormal(ray
);
40 // turn normal to front
41 if(N
.dot(ray
.direction()) > 0)
47 Vec3f R
= ray
.direction() - N
*2*N
.dot(ray
.direction());
51 Vec3f color
= m_ambient_color
* 1.0 * m_ka
;
53 // construct shadow ray
54 Vec3f shadow_org
= ray
.origin()+ray
.direction()*ray
.t();
56 shadow_ray
.setOrigin(shadow_org
);
59 int n_area_rays
= 1000;
60 std::vector
<Light
*> lights
= m_scene
->lights();
61 for(unsigned int i
= 0; i
< lights
.size(); ++i
)
65 int max_s
= (lights
[i
]->IsArea()) ? n_area_rays
: 1;
66 Vec3f
color_l(0.0f
,0.0f
,0.0f
);
67 for(int s
= 0; s
< max_s
; ++s
)
70 if(lights
[i
]->Illuminate(shadow_ray
, intensity
))
72 // check for occluders
73 if(m_scene
->Occluded(shadow_ray
))
78 float IdotN
= shadow_ray
.direction().dot(N
);
82 color_l
+= m_diffuse_color
* intensity
* IdotN
* m_kd
;
86 float IdotR
= shadow_ray
.direction().dot(R
);
89 color_l
+= m_specular_color
* intensity
* pow(IdotR
,m_ke
) * m_ks
;
93 color_l
/= static_cast<float>(max_s
);
This page took 0.048664 seconds and 5 git commands to generate.