-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsurface.cpp
264 lines (223 loc) · 9.69 KB
/
surface.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <cmath>
#include "surface.h"
#include "util.h"
#include "shader.h"
#include <iostream>
#include <algorithm>
Sphere::Sphere()
{
this->radius = 0;
this->center = { 0, 0, 0 };
};
Sphere::Sphere(float radius)
{
this->radius = radius;
this->center = { 0, 0, 0 };
};
Sphere::Sphere(float radius, Math::Vector3 center)
{
this->radius = radius;
this->center = Math::Vector3(center);
};
float Sphere::getRadius() const
{
return this->radius;
};
Math::Vector3 Sphere::getCenter() const
{
return this->center;
};
void Sphere::setRadius(float radius)
{
this->radius = radius;
};
void Sphere::setCenter(Math::Vector3 center)
{
this->center = Math::Vector3(center);
};
bool Sphere::hit(Math::Ray ray, float t0, float t1, std::shared_ptr<Util::HitRecord> & hitRecord) const
{
float discriminant = std::pow((Math::dot(ray.direction, ray.origin - this->center)), 2) -
(Math::dot(ray.direction, ray.direction)) *
(Math::dot(ray.origin - this->center, ray.origin - this->center) - this->radius * this->radius);
if (discriminant < 0) { return false; };
if (discriminant == 0)
{
const float t = -(Math::dot(ray.direction, ray.origin - this->center)) / (Math::dot(ray.direction, ray.direction));
if (t < t0 || t > t1) { return false; };
hitRecord->intersectionTime = t;
const Math::Vector3 p = ray.origin + t * ray.direction;
hitRecord->unitNormal = (p - this->center) / this->radius;
hitRecord->intersectionPoint = p;
return true;
}
const float tPlus = (Math::dot(-ray.direction, ray.origin - this->center) + std::sqrt(discriminant)) / Math::dot(ray.direction, ray.direction);
const float tMinus = (Math::dot(-ray.direction, ray.origin - this->center) - std::sqrt(discriminant)) / Math::dot(ray.direction, ray.direction);
if ((tPlus < t0 || tPlus > t1) && (tMinus < t0 || tMinus > t1)) { return false; };
// generally t will be non negative (ie the ray shoots forward and only forward from the origin). we want the first hit between t0 and t1
const float t = std::min(tPlus, tMinus) >= t0 && std::min(tPlus, tMinus) <= t1 ? std::min(tPlus, tMinus) : std::max(tPlus, tMinus);
hitRecord->intersectionTime = t;
const Math::Vector3 p = ray.origin + t * ray.direction;
hitRecord->unitNormal = (p - this->center) / this->radius;
hitRecord->intersectionPoint = p;
return true;
};
Math::Box Sphere::boundingBox() const
{
Math::Vector3 min = { this->center.getX() - radius, this->center.getY() - radius, this->center.getZ() - radius };
Math::Vector3 max = { this->center.getX() + radius, this->center.getY() + radius, this->center.getZ() + radius };
return Math::Box(min, max);
};
Triangle::Triangle()
{
this->vertex1 = { 0, 0, 0 };
this->vertex2 = { 0, 0, 0 };
this->vertex3 = { 0, 0, 0 };
}
Triangle::Triangle(Math::Vector3 vertex1, Math::Vector3 vertex2, Math::Vector3 vertex3)
{
this->vertex1 = Math::Vector3(vertex1);
this->vertex2 = Math::Vector3(vertex2);
this->vertex3 = Math::Vector3(vertex3);
}
Triangle::Triangle(Math::Vector3 vertex1, Math::Vector3 vertex2, Math::Vector3 vertex3, Math::Vector3 facingDirection)
{
if (Math::dot(Math::cross(vertex2 - vertex1, vertex3 - vertex2), facingDirection) >= 0)
{
this->vertex1 = vertex1;
this->vertex2 = vertex2;
this->vertex3 = vertex3;
return;
}
this->vertex1 = vertex2;
this->vertex2 = vertex1;
this->vertex3 = vertex3;
}
std::vector<Math::Vector3> Triangle::getVertices() const
{
std::vector<Math::Vector3> vertices = std::vector<Math::Vector3>();
vertices.push_back(this->vertex1);
vertices.push_back(this->vertex2);
vertices.push_back(this->vertex3);
return vertices;
}
Math::Vector3 Triangle::getUnitNormal() const
{
const Math::Vector3 normal = Math::cross(this->vertex2 - this->vertex1, this->vertex3 - this->vertex2);
return normal / normal.norm();
}
void Triangle::setVertices(Math::Vector3 vertex1, Math::Vector3 vertex2, Math::Vector3 vertex3)
{
this->vertex1 = vertex1;
this->vertex2 = vertex2;
this->vertex3 = vertex3;
}
bool Triangle::hit(Math::Ray ray, float t0, float t1, std::shared_ptr<Util::HitRecord> & hitRecord) const
{
const float a = this->vertex1.getX() - this->vertex2.getX();
const float b = this->vertex1.getY() - this->vertex2.getY();
const float c = this->vertex1.getZ() - this->vertex2.getZ();
const float d = this->vertex1.getX() - this->vertex3.getX();
const float e = this->vertex1.getY() - this->vertex3.getY();
const float f = this->vertex1.getZ() - this->vertex3.getZ();
const float g = ray.direction.getX();
const float h = ray.direction.getY();
const float i = ray.direction.getZ();
const float j = this->vertex1.getX() - ray.origin.getX();
const float k = this->vertex1.getY() - ray.origin.getY();
const float l = this->vertex1.getZ() - ray.origin.getZ();
const float eiMinusHf = e * i - h * f;
const float gfMinusDi = g * f - d * i;
const float dhMinusEg = d * h - e * g;
const float akMinusJb = a * k - j * b;
const float jcMinusAl = j * c - a * l;
const float blMinusKc = b * l - k * c;
const float M = a * eiMinusHf + b * gfMinusDi + c * dhMinusEg;
const float t = -((f * akMinusJb + e * jcMinusAl + d * blMinusKc) / M);
if (t < t0 || t > t1) { return false; }
const float gamma = (i * akMinusJb + h * jcMinusAl + g * blMinusKc) / M;
if (gamma < 0 || gamma > 1) { return false; }
const float beta = (j * eiMinusHf + k * gfMinusDi + l * dhMinusEg) / M;
if (beta < 0 || beta > 1 - gamma) { return false; }
hitRecord->intersectionTime = t;
hitRecord->unitNormal = Math::Vector3(this->getUnitNormal());
hitRecord->intersectionPoint = ray.origin + t * ray.direction;
return true;
}
Math::Box Triangle::boundingBox() const
{
float minX = std::min({ this->vertex1.getX(), this->vertex2.getX(), this->vertex3.getX() });
float minY = std::min({ this->vertex1.getY(), this->vertex2.getY(), this->vertex3.getY() });
float minZ = std::min({ this->vertex1.getZ(), this->vertex2.getZ(), this->vertex3.getZ() });
float maxX = std::max({ this->vertex1.getX(), this->vertex2.getX(), this->vertex3.getX() });
float maxY = std::max({ this->vertex1.getY(), this->vertex2.getY(), this->vertex3.getY() });
float maxZ = std::max({ this->vertex1.getZ(), this->vertex2.getZ(), this->vertex3.getZ() });
return Math::Box({ minX, minY, minZ }, { maxX, maxY, maxZ });
}
GroupSurface::GroupSurface()
{
this->surfaces = std::vector<std::unique_ptr<Surface>>();
this->minBound = { 0, 0, 0 };
this->maxBound = { 0, 0, 0 };
}
void GroupSurface::addSurface(std::unique_ptr<Surface> surface)
{
this->minBound = {
std::min(this->minBound.getX(), surface->boundingBox().min.getX()),
std::min(this->minBound.getY(), surface->boundingBox().min.getY()),
std::min(this->minBound.getZ(), surface->boundingBox().min.getZ())
};
this->maxBound = {
std::max(this->maxBound.getX(), surface->boundingBox().max.getX()),
std::max(this->maxBound.getY(), surface->boundingBox().max.getY()),
std::max(this->maxBound.getZ(), surface->boundingBox().max.getZ())
};
this->surfaces.push_back(std::move(surface));
}
bool GroupSurface::hit(Math::Ray ray, float t0, float t1, std::shared_ptr<Util::HitRecord> & hitRecord) const
{
bool groupHit = false;
bool surfaceHit;
float tMax = t1;
int surfaceIndex = 0;
std::shared_ptr<Util::HitRecord> surfaceHitRecord = std::shared_ptr<Util::HitRecord>(new Util::HitRecord);
for (auto & surface : this->surfaces)
{
surfaceHit = surface->hit(ray, t0, t1, surfaceHitRecord);
if (surfaceHit && surfaceHitRecord->intersectionTime >= t0 && surfaceHitRecord->intersectionTime <= tMax)
{
groupHit = true;
tMax = surfaceHitRecord->intersectionTime;
hitRecord->intersectionTime = surfaceHitRecord->intersectionTime;
hitRecord->unitNormal = surfaceHitRecord->unitNormal;
hitRecord->intersectionPoint = surfaceHitRecord->intersectionPoint;
hitRecord->hitObjectIndex = surfaceIndex;
}
surfaceIndex += 1;
}
return groupHit;
}
Math::Box GroupSurface::boundingBox() const
{
return Math::Box(this->minBound, this->maxBound);
}
void Surface::setMaterial(std::unique_ptr<Shader> shader)
{
this->shader = std::move(shader);
}
Util::Color Surface::computeColor(const std::vector<std::unique_ptr<LightSource>> &lightSources, Math::Ray viewRay, std::shared_ptr<Renderable> surface, std::shared_ptr<Util::HitRecord> hitRecord) const
{
if (this->shader == NULL) { return { 0, 0, 0 }; }
return this->shader->computeColor(lightSources, viewRay, surface, hitRecord);
}
Util::Color GroupSurface::computeColor(const std::vector<std::unique_ptr<LightSource>> &lightSources, Math::Ray viewRay, std::shared_ptr<Renderable> surface, std::shared_ptr<Util::HitRecord> hitRecord) const
{
const bool hitsGroup = this->hit(viewRay, 0, std::numeric_limits<float>::max(), hitRecord);
if (!hitsGroup) {
hitRecord->intersectionTime = -1;
return { 0, 0, 0 };
} // hitRecord shows that no hit occured
if (this->surfaces.at(hitRecord->hitObjectIndex)->shader == NULL && this->shader == NULL) { return { 0, 0, 0 }; } // hitRecord shows a hit and no shader displays black
if (this->surfaces.at(hitRecord->hitObjectIndex)->shader == NULL) { return this->shader->computeColor(lightSources, viewRay, surface, hitRecord); } // use the shader of the group surface
return this->surfaces.at(hitRecord->hitObjectIndex)->shader->computeColor(lightSources, viewRay, surface, hitRecord);
}