1 #ifndef PHONGSHADER_HXX
2 #define PHONGSHADER_HXX
3
4 #include "Shader.hxx"
5
6 class PhongShader : public Shader
7 {
8 public:
9 Vec3f ambient; // ambient radiance
10 Vec3f light_ray_org_std; // template for light_ray.org, to calculate it only once
11 float kd; // diffuse reflection coefficients
12 float ks; // specular refelection coefficients
13 float ke; // shininess exponent
14
15 PhongShader(Scene *scene,
16 Vec3f color, float ka,float kd, float ks, float ke)
17 : Shader(scene, new Material(color)),kd(kd),ks(ks),ke(ke)
18 {
19 material->ambient = ka;
20
21 ambient = material->ambient * Product(material->color, La);
22 };
23
24 virtual Vec3f Shade(Ray &ray)
25 {
26 Vec3f Lr(0,0,0),
27 Ll,
28 N = ray.hit->GetNormal(ray);
29 Ray light_ray;
30 float intensity_scale;
31
32 if (Dot(N,ray.dir) > 0)
33 N = -N;
34
35 light_ray_org_std = ray.org + ray.t * ray.dir;
36
37 for (std::vector<Light*>::iterator it = scene->lights.begin(); it != scene->lights.end(); ++it)
38 {
39 light_ray.org = light_ray_org_std;
40
41 #ifdef AREALIGHT
42 for (int i = 0; i < NUM_AREA_SAMPLES; ++i)
43 #endif
44 {
45 (*it)->Illuminate(light_ray, Ll);
46
47 // Phong shader can not handle transparency caused illumination yet
48 // It is now deprecated, why we stop working on this
49 Vec3f color(1,1,1);
50
51 if (!scene->Occluded(light_ray, color))
52 {
53 intensity_scale = Dot(light_ray.dir, N);
54
55 if (intensity_scale > 0)
56 Lr += kd * Product(material->color, Ll) * intensity_scale
57 + ks * Product(cs, Ll) * powf(Dot(light_ray.dir, ray.dir + 2 * Dot(ray.dir, -N) * N), ke);
58 }
59 }
60 }
61
62 #ifdef AREALIGHT
63 Lr /= NUM_AREA_SAMPLES;
64 #endif
65 Lr += ambient;
66 return Lr;
67 };
68 };
69
70 #endif
syntax highlighted by Code2HTML, v. 0.9.1