-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlight.cpp
112 lines (88 loc) · 3.39 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
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
#include "precomp.h"
#include "light.h"
#include "material.h"
namespace Tmpl8
{
bool light_list::hitLight(const ray r, float t_min, float t_max, const hittable* world, hit_record rec, color& light)
{
hit_record temp_rec;
bool hit_anything = false;
for (const auto& object : lightObjects)
{
if (object->hitLight(r, t_min, t_max, world, rec, light))
{
hit_anything = true;
rec = temp_rec;
}
}
return hit_anything;
}
bool point_light::hitLight(const ray r, float t_min, float t_max, const hittable* world, hit_record rec, color& light)
{
const ray shadowRay = ray(rec.p * rec.ellipsoidSize, pos - rec.p * rec.ellipsoidSize);
const float sRayNormal = dot(shadowRay.direction(), rec.normal);
//Checking the angle between the normal and shadow ray and if it doesn't even face the light no need to check
if (sRayNormal < 0)
{
light += vec3(0.f, 0.f, 0.f);
return true;
}
const float shadowRayDist = (pos - rec.p * rec.ellipsoidSize).length();
const float bisector = dot(shadowRay.direction(), pos - rec.p * rec.ellipsoidSize);
const float falloff = (1.f / (shadowRayDist * shadowRayDist));
constexpr float epsilon = .00001f;
if (falloff >= epsilon) {
if (!world->hit(shadowRay, t_min, fabsf(shadowRayDist), rec)) {
const color diffuse = (falloff * m_intensity) * sRayNormal;
const color specular = falloff * Max(0.f, powf(cos(bisector), m_specular));
light += m_color * (diffuse + specular);
}
}
return true;
}
bool area_light::hitLight(const ray r, float t_min, float t_max, const hittable* world, hit_record rec, color& light)
{
vec3 delta = pos2 - pos;
vec3 offset = pos + (random_float() * delta);
const ray shadowRay = ray(rec.p * rec.ellipsoidSize, (pos + offset) - rec.p * rec.ellipsoidSize);
const float sRaynormal = dot(shadowRay.direction(), rec.normal);
//Checking the angle between the normal and shadow ray and if it doesn't even face the light no need to check
if (sRaynormal < 0)
{
light += vec3(0.f, 0.f, 0.f);
return true;
}
const float shadowRayDist = ((pos + offset) - rec.p * rec.ellipsoidSize).length();
const float bisector = dot(shadowRay.direction(), (pos + offset) - rec.p * rec.ellipsoidSize);
const float falloff = (1.f / (shadowRayDist * shadowRayDist));
constexpr float epsilon = .00001f;
if (falloff >= epsilon) {
if (!world->hit(shadowRay, t_min, fabsf(shadowRayDist), rec)) {
const color diffuse = (falloff * m_intensity) * sRaynormal;
const color specular = falloff * Max(0.f, powf(cos(bisector), m_specular));
light += m_color * (diffuse + specular);
}
}
return true;
}
bool directional_light::hitLight(const ray r, float t_min, float t_max, const hittable* world, hit_record rec,
color& light)
{
const ray shadowRay = ray(rec.p * rec.ellipsoidSize, pos);
const float sRaynormal = dot(shadowRay.direction(), rec.normal);
//Checking the angle between the normal and shadow ray and if it doesn't even face the light no need to check
if (sRaynormal < 0)
{
light += vec3(0.f, 0.f, 0.f);
return true;
}
const float shadowRayDist = (rec.p * rec.ellipsoidSize).length();
const float bisector = dot(shadowRay.direction(), pos);
if (!world->hit(shadowRay, t_min, fabsf(shadowRayDist), rec)) {
const color diffuse = sRaynormal;
const color specular = Max(0.f, powf(cos(bisector), 50.f));
light += m_color * (diffuse + specular);
}
return true;
}
}