-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lambert.cpp
45 lines (33 loc) · 932 Bytes
/
Lambert.cpp
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
#include "Lambert.h"
#include "Ray.h"
#include "Scene.h"
#include <algorithm>
Lambert::Lambert(const Vector3 & kd) :
m_kd(kd)
{
}
Lambert::~Lambert()
{
}
Vector3
Lambert::shade(const Ray& ray, const HitInfo& hit, const Scene& scene) const
{
Vector3 L = Vector3(0.0f, 0.0f, 0.0f);
const Vector3 viewDir = -ray.d; // d is a unit vector
const Lights *lightlist = scene.lights();
// loop over all of the lights
Lights::const_iterator lightIter;
for (lightIter = lightlist->begin(); lightIter != lightlist->end(); lightIter++)
{
PointLight* pLight = *lightIter;
Vector3 l = pLight->position() - hit.P;
// the inverse-squared falloff
float falloff = l.length2();
// normalize the light direction
l /= sqrt(falloff);
// get the irradiance
Vector3 irradiance = (pLight->color() * pLight->wattage()) * std::max(0.0f, dot(hit.N, l)) / (4.0 * PI * falloff);
L += irradiance * (m_kd / PI);
}
return L;
}