-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathscene.cc
340 lines (275 loc) · 9.46 KB
/
scene.cc
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
#include <cassert>
#include <limits>
#include <algorithm>
#include "importers/tiny_obj_loader.h"
#include "importers/eson.h"
#include "importers/mesh_loader.h"
#include "scene.h"
#include "timerutil.h"
#ifdef ENABLE_EMBREE
#include "embree2/rtcore_ray.h"
#endif
namespace mallie {
namespace {
#ifdef ENABLE_EMBREE
void error_handler(const RTCError code, const char *str) {
printf("Embree: ");
switch (code) {
case RTC_UNKNOWN_ERROR:
printf("RTC_UNKNOWN_ERROR");
break;
case RTC_INVALID_ARGUMENT:
printf("RTC_INVALID_ARGUMENT");
break;
case RTC_INVALID_OPERATION:
printf("RTC_INVALID_OPERATION");
break;
case RTC_OUT_OF_MEMORY:
printf("RTC_OUT_OF_MEMORY");
break;
case RTC_UNSUPPORTED_CPU:
printf("RTC_UNSUPPORTED_CPU");
break;
default:
printf("invalid error code");
break;
}
if (str) {
printf(" (");
while (*str)
putchar(*str++);
printf(")\n");
}
abort();
}
#endif
}
void Node::UpdateTransform() {}
Scene::Scene() {}
Scene::~Scene() {
#ifdef ENABLE_EMBREE
rtcExit();
#endif
delete[] mesh_.vertices;
delete[] mesh_.faces;
delete[] mesh_.materialIDs;
}
bool Scene::Init(const std::string &objFilename,
const std::string &esonFilename,
const std::string &magicaVoxelFilename,
const std::string &materialFilename, double sceneScale, bool sceneFit) {
bool ret = false;
if (!objFilename.empty()) {
ret = MeshLoader::LoadObj(mesh_, objFilename.c_str());
if (!ret) {
printf("Mallie:err\tmsg:Failed to load .obj file [ %s ]\n",
objFilename.c_str());
return false;
} else {
printf("Mallie:info\tmsg:Success to load .obj file [ %s ]\n",
objFilename.c_str());
}
} else if (!esonFilename.empty()) {
ret = MeshLoader::LoadESON(mesh_, esonFilename.c_str());
if (!ret) {
printf("Mallie:err\tmsg:Failed to load .eson file [ %s ]\n",
esonFilename.c_str());
return false;
} else {
printf("Mallie:info\tmsg:Success to load .eson file [ %s ]\n",
esonFilename.c_str());
}
} else if (!magicaVoxelFilename.empty()) {
ret = MeshLoader::LoadMagicaVoxel(mesh_, materials_,
magicaVoxelFilename.c_str());
if (!ret) {
printf("Mallie:err\tmsg:Failed to load .vox file [ %s ]\n",
magicaVoxelFilename.c_str());
return false;
} else {
printf("Mallie:info\tmsg:Success to load .vox file [ %s ]\n",
magicaVoxelFilename.c_str());
}
}
if (ret == false) {
printf("Mallie:err\tmsg:Failed to load mesh\n");
return ret;
}
if (sceneFit) {
real3 bmin = real3(std::numeric_limits<real>::max(),
std::numeric_limits<real>::max(),
std::numeric_limits<real>::max());
real3 bmax = real3(-std::numeric_limits<real>::max(),
-std::numeric_limits<real>::max(),
-std::numeric_limits<real>::max());
for (size_t i = 0; i < mesh_.numVertices; i++) {
bmin[0] = std::min(bmin[0], mesh_.vertices[3 * i + 0]);
bmin[1] = std::min(bmin[1], mesh_.vertices[3 * i + 1]);
bmin[2] = std::min(bmin[2], mesh_.vertices[3 * i + 2]);
bmax[0] = std::max(bmax[0], mesh_.vertices[3 * i + 0]);
bmax[1] = std::max(bmax[1], mesh_.vertices[3 * i + 1]);
bmax[2] = std::max(bmax[2], mesh_.vertices[3 * i + 2]);
}
printf("bmin = %f, %f, %f\n", bmin[0], bmin[1], bmin[2]);
printf("bmax = %f, %f, %f\n", bmax[0], bmax[1], bmax[2]);
real3 bextent = (bmax - bmin);
real3 invExtent;
invExtent[0] = (bextent[0] > 0.000001) ? (1.0 / bextent[0]) : bextent[0];
invExtent[1] = (bextent[1] > 0.000001) ? (1.0 / bextent[1]) : bextent[1];
invExtent[2] = (bextent[2] > 0.000001) ? (1.0 / bextent[2]) : bextent[2];
printf("binv = %f, %f, %f\n", invExtent[0], invExtent[1], invExtent[2]);
// to [-1, 1]^3
for (size_t i = 0; i < mesh_.numVertices; i++) {
mesh_.vertices[3 * i + 0] -= bmin[0];
mesh_.vertices[3 * i + 1] -= bmin[1];
mesh_.vertices[3 * i + 2] -= bmin[2];
mesh_.vertices[3 * i + 0] *= invExtent[0];
mesh_.vertices[3 * i + 1] *= invExtent[1];
mesh_.vertices[3 * i + 2] *= invExtent[2];
mesh_.vertices[3 * i + 0] -= 0.5;
mesh_.vertices[3 * i + 1] -= 0.5;
mesh_.vertices[3 * i + 2] -= 0.5;
mesh_.vertices[3 * i + 0] *= 2.0;
mesh_.vertices[3 * i + 1] *= 2.0;
mesh_.vertices[3 * i + 2] *= 2.0;
}
} else {
for (size_t i = 0; i < mesh_.numVertices; i++) {
mesh_.vertices[3 * i + 0] *= sceneScale;
mesh_.vertices[3 * i + 1] *= sceneScale;
mesh_.vertices[3 * i + 2] *= sceneScale;
}
}
#ifdef ENABLE_EMBREE
rtcInit(NULL);
// rtcSetErrorFunction(error_handler);
scene_ = rtcNewScene(RTC_SCENE_STATIC, RTC_INTERSECT1);
#endif
mallie::timerutil t;
t.start();
#ifdef ENABLE_EMBREE
unsigned int meshID = rtcNewTriangleMesh(scene_, RTC_GEOMETRY_STATIC,
mesh_.numFaces, mesh_.numVertices);
// Set vertices. Also computes bounding box for BoundingBox().
bmin_[0] = std::numeric_limits<real>::max();
bmin_[1] = std::numeric_limits<real>::max();
bmin_[2] = std::numeric_limits<real>::max();
bmax_[0] = -std::numeric_limits<real>::max();
bmax_[1] = -std::numeric_limits<real>::max();
bmax_[2] = -std::numeric_limits<real>::max();
float *vertices = (float *)rtcMapBuffer(scene_, meshID, RTC_VERTEX_BUFFER);
for (size_t i = 0; i < mesh_.numVertices; i++) {
vertices[4 * i + 0] = mesh_.vertices[3 * i + 0];
vertices[4 * i + 1] = mesh_.vertices[3 * i + 1];
vertices[4 * i + 2] = mesh_.vertices[3 * i + 2];
vertices[4 * i + 3] = 0.0f; // not used.
bmin_[0] = std::min(bmin_[0], (real)vertices[4 * i + 0]);
bmin_[1] = std::min(bmin_[1], (real)vertices[4 * i + 1]);
bmin_[2] = std::min(bmin_[2], (real)vertices[4 * i + 2]);
bmax_[0] = std::max(bmax_[0], (real)vertices[4 * i + 0]);
bmax_[1] = std::max(bmax_[1], (real)vertices[4 * i + 1]);
bmax_[2] = std::max(bmax_[2], (real)vertices[4 * i + 2]);
}
rtcUnmapBuffer(scene_, meshID, RTC_VERTEX_BUFFER);
// Set faces
int *triangles = (int *)rtcMapBuffer(scene_, meshID, RTC_INDEX_BUFFER);
for (size_t i = 0; i < mesh_.numFaces; i++) {
triangles[3 * i + 0] = mesh_.faces[3 * i + 0];
triangles[3 * i + 1] = mesh_.faces[3 * i + 1];
triangles[3 * i + 2] = mesh_.faces[3 * i + 2];
}
rtcUnmapBuffer(scene_, meshID, RTC_INDEX_BUFFER);
rtcCommit(scene_);
#else
BVHBuildOptions options; // Use default option
printf(" BVH build option:\n");
printf(" # of leaf primitives: %d\n", options.minLeafPrimitives);
printf(" SAH binsize : %d\n", options.binSize);
ret = accel_.Build(&mesh_, options);
assert(ret);
BVHBuildStatistics stats = accel_.GetStatistics();
printf(" BVH statistics:\n");
printf(" # of leaf nodes: %d\n", stats.numLeafNodes);
printf(" # of branch nodes: %d\n", stats.numBranchNodes);
printf(" Max tree depth : %d\n", stats.maxTreeDepth);
#endif
t.end();
printf(" BVH build time: %d msecs\n", (int)t.msec());
real3 bmin, bmax;
BoundingBox(bmin, bmax);
printf(" BVH bounding box:\n");
printf(" bmin = (%f, %f, %f)\n", bmin[0], bmin[1], bmin[2]);
printf(" bmax = (%f, %f, %f)\n", bmax[0], bmax[1], bmax[2]);
return true;
}
bool Scene::Trace(Intersection &isect, Ray &ray) {
#ifdef ENABLE_EMBREE
// Convert to Embree's ray structure.
RTCRay r;
r.org[0] = ray.org[0];
r.org[1] = ray.org[1];
r.org[2] = ray.org[2];
r.dir[0] = ray.dir[0];
r.dir[1] = ray.dir[1];
r.dir[2] = ray.dir[2];
r.tnear = 0.0f;
r.tfar = std::numeric_limits<real>::infinity();
r.geomID = RTC_INVALID_GEOMETRY_ID;
r.primID = RTC_INVALID_GEOMETRY_ID;
r.mask = -1;
r.time = 0;
rtcIntersect(scene_, r);
bool hit = (r.geomID != RTC_INVALID_GEOMETRY_ID);
if (hit) {
isect.position[0] = r.org[0] + r.tfar * r.dir[0];
isect.position[1] = r.org[1] + r.tfar * r.dir[1];
isect.position[2] = r.org[2] + r.tfar * r.dir[2];
isect.t = r.tfar;
isect.faceID = r.primID;
isect.materialID = mesh_.materialIDs[r.primID];
isect.f0 = mesh_.faces[3 * isect.faceID + 0];
isect.f1 = mesh_.faces[3 * isect.faceID + 1];
isect.f2 = mesh_.faces[3 * isect.faceID + 2];
// calc geometric normal.
real3 p0, p1, p2;
p0[0] = mesh_.vertices[3 * isect.f0 + 0];
p0[1] = mesh_.vertices[3 * isect.f0 + 1];
p0[2] = mesh_.vertices[3 * isect.f0 + 2];
p1[0] = mesh_.vertices[3 * isect.f1 + 0];
p1[1] = mesh_.vertices[3 * isect.f1 + 1];
p1[2] = mesh_.vertices[3 * isect.f1 + 2];
p2[0] = mesh_.vertices[3 * isect.f2 + 0];
p2[1] = mesh_.vertices[3 * isect.f2 + 1];
p2[2] = mesh_.vertices[3 * isect.f2 + 2];
real3 p10 = p1 - p0;
real3 p20 = p2 - p0;
real3 n = vcross(p10, p20);
n.normalize();
isect.geometricNormal = n;
isect.normal = n;
}
return hit;
#else
return accel_.Traverse(isect, &mesh_, ray);
#endif
}
void Scene::BoundingBox(real3 &bmin, real3 &bmax) {
#ifdef ENABLE_EMBREE
bmin = bmin_;
bmax = bmax_;
#else
const std::vector<BVHNode> &nodes = accel_.GetNodes();
assert(nodes.size() > 0);
bmin[0] = nodes[0].bmin[0];
bmin[1] = nodes[0].bmin[1];
bmin[2] = nodes[0].bmin[2];
bmax[0] = nodes[0].bmax[0];
bmax[1] = nodes[0].bmax[1];
bmax[2] = nodes[0].bmax[2];
#endif
}
real3 Scene::GetBackgroundRadiance(real3 &dir) {
// Constant dome light
return real3(0.75, 0.75, 0.75);
}
} // namespace