-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLight.cpp
More file actions
74 lines (61 loc) · 1.97 KB
/
Light.cpp
File metadata and controls
74 lines (61 loc) · 1.97 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
#include "Light.hpp"
#include "Debug.hpp"
extern "C" {
#include "print.h"
}
DirectionalLight::DirectionalLight(Vector3 direction, Vector3 color) {
this->direction = direction.normalized();
this->color = color;
}
DirectionalLight::DirectionalLight(Vector3 direction) : DirectionalLight(direction, Vector3(1)) {
}
DirectionalLight::DirectionalLight() : DirectionalLight(Vector3(1)) {
}
LightContribution DirectionalLight::contributeLight(Hit h, Scene* sc) {
LightContribution result;
Ray shadowRay = Ray(h.position, -direction);
Hit shadowHit = sc->generateSceneHit(shadowRay, h.shape);
if (shadowHit) {
result.color = Vector3(0);
} else {
result.color = color;
}
result.direction = direction.normalized();
return result;
}
PointLight::PointLight(Vector3 position, Vector3 color) {
this->position = position;
this->color = color;
}
PointLight::PointLight(Vector3 position) : PointLight(position, Vector3(1)) {
}
PointLight::PointLight() : PointLight(Vector3(1)) {
}
LightContribution PointLight::contributeLight(Hit h, Scene* sc) {
LightContribution result;
Vector3 direction = this->position - h.position;
if (debugPrintingEnabled) {
mgba_printf("Performing shadow test");
}
if (h.shape->material->recieveShadows) {
Ray shadowRay = Ray(h.position, direction);
Hit shadowHit = sc->generateSceneHit(shadowRay, h.shape, direction.magnitude());
if (debugPrintingEnabled) {
mgba_printf("Shadow hit info:");
mgba_printf("Shadow shape: %x", shadowHit.shape);
mgba_printf("Shadow t: %x", shadowHit.t);
}
if (shadowHit) {
result.color = Vector3(0);
if (debugPrintingEnabled) {
mgba_printf("Applying shadow");
}
} else {
result.color = color;
}
} else {
result.color = color;
}
result.direction = -direction.normalized();
return result;
}