save commit
[MicroTrace.git] / QuadAreaLight.cxx
1 #include <cmath>
2 #include <cstdlib>
3
4 #include "QuadAreaLight.hxx"
5
6 QuadAreaLight::QuadAreaLight(Scene* scene,
7 const Vec3f& intensity,
8 const Vec3f& p1,
9 const Vec3f& p2,
10 const Vec3f& p3,
11 const Vec3f& p4)
12 : Light(scene),
13 m_p0(p1),
14 m_e1(p2-p1),
15 m_e2(p3-p1),
16 m_intensity(intensity)
17 {
18 m_n = m_e1.cross(m_e2);
19 m_area = m_n.norm();
20 m_n.normalize();
21 }
22
23 QuadAreaLight::QuadAreaLight()
24 : Light(0),
25 m_p0(Vec3f()),
26 m_e1(Vec3f()),
27 m_e2(Vec3f()),
28 m_n(Vec3f()),
29 m_intensity(Vec3f())
30 {
31 }
32
33 QuadAreaLight::~QuadAreaLight()
34 {
35 }
36
37 bool
38 QuadAreaLight::Illuminate(Ray& shadow_ray, Vec3f& intensity)
39 {
40 // generate random factors
41 float xi1 = drand48();
42 float xi2 = drand48();
43
44 // bilinear interpolation of position
45 Vec3f pos = m_p0 + m_e1 * xi1 + m_e2* xi2;
46 // direction of shadow ray
47 Vec3f dir = pos-shadow_ray.origin();
48
49 float dist = dir.norm();
50 float cosN = (dir.dot(m_n)) / dist;
51
52 if(cosN <= 0.0f )
53 return false;
54
55 // squared falloff and projected area
56 intensity = m_intensity * ( m_area * cosN / ( dist*dist));
57
58 dir.normalize();
59 shadow_ray.setDir(dir);
60 shadow_ray.setT(dist);
61
62 return true;
63 }
64
65 bool
66 QuadAreaLight::IsArea()
67 {
68 return true;
69 }
70
71 const Vec3f&
72 QuadAreaLight::GetNormal(const Vec3f& pos) const
73 {
74 return m_n;
75 }
This page took 0.043549 seconds and 5 git commands to generate.