1 #ifndef EYELIGHTTRANSPARENT_HXX
 2 #define EYELIGHTTRANSPARENT_HXX
 3 
 4 #include "Shader.hxx"
 5 
 6 class EyeLightTransparentShader : public Shader
 7 {
 8     private:
 9         Vec3f color;
10     
11     public:
12         EyeLightTransparentShader(Scene *scene, Vec3f color) 
13         : Shader(scene), color(color)
14         {};
15 
16         ~EyeLightTransparentShader()
17         {};
18 
19         Vec3f Shade(Ray &ray)
20         {   
21             Vec4f result = Vec4f(0,0,0,0);
22 
23             // get uv values
24             Vec2f uv = ray.hit->GetUV(ray);
25 
26             // for each assigned texture
27             for (unsigned int i = 0; i < getTextureCount(); i++)
28             {
29                 // get texture value
30                 const Vec4f& tex = getTexture(i)->GetTexel(uv);
31 
32                 // combine texture colors by blending them together
33                 result = lerp(result, tex, tex.w());
34             }
35             
36             // if pixel is transparent, then we have to trace the ray back into the scene
37             if (result.w() < 1)
38             {
39                 // create new ray which could be ray traced back into the scene
40                 Ray r = ray;
41 
42                 // the ray should move from the shaded surface
43                 r.org = ray.org + ray.t * ray.dir;
44                 r.hit = NULL;
45                 r.t = Infinity;
46 
47                 // trace ray back into the scene and get the color
48                 Vec3f c = scene->RayTrace(r);
49 
50                 // combine result witht the texture color
51                 // here traced color is our destination color and texture is a source color
52                 result = Vec4f(lerp(c, result.xyz(), result.w()), 1);
53             }
54 
55             // return result value multiplied by the eyelight shaded color
56             return result.xyz() * color * fabs(Dot(ray.dir,ray.hit->GetNormal(ray)));
57         };
58 };
59 
60 #endif


syntax highlighted by Code2HTML, v. 0.9.1