-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsurface.cpp
More file actions
402 lines (320 loc) · 12.5 KB
/
surface.cpp
File metadata and controls
402 lines (320 loc) · 12.5 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include "surface.h"
#define TINYOBJLOADER_IMPLEMENTATION
#include "tinyobjloader/tiny_obj_loader.h"
std::vector<Surface> createSurfaces(std::string pathToObj, bool isLight, uint32_t shapeIdx)
{
std::string objDirectory;
const size_t last_slash_idx = pathToObj.rfind('/');
if (std::string::npos != last_slash_idx) {
objDirectory = pathToObj.substr(0, last_slash_idx);
}
std::vector<Surface> surfaces;
tinyobj::ObjReader reader;
tinyobj::ObjReaderConfig reader_config;
if (!reader.ParseFromFile(pathToObj, reader_config)) {
if (!reader.Error().empty()) {
std::cerr << "TinyObjReader: " << reader.Error();
}
exit(1);
}
if (!reader.Warning().empty()) {
std::cout << "TinyObjReader: " << reader.Warning();
}
auto& attrib = reader.GetAttrib();
auto& shapes = reader.GetShapes();
auto& materials = reader.GetMaterials();
// Loop over shapes
for (size_t s = 0; s < shapes.size(); s++) {
Surface surf;
surf.isLight = isLight;
surf.shapeIdx = shapeIdx;
std::set<int> materialIds;
// Loop over faces(polygon)
size_t index_offset = 0;
for (size_t f = 0; f < shapes[s].mesh.num_face_vertices.size(); f++) {
size_t fv = size_t(shapes[s].mesh.num_face_vertices[f]);
if (fv > 3) {
std::cerr << "Not a triangle mesh" << std::endl;
exit(1);
}
// Loop over vertices in the face. Assume 3 vertices per-face
Vector3f vertices[3], normals[3];
Vector2f uvs[3];
for (size_t v = 0; v < fv; v++) {
// access to vertex
tinyobj::index_t idx = shapes[s].mesh.indices[index_offset + v];
tinyobj::real_t vx = attrib.vertices[3 * size_t(idx.vertex_index) + 0];
tinyobj::real_t vy = attrib.vertices[3 * size_t(idx.vertex_index) + 1];
tinyobj::real_t vz = attrib.vertices[3 * size_t(idx.vertex_index) + 2];
// Check if `normal_index` is zero or positive. negative = no normal data
if (idx.normal_index >= 0) {
tinyobj::real_t nx = attrib.normals[3 * size_t(idx.normal_index) + 0];
tinyobj::real_t ny = attrib.normals[3 * size_t(idx.normal_index) + 1];
tinyobj::real_t nz = attrib.normals[3 * size_t(idx.normal_index) + 2];
normals[v] = Vector3f(nx, ny, nz);
}
// Check if `texcoord_index` is zero or positive. negative = no texcoord data
if (idx.texcoord_index >= 0) {
tinyobj::real_t tx = attrib.texcoords[2 * size_t(idx.texcoord_index) + 0];
tinyobj::real_t ty = attrib.texcoords[2 * size_t(idx.texcoord_index) + 1];
uvs[v] = Vector2f(tx, ty);
}
vertices[v] = Vector3f(vx, vy, vz);
}
int vSize = surf.vertices.size();
Vector3i findex(vSize, vSize + 1, vSize + 2);
surf.vertices.push_back(vertices[0]);
surf.vertices.push_back(vertices[1]);
surf.vertices.push_back(vertices[2]);
surf.normals.push_back(normals[0]);
surf.normals.push_back(normals[1]);
surf.normals.push_back(normals[2]);
surf.uvs.push_back(uvs[0]);
surf.uvs.push_back(uvs[1]);
surf.uvs.push_back(uvs[2]);
surf.indices.push_back(findex);
// Create Triangle
Tri triangle;
triangle.v1 = vertices[0];
triangle.v2 = vertices[1];
triangle.v3 = vertices[2];
triangle.uv1 = uvs[0];
triangle.uv2 = uvs[1];
triangle.uv3 = uvs[2];
triangle.normal = Normalize(normals[0] + normals[1] + normals[2]);
triangle.centroid = (triangle.v1 + triangle.v2 + triangle.v3) / 3.f;
for (int i = 0; i < 3; i++) {
triangle.bbox.min = Vector3f(
std::min(triangle.bbox.min.x, vertices[i].x),
std::min(triangle.bbox.min.y, vertices[i].y),
std::min(triangle.bbox.min.z, vertices[i].z)
);
triangle.bbox.max = Vector3f(
std::max(triangle.bbox.max.x, vertices[i].x),
std::max(triangle.bbox.max.y, vertices[i].y),
std::max(triangle.bbox.max.z, vertices[i].z)
);
triangle.bbox.centroid = (triangle.bbox.min + triangle.bbox.max) / 2.f;
}
surf.tris.push_back(triangle);
// BVH indirection indices
surf.triIdxs.push_back(f);
// Update surface AABB
surf.bbox.min = Vector3f(
std::min(surf.bbox.min.x, triangle.bbox.min.x),
std::min(surf.bbox.min.y, triangle.bbox.min.y),
std::min(surf.bbox.min.z, triangle.bbox.min.z)
);
surf.bbox.max = Vector3f(
std::max(surf.bbox.max.x, triangle.bbox.max.x),
std::max(surf.bbox.max.y, triangle.bbox.max.y),
std::max(surf.bbox.max.z, triangle.bbox.max.z)
);
surf.bbox.centroid = (surf.bbox.min + surf.bbox.max) / 2.f;
// per-face material
materialIds.insert(shapes[s].mesh.material_ids[f]);
index_offset += fv;
}
if (materialIds.size() > 1) {
std::cerr << "One of the meshes has more than one material. This is not allowed." << std::endl;
exit(1);
}
if (materialIds.size() == 0) {
std::cerr << "One of the meshes has no material definition, may cause unexpected behaviour." << std::endl;
}
else {
// Load textures from Materials
auto matId = *materialIds.begin();
if (matId != -1) {
auto mat = materials[matId];
surf.diffuse = Vector3f(mat.diffuse[0], mat.diffuse[1], mat.diffuse[2]);
if (mat.diffuse_texname != "")
surf.diffuseTexture = Texture(objDirectory + "/" + mat.diffuse_texname);
surf.alpha = mat.specular[0];
if (mat.alpha_texname != "")
surf.alphaTexture = Texture(objDirectory + "/" + mat.alpha_texname);
} else {
// Assign a default diffuse color of (1,1,1)
surf.diffuse = Vector3f(1, 1, 1);
}
}
// Allocate memory for BVH & build the BVH
surf.nodes = (BVHNode*)malloc((2 * surf.triIdxs.size() - 1) * sizeof(BVHNode));
for (int i = 0; i < 2 * surf.triIdxs.size() - 1; i++) {
surf.nodes[i] = BVHNode();
}
surf.buildBVH();
surfaces.push_back(surf);
shapeIdx++;
}
return surfaces;
}
bool Surface::hasDiffuseTexture() { return this->diffuseTexture.data != 0; }
bool Surface::hasAlphaTexture() { return this->alphaTexture.data != 0; }
Interaction Surface::rayPlaneIntersect(Ray ray, Vector3f p, Vector3f n)
{
Interaction si;
float dDotN = Dot(ray.d, n);
if (dDotN != 0.f) {
float t = -Dot((ray.o - p), n) / dDotN;
if (t >= 0.f) {
si.didIntersect = true;
si.t = t;
si.n = n;
si.p = ray.o + ray.d * si.t;
}
}
return si;
}
Interaction Surface::rayTriangleIntersect(Ray ray, Vector3f v1, Vector3f v2, Vector3f v3, Vector3f n)
{
Interaction si = this->rayPlaneIntersect(ray, v1, n);
if (si.didIntersect) {
bool edge1 = false, edge2 = false, edge3 = false;
// Check edge 1
{
Vector3f nIp = Cross((si.p - v1), (v3 - v1));
Vector3f nTri = Cross((v2 - v1), (v3 - v1));
edge1 = Dot(nIp, nTri) > 0;
}
// Check edge 2
{
Vector3f nIp = Cross((si.p - v1), (v2 - v1));
Vector3f nTri = Cross((v3 - v1), (v2 - v1));
edge2 = Dot(nIp, nTri) > 0;
}
// Check edge 3
{
Vector3f nIp = Cross((si.p - v2), (v3 - v2));
Vector3f nTri = Cross((v1 - v2), (v3 - v2));
edge3 = Dot(nIp, nTri) > 0;
}
if (edge1 && edge2 && edge3) {
// Intersected triangle!
si.didIntersect = true;
si.triangleIntersected.v1 = v1;
si.triangleIntersected.v2 = v2;
si.triangleIntersected.v3 = v3;
// This is buggy. I can imagine a case when two shapes overlap and we don;t know which one to color. Many cases in fact.
for(Tri triangle : this->tris){
if(
triangle.v1 == v1 &&
triangle.v2 == v2 &&
triangle.v3 == v3
){
si.triangleIntersected.uv1 = triangle.uv1;
si.triangleIntersected.uv2 = triangle.uv2;
si.triangleIntersected.uv3 = triangle.uv3;
si.intersected_on_surface = this;
break;
}
}
}
else {
si.didIntersect = false;
}
}
return si;
}
void Surface::buildBVH()
{
// Root node
this->numBVHNodes += 1;
BVHNode& rootNode = this->nodes[0];
rootNode.firstPrim = 0;
rootNode.primCount = this->triIdxs.size();
this->updateNodeBounds(0);
this->subdivideNode(0);
}
uint32_t Surface::getIdx(uint32_t idx)
{
return this->triIdxs[idx];
}
void Surface::updateNodeBounds(uint32_t nodeIdx)
{
BVHNode& node = this->nodes[nodeIdx];
for (int i = 0; i < node.primCount; i++) {
auto triangle = this->tris[this->getIdx(i + node.firstPrim)];
node.bbox.min = Vector3f(
std::min(node.bbox.min.x, triangle.bbox.min.x),
std::min(node.bbox.min.y, triangle.bbox.min.y),
std::min(node.bbox.min.z, triangle.bbox.min.z)
);
node.bbox.max = Vector3f(
std::max(node.bbox.max.x, triangle.bbox.max.x),
std::max(node.bbox.max.y, triangle.bbox.max.y),
std::max(node.bbox.max.z, triangle.bbox.max.z)
);
node.bbox.centroid = (node.bbox.min + node.bbox.max) / 2.f;
}
}
void Surface::subdivideNode(uint32_t nodeIdx)
{
BVHNode& node = this->nodes[nodeIdx];
if (node.primCount <= 1) return;
Vector3f extent = node.bbox.max - node.bbox.min;
int ax = 0;
if (extent.y > extent.x) ax = 1;
if (extent.z > extent[ax]) ax = 2;
float split = node.bbox.min[ax] + extent[ax] * 0.5f;
int i = node.firstPrim;
int j = i + node.primCount - 1;
while (i <= j) {
if (this->tris[this->getIdx(i)].centroid[ax] < split)
i++;
else {
auto temp = this->triIdxs[i];
this->triIdxs[i] = this->triIdxs[j];
this->triIdxs[j--] = temp;
}
}
int leftCount = i - node.firstPrim;
if (leftCount == 0 || leftCount == node.primCount) return;
uint32_t lidx = this->numBVHNodes++;
BVHNode& left = this->nodes[lidx];
left.firstPrim = node.firstPrim;
left.primCount = leftCount;
this->updateNodeBounds(lidx);
uint32_t ridx = this->numBVHNodes++;
BVHNode& right = this->nodes[ridx];
right.firstPrim = i;
right.primCount = node.primCount - leftCount;
this->updateNodeBounds(ridx);
node.left = lidx;
node.right = ridx;
node.primCount = 0;
this->subdivideNode(lidx);
this->subdivideNode(ridx);
}
void Surface::intersectBVH(uint32_t nodeIdx, Ray& ray, Interaction& si)
{
BVHNode& node = this->nodes[nodeIdx];
if (!node.bbox.intersects(ray)) return;
if (node.primCount != 0) {
// Leaf
for (uint32_t i = 0; i < node.primCount; i++) {
Interaction siIntermediate = this->rayTriangleIntersect(
ray,
this->tris[this->getIdx(i + node.firstPrim)].v1,
this->tris[this->getIdx(i + node.firstPrim)].v2,
this->tris[this->getIdx(i + node.firstPrim)].v3,
this->tris[this->getIdx(i + node.firstPrim)].normal
);
if (siIntermediate.t <= ray.t && siIntermediate.didIntersect) {
si = siIntermediate;
ray.t = si.t;
}
}
}
else {
this->intersectBVH(node.left, ray, si);
this->intersectBVH(node.right, ray, si);
}
}
Interaction Surface::rayIntersect(Ray& ray)
{
Interaction si;
si.didIntersect = false;
this->intersectBVH(0, ray, si);
return si;
}