-
Notifications
You must be signed in to change notification settings - Fork 37
/
vsnray_loader.cpp
2340 lines (1962 loc) · 64.1 KB
/
vsnray_loader.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 <cstddef>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <memory>
#include <ostream>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <vector>
#include <boost/iostreams/device/mapped_file.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/utility/string_ref.hpp>
#include <boost/assign.hpp>
#include <boost/bimap.hpp>
#include <boost/filesystem.hpp>
#include <rapidjson/document.h>
#include <rapidjson/filereadstream.h>
#include <rapidjson/filewritestream.h>
#include <rapidjson/prettywriter.h>
#include <visionaray/math/constants.h>
#include <visionaray/math/forward.h>
#include <visionaray/math/unorm.h>
#include <visionaray/math/vector.h>
#include <visionaray/texture/texture.h>
#include "cfile.h"
#include "image.h"
#include "make_texture.h"
#include "model.h"
#include "sg.h"
#include "vsnray_loader.h"
using namespace visionaray;
namespace data_file
{
//-------------------------------------------------------------------------------------------------
// (included) data file meta data
//
struct meta_data
{
enum encoding_t
{
Ascii,
Binary
};
// VecN are binary compatible w/ visionaray::vecN
enum data_type_t
{
U8,
Float,
Vec2u8,
Vec2f,
Vec3u8,
Vec3f,
Vec4u8,
Vec4f,
};
static boost::bimap<data_type_t, std::string> data_type_map;
enum compression_t
{
Raw
};
std::string path;
encoding_t encoding = Binary;
data_type_t data_type = U8;
int num_items = 0;
compression_t compression = Raw;
char separator = ' ';
};
boost::bimap<meta_data::data_type_t, std::string> meta_data::data_type_map
= boost::assign::list_of<typename boost::bimap<meta_data::data_type_t, std::string>::relation>
( U8, "u8" )
( Float, "float" )
( Vec2u8, "vec2u8" )
( Vec2f, "vec2f" )
( Vec3u8, "vec3u8" )
( Vec3f, "vec3f" )
( Vec4u8, "vec4u8" )
( Vec4f, "vec4f" );
} // data_file
//-------------------------------------------------------------------------------------------------
// Floating point number parser
//
template <typename It, typename Vector>
bool parse_floats(It first, It last, Vector& vec, char separator = ' ')
{
namespace qi = boost::spirit::qi;
return qi::phrase_parse(
first,
last,
qi::float_ % *qi::char_(separator),
qi::ascii::space,
vec
);
}
template <size_t N, typename Container>
bool parse_as_vecN(data_file::meta_data md, Container& vecNs)
{
boost::iostreams::mapped_file_source file(md.path);
if (md.data_type == data_file::meta_data::Float)
{
if (md.num_items % N != 0)
{
return false;
}
std::vector<float> floats;
if (md.encoding == data_file::meta_data::Ascii)
{
boost::string_ref text(file.data(), file.size());
parse_floats(text.cbegin(), text.cend(), floats, md.separator);
if (static_cast<int>(floats.size()) != md.num_items)
{
return false;
}
}
else // Binary
{
floats.resize(md.num_items);
std::copy(
file.data(),
file.data() + file.size(),
reinterpret_cast<char*>(floats.data())
);
}
vecNs.resize(md.num_items / N);
for (size_t i = 0; i < vecNs.size(); ++i)
{
for (size_t j = 0; j < N; ++j)
{
vecNs[i][j] = floats[i * N + j];
}
}
}
else if (md.data_type == data_file::meta_data::Vec2u8)
{
if (N != 2)
{
throw std::runtime_error("Type is Vec2u8 but N != 2");
}
if (md.encoding == data_file::meta_data::Ascii)
{
// Not implemented yet
return false;
}
else // Binary
{
vecNs.resize(md.num_items);
std::copy(
file.data(),
file.data() + file.size(),
reinterpret_cast<char*>(vecNs.data())
);
}
}
else if (md.data_type == data_file::meta_data::Vec2f)
{
if (N != 2)
{
throw std::runtime_error("Type is Vec2f but N != 2");
}
if (md.encoding == data_file::meta_data::Ascii)
{
// Not implemented yet
return false;
}
else // Binary
{
vecNs.resize(md.num_items);
std::copy(
file.data(),
file.data() + file.size(),
reinterpret_cast<char*>(vecNs.data())
);
}
}
else if (md.data_type == data_file::meta_data::Vec3u8)
{
if (N != 3)
{
throw std::runtime_error("Type is Vec3u8 but N != 3");
}
if (md.encoding == data_file::meta_data::Ascii)
{
// Not implemented yet
return false;
}
else // Binary
{
vecNs.resize(md.num_items);
std::copy(
file.data(),
file.data() + file.size(),
reinterpret_cast<char*>(vecNs.data())
);
}
}
else if (md.data_type == data_file::meta_data::Vec3f)
{
if (N != 3)
{
throw std::runtime_error("Type is Vec3f but N != 3");
}
if (md.encoding == data_file::meta_data::Ascii)
{
// Not implemented yet
return false;
}
else // Binary
{
vecNs.resize(md.num_items);
std::copy(
file.data(),
file.data() + file.size(),
reinterpret_cast<char*>(vecNs.data())
);
}
}
else if (md.data_type == data_file::meta_data::Vec4u8)
{
if (N != 4)
{
throw std::runtime_error("Type is Vec3u8 but N != 4");
}
if (md.encoding == data_file::meta_data::Ascii)
{
// Not implemented yet
return false;
}
else // Binary
{
vecNs.resize(md.num_items);
std::copy(
file.data(),
file.data() + file.size(),
reinterpret_cast<char*>(vecNs.data())
);
}
}
else if (md.data_type == data_file::meta_data::Vec4f)
{
if (N != 4)
{
throw std::runtime_error("Type is Vec4f but N != 4");
}
if (md.encoding == data_file::meta_data::Ascii)
{
// Not implemented yet
return false;
}
else // Binary
{
vecNs.resize(md.num_items);
std::copy(
file.data(),
file.data() + file.size(),
reinterpret_cast<char*>(vecNs.data())
);
}
}
return true;
}
template <typename Container>
bool parse_as_vec2f(data_file::meta_data md, Container& vec2fs)
{
return parse_as_vecN<2>(md, vec2fs);
}
template <typename Container>
bool parse_as_vec3f(data_file::meta_data md, Container& vec3fs)
{
return parse_as_vecN<3>(md, vec3fs);
}
//-------------------------------------------------------------------------------------------------
// Parse tiny objects from json object
//
template <typename Object>
void parse_optional(float& f, Object const& obj, char const* member)
{
if (obj.HasMember(member))
{
auto const& val = obj[member];
if (!val.IsFloat())
{
throw std::runtime_error("FLOAT: Optional value is not of type float");
}
f = val.GetFloat();
}
}
template <size_t N, typename Object>
void parse_optional(vector<N, float>& v, Object const& obj, char const* member)
{
if (obj.HasMember(member))
{
auto const& val = obj[member];
if (!val.IsArray())
{
throw std::runtime_error("VecNf: Optional value is not an array");
}
if (val.Capacity() != N)
{
throw std::runtime_error("VecNf: Optional value's capacity is not N");
}
for (size_t i = 0; i < N; ++i)
{
v[i] = val[i].GetFloat();
}
}
}
template <typename Object>
void parse_optional(recti& r, Object const& obj, char const* member)
{
if (obj.HasMember(member))
{
auto const& val = obj[member];
if (!val.IsArray())
{
throw std::runtime_error("RECT: Optional value is not an array");
}
if (val.Capacity() != 4)
{
throw std::runtime_error("RECT: Optional value's capacity is not 4");
}
for (size_t i = 0; i < 4; ++i)
{
r.data()[i] = val[i].GetFloat();
}
}
}
//-------------------------------------------------------------------------------------------------
// Parse texture parameters
//
template <typename Object>
bool parse_tex_address_mode_impl(Object const& obj, char const* attr, tex_address_mode& am)
{
if (obj.HasMember(attr) && obj[attr].IsString())
{
std::string am_str = obj[attr].GetString();
if (am_str == "wrap")
{
am = Wrap;
}
else if (am_str == "mirror")
{
am = Mirror;
}
else if (am_str == "clamp")
{
am = Clamp;
}
else if (am_str == "border")
{
am = Border;
}
else
{
throw std::runtime_error("Address mode is unknown");
}
return true;
}
return false;
}
// For 1-d textures
template <typename Object>
std::array<tex_address_mode, 1> parse_tex_address_mode1(Object const& obj)
{
// Default: Wrap
std::array<tex_address_mode, 1> result{{ Wrap }};
tex_address_mode am = Wrap;
// First parse address_mode
if (parse_tex_address_mode_impl(obj, "address_mode", am))
{
result[0] = am;
}
// address_mode_s may override default / address_mode
if (parse_tex_address_mode_impl(obj, "address_mode_s", am))
{
result[0] = am;
}
return result;
}
// For 2-d textures
template <typename Object>
std::array<tex_address_mode, 2> parse_tex_address_mode2(Object const& obj)
{
// Default: Wrap
std::array<tex_address_mode, 2> result{{ Wrap }};
tex_address_mode am = Wrap;
// First parse address_mode
if (parse_tex_address_mode_impl(obj, "address_mode", am))
{
result[0] = am;
result[1] = am;
}
// address_mode_{s|t} may override default / address_mode
if (parse_tex_address_mode_impl(obj, "address_mode_s", am))
{
result[0] = am;
}
if (parse_tex_address_mode_impl(obj, "address_mode_t", am))
{
result[1] = am;
}
return result;
}
// For 3-d textures
template <typename Object>
std::array<tex_address_mode, 3> parse_tex_address_mode3(Object const& obj)
{
// Default: Wrap
std::array<tex_address_mode, 3> result{{ Wrap }};
tex_address_mode am = Wrap;
// First parse address_mode
if (parse_tex_address_mode_impl(obj, "address_mode", am))
{
result[0] = am;
result[1] = am;
result[2] = am;
}
// address_mode_{s|t|r} may override default / address_mode
if (parse_tex_address_mode_impl(obj, "address_mode_s", am))
{
result[0] = am;
}
if (parse_tex_address_mode_impl(obj, "address_mode_t", am))
{
result[1] = am;
}
if (parse_tex_address_mode_impl(obj, "address_mode_r", am))
{
result[2] = am;
}
return result;
}
template <typename Object>
tex_filter_mode parse_tex_filter_mode(Object const& obj)
{
if (obj.HasMember("filter_mode") && obj["filter_mode"].IsString())
{
std::string fm_str = obj["filter_mode"].GetString();
if (fm_str == "nearest")
{
return Nearest;
}
else if (fm_str == "linear")
{
return Linear;
}
else if (fm_str == "bspline")
{
return BSpline;
}
else if (fm_str == "cardinalspline")
{
return CardinalSpline;
}
else
{
throw std::runtime_error("Filter mode is unknown");
}
}
return Linear;
}
template <typename Object>
tex_color_space parse_tex_color_space(Object const& obj)
{
if (obj.HasMember("color_space") && obj["color_space"].IsString())
{
std::string cs_str = obj["color_space"].GetString();
if (cs_str == "rgb")
{
return RGB;
}
else if (cs_str == "srgb")
{
return sRGB;
}
else
{
throw std::runtime_error("Color space is unknown");
}
}
return RGB;
}
//-------------------------------------------------------------------------------------------------
// .vsnray parser
//
class vsnray_parser
{
public:
vsnray_parser(std::string filename)
: filename_(filename)
{
}
void parse_children(std::shared_ptr<sg::node> parent, rapidjson::Value const& entries);
template <typename Object>
std::shared_ptr<sg::node> parse_node(Object const& obj);
template <typename Object>
std::shared_ptr<sg::node> parse_camera(Object const& obj);
template <typename Object>
std::shared_ptr<sg::node> parse_include(Object const& obj);
template <typename Object>
std::shared_ptr<sg::node> parse_point_light(Object const& obj);
template <typename Object>
std::shared_ptr<sg::node> parse_spot_light(Object const& obj);
template <typename Object>
std::shared_ptr<sg::node> parse_reference(Object const& obj);
template <typename Object>
std::shared_ptr<sg::node> parse_transform(Object const& obj);
template <typename Object>
std::shared_ptr<sg::node> parse_surface_properties(Object const& obj);
template <typename Object>
std::shared_ptr<sg::node> parse_triangle_mesh(Object const& obj);
template <typename Object>
std::shared_ptr<sg::node> parse_indexed_triangle_mesh(Object const& obj);
template <typename Object>
data_file::meta_data parse_file_meta_data(Object const& obj);
private:
std::string filename_;
};
//-------------------------------------------------------------------------------------------------
// Parse nodes
//
void vsnray_parser::parse_children(std::shared_ptr<sg::node> parent, rapidjson::Value const& entries)
{
if (!entries.IsArray())
{
throw std::runtime_error("Children entry list is not an array");
}
parent->children().resize(entries.Capacity());
size_t i = 0;
for (auto const& c : entries.GetArray())
{
auto const& obj = c.GetObject();
parent->children().at(i++) = parse_node(obj);
}
if (i != entries.Capacity())
{
throw std::runtime_error("Entry list's capacity doesn't equal the expected number of children");
}
}
template <typename Object>
std::shared_ptr<sg::node> vsnray_parser::parse_node(Object const& obj)
{
std::shared_ptr<sg::node> result = nullptr;
if (obj.HasMember("type"))
{
// Parse individual node types
auto const& type_string = obj["type"];
if (strncmp(type_string.GetString(), "node", 4) == 0)
{
// Empty node, (may still contain children, e.g. root)
result = std::make_shared<sg::node>();
}
else if (strncmp(type_string.GetString(), "camera", 6) == 0)
{
result = parse_camera(obj);
}
else if (strncmp(type_string.GetString(), "include", 6) == 0)
{
result = parse_include(obj);
}
else if (strncmp(type_string.GetString(), "point_light", 11) == 0)
{
result = parse_point_light(obj);
}
else if (strncmp(type_string.GetString(), "spot_light", 10) == 0)
{
result = parse_spot_light(obj);
}
else if (strncmp(type_string.GetString(), "reference", 9) == 0)
{
result = parse_reference(obj);
}
else if (strncmp(type_string.GetString(), "transform", 9) == 0)
{
result = parse_transform(obj);
}
else if (strncmp(type_string.GetString(), "surface_properties", 18) == 0)
{
result = parse_surface_properties(obj);
}
else if (strncmp(type_string.GetString(), "triangle_mesh", 13) == 0)
{
result = parse_triangle_mesh(obj);
}
else if (strncmp(type_string.GetString(), "indexed_triangle_mesh", 21) == 0)
{
result = parse_indexed_triangle_mesh(obj);
}
else
{
throw std::runtime_error("Node type unknown");
}
// Parse common node properties
if (obj.HasMember("name"))
{
assert(result != nullptr);
rapidjson::Value const& name = obj["name"];
result->name() = name.GetString();
}
if (obj.HasMember("children"))
{
assert(result != nullptr);
rapidjson::Value const& children = obj["children"];
parse_children(result, children);
}
}
else
{
throw std::runtime_error("Object doesn't have a \"type\" member");
}
return result;
}
template <typename Object>
std::shared_ptr<sg::node> vsnray_parser::parse_camera(Object const& obj)
{
auto cam = std::make_shared<sg::camera>();
vec3 eye(0.0f);
parse_optional(eye, obj, "eye");
vec3 center(0.0f);
parse_optional(center, obj, "center");
vec3 up(0.0f);
parse_optional(up, obj, "up");
float fovy = 45.0f;
parse_optional(fovy, obj, "fovy");
float znear = 0.001f;
parse_optional(znear, obj, "znear");
float zfar = 1000.0f;
parse_optional(zfar, obj, "zfar");
recti viewport(0, 0, 0, 0);
parse_optional(viewport, obj, "viewport");
float lens_radius = 0.1f;
parse_optional(lens_radius, obj, "lens_radius");
float focal_distance = 10.0f;
parse_optional(focal_distance, obj, "focal_distance");
float aspect = viewport.w > 0 && viewport.h > 0
? viewport.w / static_cast<float>(viewport.h)
: 1;
cam->perspective(fovy * constants::degrees_to_radians<float>(), aspect, znear, zfar);
if (viewport.w > 0 && viewport.h > 0)
{
cam->set_viewport(viewport);
}
cam->set_lens_radius(lens_radius);
cam->set_focal_distance(focal_distance);
cam->look_at(eye, center, up);
return cam;
}
template <typename Object>
std::shared_ptr<sg::node> vsnray_parser::parse_include(Object const& obj)
{
auto inc = std::make_shared<sg::node>();
if (obj.HasMember("path"))
{
std::string path_string(obj["path"].GetString());
if (path_string[0] == '~')
{
char const* home = getenv("HOME");
if (home != nullptr)
{
path_string = std::string(home) + path_string.substr(1, path_string.size() - 1);
}
}
boost::filesystem::path p(path_string);
if (!p.is_absolute())
{
// Extract base path
boost::filesystem::path bp(filename_);
bp = bp.parent_path();
// Append path to base path
p = bp / p;
path_string = p.string();
}
model mod;
if (mod.load(path_string))
{
if (mod.scene_graph == nullptr)
{
std::unordered_map<std::string, std::shared_ptr<sg::texture2d<vector<4, unorm<8>>>>> texture_map;
for (auto it = mod.texture_map.begin(); it != mod.texture_map.end(); ++it)
{
auto tex = std::make_shared<sg::texture2d<vector<4, unorm<8>>>>();
tex->name() = it->first;
tex->resize(it->second.width(), it->second.height());
tex->reset(it->second.data());
tex->set_filter_mode(it->second.get_filter_mode());
tex->set_address_mode(it->second.get_address_mode());
texture_map.insert(std::make_pair(it->first, tex));
}
if (mod.primitives.size() > 0)
{
// Vertices (disassemble triangles..)
for (auto tri : mod.primitives)
{
if (tri.geom_id >= inc->children().size())
{
auto props = std::make_shared<sg::surface_properties>();
// Add material
auto obj = std::make_shared<sg::obj_material>();
obj->ca = mod.materials[tri.geom_id].ca;
obj->cd = mod.materials[tri.geom_id].cd;
obj->cs = mod.materials[tri.geom_id].cs;
obj->ce = mod.materials[tri.geom_id].ce;
obj->cr = mod.materials[tri.geom_id].cr;
obj->ior = mod.materials[tri.geom_id].ior;
obj->absorption = mod.materials[tri.geom_id].absorption;
obj->transmission = mod.materials[tri.geom_id].transmission;
obj->specular_exp = mod.materials[tri.geom_id].specular_exp;
obj->illum = mod.materials[tri.geom_id].illum;
props->material() = obj;
bool insert_dummy = false;
if (tri.geom_id < mod.textures.size())
{
// Find texture in texture_map
bool found = false;
for (auto it = mod.texture_map.begin(); it != mod.texture_map.end(); ++it)
{
auto ref = texture_ref<vector<4, unorm<8>>, 2>(it->second);
if (ref.data() == mod.textures[tri.geom_id].data())
{
std::string name = it->first;
// Find in local texture map
auto res = texture_map.find(name);
if (res != texture_map.end())
{
props->add_texture(res->second);
found = true;
break;
}
}
}
if (!found)
{
insert_dummy = true;
}
}
else
{
insert_dummy = true;
}
if (insert_dummy)
{
// Add a dummy texture
vector<4, unorm<8>> dummy_texel(1.0f, 1.0f, 1.0f, 1.0f);
auto tex = std::make_shared<sg::texture2d<vector<4, unorm<8>>>>();
tex->resize(1, 1);
tex->set_address_mode(Wrap);
tex->set_filter_mode(Nearest);
tex->reset(&dummy_texel);
props->add_texture(tex);
}
// Add to scene graph
props->add_child(std::make_shared<sg::triangle_mesh>());
inc->add_child(props);
}
auto mesh = std::dynamic_pointer_cast<sg::triangle_mesh>(
inc->children()[tri.geom_id]->children()[0]
);
vec3 verts[3] = {
tri.v1,
tri.v1 + tri.e1,
tri.v1 + tri.e2
};
mesh->vertices.insert(mesh->vertices.end(), verts, verts + 3);
if (mod.shading_normals.size() > tri.prim_id * 3 + 3)
{
for (int i = 0; i < 3; ++i)
{
mesh->normals.push_back(mod.shading_normals[tri.prim_id * 3 + i]);
}
}
else
{
for (int i = 0; i < 3; ++i)
{
mesh->normals.push_back(normalize(cross(tri.e1, tri.e2)));
}
}
if (mod.tex_coords.size() >= tri.prim_id * 3 + 3)
{
for (int i = 0; i < 3; ++i)
{
mesh->tex_coords.push_back(mod.tex_coords[tri.prim_id * 3 + i]);
}
}
else
{
for (int i = 0; i < 3; ++i)
{
mesh->tex_coords.push_back(vec2(0.0f, 0.0f));
}
}
if (mod.colors.size() >= tri.prim_id * 3 + 3)
{
for (int i = 0; i < 3; ++i)
{
mesh->colors.push_back(vector<3, unorm<8>>(mod.colors[tri.prim_id * 3 + i]));
}
}
else
{
for (int i = 0; i < 3; ++i)
{
mesh->colors.push_back(vector<3, unorm<8>>(1.0f, 1.0f, 1.0f));
}
}
}
}
else
{
throw std::runtime_error("Included model contains 0 primitives");
}
}
else
{
// TODO: don't allow circular references..
inc = mod.scene_graph;
}
}
else
{
throw std::runtime_error("Couldn't load include file");
}
}
else
{
throw std::runtime_error("Include path is invalid");
}
return inc;
}
template <typename Object>
std::shared_ptr<sg::node> vsnray_parser::parse_point_light(Object const& obj)
{
auto light = std::make_shared<sg::point_light>();
vec3 cl(1.0f);
parse_optional(cl, obj, "cl");
float kl = 1.0f;
parse_optional(kl, obj, "kl");
vec3 position(0.0f);
parse_optional(position, obj, "position");