include scene hpp class LambertPhongShader public Shader public Lamber

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "scene.hpp"
class LambertPhongShader : public Shader
{
public:
LambertPhongShader() : Shader()
{
name = "lambertphong";
}
Color *shade(const HitInfo & hit)
{
col.to_black();
/* Light gathering */
LightSourceMap *lights = & hit.geom->scene->light_table;
LightSourceMap::iterator it = lights->begin();
LightSourceMap::iterator end = lights->end();
while(it != end)
{
LightSource *light = it->second;
Vector3D to_light = light->pos - hit.point;
//ANYFLOAT dist_factor = light->radius - to_light.get_module();
//dist_factor = ((dist_factor>0)?dist_factor:0) / light->radius;
to_light.normalize();
Vector3D normal = hit.normal;
// is object in shadow
if(recieve_shadows)
{
Ray feeler; // shadow feeler
feeler.dir = to_light;
feeler.start = hit.point;
if(hit.geom->is_in_shadow(feeler))
{
it++;
col += ambient();
continue;
}
}
// light calculation
ANYFLOAT lambert;
if(light->emit_diffuse)
{
lambert = light->intensity *(
(to_light * normal) /
(to_light.get_module() * normal.get_module()));
}
else
lambert = 0;
ANYFLOAT phong;
if(light->emit_specular)
{
Vector3D h = to_light + hit.view;
h.normalize();
phong = light->intensity *(
(h * normal) /
(h.get_module() * normal.get_module()));
if(h * normal < 0.9)
phong = 0;
}
else
phong = 0;
col += (0.9*((lambert>0)?lambert:0) + .6*((phong>0)?phong:0)) + ambient();// * dist_factor;
it++;
}
// normal map
//col.r = fabs(hit.normal.x);
//col.g = fabs(hit.normal.y);
//col.b = fabs(hit.normal.z);
return & col;
}
};
extern "C"
{
Shader *get_shader()
{
Shader *shader = new LambertPhongShader();
return shader;
}
}