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