-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathilluminationModel.h
122 lines (91 loc) · 3.99 KB
/
illuminationModel.h
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#ifndef _ILLUMINATIONMODEL_H
#define _ILLUMINATIONMODEL_H
#include <vector>
#include <algorithm> // std::max
#include "mathHelper.h"
#include "object.h"
#include "lightSource.h"
Color ambientComponent(Object *obj, Color ambientLight, Point point) {
Color objColor = obj->getColor(point);
double ka = obj->getKa();
return ka * objColor;
}
// needs the object because we will use diffuse and specular color,
// here we will not calculate the ambient component
// the light list is the lights that the shadow array definetly hit
Color illuminatePhong(Object *obj, Vector view, Point point, Vector normal,
std::map<LightSource*, std::vector<Point> > lightsAndPointsReachedMap) {
if (lightsAndPointsReachedMap.empty())
return Color(0,0,0);
Color diffuse, diffuseFinal;
Color specular, specularFinal;
Color objColor = obj->getColor(point);
Color objSpecColor = obj->getSpecularColor();
double kd = obj->getKd();
double ks = obj->getKs();
double ke = obj->getKe();
normalize(view);
normalize(normal);
// For each light, for each point reached on the light
for (std::map<LightSource*, std::vector<Point> >::iterator it=lightsAndPointsReachedMap.begin(); it!=lightsAndPointsReachedMap.end(); ++it) {
LightSource *lightHit = (it->first);
std::vector<Point> pointsHit = (it->second);
double attenuation = lightHit->getAttenuation(point);
Color lightRadiance = lightHit->getColor();
double numSamples = lightHit->getNumSamplesOnSurface();
for(std::vector<Point>::iterator it2 = pointsHit.begin() ; it2 < pointsHit.end() ; ++it2) {
// diffuse
Vector s(point, (*it2), true);
double sn = std::max(dot( s, normal ),0.0);
// spec
Vector invs((*it2), point, true);
Vector r = reflect ( invs, normal, VECTOR_INCOMING );
normalize(r);
double rvke = std::pow( std::max(dot( r, view ), 0.0), ke );
// calculate it
diffuse += lightRadiance * objColor * sn * attenuation;
specular += lightRadiance * objSpecColor * rvke * attenuation;
}
diffuseFinal += diffuse / numSamples;
specularFinal += specular / numSamples;
}
return kd * diffuseFinal + ks * specularFinal;
}
Color illuminatePhongBlinn(Object *obj, Vector view, Point point, Vector normal,
std::map<LightSource*, std::vector<Point> > lightsAndPointsReachedMap) {
if (lightsAndPointsReachedMap.empty())
return Color(0,0,0);
Color diffuse, diffuseFinal;
Color specular, specularFinal;
Color objColor = obj->getColor(point);
Color objSpecColor = obj->getSpecularColor();
double kd = obj->getKd();
double ks = obj->getKs();
double ke = obj->getKe();
normalize(view);
normalize(normal);
// For each light, for each point reached on the light
for (std::map<LightSource*, std::vector<Point> >::iterator it=lightsAndPointsReachedMap.begin(); it!=lightsAndPointsReachedMap.end(); ++it) {
LightSource *lightHit = (it->first);
std::vector<Point> pointsHit = (it->second);
double attenuation = lightHit->getAttenuation(point);
Color lightRadiance = lightHit->getColor();
double numSamples = lightHit->getNumSamplesOnSurface();
for(std::vector<Point>::iterator it2 = pointsHit.begin() ; it2 < pointsHit.end() ; ++it2) {
// diffuse
Vector s(point, (*it2), true);
double sn = std::max(dot( s, normal ),0.0);
// spec
Vector h = s + view;
normalize(h);
double rvke = std::pow( std::max(dot( normal, h ), 0.0), ke );
// calculate it
diffuse += lightRadiance * objColor * sn * attenuation;
specular += lightRadiance * objSpecColor * rvke * attenuation;
}
diffuseFinal += diffuse / numSamples;
specularFinal += specular / numSamples;
}
return kd * diffuseFinal + ks * specularFinal;
}
#endif