fixed Box::Extend(Vec3f&)
[MicroTrace.git] / SpotLight.cxx
1 #include "SpotLight.hxx"
2
3 SpotLight::SpotLight(Scene* scene,
4 const Vec3f& pos,
5 const Vec3f& dir,
6 const Vec3f& intensity,
7 float alpha_min, // in degree
8 float alpha_max) // in degree
9 : Light(scene),
10 m_pos(pos),
11 m_dir(dir),
12 m_intensity(intensity),
13 m_alpha_min(alpha_min),
14 m_alpha_max(alpha_max)
15 {
16 m_dir.normalize();
17 }
18
19 SpotLight::~SpotLight()
20 {
21 }
22
23 SpotLight::SpotLight()
24 : Light(0),
25 m_pos(Vec3f()),
26 m_dir(Vec3f()),
27 m_intensity(Vec3f()),
28 m_alpha_min(0.0f),
29 m_alpha_max(0.0f)
30 {
31 }
32
33 bool
34 SpotLight::Illuminate(Ray& shadow_ray, Vec3f& intensity)
35 {
36 // direction vector from light source to surface point
37 Vec3f D = shadow_ray.origin()-m_pos;
38 D.normalize();
39 // angle between light source dir and shadow ray dir
40 float phi = fabs(acos(D.dot(m_dir))*180/M_PI);
41
42 // outside cone
43 if(phi > m_alpha_max)
44 {
45 return false;
46 }
47 else
48 {
49 Vec3f dir = m_pos-shadow_ray.origin();
50 float r = dir.norm()-Epsilon;
51 float falloff = 1.0f/(r*r); // falloff for distance
52
53 // modify ray for shadow computation
54 shadow_ray.setHit(0);
55 // for shadow calculation
56 shadow_ray.setT(r);
57 // set direction to light source
58 dir.normalize();
59 shadow_ray.setDir(dir);
60
61 if(phi < m_alpha_min)
62 {
63 intensity = m_intensity * falloff;
64 }
65 else
66 {
67 // linear falloff from 1 at alpha_min to 0 at alpha_max
68 float partial = 1.0f-(phi-m_alpha_min)/(m_alpha_max-m_alpha_min);
69 intensity = m_intensity * falloff * partial;
70
71 }
72 return true;
73 }
74 return true;
75 }
76
77 const Vec3f&
78 SpotLight::position() const
79 {
80 return m_pos;
81 }
82
83 const Vec3f&
84 SpotLight::direction() const
85 {
86 return m_dir;
87 }
88
89 bool
90 SpotLight::IsArea()
91 {
92 return false;
93 }
This page took 0.048644 seconds and 5 git commands to generate.