-
Notifications
You must be signed in to change notification settings - Fork 3
/
BVHTracing.cpp
409 lines (331 loc) · 11.4 KB
/
BVHTracing.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
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
403
404
405
406
407
408
409
#include <bvh/bvh.hpp>
#include <bvh/vector.hpp>
#include <bvh/triangle.hpp>
#include <bvh/sweep_sah_builder.hpp>
#include <bvh/ray.hpp>
#include <bvh/single_ray_traverser.hpp>
#include <bvh/primitive_intersectors.hpp>
#include <glm.hpp>
#include <gtc/matrix_transform.hpp>
#include <unordered_map>
#define TINYOBJLOADER_IMPLEMENTATION
#include "tiny_obj_loader.h"
#include "crc64.h"
const double PI = 3.14159265359;
const double DEG2RAD = PI / 180.0;
struct Primitive
{
glm::mat4 model_matrix;
int num_pos = 0;
int num_face = 0;
std::vector<glm::vec4> pos;
std::vector<glm::vec4> norm;
std::vector<glm::ivec3> faces;
Primitive()
{
model_matrix = glm::identity<glm::mat4>();
}
void CreateBox(float width, float height, float depth)
{
pos.clear();
norm.clear();
faces.clear();
float half_w = width * 0.5f;
float half_h = height * 0.5f;
float half_d = depth * 0.5f;
// x positive
{
int v_start = (int)pos.size();
pos.push_back({ half_w, half_h, half_d, 1.0f });
pos.push_back({ half_w, half_h, -half_d, 1.0f });
pos.push_back({ half_w, -half_h, half_d, 1.0f });
pos.push_back({ half_w, -half_h, -half_d, 1.0f });
norm.push_back({ 1.0f, 0.0f, 0.0f, 0.0f });
norm.push_back({ 1.0f, 0.0f, 0.0f, 0.0f });
norm.push_back({ 1.0f, 0.0f, 0.0f, 0.0f });
norm.push_back({ 1.0f, 0.0f, 0.0f, 0.0f });
faces.push_back({ v_start + 2, v_start + 1, v_start });
faces.push_back({ v_start + 1, v_start + 2, v_start + 3 });
}
// x negative
{
int v_start = (int)pos.size();
pos.push_back({ -half_w, half_h, -half_d, 1.0f });
pos.push_back({ -half_w, half_h, half_d, 1.0f });
pos.push_back({ -half_w, -half_h, -half_d, 1.0f });
pos.push_back({ -half_w, -half_h, half_d, 1.0f });
norm.push_back({ -1.0f, 0.0f, 0.0f, 0.0f });
norm.push_back({ -1.0f, 0.0f, 0.0f, 0.0f });
norm.push_back({ -1.0f, 0.0f, 0.0f, 0.0f });
norm.push_back({ -1.0f, 0.0f, 0.0f, 0.0f });
faces.push_back({ v_start + 2, v_start + 1, v_start });
faces.push_back({ v_start + 1, v_start + 2, v_start + 3 });
}
// y positive
{
int v_start = (int)pos.size();
pos.push_back({ -half_w, half_h, -half_d, 1.0f });
pos.push_back({ half_w, half_h, -half_d, 1.0f });
pos.push_back({ -half_w, half_h, half_d, 1.0f });
pos.push_back({ half_w, half_h, half_d, 1.0f });
norm.push_back({ 0.0f, 1.0f, 0.0f, 0.0f });
norm.push_back({ 0.0f, 1.0f, 0.0f, 0.0f });
norm.push_back({ 0.0f, 1.0f, 0.0f, 0.0f });
norm.push_back({ 0.0f, 1.0f, 0.0f, 0.0f });
faces.push_back({ v_start + 2, v_start + 1, v_start });
faces.push_back({ v_start + 1, v_start + 2, v_start + 3 });
}
// y negative
{
int v_start = (int)pos.size();
pos.push_back({ -half_w, -half_h, half_d, 1.0f });
pos.push_back({ half_w, -half_h, half_d, 1.0f });
pos.push_back({ -half_w, -half_h, -half_d, 1.0f });
pos.push_back({ half_w, -half_h, -half_d, 1.0f });
norm.push_back({ 0.0f, -1.0f, 0.0f, 0.0f });
norm.push_back({ 0.0f, -1.0f, 0.0f, 0.0f });
norm.push_back({ 0.0f, -1.0f, 0.0f, 0.0f });
norm.push_back({ 0.0f, -1.0f, 0.0f, 0.0f });
faces.push_back({ v_start + 2, v_start + 1, v_start });
faces.push_back({ v_start + 1, v_start + 2, v_start + 3 });
}
// z positive
{
int v_start = (int)pos.size();
pos.push_back({ -half_w, half_h, half_d, 1.0f });
pos.push_back({ half_w, half_h, half_d, 1.0f });
pos.push_back({ -half_w, -half_h, half_d, 1.0f });
pos.push_back({ half_w, -half_h, half_d, 1.0f });
norm.push_back({ 0.0f, 0.0f, 1.0f, 0.0f });
norm.push_back({ 0.0f, 0.0f, 1.0f, 0.0f });
norm.push_back({ 0.0f, 0.0f, 1.0f, 0.0f });
norm.push_back({ 0.0f, 0.0f, 1.0f, 0.0f });
faces.push_back({ v_start + 2, v_start + 1, v_start });
faces.push_back({ v_start + 1, v_start + 2, v_start + 3 });
}
// z negative
{
int v_start = (int)pos.size();
pos.push_back({ half_w, half_h, -half_d, 1.0f });
pos.push_back({ -half_w, half_h, -half_d, 1.0f });
pos.push_back({ half_w, -half_h, -half_d, 1.0f });
pos.push_back({ -half_w, -half_h, -half_d, 1.0f });
norm.push_back({ 0.0f, 0.0f, -1.0f, 0.0f });
norm.push_back({ 0.0f, 0.0f, -1.0f, 0.0f });
norm.push_back({ 0.0f, 0.0f, -1.0f, 0.0f });
norm.push_back({ 0.0f, 0.0f, -1.0f, 0.0f });
faces.push_back({ v_start + 2, v_start + 1, v_start });
faces.push_back({ v_start + 1, v_start + 2, v_start + 3 });
}
num_pos = (int)pos.size();
num_face = (int)faces.size();
}
void LoadObj(const char* filename)
{
pos.clear();
norm.clear();
faces.clear();
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::string err;
tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename);
std::unordered_map<uint64_t, size_t> ind_map;
int num_meshes = (int)shapes.size();
for (int i = 0; i < num_meshes; i++)
{
tinyobj::shape_t& shape = shapes[i];
int face_start = (int)faces.size();
faces.resize(face_start + shape.mesh.indices.size() / 3);
for (int j = 0; j < (int)shape.mesh.indices.size(); j++)
{
tinyobj::index_t& index = shape.mesh.indices[j];
struct Attributes
{
glm::vec3 pos = { 0.0f, 0.0f, 0.0f };
glm::vec3 norm = { 0.0f, 0.0f, 0.0f };
};
Attributes att;
float* vp = &attrib.vertices[3 * index.vertex_index];
att.pos = glm::vec3(vp[0], vp[1], vp[2]);
float* np = &attrib.normals[3 * index.normal_index];
att.norm = glm::vec3(np[0], np[1], np[2]);
int idx;
uint64_t hash = crc64(0, (unsigned char*)&att, sizeof(Attributes));
auto iter = ind_map.find(hash);
if (iter == ind_map.end())
{
idx = (int)pos.size();
pos.push_back(glm::vec4(att.pos, 1.0f));
if (attrib.normals.size() > 0)
{
norm.push_back(glm::vec4(att.norm, 0.0f));
}
ind_map[hash] = idx;
}
else
{
idx = ind_map[hash];
}
faces[face_start + j / 3][j % 3] = idx;
}
}
num_pos = (int)pos.size();
num_face = (int)faces.size();
}
};
class BLAS
{
public:
struct Intersection {
int triangle_index = -1;
float t = -1.0f;
float u = 0.0f;
float v = 0.0f;
float distance() const { return t; }
};
using ScalarType = float;
using IntersectionType = Intersection;
BLAS(BLAS&&) = default;
BLAS(const Primitive* primitive);
~BLAS();
bvh::Vector3<float> center() const;
bvh::BoundingBox<float> bounding_box() const;
std::optional<Intersection> intersect(const bvh::Ray<float>& ray, int culling) const;
BLAS& operator =(BLAS&&) = default;
private:
typedef bvh::Triangle<float, true, true> PrimitiveType;
typedef bvh::ClosestPrimitiveIntersector<bvh::Bvh<float>, PrimitiveType> IntersectorType;
typedef bvh::SingleRayTraverser<bvh::Bvh<float>> TraversorType;
std::vector<PrimitiveType> m_triangles;
bvh::BoundingBox<float> m_bounding_box;
std::unique_ptr<bvh::Bvh<float>> m_bvh;
std::unique_ptr<IntersectorType> m_intersector[3];
std::unique_ptr<TraversorType> m_traverser;
};
BLAS::BLAS(const Primitive* primitive)
{
const glm::mat4& model_matrix = primitive->model_matrix;
for (int i = 0; i < primitive->num_face; i++)
{
glm::ivec3 face = primitive->faces[i];
glm::vec4 v0, v1, v2;
v0 = primitive->pos[face.x];
v1 = primitive->pos[face.y];
v2 = primitive->pos[face.z];
v0 = model_matrix * v0;
v1 = model_matrix * v1;
v2 = model_matrix * v2;
m_triangles.emplace_back(PrimitiveType(
bvh::Vector3<float>(v0.x, v0.y, v0.z),
bvh::Vector3<float>(v1.x, v1.y, v1.z),
bvh::Vector3<float>(v2.x, v2.y, v2.z)
));
}
auto [bboxes, centers] = bvh::compute_bounding_boxes_and_centers(m_triangles.data(), m_triangles.size());
m_bounding_box = bvh::compute_bounding_boxes_union(bboxes.get(), m_triangles.size());
m_bvh = std::unique_ptr<bvh::Bvh<float>>(new bvh::Bvh<float>);
bvh::SweepSahBuilder<bvh::Bvh<float>> builder(*m_bvh);
builder.build(m_bounding_box, bboxes.get(), centers.get(), m_triangles.size());
m_intersector[0] = std::unique_ptr<IntersectorType>(new IntersectorType(*m_bvh, m_triangles.data()));
m_intersector[0]->culling = 0;
m_intersector[1] = std::unique_ptr<IntersectorType>(new IntersectorType(*m_bvh, m_triangles.data()));
m_intersector[1]->culling = 1;
m_intersector[2] = std::unique_ptr<IntersectorType>(new IntersectorType(*m_bvh, m_triangles.data()));
m_intersector[2]->culling = 2;
m_traverser = std::unique_ptr<TraversorType>(new TraversorType(*m_bvh));
}
BLAS::~BLAS()
{
}
bvh::Vector3<float> BLAS::center() const
{
return m_bounding_box.center();
}
bvh::BoundingBox<float> BLAS::bounding_box() const
{
return m_bounding_box;
}
std::optional<BLAS::Intersection> BLAS::intersect(const bvh::Ray<float>& ray, int culling) const
{
auto hit = m_traverser->traverse(ray, *m_intersector[culling]);
if (hit.has_value())
{
auto intersection = hit->intersection;
Intersection ret;
ret.triangle_index = hit->primitive_index;
ret.t = intersection.t;
ret.u = intersection.u;
ret.v = intersection.v;
return std::make_optional<Intersection>(ret);
}
return std::nullopt;
}
int main()
{
int width = 1280;
int height = 720;
float aspect = (float)width / (float)height;
glm::mat4 projection = glm::perspective(45.0f * (float)DEG2RAD, aspect, 0.1f, 100.0f);
glm::mat4 view = glm::lookAt(glm::vec3(1.0f, 2.0f, 5.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0, 1.0, 0.0));
Primitive prim;
prim.LoadObj("shaderBall.obj");
prim.model_matrix = glm::translate(prim.model_matrix, glm::vec3(0.0f, -1.0f, 0.0f));
prim.model_matrix = glm::rotate(prim.model_matrix, -(float)PI * 0.5f, glm::vec3(0.0f, 1.0f, 0.0f));
prim.model_matrix = glm::rotate(prim.model_matrix, -(float)PI * 0.5f, glm::vec3(1.0f, 0.0f, 0.0f));
glm::mat4 normalMat = glm::transpose(glm::inverse(prim.model_matrix));
BLAS blas(&prim);
glm::mat4 invProjection = glm::inverse(projection);
glm::mat4 invView = glm::inverse(view);
glm::vec3 eyePos = glm::vec3(invView[3]);
glm::ivec2 viewport_size(width, height);
std::vector<uint8_t> depth(width * height);
std::vector<glm::u8vec3> normal(width * height);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
glm::ivec2 screen(x, height - 1 - y);
glm::vec4 clip((glm::vec2(screen) + 0.5f) / glm::vec2(viewport_size) * 2.0f - 1.0f, 0.0f, 1.0f);
glm::vec4 view = invProjection * clip;
view /= view.w;
glm::vec3 world = glm::vec3(invView * view);
glm::vec3 dir = glm::normalize(world - eyePos);
bvh::Ray<float> bvh_ray = bvh::Ray<float>(
bvh::Vector3<float>(eyePos.x, eyePos.y, eyePos.z),
bvh::Vector3<float>(dir.x, dir.y, dir.z)
);
float d = 1.0f;
glm::vec3 norm = glm::vec3(0.0f, 0.0f, 1.0f);
auto intersection = blas.intersect(bvh_ray, 0);
if (intersection.has_value())
{
d = (intersection->t - 2.0f) / 5.0f;
if (d < 0.0f) d = 0.0f;
if (d > 1.0f) d = 1.0f;
int face_id = intersection->triangle_index;
float u = intersection->u;
float v = intersection->v;
glm::ivec3 face = prim.faces[face_id];
glm::vec4 norm0 = prim.norm[face.x];
glm::vec4 norm1 = prim.norm[face.y];
glm::vec4 norm2 = prim.norm[face.z];
glm::vec4 normal = (1.0f - u - v) * norm0 + u * norm1 + v * norm2;
norm = glm::vec3(normalMat * normal);
}
depth[x + y * width] = (uint8_t)(d * 255.0f + 0.5f);
normal[x + y * width] = glm::u8vec3((norm * 0.5f + 0.5f) * 255.0f + 0.5f);
}
}
{
FILE* fp = fopen("dmp_depth.raw", "wb");
fwrite(depth.data(), 1, width * height, fp);
fclose(fp);
}
{
FILE* fp = fopen("dmp_normal.raw", "wb");
fwrite(normal.data(), 3, width * height, fp);
fclose(fp);
}
return 0;
}