-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLight.cpp
60 lines (47 loc) · 2 KB
/
Light.cpp
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
/*
Light.cpp
Implements area light sources:
QuadLight
*/
#include <iostream>
#include <algorithm>
#include "Light.h"
typedef glm::dvec3 dvec3;
typedef glm::dvec4 dvec4;
QuadLight::QuadLight(dvec3 position, dvec3 upVec, dvec3 rightVec, dvec3 color)
{
this->position = position;
this->upVec = upVec;
this->rightVec = rightVec;
this->color = color;
this->normal = glm::normalize(glm::cross(upVec, rightVec));
this->area = glm::length(upVec) * glm::length(rightVec);
}
QuadLight::~QuadLight()
{
}
/*
Takes in a point on some surface and returns (via lightIntensity) the incoming radiance at that point from a random
point on the QuadLight.
Used when the DirectLighting setting is on, otherwise lighting occurs only through indirect bounces that hit the
area light geometry.
lightIntensity = L_o(x'_i, w'_i)*G(x,x'_i)*V(x,x'_i)*A, found in the lecture 10 slides.
Note: the visibility term is calculated in the DirectLighting routine
*/
void QuadLight::getSample(const dvec3& position, const dvec3& normal,
dvec3& lightIntensity, dvec3& lightRay)
{
double u = rand() / double(RAND_MAX);
double v = rand() / double(RAND_MAX);
dvec3 positionOnLight = this->position + this->upVec*u + this->rightVec*v;
lightRay = positionOnLight - position;
double lightRayLength = glm::length(lightRay);
// Calculates G(x,x'_i) = cos(θ)cos(θ'_i)/|x-x'_i|^2
double g = std::max(glm::dot(lightRay,normal) / lightRayLength,0.0) *
std::max(glm::dot(-lightRay,this->normal) / lightRayLength,0.0) /
std::pow(lightRayLength, 2.0);
//std::cout << std::max(glm::dot(lightRay,normal),0.0) / lightRayLength << "\n";
//std::cout << std::max(glm::dot(-lightRay,this->normal),0.0) / lightRayLength << "\n";
//std::cout << "<" << this->color.x << ", " << this->color.y << ", " << this->color.z << ">, " << g << ", " << area << "\n";
lightIntensity = this->color * g * area;
}