-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlight_source.cpp
More file actions
51 lines (40 loc) · 1.4 KB
/
Copy pathlight_source.cpp
File metadata and controls
51 lines (40 loc) · 1.4 KB
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
#include "light_source.h"
#include "tuple.h"
#include <cmath>
Color LightSource::lighting(const Material &m, Shape *object, const RayPoint &p,
const RayVector &eyev, const RayVector &normalv,
const bool in_shadow) const {
Color color = m.color;
if (m.pattern) {
// color = m.pattern->pattern_at(p);
color = m.pattern->pattern_at_object(object, p);
}
Color ambient = Color(0, 0, 0);
Color diffuse = Color(0, 0, 0);
Color specular = Color(0, 0, 0);
Color effective_color = color * this->intensity;
RayVector lightv = (this->position - p).normalize();
ambient = effective_color * m.ambient;
if (in_shadow) {
return ambient;
}
double light_dot_normal = lightv.dot(normalv);
if (light_dot_normal < 0.0) {
diffuse = Color(0, 0, 0);
specular = Color(0, 0, 0);
} else {
diffuse = effective_color * m.diffuse * light_dot_normal;
RayVector reflectv = (-lightv).reflect(normalv);
double reflect_dot_eye = reflectv.dot(eyev);
if (reflect_dot_eye <= 0.0) {
specular = Color(0, 0, 0);
} else {
double factor = std::pow(reflect_dot_eye, m.shininess);
specular = this->intensity * m.specular * factor;
}
}
return ambient + diffuse + specular;
}
bool LightSource::operator==(const LightSource &other) const {
return this->intensity == other.intensity && this->position == other.position;
}