-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathl2rib.C
1811 lines (1660 loc) · 57.1 KB
/
l2rib.C
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
/*
* l2rib.C version 1.00 - http://www.levork.org/l2rib.html
*
* Copyright (C) 2001-2005 by Julian Fong (http://www.levork.org/). All
* rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* The RenderMan (R) Interface Procedures and RIB Protocol are:
* Copyright 1988, 1989, Pixar. All rights reserved.
* RenderMan (R) is a registered trademark of Pixar.
*
* This is l2rib, a program for converting LDraw .DAT files to
* RenderMan Interface Bytestream .RIB files for use in a RenderMan
* compliant renderer. In practice, this has primarily been tested
* with one renderer: Pixar's PhotoRealistic RenderMan.
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifndef _WIN32
#include <sys/time.h>
#include <unistd.h>
#else
#include <time.h>
#include <windows.h>
#include <shfolder.h>
#endif
#include <math.h>
#include <string>
#ifdef _WIN32
#include <iostream>
#include <sstream>
#include <fstream>
#include <direct.h>
#else
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#endif
#include <vector>
#include <map>
#include <set>
#if __GNUC__ >= 3
#include <ext/hash_map>
using namespace __gnu_cxx;
// This is stupid.
// (see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13342)
namespace __gnu_cxx
{
using namespace std;
template<>
struct hash<string>
{
size_t operator()(const string& s) const
{
const collate<char>& c = use_facet<collate<char> >(locale::classic());
return c.hash(s.c_str(), s.c_str() + s.size());
}
};
}
#elif __GNUC__ >= 2
#include <hash_map>
template<> struct hash<std::string>
{
size_t operator()(const std::string& __x) const
{
return __stl_hash_string(__x.c_str());
}
};
#else
#include <hash_map>
#endif
#ifdef _WIN32
#define PATHSEP "\\"
#else
#define PATHSEP "/"
#endif
#define REMOVE_SPACES(x) x.erase(std::remove(x.begin(), x.end(), ' '), x.end())
#define REMOVE_CRS(x) x.erase(std::remove(x.begin(), x.end(), '\r'), x.end())
using namespace std;
struct ColourCode {
ColourCode() : r(0), g(0), b(0), shader(0), transparent(false), transparency(0), init(false), warned(false), custom(false), edger(0), edgeg(0), edgeb(0), edgecode(-1) {}
string name;
float r, g, b;
int shader;
bool transparent;
float transparency;
bool init;
bool warned;
bool custom;
float edger, edgeg, edgeb;
int edgecode;
string customShader;
};
struct Point {
Point(float _x = 0, float _y = 0, float _z = 0) : x(_x), y(_y), z(_z) {}
Point(const Point& p) : x(p.x), y(p.y), z(p.z) {}
float x;
float y;
float z;
Point& operator= (const Point& p) { x = p.x; y = p.y; z = p.z; return *this;}
Point& operator+= (const Point& p) { x += p.x; y += p.y; z += p.z; return *this;}
};
ostream& operator<< (ostream& o, const Point& p) {
return o << p.x << ' ' << p.y << ' ' << p.z;
}
istream& operator>> (istream& i, Point& p) {
i >> p.x >> p.y >> p.z;
return i;
}
Point operator+(const Point& p1, const Point& p2) {
return Point(p1.x + p2.x, p1.y + p2.y, p1.z + p2.z);
}
Point operator- (const Point& p1, const Point& p2) {
return Point(p1.x - p2.x, p1.y - p2.y, p1.z - p2.z);
}
bool operator< (const Point& p1, const Point& p2) {
if (p1.x > p2.x) return false;
if (p1.x < p2.x) return true;
if (p1.y > p2.y) return false;
if (p1.y < p2.y) return true;
if (p1.z > p2.z) return false;
if (p1.z < p2.z) return true;
// all compare the same
return false;
}
struct Bound {
Bound() : init(false), mpdincomplete(false) {}
float minx;
float maxx;
float miny;
float maxy;
float minz;
float maxz;
bool init;
// This flag indicates whether the bound is incomplete due to a
// forward part reference to a MPD file. In that case, the bound
// cannot be accurate until another pass through the file.
bool mpdincomplete;
Bound& operator=(const Point &p) {
minx = maxx = p.x;
miny = maxy = p.y;
minz = maxz = p.z;
init = true;
return *this;
}
void expand(const Point &p) {
if (!init) *this = p;
else {
if (p.x < minx) minx = p.x;
if (p.x > maxx) maxx = p.x;
if (p.y < miny) miny = p.y;
if (p.y > maxy) maxy = p.y;
if (p.z < minz) minz = p.z;
if (p.z > maxz) maxz = p.z;
}
}
void expand(const Bound& newbound, float* matrix) {
if (!newbound.init) return;
// Transform all 8 corners of the bounding box
Point p[8] = {
Point (
matrix[0] * newbound.minx + matrix[4] * newbound.miny + matrix[8] * newbound.minz + matrix[12],
matrix[1] * newbound.minx + matrix[5] * newbound.miny + matrix[9] * newbound.minz + matrix[13],
matrix[2] * newbound.minx + matrix[6] * newbound.miny + matrix[10] * newbound.minz + matrix[14]
),
Point (
matrix[0] * newbound.minx + matrix[4] * newbound.miny + matrix[8] * newbound.maxz + matrix[12],
matrix[1] * newbound.minx + matrix[5] * newbound.miny + matrix[9] * newbound.maxz + matrix[13],
matrix[2] * newbound.minx + matrix[6] * newbound.miny + matrix[10] * newbound.maxz + matrix[14]
),
Point (
matrix[0] * newbound.minx + matrix[4] * newbound.maxy + matrix[8] * newbound.minz + matrix[12],
matrix[1] * newbound.minx + matrix[5] * newbound.maxy + matrix[9] * newbound.minz + matrix[13],
matrix[2] * newbound.minx + matrix[6] * newbound.maxy + matrix[10] * newbound.minz + matrix[14]
),
Point (
matrix[0] * newbound.minx + matrix[4] * newbound.maxy + matrix[8] * newbound.maxz + matrix[12],
matrix[1] * newbound.minx + matrix[5] * newbound.maxy + matrix[9] * newbound.maxz + matrix[13],
matrix[2] * newbound.minx + matrix[6] * newbound.maxy + matrix[10] * newbound.maxz + matrix[14]
),
Point (
matrix[0] * newbound.maxx + matrix[4] * newbound.miny + matrix[8] * newbound.minz + matrix[12],
matrix[1] * newbound.maxx + matrix[5] * newbound.miny + matrix[9] * newbound.minz + matrix[13],
matrix[2] * newbound.maxx + matrix[6] * newbound.miny + matrix[10] * newbound.minz + matrix[14]
),
Point (
matrix[0] * newbound.maxx + matrix[4] * newbound.miny + matrix[8] * newbound.maxz + matrix[12],
matrix[1] * newbound.maxx + matrix[5] * newbound.miny + matrix[9] * newbound.maxz + matrix[13],
matrix[2] * newbound.maxx + matrix[6] * newbound.miny + matrix[10] * newbound.maxz + matrix[14]
),
Point (
matrix[0] * newbound.maxx + matrix[4] * newbound.maxy + matrix[8] * newbound.minz + matrix[12],
matrix[1] * newbound.maxx + matrix[5] * newbound.maxy + matrix[9] * newbound.minz + matrix[13],
matrix[2] * newbound.maxx + matrix[6] * newbound.maxy + matrix[10] * newbound.minz + matrix[14]
),
Point (
matrix[0] * newbound.maxx + matrix[4] * newbound.maxy + matrix[8] * newbound.maxz + matrix[12],
matrix[1] * newbound.maxx + matrix[5] * newbound.maxy + matrix[9] * newbound.maxz + matrix[13],
matrix[2] * newbound.maxx + matrix[6] * newbound.maxy + matrix[10] * newbound.maxz + matrix[14]
)
};
for (int i = 0; i < 8; ++i) {
expand(p[i]);
}
}
};
ostream& operator<< (ostream& o, const Bound& bound) {
return o << bound.minx << ' ' << bound.maxx << ' ' << bound.miny << ' ' << bound.maxy << ' ' << bound.minz << ' ' << bound.maxz;
}
istream& operator>> (istream& i, Bound& bound) {
i >> bound.minx >> bound.maxx >> bound.miny >> bound.maxy >> bound.minz >> bound.maxz;
return i;
}
//////////////////////////////////////////////////
// Globals
//////////////////////////////////////////////////
ColourCode ColourCodes[512];
bool doRaytrace = false;
bool doLines = false;
bool doStudLogo = false;
bool doDRA = true;
string ldrawdir;
string l2ribdir;
string cachedir;
string colorcfg;
float bgcolor[3] = {1.0, 1.0, 1.0};
float camDistance = 1.5;
Point cameraFrom(-1, -1, -1);
Point cameraTo(0, 0, 0);
Point cameraUp(0, -1, 0);
float floorScale = 0;
vector<Point> lightPositions;
vector<float> lightColours;
vector<float> lightIntensities;
vector<string> lightShadowTypes;
int pixelSamples = 2;
float shadingRate = 1.0;
time_t programTime;
bool usecache = true;
int formatX = 640;
int formatY = 480;
int shadowFormat = 1024;
// MPD processing
bool doMPD = false;
set<string> mpdNames;
hash_map<string, Bound> mpdBounds;
const string searchpath[] = {
"p" PATHSEP "48",
"p",
"parts",
"models"
};
//////////////////////////////////////////////////
bool parseFile(ostream& out, ifstream& in, const string& partname, Bound& bound);
bool fileExists(string filename, bool checkTime=false) {
struct stat buf;
if (stat(filename.c_str(), &buf) == 0) {
if (checkTime) {
// check the time stamp on the file. If it's older than
// the app runtime, we assume the file does not exist
// and needs to be clobbered
return (buf.st_mtime >= programTime);
}
return true;
} else {
return false;
}
}
//////////////////////////////////////////////////
// String handling
//////////////////////////////////////////////////
string tokenize(string& s)
{
string::size_type length = s.length();
if (length == 0)
return s;
string::size_type start;
// Ignore leading space
for (start = 0; start < length; ++start) {
if (!isspace(s[start])) {
break;
}
}
for (string::size_type i = start + 1; i < length; ++i) {
if (isspace(s[i])) {
string retval = s.substr(start, i - start);
s = s.substr(i);
return retval;
}
}
string retval = s.substr(start, length - start);
s = "";
return retval;
}
bool isWhitespaceString(const string& s) {
string::size_type length = s.length();
if (length == 0)
return true;
string::size_type start;
for (start = 0; start < length; ++start) {
if (!isspace(s[start])) {
return false;
}
}
return true;
}
bool isNumericString(const string& s) {
for (string::const_iterator i = s.begin(); i != s.end(); i++)
if (!isdigit(*i))
return false;
return true;
}
bool isFloatString(const string& s) {
float t;
return (sscanf(s.c_str(), "%f", &t) == 1);
}
string fixFileName(const string& s)
{
string retval(s);
REMOVE_SPACES(retval);
REMOVE_CRS(retval);
for (string::iterator i = retval.begin(); i != retval.end(); i++) {
if (*i == '\\')
*i = '/';
else if (*i == ' ')
*i = '_';
else
*i = tolower(*i);
}
return retval;
}
// Fix file names when they get written to RIB
string fixRIBFileName(const string& s)
{
string retval;
for (string::const_iterator i = s.begin(); i != s.end(); i++) {
if (*i == '\\')
retval += "/";
else
retval += tolower(*i);
}
return retval;
}
//////////////////////////////////////////////////
// Command handling
//////////////////////////////////////////////////
// Stores bounds in memory to avoid going to disk
map<string, Bound> boundsMap;
void getBound(Bound& bound, const string& filename, const string& partname) {
map<string, Bound>::const_iterator i = boundsMap.find(partname);
if (i != boundsMap.end()) {
bound = i->second;
} else {
// We have to read the first line from the file
ifstream in(filename.c_str());
if (!in) {
cerr << "Unable to read " << filename << " for bounds computation\n";
return;
}
string line;
getline(in, line);
if (tokenize(line) == "Bound") {
istringstream lineStream(line.c_str());
lineStream >> bound;
bound.init = true;
boundsMap[partname] = bound;
} else {
cerr << "Unable to read bound from " << filename << "\n";
}
}
}
void parseColour(string line) {
string command, value;
ColourCode c;
int code = -1;
bool edgevalid = false;
c.name = tokenize(line);
do {
command = tokenize(line);
if (command.empty()) break;
if (command == "CODE") {
value = tokenize(line);
if (value.empty()) break;
code = atoi(value.c_str());
}
else if (command == "VALUE") {
value = tokenize(line);
if (value.empty()) break;
// Parse hex
unsigned int r, g, b;
if (sscanf(value.c_str(), "#%2x%2x%2x\n", &r, &g, &b) != 3 && sscanf(value.c_str(), "0x%2x%2x%2x\n", &r, &g, &b) != 3) break;
c.r = r / 255.0f;
c.g = g / 255.0f;
c.b = b / 255.0f;
}
else if (command == "LUMINANCE") {
// currently ignored
value = tokenize(line);
if (value.empty()) break;
}
else if (command == "EDGE") {
value = tokenize(line);
if (value.empty()) break;
edgevalid = true;
// Check whether the edge looks like a code
if (isNumericString(value)) {
c.edgecode = atoi(value.c_str());
} else {
// Parse hex
unsigned int r, g, b;
if (sscanf(value.c_str(), "#%2x%2x%2x\n", &r, &g, &b) != 3 && sscanf(value.c_str(), "0x%2x%2x%2x\n", &r, &g, &b) != 3) break;
c.edger = r / 255.0f;
c.edgeg = g / 255.0f;
c.edgeb = b / 255.0f;
}
}
else if (command == "ALPHA") {
value = tokenize(line);
if (value.empty()) break;
c.transparent = true;
c.transparency = atoi(value.c_str()) / 255.0f;
c.shader = 3;
}
else if (command == "CHROME" || command == "METALLIC" || command == "MATTE_METALLIC") {
// okay, these really should be differentiated.
c.shader = 2;
}
else if (command == "RUBBER") {
c.shader = 4;
}
else if (command == "PEARLESCENT") {
// not yet implemented
}
else if (command == "MATERIAL") {
// Parse the rest of the line as a "shader" definition
c.custom = true;
c.customShader = line;
break;
}
} while (!command.empty());
if (code != -1 && code < 512) {
c.init = true;
// If we never saw an edge, synthesize one
if (!edgevalid) {
c.edger = 1.0f - c.r;
c.edgeg = 1.0f - c.g;
c.edgeb = 1.0f - c.b;
}
ColourCodes[code] = c;
}
}
void writeColour(ostream& out, string colour) {
if (isNumericString(colour)) {
int colIndex = atoi(colour.c_str());
if (colIndex < 0 || colIndex >= 512) {
cerr << "Invalid color: " << colIndex << '\n';
out << "Color 1.0 0.0 0.0\n";
out << "Attribute \"user\" \"uniform color l2ribEdgeColor\" [0.0 1.0 1.0]\n";
return;
}
if (colIndex == 16) {
// Inherit the previous color/opacity, so don't output
// anything and the stack will take care of it.
return;
}
if (colIndex == 24) {
// Because we're using ReadArchives we can't just write
// out a color - this would bake a reversed color into the
// RIB and if the RIB gets read by another file later on
// the color would be wrong. The solution is to use a
// shader which examines the user attribute "edgecolor".
out << "Surface \"edgeConstant\"\n";
return;
}
ColourCode& c = ColourCodes[colIndex];
if (!c.init) {
// Synthesize a dithered color if we can
if (colIndex > 256) {
int colIndexA, colIndexB;
colIndexA = (colIndex - 256) % 16;
colIndexB = (colIndex - 256) / 16;
ColourCode&a = ColourCodes[colIndexA];
ColourCode&b = ColourCodes[colIndexB];
if (a.init && b.init) {
c.name = "Dither of " + a.name + " and " + b.name;
c.r = 0.5 * (a.r + b.r);
c.g = 0.5 * (a.g + b.g);
c.b = 0.5 * (a.b + b.b);
if (a.transparent || b.transparent) {
c.transparent = true;
}
c.init = true;
cerr << "Warning: unknown color code " << colIndex << ", will use a dither of " << a.name << " and " << b.name << "\n";
} else {
if (!c.warned) {
cerr << "Warning: unknown color code " << colIndex << " encountered, and unable to use dither\n";
c.warned = true;
}
out << "# Unknown color code: " << colIndex << "\n";
out << "Color 1.0 0.0 0.0\n";
out << "Attribute \"user\" \"uniform color l2ribEdgeColor\" [0.0 1.0 1.0]\n";
return;
}
} else {
if (!c.warned) {
cerr << "Warning: unknown color code " << colIndex << " encountered\n";
c.warned = true;
}
out << "# Unknown color code: " << colIndex << "\n";
out << "Color 1.0 0.0 0.0\n";
out << "Attribute \"user\" \"uniform color l2ribEdgeColor\" [0.0 1.0 1.0]\n";
return;
}
}
out << "# " << c.name << "\n";
if (c.custom) {
out << "# custom material\n";
out << c.customShader << "\n";
return;
}
if (c.transparent) {
out << "Opacity " << c.transparency << ' ' << c.transparency << ' ' << c.transparency << '\n';
}
out << "Color " << c.r << ' ' << c.g << ' ' << c.b << '\n';
out << "Attribute \"user\" \"uniform color l2ribEdgeColor\" [" << c.edger << ' ' << c.edgeg << ' ' << c.edgeb << "]\n";
switch (c.shader) {
case 0:
break;
case 1:
out << "Surface \"plastic\" \"Ks\" [0.8]\n";
break;
case 2:
out << "Surface \"metal\"\n";
break;
case 3:
out << "Surface \"glass\" \"Kd\" [0.3] \"uniform float eta\" [1.33] \"uniform float refrraysamples\" [3] \n";
break;
case 4:
// Matte seems as good as anything for rubber
out << "Surface \"matte\"\n";
break;
}
} else {
// MLCad and L3P extended color syntax. From the description
// on L3P's home page.
unsigned hex;
if (sscanf(colour.c_str(), "%x", &hex) == 1) {
// L3P color syntax
if (hex & 0x02000000) {
out << "Color " << ((hex & 0x00FF0000) >> 16) / 255.0f << ' ' << ((hex & 0x0000FF00) >> 8) / 255.0f << ' ' << (hex & 0x000000FF) / 255.0f << '\n';
out << "Opacity 1 1 1\n";
} else if (hex & 0x03000000) {
out << "Color " << ((hex & 0x00FF0000) >> 16) / 255.0f << ' ' << ((hex & 0x0000FF00) >> 8) / 255.0f << ' ' << (hex & 0x000000FF) / 255.0f << '\n';
out << "Opacity 0.5 0.5 0.5\n";
}
out << "Surface \"plastic\"\n";
}
else {
cerr << "Unknown colour " << colour << "\n";
}
}
}
void writeMatrix(ostream& out, float* matrix) {
out << "ConcatTransform [ "
<< matrix[0] << ' ' << matrix[1] << ' ' << matrix[2] << ' ' << matrix[3] << ' '
<< matrix[4] << ' ' << matrix[5] << ' ' << matrix[6] << ' ' << matrix[7] << ' '
<< matrix[8] << ' ' << matrix[9] << ' ' << matrix[10] << ' ' << matrix[11] << ' '
<< matrix[12] << ' ' << matrix[13] << ' ' << matrix[14] << ' ' << matrix[15] << "]\n";
}
void insertPart(ostream& out, string colour, float* matrix, const string& partname, Bound& bound) {
string realfile;
bool found = false;
bool isPart = false;
bool isMPD = false;
// Handle the special case of p/48/part.dat by ignoring the 48
// initially; we'll defer to the search path instead. This gives
// us a chance to insert RIB substitutions instead
string realpart;
if (partname.length() > 3 && partname.substr(0, 2) == "48" && (partname[2] == '\\' || partname[2] == '/')) {
realpart = partname.substr(3, string::npos);
} else {
realpart = partname;
}
// Check for MPD file
if (mpdNames.find(partname) != mpdNames.end()) {
found = true;
isMPD = true;
}
// Look for file in search path
if (!found) {
for (int i = 0; i < 4; ++i) {
realfile = ldrawdir + PATHSEP + searchpath[i] + PATHSEP + realpart;
if (fileExists(realfile)) {
found = true;
isPart = (i == 1);
break;
}
}
}
// Look in current directory
if (!found) {
realfile = realpart;
found = fileExists(realfile);
}
if (!found) {
cerr << "Unable to open file for part: " << realpart << '\n';
return;
}
out << "AttributeBegin\n";
#if 0
if (isPart) {
out << "Scale 0.95 0.95 0.95\n";
}
#endif
out << "Attribute \"identifier\" \"string name\" [\"" << realpart << "\"]\n";
out << "IfBegin \"$user:l2ribPass == 'main'\"\n";
writeColour(out, colour);
out << "ElseIf \"$user:l2ribPass == 'shadow'\"\n";
out << "Surface \"null\"\n";
out << "IfEnd\n";
writeMatrix(out, matrix);
if (isMPD) {
string ribname = partname;
ribname.replace(ribname.length() - 3, 3, "rib");
// Look for the MPD part bound. If the MPD part bound is
// itself incomplete, we'll have to set our own bound to
// incomplete as well.
hash_map<string,Bound>::iterator mBi = mpdBounds.find(ribname);
if (mBi != mpdBounds.end()) {
Bound& mpdbound = mBi->second;
if (!mpdbound.init || mpdbound.mpdincomplete) {
bound.mpdincomplete = true;
out << "ReadArchive \"" << ribname << "\"\n";
} else {
if (doDRA) {
out << "Procedural \"DelayedReadArchive\" [\"" << ribname << "\"] [" << mpdbound << "]\n";
} else {
out << "ReadArchive \"" << ribname << "\"\n";
}
bound.expand(mpdbound, matrix);
}
} else {
// Can't find it at all. Probably a forward part reference
bound.mpdincomplete = true;
out << "ReadArchive \"" << ribname << "\"\n";
}
}
else {
Bound newbound;
ifstream in(realfile.c_str());
if (!in) {
cerr << "Unable to open file: " << realfile << '\n';
return;
}
parseFile(out, in, realpart, newbound);
// The newbound needs to be transformed by the matrix, then
// expands the old bound. Thankfully, the only time we need
// to do a matrix multiply.
bound.expand(newbound, matrix);
}
out << "AttributeEnd\n";
}
void drawBound(ostream &out, const string& colour, Bound &bound) {
out << "AttributeBegin\n";
// Lines should never be visible to raytracing
out << "Attribute \"visibility\" \"int trace\" [0] \"string transmission\" [\"transparent\"]\n";
writeColour(out, colour);
out << "Procedural \"DynamicLoad\" [\"line.rll\" \"12 1 ";
out << bound.minx << ' ' << bound.miny << ' ' << bound.minz << ' '
<< bound.minx << ' ' << bound.maxy << ' ' << bound.minz << ' '
<< bound.minx << ' ' << bound.maxy << ' ' << bound.minz << ' '
<< bound.maxx << ' ' << bound.maxy << ' ' << bound.minz << ' '
<< bound.maxx << ' ' << bound.maxy << ' ' << bound.minz << ' '
<< bound.maxx << ' ' << bound.miny << ' ' << bound.minz << ' '
<< bound.maxx << ' ' << bound.miny << ' ' << bound.minz << ' '
<< bound.minx << ' ' << bound.miny << ' ' << bound.minz << ' '
<< bound.minx << ' ' << bound.miny << ' ' << bound.maxz << ' '
<< bound.minx << ' ' << bound.maxy << ' ' << bound.maxz << ' '
<< bound.minx << ' ' << bound.maxy << ' ' << bound.maxz << ' '
<< bound.maxx << ' ' << bound.maxy << ' ' << bound.maxz << ' '
<< bound.maxx << ' ' << bound.maxy << ' ' << bound.maxz << ' '
<< bound.maxx << ' ' << bound.miny << ' ' << bound.maxz << ' '
<< bound.maxx << ' ' << bound.miny << ' ' << bound.maxz << ' '
<< bound.minx << ' ' << bound.miny << ' ' << bound.maxz << ' '
<< bound.minx << ' ' << bound.miny << ' ' << bound.minz << ' '
<< bound.minx << ' ' << bound.miny << ' ' << bound.maxz << ' '
<< bound.maxx << ' ' << bound.miny << ' ' << bound.minz << ' '
<< bound.maxx << ' ' << bound.miny << ' ' << bound.maxz << ' '
<< bound.minx << ' ' << bound.maxy << ' ' << bound.minz << ' '
<< bound.minx << ' ' << bound.maxy << ' ' << bound.maxz << ' '
<< bound.maxx << ' ' << bound.maxy << ' ' << bound.minz << ' '
<< bound.maxx << ' ' << bound.maxy << ' ' << bound.maxz;
out << "\"] [ " << bound << "]\n";
out << "AttributeEnd\n";
}
void drawLines(ostream& out, const string& colour, vector<Point>& points, bool doOptionalLines) {
Bound bound;
out << "IfBegin \"$user:l2ribLines == 1\"\n";
out << "AttributeBegin\n";
out << "Attribute \"visibility\" \"int trace\" [0] \"string transmission\" [\"transparent\"]\n";
writeColour(out, colour);
if (doOptionalLines) {
out << "Procedural \"DynamicLoad\" [\"line.rll\" \"" << (points.size() / 4) << " 2";
} else {
out << "Procedural \"DynamicLoad\" [\"line.rll\" \"" << (points.size() / 2) << " 1";
}
for (vector<Point>::const_iterator i = points.begin(); i != points.end(); ++i) {
bound.expand(*i);
out << ' ' << *i;
}
out << "\"] [ " << bound << "]\n";
out << "AttributeEnd\n";
out << "IfEnd\n";
points.clear();
}
void drawPolys(ostream& out, const string& colour, vector<int>& polySizes, vector<Point>& polyPoints) {
int total = 0;
out << "AttributeBegin\n";
writeColour(out, colour);
out << "PointsPolygons [";
vector<Point>::const_iterator k = polyPoints.begin();
for (vector<int>::const_iterator i = polySizes.begin(); i != polySizes.end(); ++i) {
out << ' ' << *i;
total += *i;
}
out << "]\n[";
for (int j = 0; j < total; ++j) {
out << ' ' << j;
}
out << "]\n\"P\" [";
for (k = polyPoints.begin(); k != polyPoints.end(); ++k) {
out << ' ' << *k;
}
out << "]\n";
out << "AttributeEnd\n";
polySizes.clear();
polyPoints.clear();
}
bool isBowtie(const Point& p1, const Point& p2, const Point& p3, const Point& p4) {
// This is not the most optimal algorithm.
// Assume that we're somewhat coplanar, and not colinear. First,
// figure out the plane which goes through the first 3 points.
// v1 = normalize(p2 - p1)
Point v1 = p2 - p1;
float length = sqrt(v1.x * v1.x + v1.y * v1.y + v1.z * v1.z);
v1.x /= length; v1.y /= length; v1.z /= length;
// v2 = normalize(p3 - p1)
Point v2 = p3 - p1;
length = sqrt(v2.x * v2.x + v2.y * v2.y + v2.z * v2.z);
v2.x /= length; v2.y /= length; v2.z /= length;
// n = v1 x v2 (n of the plane going through p1, p2, p3)
Point n(v1.y*v2.z - v1.z*v2.y, v1.z*v2.x - v1.x*v2.z, v1.x*v2.y - v1.y*v2.x);
// Project p4 - p0 onto the normal vector starting at p0 and add
// from p4 in order to project p4 onto the plane to get 4 fully
// coplanar points
float dotP = (p4.x - p1.x) * n.x + (p4.y - p1.y) * n.y + (p4.z - p1.z) * n.z;
Point p4p(p4.x - dotP * n.x, p4.y - dotP * n.y, p4.z - dotP * n.z);
// Find plane going through p1 and p3 perpendicular to original
// plane..
Point n2(n.y*v2.z - n.z*v2.y, n.z*v2.x - n.x*v2.z, n.x*v2.y - n.y*v2.x);
float d = n2.x * p1.x + n2.y * p1.y + n2.z * p1.z;
// .. and finally check whether p2 and p4 are on same side of this
// new plane; if so we are a bowtie quad.
return (((n2.x * p2.x + n2.y * p2.y + n2.z * p2.z) > d) == ((n2.x * p4p.x + n2.y * p4p.y + n2.z * p4p.z) > d));
}
void mpdScan(ifstream &in) {
string line;
while (in && in.peek() != EOF) {
getline(in, line);
string token = tokenize(line);
if (!token.empty() && isNumericString(token) && atoi(token.c_str()) == 0) {
token = tokenize(line);
if (token == "FILE") {
if (!doMPD) {
// This is the first FILE command
// encountered. Ignore the command because it'll
// be the main file, but set our MPD processing
// flag
doMPD = true;
} else {
// Add the file name to the list
string filename = fixFileName(line);
mpdNames.insert(filename);
}
}
}
}
}
void mpdSkipFirstFile(ifstream &in) {
string line;
while (in && in.peek() != EOF) {
getline(in, line);
string token = tokenize(line);
if (!token.empty() && isNumericString(token) && atoi(token.c_str()) == 0) {
token = tokenize(line);
if (token == "FILE")
return;
}
}
}
string mpdGetNextFileName(ifstream &in) {
string line;
while (in && in.peek() != EOF) {
getline(in, line);
string token = tokenize(line);
if (!token.empty() && isNumericString(token) && atoi(token.c_str()) == 0) {
token = tokenize(line);
if (token == "FILE") {
string filename = fixFileName(line);
filename.replace(filename.length() -3, 3, "rib");
return filename;
}
}
}
return "";
}
bool parseFile(ostream &out, ifstream &in, const string& partname, Bound& bound) {
if (!partname.empty()) {
string ribname = partname;
ribname.replace(ribname.length() - 3, 3, "rib");
// Check whether a prebuilt rib exists
string pfilename = l2ribdir + PATHSEP + "prebuilt" + PATHSEP + ribname;
if (fileExists(pfilename)) {
getBound(bound, pfilename, partname);
if (doDRA) {
out << "Procedural \"DelayedReadArchive\" [\"" << fixRIBFileName(ribname) << "\"] [" << bound << "]\n";
} else {
out << "ReadArchive \"" << fixRIBFileName(ribname) << "\"\n";
}
return true;
}
// Check whether a cached rib exists
string ofilename = cachedir + PATHSEP + ribname;
// Note this call may check the datestamp on the RIB file
if (fileExists(ofilename, !usecache)) {
getBound(bound, ofilename, partname);
if (doDRA) {
out << "Procedural \"DelayedReadArchive\" [\"" << fixRIBFileName(ribname) << "\"] [" << bound << "]\n";
} else {
out << "ReadArchive \"" << fixRIBFileName(ribname) << "\"\n";
}
return true;
}
}
// Internal buffers
ostringstream ostr;
vector<int> polySizes;
vector<Point> polyPoints;
vector<Point> linePoints;
bool doOptionalLines = false;
string lastLineColour = "";
string lastPolyColour = "";
int i;
string line;
while (in && in.peek() != EOF) {
int curpos = in.tellg();
getline(in, line);
string token = tokenize(line);
if (!token.empty() && isNumericString(token)) {
switch(atoi(token.c_str())) {
case 0:
{
// comments, some of which masquerade as commands
// we might be interested in.
string command = tokenize(line);
// MPD extension. The outer loop handles the real logic
if (command == "FILE") {
// Rewind the file
in.seekg(curpos, ios::beg);
// And finish it up
goto endfile;
}
// Colour codes
else if (command == "!COLOUR") {
parseColour(line);
}
// Write/print
else if (command == "WRITE" || command == "PRINT") {
out << "# " << line << "\n";
}
break;
}
case 1:
{
// Flush line buffer
if (!linePoints.empty()) {
drawLines(ostr, lastLineColour, linePoints, doOptionalLines);
lastLineColour = "";
}
// Flush poly buffer