-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathmain.cc
709 lines (588 loc) · 21.8 KB
/
main.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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
#include "tiny_obj_loader.h"
#define TINYEXR_IMPLEMENTATION
#include "tinyexr.h"
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wshadow"
#pragma clang diagnostic ignored "-Wdisabled-macro-expansion"
#pragma clang diagnostic ignored "-Wreserved-id-macro"
#pragma clang diagnostic ignored "-Wextra-semi"
#pragma clang diagnostic ignored "-Wold-style-cast"
#pragma clang diagnostic ignored "-Wexit-time-destructors"
#pragma clang diagnostic ignored "-Wdocumentation"
#pragma clang diagnostic ignored "-Wundefined-reinterpret-cast"
#pragma clang diagnostic ignored "-Wunused-parameter"
#pragma clang diagnostic ignored "-Wsign-conversion"
#pragma clang diagnostic ignored "-Wdouble-promotion"
#pragma clang diagnostic ignored "-Wimplicit-fallthrough"
#pragma clang diagnostic ignored "-Wcovered-switch-default"
#pragma clang diagnostic ignored "-Wfloat-conversion"
#pragma clang diagnostic ignored "-Wfloat-equal"
#pragma clang diagnostic ignored "-Wunused-variable"
#pragma clang diagnostic ignored "-Wconversion"
#pragma clang diagnostic ignored "-Wcast-align"
#pragma clang diagnostic ignored "-Wmissing-prototypes"
#pragma clang diagnostic ignored "-Wswitch-enum"
#pragma clang diagnostic ignored "-Wunused-macros"
#pragma clang diagnostic ignored "-Wweak-vtables"
#pragma clang diagnostic ignored "-Wmissing-variable-declarations"
#if __has_warning("-Wcomma")
#pragma clang diagnostic ignored "-Wcomma"
#endif
#if __has_warning("-Wzero-as-null-pointer-constant")
#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
#endif
#if __has_warning("-Wcast-qual")
#pragma clang diagnostic ignored "-Wcast-qual"
#endif
#endif
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#include "nanort.h"
#include <iostream>
namespace {
// This class is NOT thread-safe timer!
#ifdef _WIN32
#ifdef __cplusplus
extern "C" {
#endif
#include <mmsystem.h>
#include <windows.h>
#ifdef __cplusplus
}
#endif
#pragma comment(lib, "winmm.lib")
#else
#if defined(__unix__) || defined(__APPLE__)
#include <sys/time.h>
#else
#include <ctime>
#endif
#endif
#ifdef __clang__
#pragma clang diagnostic ignored "-Wold-style-cast"
#pragma clang diagnostic ignored "-Wsign-conversion"
#pragma clang diagnostic ignored "-Wdouble-promotion"
#pragma clang diagnostic ignored "-Wunused-member-function"
#if __has_warning("-Wzero-as-null-pointer-constant")
#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
#endif
#endif
class timerutil {
public:
#ifdef _WIN32
typedef DWORD time_t;
timerutil() { ::timeBeginPeriod(1); }
~timerutil() { ::timeEndPeriod(1); }
void start() { t_[0] = ::timeGetTime(); }
void end() { t_[1] = ::timeGetTime(); }
time_t sec() { return (time_t)((t_[1] - t_[0]) / 1000); }
time_t msec() { return (time_t)((t_[1] - t_[0])); }
time_t usec() { return (time_t)((t_[1] - t_[0]) * 1000); }
time_t current() { return ::timeGetTime(); }
#else
#if defined(__unix__) || defined(__APPLE__)
typedef unsigned long int time_t;
void start() { gettimeofday(tv + 0, &tz); }
void end() { gettimeofday(tv + 1, &tz); }
time_t sec() { return (time_t)(tv[1].tv_sec - tv[0].tv_sec); }
time_t msec() {
return this->sec() * 1000 +
(time_t)((tv[1].tv_usec - tv[0].tv_usec) / 1000);
}
time_t usec() {
return this->sec() * 1000000 + (time_t)(tv[1].tv_usec - tv[0].tv_usec);
}
time_t current() {
struct timeval t;
gettimeofday(&t, NULL);
return (time_t)(t.tv_sec * 1000 + t.tv_usec);
}
#else // C timer
// using namespace std;
typedef clock_t time_t;
void start() { t_[0] = clock(); }
void end() { t_[1] = clock(); }
time_t sec() { return (time_t)((t_[1] - t_[0]) / CLOCKS_PER_SEC); }
time_t msec() { return (time_t)((t_[1] - t_[0]) * 1000 / CLOCKS_PER_SEC); }
time_t usec() { return (time_t)((t_[1] - t_[0]) * 1000000 / CLOCKS_PER_SEC); }
time_t current() { return (time_t)clock(); }
#endif
#endif
private:
#ifdef _WIN32
DWORD t_[2];
#else
#if defined(__unix__) || defined(__APPLE__)
struct timeval tv[2];
struct timezone tz;
#else
time_t t_[2];
#endif
#endif
};
struct float3 {
float3() {}
float3(float xx, float yy, float zz) {
x = xx;
y = yy;
z = zz;
}
float3(const float *p) {
x = p[0];
y = p[1];
z = p[2];
}
float3 operator*(float f) const { return float3(x * f, y * f, z * f); }
float3 operator-(const float3 &f2) const {
return float3(x - f2.x, y - f2.y, z - f2.z);
}
float3 operator*(const float3 &f2) const {
return float3(x * f2.x, y * f2.y, z * f2.z);
}
float3 operator+(const float3 &f2) const {
return float3(x + f2.x, y + f2.y, z + f2.z);
}
float3 &operator+=(const float3 &f2) {
x += f2.x;
y += f2.y;
z += f2.z;
return (*this);
}
float3 operator/(const float3 &f2) const {
return float3(x / f2.x, y / f2.y, z / f2.z);
}
float operator[](int i) const { return (&x)[i]; }
float &operator[](int i) { return (&x)[i]; }
float3 neg() { return float3(-x, -y, -z); }
float length() { return sqrtf(x * x + y * y + z * z); }
void normalize() {
float len = length();
if (std::fabs(len) > 1.0e-6f) {
float inv_len = 1.0f / len;
x *= inv_len;
y *= inv_len;
z *= inv_len;
}
}
float x, y, z;
// float pad; // for alignment
};
// inline float3 operator*(float f, const float3 &v) {
// return float3(v.x * f, v.y * f, v.z * f);
//}
inline float3 vcross(float3 a, float3 b) {
float3 c;
c[0] = a[1] * b[2] - a[2] * b[1];
c[1] = a[2] * b[0] - a[0] * b[2];
c[2] = a[0] * b[1] - a[1] * b[0];
return c;
}
// inline float vdot(float3 a, float3 b) {
// return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
//}
typedef struct {
size_t num_vertices;
size_t num_faces;
float *vertices; /// [xyz] * num_vertices
float *facevarying_normals; /// [xyz] * 3(triangle) * num_faces
float *facevarying_tangents; /// [xyz] * 3(triangle) * num_faces
float *facevarying_binormals; /// [xyz] * 3(triangle) * num_faces
float *facevarying_uvs; /// [xyz] * 3(triangle) * num_faces
float *facevarying_vertex_colors; /// [xyz] * 3(triangle) * num_faces
unsigned int *faces; /// triangle x num_faces
unsigned int *material_ids; /// index x num_faces
} Mesh;
struct Material {
float ambient[3];
float diffuse[3];
float reflection[3];
float refraction[3];
int id;
int diffuse_texid;
int reflection_texid;
int transparency_texid;
int bump_texid;
int normal_texid; // normal map
int alpha_texid; // alpha map
Material() {
ambient[0] = 0.0;
ambient[1] = 0.0;
ambient[2] = 0.0;
diffuse[0] = 0.5;
diffuse[1] = 0.5;
diffuse[2] = 0.5;
reflection[0] = 0.0;
reflection[1] = 0.0;
reflection[2] = 0.0;
refraction[0] = 0.0;
refraction[1] = 0.0;
refraction[2] = 0.0;
id = -1;
diffuse_texid = -1;
reflection_texid = -1;
transparency_texid = -1;
bump_texid = -1;
normal_texid = -1;
alpha_texid = -1;
}
};
void calcNormal(float3 &N, float3 v0, float3 v1, float3 v2) {
float3 v10 = v1 - v0;
float3 v20 = v2 - v0;
N = vcross(v20, v10);
N.normalize();
}
#if 0
// Save in RAW headerless format, for use when exr tools are not available in
// system
void SaveImageRaw(const char *filename, const float *rgb, int width,
int height) {
std::vector<unsigned char> rawbuf;
rawbuf.resize(size_t(3 * width * height));
unsigned char *raw = &rawbuf.at(0);
// @note { Apply gamma correction would be nice? }
for (int i = 0; i < width * height; i++) {
raw[i * 3] = (char)(rgb[3 * i + 0] * 255.0f);
raw[i * 3 + 1] = (char)(rgb[3 * i + 1] * 255.0f);
raw[i * 3 + 2] = (char)(rgb[3 * i + 2] * 255.0f);
}
FILE *f = fopen(filename, "wb");
if (!f) {
printf("Error: Couldnt open output binary file %s\n", filename);
return;
}
fwrite(raw, 3 * width * height, 1, f);
fclose(f);
printf("Info: Saved RAW RGB image of [%dx%d] dimensions to [ %s ]\n", width,
height, filename);
}
#endif
void SaveImageEXR(const char *filename, const float *rgb, int width,
int height) {
float *image_ptr[3];
std::vector<float> images[3];
images[0].resize(width * height);
images[1].resize(width * height);
images[2].resize(width * height);
for (int i = 0; i < width * height; i++) {
images[0][i] = rgb[3 * i + 0];
images[1][i] = rgb[3 * i + 1];
images[2][i] = rgb[3 * i + 2];
}
image_ptr[0] = &(images[2].at(0)); // B
image_ptr[1] = &(images[1].at(0)); // G
image_ptr[2] = &(images[0].at(0)); // R
EXRHeader header;
InitEXRHeader(&header);
EXRImage image;
InitEXRImage(&image);
header.num_channels = 3;
header.channels =
(EXRChannelInfo *)malloc(sizeof(EXRChannelInfo) * header.num_channels);
// Must be (A)BGR order, since most of EXR viewers expect this channel order.
strncpy(header.channels[0].name, "B", 255);
header.channels[0].name[strlen("B")] = '\0';
strncpy(header.channels[1].name, "G", 255);
header.channels[1].name[strlen("G")] = '\0';
strncpy(header.channels[2].name, "R", 255);
header.channels[2].name[strlen("R")] = '\0';
header.pixel_types = (int *)malloc(sizeof(int) * header.num_channels);
header.requested_pixel_types =
(int *)malloc(sizeof(int) * header.num_channels);
for (int i = 0; i < header.num_channels; i++) {
header.pixel_types[i] =
TINYEXR_PIXELTYPE_FLOAT; // pixel type of input image
header.requested_pixel_types[i] =
TINYEXR_PIXELTYPE_HALF; // pixel type of output image to be stored in
// .EXR
}
image.num_channels = header.num_channels;
image.images = (unsigned char **)image_ptr;
image.width = width;
image.height = height;
const char *err;
int fail = SaveEXRImageToFile(&image, &header, filename, &err);
if (fail) {
fprintf(stderr, "Error: %s\n", err);
} else {
printf("Saved image to [ %s ]\n", filename);
}
free(header.requested_pixel_types);
free(header.channels);
free(header.pixel_types);
}
void SaveImagePNG(const char *filename, const float *rgb, int width,
int height) {
unsigned char *bytes = new unsigned char[width * height * 3];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
const int index = y * width + x;
bytes[index * 3 + 0] = (unsigned char)std::max(
0.0f, std::min(rgb[index * 3 + 0] * 255.0f, 255.0f));
bytes[index * 3 + 1] = (unsigned char)std::max(
0.0f, std::min(rgb[index * 3 + 1] * 255.0f, 255.0f));
bytes[index * 3 + 2] = (unsigned char)std::max(
0.0f, std::min(rgb[index * 3 + 2] * 255.0f, 255.0f));
}
}
int n = stbi_write_png(filename, width, height, 3, bytes, width * 3);
delete[] bytes;
if (n == 0) {
fprintf(stderr, "Error to save PNG image:\n");
} else {
printf("Saved image to [ %s ]\n", filename);
}
}
bool LoadObj(Mesh &mesh, const char *filename, float scale) {
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::string err = tinyobj::LoadObj(shapes, materials, filename);
if (!err.empty()) {
std::cerr << err << std::endl;
return false;
}
std::cout << "[LoadOBJ] # of shapes in .obj : " << shapes.size() << std::endl;
std::cout << "[LoadOBJ] # of materials in .obj : " << materials.size()
<< std::endl;
size_t num_vertices = 0;
size_t num_faces = 0;
for (size_t i = 0; i < shapes.size(); i++) {
printf(" shape[%ld].name = %s\n", i, shapes[i].name.c_str());
printf(" shape[%ld].indices: %ld\n", i, shapes[i].mesh.indices.size());
assert((shapes[i].mesh.indices.size() % 3) == 0);
printf(" shape[%ld].vertices: %ld\n", i, shapes[i].mesh.positions.size());
assert((shapes[i].mesh.positions.size() % 3) == 0);
printf(" shape[%ld].normals: %ld\n", i, shapes[i].mesh.normals.size());
assert((shapes[i].mesh.normals.size() % 3) == 0);
num_vertices += shapes[i].mesh.positions.size() / 3;
num_faces += shapes[i].mesh.indices.size() / 3;
}
std::cout << "[LoadOBJ] # of faces: " << num_faces << std::endl;
std::cout << "[LoadOBJ] # of vertices: " << num_vertices << std::endl;
// @todo { material and texture. }
// Shape -> Mesh
mesh.num_faces = num_faces;
mesh.num_vertices = num_vertices;
mesh.vertices = new float[num_vertices * 3];
mesh.faces = new unsigned int[num_faces * 3];
mesh.material_ids = new unsigned int[num_faces];
memset(mesh.material_ids, 0, sizeof(int) * num_faces);
mesh.facevarying_normals = new float[num_faces * 3 * 3];
mesh.facevarying_uvs = new float[num_faces * 3 * 2];
memset(mesh.facevarying_uvs, 0, sizeof(float) * 2 * 3 * num_faces);
// @todo {}
mesh.facevarying_tangents = NULL;
mesh.facevarying_binormals = NULL;
size_t vertexIdxOffset = 0;
size_t faceIdxOffset = 0;
for (size_t i = 0; i < shapes.size(); i++) {
for (size_t f = 0; f < shapes[i].mesh.indices.size() / 3; f++) {
mesh.faces[3 * (faceIdxOffset + f) + 0] =
shapes[i].mesh.indices[3 * f + 0];
mesh.faces[3 * (faceIdxOffset + f) + 1] =
shapes[i].mesh.indices[3 * f + 1];
mesh.faces[3 * (faceIdxOffset + f) + 2] =
shapes[i].mesh.indices[3 * f + 2];
mesh.faces[3 * (faceIdxOffset + f) + 0] += vertexIdxOffset;
mesh.faces[3 * (faceIdxOffset + f) + 1] += vertexIdxOffset;
mesh.faces[3 * (faceIdxOffset + f) + 2] += vertexIdxOffset;
mesh.material_ids[faceIdxOffset + f] = shapes[i].mesh.material_ids[f];
}
for (size_t v = 0; v < shapes[i].mesh.positions.size() / 3; v++) {
mesh.vertices[3 * (vertexIdxOffset + v) + 0] =
scale * shapes[i].mesh.positions[3 * v + 0];
mesh.vertices[3 * (vertexIdxOffset + v) + 1] =
scale * shapes[i].mesh.positions[3 * v + 1];
mesh.vertices[3 * (vertexIdxOffset + v) + 2] =
scale * shapes[i].mesh.positions[3 * v + 2];
}
if (shapes[i].mesh.normals.size() > 0) {
for (size_t f = 0; f < shapes[i].mesh.indices.size() / 3; f++) {
int f0, f1, f2;
f0 = shapes[i].mesh.indices[3 * f + 0];
f1 = shapes[i].mesh.indices[3 * f + 1];
f2 = shapes[i].mesh.indices[3 * f + 2];
float3 n0, n1, n2;
n0[0] = shapes[i].mesh.normals[3 * f0 + 0];
n0[1] = shapes[i].mesh.normals[3 * f0 + 1];
n0[2] = shapes[i].mesh.normals[3 * f0 + 2];
n1[0] = shapes[i].mesh.normals[3 * f1 + 0];
n1[1] = shapes[i].mesh.normals[3 * f1 + 1];
n1[2] = shapes[i].mesh.normals[3 * f1 + 2];
n2[0] = shapes[i].mesh.normals[3 * f2 + 0];
n2[1] = shapes[i].mesh.normals[3 * f2 + 1];
n2[2] = shapes[i].mesh.normals[3 * f2 + 2];
mesh.facevarying_normals[3 * (3 * (faceIdxOffset + f) + 0) + 0] = n0[0];
mesh.facevarying_normals[3 * (3 * (faceIdxOffset + f) + 0) + 1] = n0[1];
mesh.facevarying_normals[3 * (3 * (faceIdxOffset + f) + 0) + 2] = n0[2];
mesh.facevarying_normals[3 * (3 * (faceIdxOffset + f) + 1) + 0] = n1[0];
mesh.facevarying_normals[3 * (3 * (faceIdxOffset + f) + 1) + 1] = n1[1];
mesh.facevarying_normals[3 * (3 * (faceIdxOffset + f) + 1) + 2] = n1[2];
mesh.facevarying_normals[3 * (3 * (faceIdxOffset + f) + 2) + 0] = n2[0];
mesh.facevarying_normals[3 * (3 * (faceIdxOffset + f) + 2) + 1] = n2[1];
mesh.facevarying_normals[3 * (3 * (faceIdxOffset + f) + 2) + 2] = n2[2];
}
} else {
// calc geometric normal
for (size_t f = 0; f < shapes[i].mesh.indices.size() / 3; f++) {
int f0, f1, f2;
f0 = shapes[i].mesh.indices[3 * f + 0];
f1 = shapes[i].mesh.indices[3 * f + 1];
f2 = shapes[i].mesh.indices[3 * f + 2];
float3 v0, v1, v2;
v0[0] = shapes[i].mesh.positions[3 * f0 + 0];
v0[1] = shapes[i].mesh.positions[3 * f0 + 1];
v0[2] = shapes[i].mesh.positions[3 * f0 + 2];
v1[0] = shapes[i].mesh.positions[3 * f1 + 0];
v1[1] = shapes[i].mesh.positions[3 * f1 + 1];
v1[2] = shapes[i].mesh.positions[3 * f1 + 2];
v2[0] = shapes[i].mesh.positions[3 * f2 + 0];
v2[1] = shapes[i].mesh.positions[3 * f2 + 1];
v2[2] = shapes[i].mesh.positions[3 * f2 + 2];
float3 N;
calcNormal(N, v0, v1, v2);
mesh.facevarying_normals[3 * (3 * (faceIdxOffset + f) + 0) + 0] = N[0];
mesh.facevarying_normals[3 * (3 * (faceIdxOffset + f) + 0) + 1] = N[1];
mesh.facevarying_normals[3 * (3 * (faceIdxOffset + f) + 0) + 2] = N[2];
mesh.facevarying_normals[3 * (3 * (faceIdxOffset + f) + 1) + 0] = N[0];
mesh.facevarying_normals[3 * (3 * (faceIdxOffset + f) + 1) + 1] = N[1];
mesh.facevarying_normals[3 * (3 * (faceIdxOffset + f) + 1) + 2] = N[2];
mesh.facevarying_normals[3 * (3 * (faceIdxOffset + f) + 2) + 0] = N[0];
mesh.facevarying_normals[3 * (3 * (faceIdxOffset + f) + 2) + 1] = N[1];
mesh.facevarying_normals[3 * (3 * (faceIdxOffset + f) + 2) + 2] = N[2];
}
}
if (shapes[i].mesh.texcoords.size() > 0) {
for (size_t f = 0; f < shapes[i].mesh.indices.size() / 3; f++) {
int f0, f1, f2;
f0 = shapes[i].mesh.indices[3 * f + 0];
f1 = shapes[i].mesh.indices[3 * f + 1];
f2 = shapes[i].mesh.indices[3 * f + 2];
float3 n0, n1, n2;
n0[0] = shapes[i].mesh.texcoords[2 * f0 + 0];
n0[1] = shapes[i].mesh.texcoords[2 * f0 + 1];
n1[0] = shapes[i].mesh.texcoords[2 * f1 + 0];
n1[1] = shapes[i].mesh.texcoords[2 * f1 + 1];
n2[0] = shapes[i].mesh.texcoords[2 * f2 + 0];
n2[1] = shapes[i].mesh.texcoords[2 * f2 + 1];
mesh.facevarying_uvs[2 * (3 * (faceIdxOffset + f) + 0) + 0] = n0[0];
mesh.facevarying_uvs[2 * (3 * (faceIdxOffset + f) + 0) + 1] = n0[1];
mesh.facevarying_uvs[2 * (3 * (faceIdxOffset + f) + 1) + 0] = n1[0];
mesh.facevarying_uvs[2 * (3 * (faceIdxOffset + f) + 1) + 1] = n1[1];
mesh.facevarying_uvs[2 * (3 * (faceIdxOffset + f) + 2) + 0] = n2[0];
mesh.facevarying_uvs[2 * (3 * (faceIdxOffset + f) + 2) + 1] = n2[1];
}
}
vertexIdxOffset += shapes[i].mesh.positions.size() / 3;
faceIdxOffset += shapes[i].mesh.indices.size() / 3;
}
return true;
}
} // namespace
int main(int argc, char **argv) {
int width = 512;
int height = 512;
float scale = 1.0f;
std::string objFilename = "cornellbox_suzanne.obj";
if (argc > 1) {
objFilename = std::string(argv[1]);
}
if (argc > 2) {
scale = float(atof(argv[2]));
}
bool ret = false;
Mesh mesh;
ret = LoadObj(mesh, objFilename.c_str(), scale);
if (!ret) {
fprintf(stderr, "Failed to load [ %s ]\n", objFilename.c_str());
return -1;
}
nanort::BVHBuildOptions<float> build_options; // Use default option
build_options.cache_bbox = false;
printf(" BVH build option:\n");
printf(" # of leaf primitives: %d\n", build_options.min_leaf_primitives);
printf(" SAH binsize : %d\n", build_options.bin_size);
timerutil t;
t.start();
nanort::TriangleMesh<float> triangle_mesh(mesh.vertices, mesh.faces,
sizeof(float) * 3);
nanort::TriangleSAHPred<float> triangle_pred(mesh.vertices, mesh.faces,
sizeof(float) * 3);
std::cout << "num_triangles = " << mesh.num_faces << std::endl;
nanort::BVHAccel<float> accel;
ret =
accel.Build(static_cast<unsigned int>(mesh.num_faces), triangle_mesh, triangle_pred, build_options);
assert(ret);
t.end();
printf(" BVH build time: %f secs\n", t.msec() / 1000.0);
nanort::BVHBuildStatistics stats = accel.GetStatistics();
printf(" BVH statistics:\n");
printf(" # of leaf nodes: %d\n", stats.num_leaf_nodes);
printf(" # of branch nodes: %d\n", stats.num_branch_nodes);
printf(" Max tree depth : %d\n", stats.max_tree_depth);
float bmin[3], bmax[3];
accel.BoundingBox(bmin, bmax);
printf(" Bmin : %f, %f, %f\n", bmin[0], bmin[1], bmin[2]);
printf(" Bmax : %f, %f, %f\n", bmax[0], bmax[1], bmax[2]);
std::vector<float> rgb(width * height * 3, 0.0f);
t.start();
// Shoot rays.
#ifdef _OPENMP
#pragma omp parallel for
#endif
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// Simple camera. change eye pos and direction fit to .obj model.
nanort::Ray<float> ray;
ray.org[0] = 0.0f;
ray.org[1] = 5.0f;
ray.org[2] = 20.0f;
float3 dir;
dir[0] = (x / (float)width) - 0.5f;
dir[1] = (y / (float)height) - 0.5f;
dir[2] = -1.0f;
dir.normalize();
ray.dir[0] = dir[0];
ray.dir[1] = dir[1];
ray.dir[2] = dir[2];
float kFar = 1.0e+30f;
ray.min_t = 0.0f;
ray.max_t = kFar;
nanort::TriangleIntersector<> triangle_intersector(
mesh.vertices, mesh.faces, sizeof(float) * 3);
nanort::TriangleIntersection<> isect;
bool hit = accel.Traverse(ray, triangle_intersector, &isect);
if (hit) {
// Write your shader here.
float3 normal(0.0f, 0.0f, 0.0f);
unsigned int fid = isect.prim_id;
if (mesh.facevarying_normals) {
normal[0] = mesh.facevarying_normals[9 * fid + 0];
normal[1] = mesh.facevarying_normals[9 * fid + 1];
normal[2] = mesh.facevarying_normals[9 * fid + 2];
}
// Flip Y
rgb[3 * ((height - y - 1) * width + x) + 0] = fabsf(normal[0]);
rgb[3 * ((height - y - 1) * width + x) + 1] = fabsf(normal[1]);
rgb[3 * ((height - y - 1) * width + x) + 2] = fabsf(normal[2]);
}
}
}
t.end();
printf("Render %f secs\n", t.msec() / 1000.0);
delete [] mesh.vertices;
delete [] mesh.faces;
delete [] mesh.material_ids;
delete [] mesh.facevarying_normals;
delete [] mesh.facevarying_uvs;
// Save image.
SaveImageEXR("render.exr", &rgb.at(0), width, height);
// Save Raw Image that can be opened by tools like GIMP
// SaveImageRaw("render.data", &rgb.at(0), width, height);
SaveImagePNG("render.png", &rgb.at(0), width, height);
return 0;
}