-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLights.cpp
More file actions
63 lines (54 loc) · 2.58 KB
/
Copy pathLights.cpp
File metadata and controls
63 lines (54 loc) · 2.58 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
#include <iostream>
#include "Lights.h"
using std::max;
void PointLight::printInfo() {
std::cout <<
"Light Type : Point \n\
Location : " << xyz[0] << " " << xyz[1] << " " << xyz[2] << "\n\
Colour: " << rgb[0] << " " << rgb[1] << " " << rgb[2] << "\n\
AttenuationCoeff: " << attenuation[0] << " " << attenuation[1] << " " << attenuation[2] << "\n";
}
vec3 PointLight::computeLight(vec3 hitPoint, vec3 directionToEye,
vec3 diffuse, vec3 specular, float shininess,
vec3 objectNormal) {
// Compute the direction and distance to the light
vec3 directionToLight = normalize(xyz - hitPoint);
float distanceToLight = length(xyz - hitPoint);
vec3 lightRGB = rgb/(attenuation[0] +
attenuation[1]*distanceToLight +
attenuation[2]*distanceToLight*distanceToLight
);
// Compute the half vector for specular calculation
vec3 halfVector = normalize (directionToLight + directionToEye);
vec3 diffuseLight = diffuse * max( dot(objectNormal, directionToLight), 0.0f);
vec3 specularLight = specular * (float) pow(max( dot(objectNormal, halfVector), 0.0f), shininess);
lightRGB = lightRGB*(diffuseLight + specularLight);
return lightRGB;
}
void DirectionalLight::printInfo() {
std::cout <<
"Light Type : Directional \n\
Location : " << xyz[0] << " " << xyz[1] << " " << xyz[2] << "\n\
Colour: " << rgb[0] << " " << rgb[1] << " " << rgb[2] << "\n\
AttenuationCoeff: " << attenuation[0] << " " << attenuation[1] << " " << attenuation[2] << "\n";
}
vec3 DirectionalLight::computeLight(vec3 hitPoint, vec3 directionToEye,
vec3 diffuse, vec3 specular, float shininess,
vec3 objectNormal) {
// Default - No attenuation of light intensity
vec3 attenuation(1.,0.,0.);
// For directional light, the direction remains the same regardless of hitPoint
vec3 directionToLight = normalize(xyz);
// Directional light is located infinitely far away
float distanceToLight = -1.;
vec3 lightRGB = rgb/(attenuation[0] +
attenuation[1]*distanceToLight +
attenuation[2]*distanceToLight*distanceToLight
);
// Compute the half vector for specular calculation
vec3 halfVector = normalize (directionToLight + directionToEye);
vec3 diffuseLight = diffuse * max( dot(objectNormal, directionToLight), 0.0f);
vec3 specularLight = specular * (float) pow(max( dot(objectNormal, halfVector), 0.0f), shininess);
lightRGB = lightRGB*(diffuseLight + specularLight);
return lightRGB;
}