1 #ifndef BMPSHADER_HXX
 2 #define BMPSHADER_HXX
 3 
 4 #include "Shader.hxx"
 5 
 6 #define dPdu             Vec3f(1,0,0)
 7 #define dPdv             Vec3f(0,0,1)
 8 
 9 // This class is mostly equivalent to the normal PhongShader, plese refer
10 // there for further explanations
11 
12 class BumpMappedPhongShader : public Shader
13 {
14 public:
15 	Vec3f ambient; // ambient radiance 
16 	Vec3f p;       // intersection point
17 	float kd;      // diffuse reflection coefficients
18 	float ks;      // specular refelection coefficients
19 	float ke;      // shininess exponent
20 
21 	BumpMappedPhongShader(Scene *scene,
22 	                      Vec3f color, float ka,float kd, float ks, float ke)
23 		: Shader(scene, new Material(color)),kd(kd),ks(ks),ke(ke)
24 	{
25 		material->ambient = ka;
26 
27 		ambient = ka * Product(material->color, La);
28 	};
29 
30 	virtual Vec3f Shade(Ray &ray)
31 	{
32                 Vec3f Lr(0,0,0),
33                       Ll,
34                       N = ray.hit->GetNormal(ray);
35                 Ray   light_ray;
36                 float intensity_scale,
37 		      specular_scale;
38 
39                 p = ray.org + ray.t * ray.dir;
40 
41                 if (Dot(N,ray.dir) > 0)
42                         N = -N;
43 
44 		// Bumpmapping must be applied here, even though the sample solution
45 		// does it 3 lines above... But that is nonsense (which you can also
46 		// observe in their image)
47 		N = N
48 		  + 0.5 * cos( 3 * p.x * sin(p.z)) * dPdu
49 		  + 0.5 * sin(13 * p.z)            * dPdv;
50 		Normalize(N);
51 
52                 for (std::vector<Light*>::iterator it = scene->lights.begin(); it != scene->lights.end(); ++it)
53                 {
54                         light_ray.org  = p;
55 
56 #ifdef AREALIGHT
57                         for (int i = 0; i < NUM_AREA_SAMPLES; ++i)
58 #endif
59                         {
60                                 (*it)->Illuminate(light_ray, Ll);
61 
62 				// Phong shader can not handle transparency caused illumination yet
63 				// TODO
64 				// (Might become superfluous when Photon Mapping is applied)
65 				Vec3f color(1,1,1);
66 
67                                 if (!scene->Occluded(light_ray, color))
68                                 {
69                                         intensity_scale = Dot(light_ray.dir, N);
70                                         if (intensity_scale > 0)
71                                                 Lr += kd * Product(material->color, Ll) * intensity_scale;
72 
73 					specular_scale  = Dot(light_ray.dir, ray.dir - 2 * Dot(N, ray.dir) * N);
74 					if (specular_scale  > 0)
75                                                 Lr += ks * Product(cs, Ll) * powf(specular_scale, ke);
76                                 }
77                         }
78                 }
79 
80 #ifdef AREALIGHT
81                 Lr /= NUM_AREA_SAMPLES;
82 #endif
83 		Lr += ambient;
84                 return Lr;
85 	};
86 };
87 
88 #endif


syntax highlighted by Code2HTML, v. 0.9.1