-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathac3d.cpp
5900 lines (5081 loc) · 198 KB
/
ac3d.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
/*
* acclint - A tool that detects errors in AC3D files.
* Copyright (C) 2020-2024 Robert Reif
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
#if defined(_WIN32)
#pragma warning( disable : 4996)
#endif
#include "ac3d.h"
#include <limits>
#include <filesystem>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <chrono>
#include <omp.h>
#include <png.h>
#include "triangleintersects.hpp"
const std::string MATERIAL_token("MATERIAL");
const std::string rgb_token("rgb");
const std::string amb_token("amb");
const std::string emis_token("emis");
const std::string spec_token("spec");
const std::string shi_token("shi");
const std::string trans_token("trans");
const std::string data_token("data");
const std::string MAT_token("MAT");
const std::string ENDMAT_token("ENDMAT");
const std::string OBJECT_token("OBJECT");
const std::string name_token("name");
const std::string texture_token("texture");
const std::string shader_token("shader");
const std::string texrep_token("texrep");
const std::string texoff_token("texoff");
const std::string subdiv_token("subdiv");
const std::string crease_token("crease");
const std::string rot_token("rot");
const std::string loc_token("loc");
const std::string url_token("url");
const std::string hidden_token("hidden");
const std::string locked_token("locked");
const std::string folded_token("folded");
const std::string numvert_token("numvert");
const std::string numsurf_token("numsurf");
const std::string SURF_token("SURF");
const std::string mat_token("mat");
const std::string refs_token("refs");
const std::string kids_token("kids");
const std::string empty_token;
const std::string world_token("world");
const std::string poly_token("poly");
const std::string group_token("group");
const std::string light_token("light");
std::ostream & operator << (std::ostream &out, const AC3D::quoted_string &s)
{
if (s == "empty_texture_no_mapping")
out << reinterpret_cast<const std::string &>(s);
else
out << '\"' << reinterpret_cast<const std::string &>(s) << '\"';
return out;
}
std::istream & operator >> (std::istream &in, AC3D::quoted_string &s)
{
in >> std::ws;
if (in.peek() != '\"')
in >> reinterpret_cast<std::string &>(s);
else
{
in.get(); // skip quote
while (in.good())
{
std::istream::char_type c;
in.get(c);
if (c == '\"' && (s.empty() || s.back() != '\\'))
break;
s += c;
}
}
return in;
}
template <size_t s>
std::ostream & operator << (std::ostream &out, const std::array<double,s> &a)
{
for (size_t i = 0; i < s; ++i)
{
if (i != 0)
out << ' ';
out << std::setprecision(12) << a[i];
}
return out;
}
template <size_t s>
std::istream & operator >> (std::istream &in, std::array<double,s> &a)
{
for (size_t i = 0; i < s; ++i)
in >> std::setprecision(12) >> a[i];
return in;
}
std::ostream & operator << (std::ostream &out, const AC3D::Vertex &v)
{
out << v.vertex;
if (v.has_normal)
out << " " << v.normal;
return out;
}
bool isWhitespace(const std::string &s)
{
return (s.find_first_not_of(" \n\r\t") == std::string::npos);
}
bool hasTrailing(const std::istringstream &s)
{
std::streambuf *buf = s.rdbuf();
const std::streampos pos = buf->pubseekoff(0, std::ios_base::cur, std::ios_base::in);
const std::streampos end = buf->pubseekoff(0, std::ios_base::end, std::ios_base::in);
buf->pubseekpos(pos, std::ios_base::in);
return end != pos;
}
std::string getTrailing(const std::istringstream &s)
{
std::streambuf *buf = s.rdbuf();
const std::streampos pos = buf->pubseekoff(0, std::ios_base::cur, std::ios_base::in);
buf->pubseekpos(pos, std::ios_base::in);
return s.str().substr(pos);
}
size_t offsetOfToken(const std::istringstream &in, size_t index)
{
const std::string text = in.str();
size_t token_count = 0;
size_t current_offset = 0;
bool in_token = false;
for (auto c : text)
{
if (!in_token && c != ' ')
{
in_token = true;
if (token_count == index)
return current_offset;
token_count++;
}
else if (in_token && c == ' ')
{
in_token = false;
}
current_offset++;
}
return current_offset;
}
void AC3D::showLine(std::istringstream &in) const
{
if (!m_quite)
{
std::cerr << in.str() << std::endl;
std::streambuf *buf = in.rdbuf();
const std::streampos pos = buf->pubseekoff(0, std::ios_base::cur, std::ios_base::in);
buf->pubseekpos(pos, std::ios_base::in);
for (int i = 0; i < pos; ++i)
std::cerr << ' ';
std::cerr << '^' << std::endl;
}
}
void AC3D::showLine(const std::istringstream &in, const std::streampos &pos) const
{
if (!m_quite)
{
std::cerr << in.str() << std::endl;
for (int i = 0; i < pos; ++i)
std::cerr << ' ';
std::cerr << '^' << std::endl;
}
}
void AC3D::showLine(std::istream &in, const std::streampos &pos, int offset) const
{
if (!m_quite)
{
const std::streampos current = in.tellg();
std::string line;
in.seekg(pos);
std::getline(in, line);
// remove CR
if (line.back() == '\r')
line.pop_back();
std::cerr << line << std::endl;
if (offset < 0)
offset = static_cast<int>(line.size());
for (int i = 0; i < offset; ++i)
std::cerr << ' ';
std::cerr << '^' << std::endl;
in.seekg(current);
}
}
class newline
{
bool m_crlf;
public:
explicit newline(bool crlf) : m_crlf(crlf) {}
friend std::ostream &operator << (std::ostream &os, const newline &nl)
{
if (nl.m_crlf)
os << '\r';
os << '\n';
return os;
}
};
bool AC3D::getLine(std::istream &in)
{
m_line_pos = in.tellg();
bool empty = false;
do
{
empty = false;
std::getline(in, m_line);
if (!in)
return false;
m_line_number++;
if (!m_line.empty() && m_line.back() == '\r')
{
m_line.pop_back();
m_crlf = true;
}
if (m_line.empty() || isWhitespace(m_line))
{
if (m_blank_line)
warningWithCount(m_blank_line_count) << "blank line" << std::endl;
empty = true;
}
} while (empty);
return true;
}
bool AC3D::ungetLine(std::istream &in)
{
in.seekg(m_line_pos);
m_line_number--;
return true;
}
std::ostream &AC3D::warning(size_t line_number)
{
m_warnings++;
if (!m_quite)
{
if (line_number > 0)
std::cerr << m_file << ":" << line_number << " warning: ";
else
std::cerr << m_file << ":" << m_line_number << " warning: ";
}
return std::cerr;
}
std::ostream &AC3D::warningWithCount(size_t &count, size_t line_number)
{
count++;
m_warnings++;
if (!m_quite)
{
if (line_number > 0)
std::cerr << m_file << ":" << line_number << " warning: ";
else
std::cerr << m_file << ":" << m_line_number << " warning: ";
return std::cerr;
}
return m_null_stream;
}
std::ostream &AC3D::error(size_t line_number)
{
m_errors++;
if (line_number > 0)
std::cerr << m_file << ":" << line_number << " error: ";
else
std::cerr << m_file << ":" << m_line_number << " error: ";
return std::cerr;
}
std::ostream &AC3D::errorWithCount(size_t &count, size_t line_number)
{
count++;
m_errors++;
if (!m_quite)
{
if (line_number > 0)
std::cerr << m_file << ":" << line_number << " error: ";
else
std::cerr << m_file << ":" << m_line_number << " error: ";
return std::cerr;
}
return m_null_stream;
}
std::ostream &AC3D::note(size_t line_number)
{
if (!m_quite)
{
std::cerr << m_file << ":" << line_number << " note: ";
return std::cerr;
}
return m_null_stream;
}
void AC3D::checkTrailing(std::istringstream &iss)
{
if (!m_trailing_text)
return;
const std::streampos pos = iss.tellg();
if (hasTrailing(iss))
{
warningWithCount(m_trailing_text_count) << "trailing text: \"" << getTrailing(iss) << "\"" << std::endl;
showLine(iss, pos);
}
}
bool AC3D::readRef(std::istringstream &in, AC3D::Ref &ref)
{
ref.line_number = m_line_number;
ref.line_pos = m_line_pos;
in >> ref.index;
if (!in)
{
error() << "reading index" << std::endl;
showLine(in);
return false;
}
Point2 uv{ 0.0, 0.0 };
in >> uv;
if (in)
{
ref.coordinates.push_back(uv);
if (hasTrailing(in))
{
std::streampos pos = in.tellg();
std::string trailing = getTrailing(in);
if (m_is_ac)
{
if (m_trailing_text)
{
warningWithCount(m_trailing_text_count) << "trailing text: \"" << trailing << "\"" << std::endl;
showLine(in, pos);
}
}
else
{
in >> uv;
if (in)
{
ref.coordinates.push_back(uv);
if (hasTrailing(in))
{
pos = in.tellg();
trailing = getTrailing(in);
in >> uv;
if (in)
{
ref.coordinates.push_back(uv);
if (hasTrailing(in))
{
pos = in.tellg();
trailing = getTrailing(in);
in >> uv;
if (in)
{
ref.coordinates.push_back(uv);
if (hasTrailing(in))
{
if (m_trailing_text)
{
pos = in.tellg();
trailing = getTrailing(in);
warningWithCount(m_trailing_text_count) << "trailing text: \"" << trailing << "\"" << std::endl;
showLine(in, pos);
}
}
}
else
{
if (isWhitespace(trailing))
{
if (m_trailing_text)
{
warningWithCount(m_trailing_text_count) << "trailing text: \"" << trailing << "\"" << std::endl;
showLine(in, pos);
}
}
else if (m_invalid_texture_coordinate)
{
error() << "reading forth texture cordinate: \"" << trailing << "\"" << std::endl;
showLine(in);
}
}
}
}
else
{
if (isWhitespace(trailing))
{
if (m_trailing_text)
{
warningWithCount(m_trailing_text_count) << "trailing text: \"" << trailing << "\"" << std::endl;
showLine(in, pos);
}
}
else if (m_invalid_texture_coordinate)
{
error() << "reading third texture cordinate: \"" << trailing << "\"" << std::endl;
showLine(in);
}
}
}
}
else
{
if (isWhitespace(trailing))
{
if (m_trailing_text)
{
warningWithCount(m_trailing_text_count) << "trailing text: \"" << trailing << "\"" << std::endl;
showLine(in, pos);
}
}
else if (m_invalid_texture_coordinate)
{
error() << "reading second texture cordinate: \"" << trailing << "\"" << std::endl;
showLine(in);
}
}
}
}
}
else if (m_invalid_texture_coordinate)
{
error() << "reading texture coordinate" << std::endl;
showLine(in);
return false;
}
return true;
}
void AC3D::writeRef(std::ostream &out, const AC3D::Ref &ref) const
{
out << ref.index;
for (const auto &coord : ref.coordinates)
out << ' ' << coord;
out << newline(m_crlf);
}
bool AC3D::readSurface(std::istream &in, Surface &surface, Object &object, bool get_line)
{
if (get_line)
{
if (!getLine(in))
return false;
}
surface.line_number = m_line_number;
surface.line_pos = m_line_pos;
std::istringstream iss(m_line);
std::string token;
iss >> token;
if (token == SURF_token)
{
iss >> std::ws;
const std::streampos pos = iss.tellg();
iss >> std::hex >> surface.flags >> std::dec;
if (iss)
{
if (m_invalid_surface_type && !surface.isValidFlags(m_is_ac))
{
error() << "invalid surface type" << std::endl;
showLine(iss, pos);
}
checkTrailing(iss);
}
else
{
error() << "reading type" << std::endl;
showLine(iss, pos);
}
if (!getLine(in))
{
error() << "invalid surface" << std::endl;
return true;
}
iss.clear();
iss.str(m_line);
iss >> token;
}
else if (token == kids_token)
{
error() << "less surfaces than specified" << std::endl;
showLine(iss, 0);
note(object.numsurf.line_number) << "number specified" << std::endl;
showLine(in, object.numsurf.line_pos, object.numsurf.number_offset);
ungetLine(in);
return false;
}
else
{
error() << "invalid surface" << std::endl;
showLine(iss, 0);
return true;
}
if (token == mat_token)
{
size_t mat = 0;
iss >> std::ws;
const std::streampos pos = iss.tellg();
iss >> mat;
if (iss)
{
setMaterialUsed(mat);
if (mat >= m_materials.size())
{
if (m_invalid_material_index)
{
error() << "invalid material index: " << mat << " of "
<< m_materials.size() << std::endl;
showLine(iss, pos);
}
}
surface.mats.emplace_back(m_line_number, m_line_pos, mat);
checkTrailing(iss);
}
else
{
error() << "reading material index" << std::endl;
showLine(iss);
}
if (!getLine(in))
{
error() << "invalid surface" << std::endl;
return true;
}
iss.clear();
iss.str(m_line);
iss >> token;
}
if (token == refs_token)
{
surface.refs.line_number = m_line_number;
surface.refs.line_pos = m_line_pos;
iss >> surface.refs.declared_size;
if (iss)
checkTrailing(iss);
else
{
error() << "reading refs count" << std::endl;
showLine(iss);
}
for (int j = 0; j < surface.refs.declared_size; ++j)
{
if (getLine(in))
{
Ref ref;
std::istringstream iss1(m_line);
if (readRef(iss1, ref))
{
if (ref.index >= object.vertices.size())
{
if (m_invalid_vertex_index)
{
error() << "invalid vertex index: " << ref.index << " of "
<< object.vertices.size() << std::endl;
showLine(iss1, 0);
}
}
else
object.vertices[ref.index].used = true;
}
const size_t valid_textures = object.getTexturesSize();
if (ref.coordinates.size() < valid_textures)
{
if (m_missing_uv_coordinates)
{
warningWithCount(m_missing_uv_coordinates_count) << "missing uv coordinates: "
<< ref.coordinates.size() << " coordinate" << (ref.coordinates.size() != 1 ? "s" : "") << " "
<< valid_textures << " texture" << (valid_textures != 1 ? "s" : "") << std::endl;
showLine(iss1, iss1.str().size() + 1);
}
}
if (ref.coordinates.size() > 1 && ref.coordinates.size() > valid_textures)
{
if (m_extra_uv_coordinates)
{
warningWithCount(m_extra_uv_coordinates_count) << "extra uv coordinates: "
<< ref.coordinates.size() << " coordinates "
<< valid_textures << " texture" << (valid_textures != 1 ? "s" : "") << std::endl;
showLine(iss1, offsetOfToken(iss1, valid_textures * 2 + 1));
}
}
surface.refs.push_back(ref);
}
}
surface.setTriangleStrip(object);
checkDuplicateSurfaceVertices(in, object, surface);
checkCollinearSurfaceVertices(in, object, surface);
checkSurfaceCoplanar(in, object, surface);
checkSurfacePolygonType(in, object, surface);
checkSurfaceSelfIntersecting(in, object, surface);
checkSurfaceStripHole(in, object, surface);
checkSurfaceStripSize(in, object, surface);
checkSurfaceStripDegenerate(in, object, surface);
checkSurfaceStripDuplicateTriangles(in, object, surface);
checkSurfaceNoTexture(in, object, surface);
checkSurface2SidedOpaque(in, object, surface);
}
else
{
error() << "invalid surface" << std::endl;
return false;
}
return true;
}
void AC3D::writeSurface(std::ostream &out, const Surface &surface) const
{
out << "SURF 0x" << std::hex << surface.flags << std::dec << newline(m_crlf);
if (!surface.mats.empty())
out << "mat " << surface.mats[0].mat << newline(m_crlf);
out << "refs " << surface.refs.size() << newline(m_crlf);
for (const auto &ref : surface.refs)
writeRef(out, ref);
}
void AC3D::writeSurfaces(std::ostream &out, const Object &object) const
{
if (!object.surfaces.empty())
{
out << "numsurf " << object.surfaces.size() << newline(m_crlf);
for (const auto& surface : object.surfaces)
writeSurface(out, surface);
}
}
void AC3D::writeVertices(std::ostream &out, const Object &object) const
{
if (!object.vertices.empty())
{
out << "numvert " << object.vertices.size() << newline(m_crlf);
for (const auto& vertex : object.vertices)
{
out << vertex.vertex;
if (vertex.has_normal)
out << ' ' << vertex.normal;
out << newline(m_crlf);
}
}
}
void AC3D::convertObjectsToAc(std::vector<Object> &objects)
{
for (auto &object : objects)
{
convertObjectToAc(object);
convertObjectsToAc(object.kids);
}
}
void AC3D::convertObjectToAc(Object &object)
{
std::vector<Surface> surfaces;
for (size_t i = 0; i < object.surfaces.size(); ++i)
{
if (!object.surfaces[i].isTriangleStrip())
surfaces.push_back(object.surfaces[i]);
else
{
for (size_t j = 0; j < object.surfaces[i].refs.size() - 2; ++j)
{
if (object.surfaces[i].refs[j].index >= object.vertices.size() ||
object.surfaces[i].refs[j + 1].index >= object.vertices.size() ||
object.surfaces[i].refs[j + 2].index >= object.vertices.size())
continue;
if (object.vertices[object.surfaces[i].refs[j].index].vertex == object.vertices[object.surfaces[i].refs[j + 1].index].vertex ||
object.vertices[object.surfaces[i].refs[j].index].vertex == object.vertices[object.surfaces[i].refs[j + 2].index].vertex ||
object.vertices[object.surfaces[i].refs[j + 1].index].vertex == object.vertices[object.surfaces[i].refs[j + 2].index].vertex)
continue;
Surface surface;
surface.mats = object.surfaces[i].mats;
surface.flags = object.surfaces[i].flags & Surface::FaceMask;
if ((j & 1U) == 0)
{
Ref ref1;
ref1.index = object.surfaces[i].refs[j].index;
ref1.coordinates.push_back(object.surfaces[i].refs[j].coordinates.front());
surface.refs.push_back(ref1);
Ref ref2;
ref2.index = object.surfaces[i].refs[j + 1].index;
ref2.coordinates.push_back(object.surfaces[i].refs[j + 1].coordinates.front());
surface.refs.push_back(ref2);
}
else
{
Ref ref1;
ref1.index = object.surfaces[i].refs[j + 1].index;
ref1.coordinates.push_back(object.surfaces[i].refs[j + 1].coordinates.front());
surface.refs.push_back(ref1);
Ref ref2;
ref2.index = object.surfaces[i].refs[j].index;
ref2.coordinates.push_back(object.surfaces[i].refs[j].coordinates.front());
surface.refs.push_back(ref2);
}
Ref ref3;
ref3.index = object.surfaces[i].refs[j + 2].index;
ref3.coordinates.push_back(object.surfaces[i].refs[j + 2].coordinates.front());
surface.refs.push_back(ref3);
surfaces.push_back(surface);
}
}
}
object.surfaces.clear();
object.surfaces = surfaces;
// remove textures
if (object.textures.size() > 1)
object.textures.resize(1);
// remove texture type
if (!object.textures.empty())
object.textures[0].type.clear();
// remove normals
for (auto &vertex : object.vertices)
vertex.has_normal = false;
// removing normals on vertices can create duplicate vertices
cleanVertices(object);
cleanSurfaces(object);
}
void AC3D::convertObjectsToAcc(std::vector<Object> &objects)
{
for (auto &object : objects)
{
if (object.type.type == "poly")
convertObjectToAcc(object);
else
convertObjectsToAcc(object.kids);
}
}
void AC3D::convertObjectToAcc(Object &object)
{
struct Triangle
{
Triangle() = delete;
Triangle(unsigned int flags, size_t mat,
const Point3 &v0, const Point3 &v1, const Point3 &v2,
const std::vector<Point2> &uv0, const std::vector<Point2> &uv1, const std::vector<Point2> &uv2)
: m_flags(flags), m_mat(mat)
{
m_vertices[0] = v0;
m_vertices[1] = v1;
m_vertices[2] = v2;
m_coordinates[0] = uv0;
m_coordinates[1] = uv1;
m_coordinates[2] = uv2;
if (isFlatShaded())
{
m_normal = normalizedNormal(v0, v1, v2);
m_normals[0].push_back(m_normal);
m_normals[1].push_back(m_normal);
m_normals[2].push_back(m_normal);
}
else
{
m_normal = unnormalizedNormal(v0, v1, v2);
const double a1 = (v1 - v0).angleRadians(v2 - v0);
const double a2 = (v2 - v1).angleRadians(v0 - v1);
const double a3 = (v0 - v2).angleRadians(v1 - v2);
m_normals[0].push_back(m_normal * a1);
m_normals[1].push_back(m_normal * a2);
m_normals[2].push_back(m_normal * a3);
}
}
bool isFlatShaded() const
{
return (m_flags & Surface::ShadeMask) == 0;
}
bool isSmoothShaded() const
{
return (m_flags & Surface::ShadeMask) == Surface::Shaded;
}
bool good() const
{
return m_vertices[0] != m_vertices[1] && m_vertices[0] != m_vertices[2] && m_vertices[1] != m_vertices[2];
}
void smooth(Triangle &other)
{
for (size_t i = 0; i < 3; i++)
{
for (size_t j = 0; j < 3; j++)
{
if (m_vertices[i] == other.m_vertices[j])
{
m_normals[i].emplace_back(other.m_normals[j][0]);
other.m_normals[j].emplace_back(m_normals[i][0]);
}
}
}
}
Vertex vertex(size_t index, const Object &object) const
{
Vertex vertex;
vertex.has_normal = true;
vertex.vertex = m_vertices[index];
vertex.normal = m_normals[index][0];
for (size_t i = 1; i < m_normals[index].size(); i++)
{
const double angle = vertex.normal.angleDegrees(m_normals[index][i]);
if (!object.creases.empty() && angle < object.creases[0].crease)
vertex.normal += m_normals[index][i];
else if (object.creases.empty() && angle < 179.999)
vertex.normal += m_normals[index][i];
}
vertex.normal.normalize();
return vertex;
}
Point3 m_vertices[3];
std::vector<Point2> m_coordinates[3];
std::vector<Point3> m_normals[3];
Point3 m_normal;
unsigned int m_flags;
size_t m_mat;
size_t m_vertex_index[3]{ 0, 0, 0 };
};
std::vector<Triangle> triangles;
for (auto &surface : object.surfaces)
{
if (surface.isPolygon())
{
if (surface.refs.size() > 3)
{
if (surface.concave)
{
// TODO
return;
}
else // triangle fan
{
for (size_t i = 2; i < static_cast<size_t>(surface.refs.size()); i++)
{
const Triangle triangle(surface.flags, surface.mats[0].mat,
object.vertices[surface.refs[0].index].vertex,
object.vertices[surface.refs[i - 1].index].vertex,
object.vertices[surface.refs[i].index].vertex,
surface.refs[0].coordinates,
surface.refs[i - 1].coordinates,
surface.refs[i].coordinates);
if (triangle.good())
triangles.emplace_back(triangle);
}
}
}
else
{
const Triangle triangle(surface.flags, surface.mats[0].mat,
object.vertices[surface.refs[0].index].vertex,
object.vertices[surface.refs[1].index].vertex,
object.vertices[surface.refs[2].index].vertex,
surface.refs[0].coordinates,
surface.refs[1].coordinates,
surface.refs[2].coordinates);
if (triangle.good())
triangles.emplace_back(triangle);
}
}
}
if (triangles.empty())
return;
const size_t size = triangles.size();
for (size_t i = 0, end = size - 1; i < end; i++)
{
for (size_t j = i + 1; j < size; j++)
{
if (triangles[i].isSmoothShaded() && triangles[j].isSmoothShaded())
triangles[i].smooth(triangles[j]);
}
}
std::vector<Vertex> vertices;
for (auto &triangle : triangles)
{
for (size_t i = 0; i < 3; i++)
{
bool found = false;
const Vertex vertex = triangle.vertex(i, object);
for (size_t j = 0; j < vertices.size(); j++)
{
if (vertices[j] == vertex)
{
triangle.m_vertex_index[i] = j;
found = true;
break;