-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlight_source.cpp
More file actions
executable file
·167 lines (123 loc) · 4.2 KB
/
light_source.cpp
File metadata and controls
executable file
·167 lines (123 loc) · 4.2 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/***********************************************************
Starter code for Assignment 3
Implements light_source.h
***********************************************************/
#include <cmath>
#include "light_source.h"
void PointLight::shade(Ray3D& ray) {
// TODO: implement this function to fill in values for ray.col
// using phong shading. Make sure your vectors are normalized, and
// clamp colour values to 1.0.
//
// It is assumed at this point that the intersection information in ray
// is available. So be sure that traverseScene() is called on the ray
// before this function.
// Find the position of the light from the intersection position.
// TODO use phong()
Vector3D lightDirection =
((this -> pos) - ray.intersection.point);
lightDirection.normalize();
// in [0,1]
double diffuseIntensity =
std::max(0.0, ray.intersection.normal.dot(lightDirection));
Vector3D mirrorRay = (
(2.0 * ray.intersection.normal.dot(lightDirection) * ray.intersection.normal)
- lightDirection
);
mirrorRay.normalize();
// Just in case
ray.dir.normalize();
double specularIntensity = pow(
std::max(0.0, ray.dir.dot(-1 * mirrorRay)),
ray.intersection.mat -> specular_exp
);
Color diffuseColor = diffuseIntensity * this -> col_diffuse;
diffuseColor.clamp();
Color specularColor = specularIntensity * this -> col_specular;
specularColor.clamp();
ray.col = (
(this -> col_ambient * ray.intersection.mat -> ambient) +
(diffuseColor * ray.intersection.mat -> diffuse) +
(specularColor * ray.intersection.mat -> specular)
);
ray.col.clamp();
}
Color Ambient(LightList& lights, Material* mat) {
Color c(0.0, 0.0, 0.0);
return c + mat -> ambient;
}
Color PointLight::get_ambient() {
return this -> col_ambient;
}
std::vector<Ray3D*> PointLight::generateSamples(Ray3D& ray) {
std::vector<Ray3D*> retVal;
Ray3D* principleRay = new Ray3D();
principleRay -> origin = ray.origin;
principleRay -> dir = ray.dir;
// Push principle ray
retVal.push_back(principleRay);
return retVal;
}
std::vector<Ray3D*> ExtendedPointLight::generateSamples(Ray3D& ray) {
std::vector<Ray3D*> retVal;
Ray3D* principleRay = new Ray3D();
principleRay -> origin = ray.origin;
principleRay -> dir = ray.dir;
// Push principle ray
retVal.push_back(principleRay);
if (SOFT_SHADOWS_ENABLE) {
double k = SOFT_SHADOWS_DELTA;
double dr = (this -> radius) / k;
double dtheta = 2 * PI / k;
double dphi = PI / k;
for (double r = dr; r < this -> radius; r += dr) {
for (double theta = dtheta; theta < 2 * PI; theta += dtheta) {
for (double phi = dphi; phi < PI; phi += dphi) {
Point3D randLightPos = this-> pos + Vector3D(r* cos(theta)*sin(phi),
r * sin(theta)*sin(phi),
r * cos(phi));
Vector3D lightDirection =
(randLightPos - ray.origin);
lightDirection.normalize();
Ray3D* newRay = new Ray3D();
newRay -> origin = ray.origin;
newRay -> dir = lightDirection;
retVal.push_back(newRay);
}
}
}
}
return retVal;
}
void ExtendedPointLight::shade(Ray3D& ray){
Vector3D lightDirection = (this->pos) - ray.intersection.point;
lightDirection.normalize();
// in [0,1]
double diffuseIntensity =
std::max(0.0, ray.intersection.normal.dot(lightDirection));
Vector3D mirrorRay = (
(2.0 * ray.intersection.normal.dot(lightDirection) * ray.intersection.normal)
- lightDirection
);
mirrorRay.normalize();
// Just in case
ray.dir.normalize();
double specularIntensity = pow(
std::max(0.0, ray.dir.dot(-1 * mirrorRay)),
ray.intersection.mat -> specular_exp
);
Color diffuseColor = diffuseIntensity * this -> col_diffuse;
diffuseColor.clamp();
Color specularColor = specularIntensity * this -> col_specular;
specularColor.clamp();
// TODO: prevent light source overwrite.
ray.col = (
(this -> col_ambient * ray.intersection.mat -> ambient) +
(diffuseColor * ray.intersection.mat -> diffuse) +
(specularColor * ray.intersection.mat -> specular)
);
ray.col.clamp();
}
Color ExtendedPointLight::get_ambient() {
return this -> col_ambient;
}