-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLight.cpp
More file actions
86 lines (67 loc) · 2.11 KB
/
Light.cpp
File metadata and controls
86 lines (67 loc) · 2.11 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
#include "Light.h"
#include <math.h>
#include <limits>
/* Constructor. Implemented for you. */
PointLight::PointLight(const Vector3f & position, const Vector3f & intensity)
: position(position), intensity(intensity)
{
}
// Compute the contribution of light at point p using the
// inverse square law formula
Vector3f PointLight::computeLightContribution(const Vector3f& p)
{
/***********************************************
* *
* TODO: Implement this function *
* *
***********************************************
*/
Vector3f wi = vectorSubtraction(position, p);
float R = dotProduct(wi, wi);
return scalarMultiplication((1/(R*R)),intensity);
//return intensity over r square
}
Vector3f PointLight::normal( Vector3f a, Vector3f b) const{
Vector3f result;
result.x = a.y * b.z - a.z*b.y;
result.y = a.z * b.x - a.x*b.z;
result.z = a.x * b.y - a.y*b.x;
return result;
}
Vector3f PointLight::normalize( Vector3f v) const{
float len = std::sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
if (len != 0.0)
{
v.x /= len;
v.y /= len;
v.z /= len;
}
return v;
}
float PointLight::determinant(Vector3f a, Vector3f b, Vector3f c) const{
return a.x*b.y*c.z + b.x*c.y*a.z + c.x*a.y*b.z - a.z*b.y*c.x - b.z*c.y*a.x - c.z*a.y*b.x;
}
Vector3f PointLight::scalarMultiplication(float t, Vector3f direction) const{
Vector3f result;
result.x = direction.x * t;
result.y = direction.y * t;
result.z = direction.z * t;
return result;
}
Vector3f PointLight::vectorSubtraction(Vector3f a, Vector3f b) const{
Vector3f result;
result.x = a.x - b.x;
result.y = a.y - b.y;
result.z = a.z - b.z;
return result;
}
Vector3f PointLight::vectorAddition(Vector3f a, Vector3f b) const{
Vector3f result;
result.x = a.x + b.x;
result.y = a.y + b.y;
result.z = a.z + b.z;
return result;
}
float PointLight::dotProduct(Vector3f a, Vector3f b) const{
return (a.x * b.x) + (a.y * b.y) + (a.z * b.z);
}