-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscene.cpp
162 lines (123 loc) · 3.85 KB
/
scene.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// File: scene.cpp
// Author: Samuel McFalls
// Description: Implements the Scene class
#include "scene.hpp"
Scene::Scene() {
}
double Scene::getCameraX() const {
return camera.getSizeX();
}
double Scene::getCameraY() const {
return camera.getSizeY();
}
void Scene::setCamera(const Camera &cameraSet) {
camera = cameraSet;
}
void Scene::addSphere(Sphere sphere) {
spheres.push_back(sphere);
}
void Scene::addPlane(Plane plane) {
planes.push_back(plane);
}
void Scene::addLight(Light light) {
lights.push_back(light);
}
void Scene::addLights(std::vector<Light> lights) {
for (auto& it : lights) {
addLight(it);
}
}
Color Scene::pixelByTrace(int x, int y) const {
Ray primary = camera.pixelRay(x, y);
double t;
Vec3 intersection;
Sphere intSphere;
Plane intPlane;
std::string type;
t = intersectsObject(primary, intSphere, intPlane, type);
intersection = primary.getOrigin() + t * primary.getDirection();
Color pixelColor = Color(0, 0, 0);
if (t == std::numeric_limits<double>::infinity()) {return Color(0, 0, 0);}
for (unsigned int i = 0; i < lights.size(); i++) {
Light sLight = lights[i];
Ray shadow = Ray(intersection, sLight.getLocation());
if (type == "sphere") {
Sphere shSphere;
Plane shPlane;
std::string shType;
t = intersectsObject(shadow, shSphere, shPlane, shType);
double camDist = intersectsCamera(shadow);
if (t != std::numeric_limits<double>::infinity() && t < camDist) {
double lDist = (shadow.getDestination() - shadow.getOrigin()).magnitude();
if (t < lDist) {continue;}
}
Ray surfaceNormalRay = Ray(intSphere.getCenter(), intersection);
Vec3 surfaceNormalVec = surfaceNormalRay.getDirection();
Ray lightRay = Ray(intersection, sLight.getLocation());
Vec3 surfaceLightVec = lightRay.getDirection();
double scale = surfaceNormalVec.dot(surfaceLightVec) * intSphere.getLambert();
scale = (scale < 0) ? 0 : scale;
pixelColor += scale * sLight.getIntensity() * intSphere.getColor();
}
else if (type == "plane") {
Sphere shSphere;
Plane shPlane;
std::string shType;
t = intersectsObject(shadow, shSphere, shPlane, shType);
double camDist = intersectsCamera(shadow);
if (t != std::numeric_limits<double>::infinity() && t < camDist) {
double lDist = (shadow.getDestination() - shadow.getOrigin()).magnitude();
if (t < lDist) {continue;}
}
double angleDet = intPlane.getNormal().dot(primary.getDirection());
Vec3 surfaceNormalVec;
if (angleDet < 0) {
surfaceNormalVec = intPlane.getNormal().normalize() * -1;
}
else {surfaceNormalVec = intPlane.getNormal().normalize();}
Ray lightRay = Ray(sLight.getLocation(), intersection);
Vec3 surfaceLightVec = lightRay.getDirection();
double scale = surfaceNormalVec.dot(surfaceLightVec) * intPlane.getLambert();
scale = (scale < 0) ? 0 : scale;
pixelColor += scale * sLight.getIntensity() * intPlane.getColor();
}
else {continue;}
}
return pixelColor;
}
double Scene::intersectsObject(Ray ray, Sphere &sphere, Plane &plane, std::string &type) const {
double intLoc;
double t = std::numeric_limits<double>::infinity();
type = "none";
for (unsigned int i = 0; i < spheres.size(); i++) {
intLoc = spheres[i].intersectedBy(ray);
if (intLoc < t) {
sphere = spheres[i];
type = "sphere";
t = intLoc;
}
}
for (unsigned int i = 0; i < planes.size(); i++) {
intLoc = planes[i].intersectedBy(ray);
if (intLoc < t) {
plane = planes[i];
type = "plane";
t = intLoc;
}
}
return t;
}
double Scene::intersectsCamera(Ray ray) const {
Vec3 n = camera.getNormal().normalize();
Vec3 l = ray.getDirection().normalize();
double denominator = n.dot(l);
double inf = std::numeric_limits<double>::infinity();
if (std::abs(denominator) > 1e-6) {
double t = ((camera.getCenter() - ray.getOrigin()).dot(n)) / denominator;
if (t <= 1e-6) {
return inf;
}
return t;
}
return inf;
}