-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathviewer.cpp
3227 lines (2735 loc) · 97.2 KB
/
viewer.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
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is distributed under the MIT license.
// See the LICENSE file for details.
#include <common/config.h>
#include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <exception>
#include <fstream>
#include <future>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <memory>
#include <mutex>
#include <new>
#include <ostream>
#include <set>
#include <string>
#include <thread>
#include <vector>
#include <boost/filesystem.hpp>
#if VSNRAY_COMMON_HAVE_CUDA
#include <cuda_runtime_api.h>
#include <thrust/device_vector.h>
#endif
#include <imgui.h>
#include <Support/CmdLine.h>
#include <Support/CmdLineUtil.h>
#include <visionaray/math/math.h>
#include <visionaray/texture/texture.h>
#include <visionaray/aligned_vector.h>
#include <visionaray/area_light.h>
#include <visionaray/bvh.h>
#include <visionaray/environment_light.h>
#include <visionaray/generic_material.h>
#include <visionaray/kernels.h>
#include <visionaray/material.h>
#include <visionaray/pinhole_camera.h>
#include <visionaray/point_light.h>
#include <visionaray/scheduler.h>
#include <visionaray/spot_light.h>
#include <visionaray/thin_lens_camera.h>
#if defined(__INTEL_COMPILER) || defined(__MINGW32__) || defined(__MINGW64__)
#include <visionaray/detail/tbb_sched.h>
#endif
#include <common/input/keyboard.h>
#include <common/manip/arcball_manipulator.h>
#include <common/manip/pan_manipulator.h>
#include <common/manip/zoom_manipulator.h>
#include <common/bvh_outline_renderer.h>
#include <common/gl_debug_callback.h>
#include <common/inifile.h>
#include <common/make_materials.h>
#include <common/make_texture.h>
#include <common/model.h>
#include <common/image.h>
#include <common/sg.h>
#include <common/timer.h>
#include <common/viewer_glut.h>
#if VSNRAY_COMMON_HAVE_PTEX
#include <common/ptex.h>
#endif
#include "call_kernel.h"
#include "host_device_rt.h"
#include "render.h"
using namespace visionaray;
using viewer_type = viewer_glut;
//-------------------------------------------------------------------------------------------------
// Helpers
//
enum class copy_kind
{
HostToHost,
HostToDevice,
DeviceToHost,
DeviceToDevice,
};
template <typename DestInstances, typename DestTopLevel, typename SourceInstances, typename SourceTopLevel>
static void copy_bvhs(
DestInstances& dest_instance_bvhs,
DestTopLevel& dest_top_level_bvh,
SourceInstances const& source_instance_bvhs,
SourceTopLevel const& source_top_level_bvh,
copy_kind ck
)
{
// Build up lower level bvhs first
dest_instance_bvhs.resize(source_instance_bvhs.size());
for (size_t i = 0; i < source_instance_bvhs.size(); ++i)
{
dest_instance_bvhs[i] = typename DestInstances::value_type(source_instance_bvhs[i]);
}
// Make a deep copy of the top level bvh
if (source_top_level_bvh.num_primitives() > 0)
{
dest_top_level_bvh.primitives().resize(source_top_level_bvh.num_primitives());
dest_top_level_bvh.nodes().resize(source_top_level_bvh.num_nodes());
dest_top_level_bvh.indices().resize(source_top_level_bvh.num_indices());
// Linear search for each inst: This may obviously become fairly inefficient..
for (size_t i = 0; i < source_top_level_bvh.num_primitives(); ++i)
{
size_t index = size_t(-1);
for (size_t j = 0; j < source_instance_bvhs.size(); ++j)
{
if (source_instance_bvhs[j].ref() == source_top_level_bvh.primitive(i).get_ref())
{
index = j;
break;
}
}
assert(index < size_t(-1));
int indirect_index = source_top_level_bvh.indices()[i];
dest_top_level_bvh.primitives()[indirect_index] = {
dest_instance_bvhs[index].ref(),
mat4x3(
inverse(source_top_level_bvh.primitive(i).affine_inv()),
-source_top_level_bvh.primitive(i).trans_inv()
)
};
}
// Copy nodes and indices
if (ck == copy_kind::HostToHost)
{
}
#if VSNRAY_COMMON_HAVE_CUDA
else if (ck == copy_kind::HostToDevice)
{
cudaMemcpy(
(void*)detail::get_pointer(dest_top_level_bvh.nodes()),
detail::get_pointer(source_top_level_bvh.nodes()),
sizeof(bvh_node) * source_top_level_bvh.num_nodes(),
cudaMemcpyHostToDevice
);
cudaMemcpy(
(void*)detail::get_pointer(dest_top_level_bvh.indices()),
detail::get_pointer(source_top_level_bvh.indices()),
sizeof(unsigned) * source_top_level_bvh.num_indices(),
cudaMemcpyHostToDevice
);
}
else if (ck == copy_kind::DeviceToHost)
{
cudaMemcpy(
(void*)detail::get_pointer(dest_top_level_bvh.nodes()),
detail::get_pointer(source_top_level_bvh.nodes()),
sizeof(bvh_node) * source_top_level_bvh.num_nodes(),
cudaMemcpyDeviceToHost
);
cudaMemcpy(
(void*)detail::get_pointer(dest_top_level_bvh.indices()),
detail::get_pointer(source_top_level_bvh.indices()),
sizeof(unsigned) * source_top_level_bvh.num_indices(),
cudaMemcpyDeviceToHost
);
}
else if (ck == copy_kind::DeviceToDevice)
{
cudaMemcpy(
(void*)detail::get_pointer(dest_top_level_bvh.nodes()),
detail::get_pointer(source_top_level_bvh.nodes()),
sizeof(bvh_node) * source_top_level_bvh.num_nodes(),
cudaMemcpyDeviceToDevice
);
cudaMemcpy(
(void*)detail::get_pointer(dest_top_level_bvh.indices()),
detail::get_pointer(source_top_level_bvh.indices()),
sizeof(unsigned) * source_top_level_bvh.num_indices(),
cudaMemcpyDeviceToDevice
);
}
#else
else
{
assert(0);
}
#endif
}
}
//-------------------------------------------------------------------------------------------------
// Renderer, stores state, geometry, normals, ...
//
struct renderer : viewer_type
{
using primitive_type = model::triangle_type;
using normal_type = model::normal_type;
using tex_coord_type = model::tex_coord_type;
using color_type = model::color_type;
using host_bvh_type = index_bvh<primitive_type>;
#if VSNRAY_COMMON_HAVE_CUDA
using device_bvh_type = cuda_index_bvh<primitive_type>;
using device_tex_type = cuda_texture<vector<4, unorm<8>>, 2>;
using device_tex_ref_type = typename device_tex_type::ref_type;
#endif
enum bvh_build_strategy
{
Binned = 0, // Binned SAH builder, no spatial splits
Split, // Split BVH, also binned and with SAH
LBVH, // LBVH builder on the CPU
};
enum texture_format { Ptex, UV };
renderer()
: viewer_type(800, 800, "Visionaray Viewer")
, host_sched(std::thread::hardware_concurrency())
, rt(
host_device_rt::CPU,
true /* double buffering */,
true /* direct rendering */,
host_device_rt::SRGB
)
#if VSNRAY_COMMON_HAVE_CUDA
, device_sched(8, 8)
#endif
, env_map(0, 0)
, mouse_pos(0)
{
using namespace support;
// Init null environment light
env_light.texture() = texture_ref<vec4, 2>(env_map);
// Parse inifile (but cmdline overrides!)
parse_inifile({ "vsnray-viewer.ini", "viewer.ini" });
// Add cmdline options
add_cmdline_option( cl::makeOption<std::set<std::string>&>(
cl::Parser<>(),
"filenames",
cl::Desc("Input files in wavefront obj format"),
cl::Positional,
cl::OneOrMore,
cl::init(filenames)
) );
add_cmdline_option( cl::makeOption<std::string&>(
cl::Parser<>(),
"camera",
cl::Desc("Text file with camera parameters"),
cl::ArgRequired,
cl::init(this->initial_camera)
) );
add_cmdline_option( cl::makeOption<std::string&>(
cl::Parser<>(),
"screenshotbasename",
cl::Desc("Base name (w/o suffix!) for screenshot files"),
cl::ArgRequired,
cl::init(this->screenshot_file_base)
) );
add_cmdline_option( cl::makeOption<std::string&>(
cl::Parser<>(),
"envmap",
cl::Desc("HDR environment map"),
cl::ArgRequired,
cl::init(this->env_map_filename)
) );
add_cmdline_option( cl::makeOption<algorithm&>({
{ "simple", Simple, "Simple ray casting kernel" },
{ "whitted", Whitted, "Whitted style ray tracing kernel" },
{ "pathtracing", Pathtracing, "Pathtracing global illumination kernel" },
{ "costs", Costs, "BVH cost kernel" }
},
"algorithm",
cl::Desc("Rendering algorithm"),
cl::ArgRequired,
cl::init(this->algo)
) );
add_cmdline_option( cl::makeOption<bvh_build_strategy&>({
{ "default", Binned, "Binned SAH" },
{ "split", Split, "Binned SAH with spatial splits" },
{ "lbvh", LBVH, "LBVH (CPU)" }
},
"bvh",
cl::Desc("BVH build strategy"),
cl::ArgRequired,
cl::init(this->build_strategy)
) );
// The following two options both manipulate spp
add_cmdline_option( cl::makeOption<unsigned&>({
{ "1", 1, "1x supersampling" },
{ "2", 2, "2x supersampling" },
{ "4", 4, "4x supersampling" },
{ "8", 8, "8x supersampling" }
},
"ssaa",
cl::Desc("Supersampling anti-aliasing factor"),
cl::ArgRequired,
cl::init(this->spp)
) );
add_cmdline_option( cl::makeOption<unsigned&>(
cl::Parser<>(),
"spp",
cl::Desc("Pixels per sample for path tracing"),
cl::ArgRequired,
cl::init(this->spp)
) );
add_cmdline_option( cl::makeOption<bool&>(
cl::Parser<>(),
"headlight",
cl::Desc("Activate headlight"),
cl::ArgRequired,
cl::init(this->use_headlight)
) );
add_cmdline_option( cl::makeOption<bool&>(
cl::Parser<>(),
"groundplane",
cl::Desc("Add a ground plane"),
cl::ArgRequired,
cl::init(this->use_groundplane)
) );
add_cmdline_option( cl::makeOption<bool&>(
cl::Parser<>(),
"dof",
cl::Desc("Activate depth of field"),
cl::ArgRequired,
cl::init(this->use_dof)
) );
add_cmdline_option( cl::makeOption<unsigned&>(
cl::Parser<>(),
"bounces",
cl::Desc("Number of bounces for recursive ray tracing"),
cl::ArgRequired,
cl::init(this->bounces)
) );
add_cmdline_option( cl::makeOption<unsigned&>(
cl::Parser<>(),
"frames",
cl::Desc("Number of path tracer convergence frames"),
cl::ArgRequired,
cl::init(this->frames)
) );
add_cmdline_option( cl::makeOption<vec3&, cl::ScalarType>(
[&](StringRef name, StringRef /*arg*/, vec3& value)
{
cl::Parser<>()(name + "-r", cmd_line_inst().bump(), value.x);
cl::Parser<>()(name + "-g", cmd_line_inst().bump(), value.y);
cl::Parser<>()(name + "-b", cmd_line_inst().bump(), value.z);
},
"ambient",
cl::Desc("Ambient color"),
cl::ArgDisallowed,
cl::init(this->ambient)
) );
add_cmdline_option( cl::makeOption<host_device_rt::color_space_type&>({
{ "rgb", host_device_rt::RGB, "RGB color space for display" },
{ "srgb", host_device_rt::SRGB, "sRGB color space for display" },
},
"colorspace",
cl::Desc("Color space"),
cl::ArgRequired,
cl::init(rt.color_space())
) );
#if VSNRAY_COMMON_HAVE_CUDA
add_cmdline_option( cl::makeOption<host_device_rt::mode_type&>({
{ "cpu", host_device_rt::CPU, "Rendering on the CPU" },
{ "gpu", host_device_rt::GPU, "Rendering on the GPU" },
},
"device",
cl::Desc("Rendering device"),
cl::ArgRequired,
cl::init(rt.mode())
) );
#endif
}
void parse_inifile(std::set<std::string> filenames)
{
// First parse base's options
viewer_base::parse_inifile(filenames);
// Process the first (if any) valid inifile
for (auto filename : filenames)
{
inifile ini(filename);
if (ini.good())
{
inifile::error_code err = inifile::Ok;
// algorithm
std::string algo = "";
err = ini.get_string("algorithm", algo);
if (err == inifile::Ok)
{
if (algo == "simple")
{
this->algo = Simple;
}
else if (algo == "whitted")
{
this->algo = Whitted;
}
else if (algo == "pathtracing")
{
this->algo = Pathtracing;
}
else if (algo == "costs")
{
this->algo = Costs;
}
}
// ambient
vec3 ambient = this->ambient;
err = ini.get_vec3f("ambient", ambient.x, ambient.y, ambient.z);
if (err == inifile::Ok)
{
this->ambient = ambient;
}
// Initial camera
std::string camera = initial_camera;
err = ini.get_string("camera", camera, true /*remove quotes*/);
if (err == inifile::Ok)
{
initial_camera = camera;
}
// Screenshot file base name
std::string screenshotbasename = screenshot_file_base;
err = ini.get_string("screenshotbasename", screenshotbasename, true /*remove quotes*/);
if (err == inifile::Ok)
{
screenshot_file_base = screenshotbasename;
}
// bounces
uint32_t bounces = this->bounces;
err = ini.get_uint32("bounces", bounces);
if (err == inifile::Ok)
{
this->bounces = bounces;
}
// bvh
std::string bvh = "";
err = ini.get_string("bvh", bvh);
if (err == inifile::Ok)
{
if (bvh == "default")
{
build_strategy = Binned;
}
else if (bvh == "split")
{
build_strategy = Split;
}
else if (bvh == "lbvh")
{
build_strategy = LBVH;
}
}
// color space
std::string colorspace = "";
err = ini.get_string("colorspace", colorspace);
if (err == inifile::Ok)
{
if (colorspace == "rgb")
{
rt.color_space() = host_device_rt::RGB;
}
else
{
rt.color_space() = host_device_rt::SRGB;
}
}
// depth of field
bool dof = use_dof;
err = ini.get_bool("dof", dof);
if (err == inifile::Ok)
{
use_dof = dof;
}
// lens radius
float lr = cam.get_lens_radius();
err = ini.get_float("lens_radius", lr);
if (err == inifile::Ok)
{
cam.set_lens_radius(lr);
}
// focal distance
float fd = cam.get_focal_distance();
err = ini.get_float("focal_distance", fd);
if (err == inifile::Ok)
{
cam.set_focal_distance(fd);
}
// asynchronous rendering
bool async = render_async;
err = ini.get_bool("render_async", async);
if (err == inifile::Ok)
{
render_async = async;
}
// ImGui menu
bool hud = show_hud;
err = ini.get_bool("hud", hud);
if (err == inifile::Ok)
{
show_hud = hud;
}
// headlight
bool headlight = use_headlight;
err = ini.get_bool("headlight", headlight);
if (err == inifile::Ok)
{
use_headlight = headlight;
}
// ground plane
bool groundplane = use_groundplane;
err = ini.get_bool("groundplane", groundplane);
if (err == inifile::Ok)
{
use_groundplane = groundplane;
}
// Environment map
std::string envmap = env_map_filename;
err = ini.get_string("envmap", envmap, true /*remove quotes*/);
if (err == inifile::Ok)
{
env_map_filename = envmap;
}
// Supersampling
uint32_t ssaa = spp;
err = ini.get_uint32("ssaa", ssaa);
if (err == inifile::Ok)
{
spp = ssaa;
}
// Jittered supersampling (also manipulates this->spp!)
uint32_t local_spp = spp;
err = ini.get_uint32("spp", local_spp);
if (err == inifile::Ok)
{
spp = local_spp;
}
#if VSNRAY_COMMON_HAVE_CUDA
// Device (CPU or GPU)
std::string device = "";
err = ini.get_string("device", device);
if (err == inifile::Ok)
{
if (device == "cpu")
{
rt.mode() = host_device_rt::CPU;
}
else if (device == "gpu")
{
rt.mode() = host_device_rt::GPU;
}
}
#endif
// Don't consider other files
break;
}
}
}
int w = 800;
int h = 800;
unsigned frame_num = 0;
unsigned bounces = 0;
unsigned spp = 1;
algorithm algo = Simple;
bvh_build_strategy build_strategy = Binned;
bool use_headlight = true;
bool use_groundplane = false;
bool use_dof = false;
bool show_hud = true;
bool show_bvh = false;
std::set<std::string> filenames;
std::string initial_camera;
std::string current_cam;
std::string screenshot_file_base = "screenshot";
model mod;
vec3 ambient = vec3(-1.0f);
index_bvh<host_bvh_type::bvh_inst> host_top_level_bvh;
aligned_vector<host_bvh_type> host_bvhs;
aligned_vector<host_bvh_type::bvh_inst> host_instances;
aligned_vector<plastic<float>> plastic_materials;
aligned_vector<generic_material_t> generic_materials;
aligned_vector<point_light<float>> point_lights;
aligned_vector<spot_light<float>> spot_lights;
aligned_vector<area_light<float,
basic_triangle<3, float>>> area_lights;
#if VSNRAY_COMMON_HAVE_PTEX
aligned_vector<ptex::face_id_t> ptex_tex_coords;
aligned_vector<ptex::texture> ptex_textures;
#endif
#if VSNRAY_COMMON_HAVE_CUDA
cuda_index_bvh<device_bvh_type::bvh_inst> device_top_level_bvh;
std::vector<device_bvh_type> device_bvhs;
thrust::device_vector<normal_type> device_geometric_normals;
thrust::device_vector<normal_type> device_shading_normals;
thrust::device_vector<tex_coord_type> device_tex_coords;
thrust::device_vector<plastic<float>> device_plastic_materials;
thrust::device_vector<generic_material_t> device_generic_materials;
thrust::device_vector<color_type> device_colors;
std::map<std::string, device_tex_type> device_texture_map;
thrust::device_vector<device_tex_ref_type> device_textures;
#endif
host_sched_t<ray_type_cpu> host_sched;
host_device_rt rt;
#if VSNRAY_COMMON_HAVE_CUDA
cuda_sched<ray_type_gpu> device_sched;
#endif
thin_lens_camera cam;
std::string env_map_filename;
visionaray::texture<vec4, 2> env_map;
host_environment_light env_light;
#if VSNRAY_COMMON_HAVE_CUDA
visionaray::cuda_texture<vec4, 2> device_env_map;
device_environment_light device_env_light;
#endif
// List of cameras, e.g. read from scene graph
aligned_vector<std::pair<
std::string, thin_lens_camera>> cameras;
mouse::pos mouse_pos;
texture_format tex_format = UV;
visionaray::frame_counter counter;
double last_frame_time = 0.0;
bvh_outline_renderer outlines;
gl::debug_callback gl_debug_callback;
// Control if new path tracer convergence frames are accumulated
bool paused = false;
// Number of path tracer convergece frames to be rendered (default: inf)
unsigned frames = unsigned(-1);
bool render_async = false;
std::future<void> render_future;
std::mutex display_mutex;
static const std::string camera_file_base;
static const std::string camera_file_suffix;
void build_scene();
protected:
void on_close();
void on_display();
void on_key_press(visionaray::key_event const& event);
void on_mouse_move(visionaray::mouse_event const& event);
void on_space_mouse_move(visionaray::space_mouse_event const& event);
void on_resize(int w, int h);
private:
void load_camera(std::string filename);
void init_bvh_outlines();
void clear_frame();
void screenshot();
void render_hud();
void render_impl();
};
const std::string renderer::camera_file_base = "visionaray-camera";
const std::string renderer::camera_file_suffix = ".txt";
//-------------------------------------------------------------------------------------------------
// Map obj material to generic material
//
inline generic_material_t map_material(sg::obj_material const& mat)
{
// Add emissive material if emissive component > 0
if (length(mat.ce) > 0.0f)
{
emissive<float> em;
em.ce() = from_rgb(mat.ce);
em.ls() = 1.0f;
return em;
}
else if (mat.illum == 1)
{
matte<float> ma;
ma.ca() = from_rgb(mat.ca);
ma.cd() = from_rgb(mat.cd);
ma.ka() = 1.0f;
ma.kd() = 1.0f;
return ma;
}
else if (mat.illum == 3)
{
mirror<float> mi;
mi.cr() = from_rgb(mat.cs);
mi.kr() = 1.0f;
mi.ior() = spectrum<float>(0.0f);
mi.absorption() = spectrum<float>(0.0f);
return mi;
}
else if (mat.illum == 4 && mat.transmission > 0.0f)
{
glass<float> gl;
gl.ct() = from_rgb(mat.cd);
gl.kt() = 1.0f;
gl.cr() = from_rgb(mat.cs);
gl.kr() = 1.0f;
gl.ior() = from_rgb(mat.ior);
return gl;
}
else
{
plastic<float> pl;
pl.ca() = from_rgb(mat.ca);
pl.cd() = from_rgb(mat.cd);
pl.cs() = from_rgb(mat.cs);
pl.ka() = 1.0f;
pl.kd() = 1.0f;
pl.ks() = 1.0f;
pl.specular_exp() = mat.specular_exp;
return pl;
}
}
//-------------------------------------------------------------------------------------------------
// Map disney material to generic material
//
inline generic_material_t map_material(sg::disney_material const& mat)
{
// TODO..
if (mat.refractive > 0.0f)
{
glass<float> gl;
gl.ct() = from_rgb(mat.base_color.xyz());
gl.kt() = 1.0f;
gl.cr() = from_rgb(vec3(mat.spec_trans));
gl.kr() = 1.0f;
gl.ior() = from_rgb(vec3(mat.ior));
return gl;
}
else
{
matte<float> ma;
ma.ca() = from_rgb(vec3(0.0f));
ma.cd() = from_rgb(vec3(mat.base_color.xyz()));
ma.ka() = 1.0f;
ma.kd() = 1.0f;
return ma;
}
}
//-------------------------------------------------------------------------------------------------
// Map metal material to generic material
//
inline generic_material_t map_material(sg::metal_material const& mat)
{
// TODO: consolidate glass and sg::glass_material (?)
metal<float> mt;
mt.roughness() = mat.roughness;
mt.absorption() = from_rgb(mat.absorption);
mt.ior() = from_rgb(mat.ior);
return mt;
}
//-------------------------------------------------------------------------------------------------
// Map glass material to generic material
//
inline generic_material_t map_material(sg::glass_material const& mat)
{
// TODO: consolidate glass and sg::glass_material (?)
glass<float> gl;
gl.ct() = from_rgb(mat.ct);
gl.kt() = 1.0f;
gl.cr() = from_rgb(mat.cr);
gl.kr() = 1.0f;
gl.ior() = from_rgb(mat.ior);
return gl;
}
//-------------------------------------------------------------------------------------------------
// I/O utility for camera lookat only - not fit for the general case!
//
inline std::istream& operator>>(std::istream& in, pinhole_camera& cam)
{
vec3 eye;
vec3 center;
vec3 up;
in >> eye >> std::ws >> center >> std::ws >> up >> std::ws;
cam.look_at(eye, center, up);
return in;
}
inline std::ostream& operator<<(std::ostream& out, pinhole_camera const& cam)
{
out << cam.eye() << '\n';
out << cam.center() << '\n';
out << cam.up() << '\n';
return out;
}
//-------------------------------------------------------------------------------------------------
// Approximate sphere with icosahedron
// cf. https://schneide.blog/2016/07/15/generating-an-icosphere-in-c/
//
struct icosahedron
{
aligned_vector<basic_triangle<3, float>> triangles;
aligned_vector<vec3> normals;
};
inline icosahedron make_icosahedron()
{
static constexpr float X = 0.525731112119133606f;
static constexpr float Z = 0.850650808352039932f;
static constexpr float N = 0.0f;
static const vec3 vertices[] = {
{ -X, N, Z },
{ X, N, Z },
{ -X, N, -Z },
{ X, N, -Z },
{ N, Z, X },
{ N, Z, -X },
{ N, -Z, X },
{ N, -Z, -X },
{ Z, X, N },
{ -Z, X, N },
{ Z, -X, N },
{ -Z, -X, N }
};
static const vec3i indices[] {
{ 0, 4, 1 },
{ 0, 9, 4 },
{ 9, 5, 4 },
{ 4, 5, 8 },
{ 4, 8, 1 },
{ 8, 10, 1 },
{ 8, 3, 10 },
{ 5, 3, 8 },
{ 5, 2, 3 },
{ 2, 7, 3 },
{ 7, 10, 3 },
{ 7, 6, 10 },
{ 7, 11, 6 },
{ 11, 0, 6 },
{ 0, 1, 6 },
{ 6, 1, 10 },
{ 9, 0, 11 },
{ 9, 11, 2 },
{ 9, 2, 5 },
{ 7, 2, 11 }
};
auto make_triangle = [&](int index)
{
vec3i idx = indices[index];
return basic_triangle<3, float>(
vertices[idx.x],
vertices[idx.y] - vertices[idx.x],
vertices[idx.z] - vertices[idx.x]
);
};
icosahedron result;
result.triangles.resize(20);
result.normals.resize(20 * 3);
for (int i = 0; i < 20; ++i)
{
result.triangles[i] = make_triangle(i);
vec3i idx = indices[i];
vec3 v1 = vertices[idx.x];
vec3 v2 = vertices[idx.y];
vec3 v3 = vertices[idx.z];
result.normals[i * 3] = normalize(v1);
result.normals[i * 3 + 1] = normalize(v2);
result.normals[i * 3 + 2] = normalize(v3);
}
return result;
}
//-------------------------------------------------------------------------------------------------
// Reset triangle mesh flags to 0
//
struct reset_flags_visitor : sg::node_visitor
{
using node_visitor::apply;
void apply(sg::surface_properties& sp)
{
sp.flags() = 0;
node_visitor::apply(sp);
}
void apply(sg::sphere& sph)
{
sph.flags() = 0;
node_visitor::apply(sph);
}
void apply(sg::triangle_mesh& tm)
{
tm.flags() = 0;
node_visitor::apply(tm);
}