-
Notifications
You must be signed in to change notification settings - Fork 0
/
Primitives.cpp
executable file
·275 lines (200 loc) · 6.97 KB
/
Primitives.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
265
266
267
268
269
270
271
272
273
274
275
#include "Primitives.h"
#include "algebra3.h"
#include <cfloat>
///////////////////////////////////////////////
// GeoPrimitive Class //
///////////////////////////////////////////////
/* Constructors */
GeoPrimitive::GeoPrimitive(Shape *shape, Material* mat) {
this->shape = shape;
material = mat;
}
GeoPrimitive::~GeoPrimitive() {
delete shape;
}
/* Instance methods */
bool GeoPrimitive::intersect(Ray& ray, IntersectRecord* rec) {
// Just this for now...later on we'll need populate other
// members of "rec" before returning.
bool hit = shape->intersect(ray, rec);
if (hit) {
rec->primitive = this;
return true;
} else return false;
}
Reflectance GeoPrimitive::getReflectance(const vec3& point) {
return material->getReflectance(point, shape);
}
BoundingBox GeoPrimitive::getBoundingBox() {
return shape->getBoundingBox();
}
Primitive* GeoPrimitive::instance(const mat4& transform, Material* mat) {
return new GeoPrimitive(new TransformedShape(shape, transform), mat);
}
///////////////////////////////////////////////
// BoundingBoxTree Class //
///////////////////////////////////////////////
BoundingBoxTree::BoundingBoxTree(const vector<Primitive*>& objects, int splitAxis) {
this->splitAxis = splitAxis;
unsigned int length = objects.size();
if (length == 1) {
low = objects[0];
high = NULL;
box = objects[0]->getBoundingBox();
} else if (length == 2) {
low = objects[0];
high = objects[1];
box = BoundingBox::combine(low->getBoundingBox(), high->getBoundingBox());
} else {
vector<Primitive*> lowVec;
vector<Primitive*> highVec;
partition(splitAxis, objects, &lowVec, &highVec);
low = lowVec.size() > 0 ? new BoundingBoxTree(lowVec, (splitAxis + 1) % 3) : NULL;
high = highVec.size() > 0 ? new BoundingBoxTree(highVec, (splitAxis + 1) % 3) : NULL;
if (low == NULL)
box = high->getBoundingBox();
else if (high == NULL)
box = low->getBoundingBox();
else box = BoundingBox::combine(low->getBoundingBox(), high->getBoundingBox());
}
}
void BoundingBoxTree::partition(int axis, const vector<Primitive*>& all, vector<Primitive*>* lowVec, vector<Primitive*>* highVec) {
double min, max;
min = DBL_MAX;
max = -DBL_MAX;
for(unsigned int i = 0; i < all.size(); i++) {
BoundingBox bbox = all[i]->getBoundingBox();
min = MIN(min, bbox.minCoordinate(axis));
max = MAX(max, bbox.maxCoordinate(axis));
}
double pivot = (max + min) / 2;
for (unsigned int i = 0; i < all.size(); i++) {
BoundingBox bbox = all[i]->getBoundingBox();
double centerCoord = (bbox.maxCoordinate(axis) + bbox.minCoordinate(axis)) / 2;
if (centerCoord < pivot)
lowVec->push_back(all[i]);
else highVec->push_back(all[i]);
}
// Check to see if we failed to make a partition. If so, grab the closest thing
// that isn't on the other side of the partition and throw it into the empty list.
// CASE: Everything ended up in highVec
if (highVec->size() == all.size()) {
double minCoord = DBL_MAX;
unsigned int index;
Primitive* obj;
for (unsigned int i = 0; i < highVec->size(); i++) {
BoundingBox bbox = (*highVec)[i]->getBoundingBox();
double centerCoord = (bbox.maxCoordinate(axis) + bbox.minCoordinate(axis)) / 2;
if (centerCoord < minCoord) {
minCoord = centerCoord;
index = i;
obj = (*highVec)[i];
}
}
highVec->erase(highVec->begin() + index);
lowVec->push_back(obj);
}
// CASE: Everything ended up in lowVec
else if (lowVec->size() == all.size()) {
double maxCoord = -DBL_MAX;
unsigned int index;
Primitive* obj;
for (unsigned int i = 0; i < lowVec->size(); i++) {
BoundingBox bbox = (*lowVec)[i]->getBoundingBox();
double centerCoord = (bbox.maxCoordinate(axis) + bbox.minCoordinate(axis)) / 2;
if (centerCoord > maxCoord) {
maxCoord = centerCoord;
index = i;
obj = (*lowVec)[i];
}
}
lowVec->erase(lowVec->begin() + index);
highVec->push_back(obj);
}
}
BoundingBoxTree::BoundingBoxTree(BoundingBoxTree* otherTree, const mat4& transMat, Material* mat) {
splitAxis = otherTree->splitAxis;
high = (otherTree->high == NULL ? NULL : otherTree->high->instance(transMat, mat));
low = (otherTree->low == NULL ? NULL : otherTree->low->instance(transMat, mat));
if (low == NULL)
box = high->getBoundingBox();
else if (high == NULL)
box = low->getBoundingBox();
else box = BoundingBox::combine(low->getBoundingBox(), high->getBoundingBox());
}
BoundingBoxTree::~BoundingBoxTree() {
delete high;
delete low;
}
Reflectance BoundingBoxTree::getReflectance(const vec3& point) {
// This should never be called.
throw "BoundingBoxTree does not implement this method.";
}
BoundingBox BoundingBoxTree::getBoundingBox() {
return box;
}
Primitive* BoundingBoxTree::instance(const mat4& transform, Material* mat) {
return new BoundingBoxTree(this, transform, mat);
}
bool BoundingBoxTree::intersect(Ray& ray, IntersectRecord* rec) {
if (!box.intersect(ray, rec))
return false;
rec->t = ray.getUpperBound();
bool hit1 = false, hit2 = false;
double oldMax = rec->t;
// CASE: Ray is moving in positive direction
if (ray.getSign(splitAxis) == 0) {
hit1 = (low != NULL && low->intersect(ray, rec));
ray.setBounds(ray.getLowerBound(), rec->t);
hit2 = (high != NULL && high->intersect(ray, rec));
ray.setBounds(ray.getLowerBound(), oldMax); // Reset ray bounds before returning
return (hit1 || hit2);
}
// CASE: Ray is moving in negative direction
else {
hit1 = (high != NULL && high->intersect(ray, rec));
ray.setBounds(ray.getLowerBound(), rec->t);
hit2 = (low != NULL && low->intersect(ray, rec));
ray.setBounds(ray.getLowerBound(), oldMax); // Reset ray bounds before returning
return (hit1 || hit2);
}
}
///////////////////////////////////////////////
// MeshPrimitive Class //
///////////////////////////////////////////////
MeshPrimitive::MeshPrimitive(Mesh* mesh, vector<Shape*> triangles, Material* mat) {
this->mesh = mesh;
this->mat = mat;
lastIntersected = NULL;
vector<Primitive*> prims;
for (unsigned int i = 0; i < triangles.size(); i++)
prims.push_back(new GeoPrimitive(triangles[i], mat));
this->triangleTree = new BoundingBoxTree(prims, VZ);
}
MeshPrimitive::MeshPrimitive(MeshPrimitive* otherMesh, const mat4& transMat, Material* mat) {
this->mesh = otherMesh->mesh;
this->mat = mat;
lastIntersected = NULL;
triangleTree = (BoundingBoxTree*)otherMesh->triangleTree->instance(transMat, mat);
}
MeshPrimitive::~MeshPrimitive() {
delete triangleTree;
}
bool MeshPrimitive::intersect(Ray& ray, IntersectRecord* rec) {
bool hit = triangleTree->intersect(ray, rec);
if (hit) {
lastIntersected = rec->primitive;
rec->primitive = this;
}
return hit;
}
// Assumes that we've just intersected at "point."
Reflectance MeshPrimitive::getReflectance(const vec3& point) {
return lastIntersected->getReflectance(point);
}
BoundingBox MeshPrimitive::getBoundingBox() {
return triangleTree->getBoundingBox();
}
Primitive* MeshPrimitive::instance(const mat4& transform, Material* mat) {
return new MeshPrimitive(this, transform, mat);
}