lsWriter.C
37.4 KB
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
/******************************************************************************\ Copyright 1995 The University of North Carolina at Chapel Hill.
All Rights Reserved.
Permission to use, copy, modify and distribute this software and its
documentation for educational, research and non-profit purposes, without
fee, and without a written agreement is hereby granted, provided that the
above copyright notice and the following three paragraphs appear in all
copies.
IN NO EVENT SHALL THE UNIVERSITY OF NORTH CAROLINA
AT CHAPEL HILL BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE
UNIVERSITY OF NORTH CAROLINA HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
THE UNIVERSITY OF NORTH CAROLINA SPECIFICALLY DISCLAIM ANY WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HERUNDER IS
ON AN "AS IS" BASIS, AND THE UNIVERSITY OF NORTH CAROLINA HAS NO OBLIGATIONS
TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
The author may be contacted via:
US Mail: Mike Goslin
Department of Computer Science
Sitterson Hall, CB #3175
University of N. Carolina
Chapel Hill, NC 27599-3175
Phone: (919)962-1719
EMail: goslin@cs.unc.edu
*******************************************************************************/
/*******************************************************************************
*
* FILENAME: lsWriter.C
* CLASS: LsWriter
* DESCRIPTION: Lightscape preparation file writer class
* AUTHOR: Mike Goslin
* CREATED: 3/17/95
* REVISIONS:
*
*******************************************************************************/
#include <stdlib.h>
#include <iostream.h>
#include <math.h>
#include <string.h>
#include <unistd.h>
#include "fileWriter.H"
/*******************************************************************************
* Function: Indent
* Access: Public
* Description:
* Input:
* Output:
*******************************************************************************/
void LsWriter::indent(void)
{
int i;
for (i=0; i<travType; i++)
fprintf(file, "\t");
}
/*******************************************************************************
* Function: Constructor
* Class: LsWriter
* Access: Public
* Description:
* Input:
* Output:
*******************************************************************************/
LsWriter::LsWriter(DataBase *_dbase)
{
file = NULL;
strcpy(fname, "DEFAULT");
dbase = _dbase;
attribList = NULL;
vtxpool = NULL;
texList = NULL;
minExt[0] = minExt[1] = minExt[2] = INFINITY;
maxExt[0] = maxExt[1] = maxExt[2] = 0.0;
textureInfo = NULL;
}
/*******************************************************************************
* Function: writeDefaults
* Class: LsWriter
* Access: Private
* Description: Writes Lightscape Defaults section
* Input:
* Output:
*******************************************************************************/
void LsWriter::writeDefaults(void)
{
fprintf(file, "\nLightscape Defaults (V4)\n");
fprintf(file, "Units : Meters\n"); // All units must be in meters
fprintf(file, "Lighting Units : AS\n");
fprintf(file, "Time Units : Seconds\n");
fprintf(file, "Ambient : 100 Off\n");
fprintf(file, "Brightness : 50\n");
fprintf(file, "Contrast : 50\n");
fprintf(file, "Ray Offset : 0.02\n");
fprintf(file, "Length Tolerance : 0.001\n");
fprintf(file, "Receiver Size Bounds : 0.2 2\n");
fprintf(file, "Source Size Bounds : 0.2 2\n");
fprintf(file, "Receiver Contrast Threshold : 0.6\n");
fprintf(file, "Source Accuracy : 0.6\n");
fprintf(file, "Allow Solution Changes : On\n");
fprintf(file, "Speed : 1\n");
fprintf(file, "Background : 0 0 0\n");
}
/*******************************************************************************
* Function: writeView
* Class: LsWriter
* Access: Private
* Description: Writes Lightscape View section
* Input:
* Output:
*******************************************************************************/
void LsWriter::writeView(void)
{
double center[3];
center[0] = (maxExt[0] + minExt[0])/2.0;
center[1] = (maxExt[1] + minExt[1])/2.0;
center[2] = (maxExt[2] + minExt[2])/2.0;
fprintf(file, "\nLightscape View (V1)\n");
fprintf(file, "projection: top\n");
fprintf(file, "foreshortening ratio: 0\n");
fprintf(file, "intersection angle: 0\n");
fprintf(file, "aspect zoom size: 1.11173 1 18.648\n");
fprintf(file, "center: %lf, %lf, %lf\n", center[0], center[1], center[2]);
fprintf(file, "a i t: 0, 0, 0\n");
fprintf(file, "window: -10.1736 10.1736 -10.1736 10.1736\n");
fprintf(file, "clip: -10.1736 10.1736\n");
}
/*******************************************************************************
* Function: writeNaturalLight
* Class: LsWriter
* Access: Private
* Description: Writes Natural Light Information section
* Input:
* Output:
*******************************************************************************/
void LsWriter::writeNaturalLight(void)
{
fprintf(file, "\nLightscape Natural Light Version 1, Release 0\n");
fprintf(file, "\nNatural light is: Off\n");
fprintf(file, "In/Out: In\n");
fprintf(file, "North: 0\n");
fprintf(file, "Latitude: 41\n");
fprintf(file, "Longitude: 74\n");
fprintf(file, "Day: 172\n");
fprintf(file, "Daylight Savings Time: Yes\n");
fprintf(file, "Local Time: 10\n");
fprintf(file, "Time Zone: 1.309\n");
fprintf(file, "Sky Cover: 0\n");
fprintf(file, "Sun Color: 1 0.95 0.9\n");
fprintf(file, "Sky Color: 0.95 0.95 1\n");
}
/*******************************************************************************
* Function: writeTextureTable
* Class: LsWriter
* Access: Private
* Description: Writes Texture Table section
* Input:
* Output:
*******************************************************************************/
void LsWriter::writeTextureTable(void)
{
FILE *texatt=NULL;
TextureBead *temptex;
mgTextureRef tref;
mgTextureAttr tatt;
int writedefaults=0;
char attpath[256];
int minfilter=TEXTURE_MIN_POINT, magfilter=TEXTURE_MAG_POINT;
TexInfo *tinfo;
fprintf(file, "\nLightscape Texture Table (V2)\n");
fprintf(file, "\nTexture Table:\n");
temptex = texList;
// this list stores scale sizes in u and v for each texture
tinfo = textureInfo;
while (temptex != NULL)
{
tref = temptex->fltStruct();
strcpy(attpath, tref.file);
strcat(attpath, ".attr");
if ((texatt = fopen(attpath, "r")) == NULL)
{
fprintf(stderr, "Unable to open texture attribute file: %s\n",
attpath);
fprintf(stderr, "Printing default attribute values\n");
writedefaults = 1;
}
else if (fread(&tatt, sizeof(mgTextureAttr), 1, texatt) <= 0)
{
fprintf(stderr, "Unable to read att structure from file: %s\n",
attpath);
fprintf(stderr, "Printing default attribute values\n");
writedefaults = 1;
}
fprintf(file, "\tTexture: %s\n", tref.file);
int flags = 0x0000;
if (writedefaults)
{
fprintf(file, "\t\tFlags: 0x%x\n", flags);
fprintf(file, "\t\tSize: 1 1\n");
fprintf(file, "\t\tFilters: 0 0\n");
}
else
{
// If .attr file was read, use the size indicated in that file
flags |= TEXTURE_ABSOLUTE_SIZE_BIT;
fprintf(file, "\t\tFlags: 0x%x\n", flags);
long size1=1, size2=1;
#ifdef USE_ATTRIBUTE_FILE
if (tatt.rwsizeu > 0) size1 = tatt.rwsizeu;
if (tatt.rwsizev > 0) size2 = tatt.rwsizev;
#else
if (tinfo->avgsizeU > 0)
size1 = tinfo->avgsizeU/(double)tinfo->number;
if (tinfo->avgsizeV > 0)
size2 = tinfo->avgsizeV/(double)tinfo->number;
#endif
fprintf(file, "\t\tSize: %ld %ld\n", size1, size2);
switch (tatt.minfilter)
{
case TX_BILINEAR: minfilter = TEXTURE_MIN_LINEAR; break;
case TX_MIPMAP_POINT: minfilter = TEXTURE_MIN_POINT_MM; break;
case TX_MIPMAP_LINEAR: minfilter = TEXTURE_MIN_LINEAR_MM; break; case TX_MIPMAP_BILINEAR: minfilter = TEXTURE_MIN_BILINEAR_MM;
break;
case TX_MIPMAP_TRILINEAR: minfilter = TEXTURE_MIN_TRILINEAR_MM;
break;
default: break;
}
if (tatt.magfilter == TX_BILINEAR) magfilter = TEXTURE_MAG_LINEAR;
fprintf(file, "\t\tFilters: %d %d\n", minfilter, magfilter);
}
temptex = (TextureBead *)temptex->right();
tinfo = tinfo->next;
}
fprintf(file, "\nTable End\n");
}
/*******************************************************************************
* Function: writeAttributeTable
* Class: LsWriter
* Access: Private
* Description: Writes Attribute Table section
* Input:
* Output:
*******************************************************************************/
void LsWriter::writeAttributeTable(void)
{
attribute *attrib;
int attnum=0;
double color[3];
int flags = ATTR_DIFF_REFL;
fprintf(file, "\nLightscape Attribute Table (V3)\n");
fprintf(file, "\nAttribute Table: Local Attr DB\n");
fprintf(file, "\tAttr: .Default Attr\n");
fprintf(file, "\t\tFlags: 0x%x\n", flags);
fprintf(file, "\t\tDiffuse: 0.7 0.7 0.7\n");
fprintf(file, "\t\tSpecular: 0.7 0.7 0.7\n");
fprintf(file, "\t\tGlow: 0 0 0 0\n");
fprintf(file, "\t\tK_dr: -1\n");
fprintf(file, "\t\tK_sr: 0\n");
fprintf(file, "\t\tK_dt: -1\n");
fprintf(file, "\t\tK_st: 0\n");
fprintf(file, "\t\tRefraction Index: 1\n");
fprintf(file, "\t\tAbsorption Coeff: 0\n");
fprintf(file, "\t\tRoughness: -1\n");
fprintf(file, "\t\tTexture: _NIL\n");
attrib = attribList;
while (attrib != NULL)
{
if (convertRGB(colTable, attrib->index, color) == 0)
{
fprintf(stderr, "Unknown color index\n");
color[0] = color[1] = color[2] = 0.0;
}
else // Scale to range from 0 to 1
{
color[0] /= 255.0;
color[1] /= 255.0;
color[2] /= 255.0;
}
fprintf(file, "\tAttr: %s_%d\n", rootname, attnum++);
// Default is diffuse reflector
fprintf(file, "\t\tFlags: 0x%x\n", flags);
fprintf(file, "\t\tDiffuse: %lf %lf %lf\n", color[0],color[1],color[2]);
fprintf(file, "\t\tSpecular: 0 0 0\n");
fprintf(file, "\t\tGlow: %lf %lf %lf 0\n", color[0], color[1],color[2]);
fprintf(file, "\t\tK_dr: -1\n");
fprintf(file, "\t\tK_sr: 0\n");
fprintf(file, "\t\tK_dt: -1\n");
fprintf(file, "\t\tK_st: 0\n");
fprintf(file, "\t\tRefraction Index: 1\n");
fprintf(file, "\t\tAbsorption Coeff: 0\n");
fprintf(file, "\t\tRoughness: -1\n");
fprintf(file, "\t\tTexture: _NIL\n");
attrib = attrib->next;
}
// Now print out texture references (if any)
TextureBead *tbead;
tbead = texList;
mgTextureRef tref;
attnum = 0;
while (tbead != NULL)
{
flags |= ATTR_TEXTURED;
tref = tbead->fltStruct();
fprintf(file, "\tAttr: %s_image%d\n", rootname, attnum++);
fprintf(file, "\t\tFlags: 0x%x\n", flags);
fprintf(file, "\t\tDiffuse: 1.0 1.0 1.0\n");
fprintf(file, "\t\tSpecular: 0 0 0\n");
fprintf(file, "\t\tGlow: 1.0 1.0 1.0\n");
fprintf(file, "\t\tK_dr: -1\n");
fprintf(file, "\t\tK_sr: 0\n");
fprintf(file, "\t\tK_dt: -1\n");
fprintf(file, "\t\tK_st: 0\n");
fprintf(file, "\t\tRefraction Index: 1\n");
fprintf(file, "\t\tAbsorption Coeff: 0\n");
fprintf(file, "\t\tRoughness: -1\n");
fprintf(file, "\t\tTexture: %s\n", tref.file);
tbead = (TextureBead *)tbead->right();
}
fprintf(file, "\nTable End\n");
}
/******************************************************************************
* Function: writeLayerTable
* Class: LsWriter
* Access: Private
* Description: Writes layer table section
* Input:
* Output:
*******************************************************************************/
void LsWriter::writeLayerTable(void)
{
char *cp,shortname[80];
cp=strchr(fname,'.');
strncpy(shortname,fname,cp-fname);
shortname[cp-fname]='\0';
fprintf(file, "\nLightscape Layer Table (V1)\n");
fprintf(file, "\nLayer Table:\n");
fprintf(file, "\tLayer: %s\n",shortname);
fprintf(file, "\t\tFlags: 0x2000\n");
fprintf(file, "\t\tAttr: .Default Attr\n"); // Specify any attribute in
// attribute table
fprintf(file, "\t\tFocus Point: 0 0 0\n");
fprintf(file, "\nTable End\n");
}
/*******************************************************************************
* Function: writeSourceTable
* Class: LsWriter
* Access: Private
* Description: Writes Source Table section with a default entry for each
* source type
* Input:
* Output:
*******************************************************************************/
void LsWriter::writeSourceTable(void)
{
fprintf(file, "\nLightscape Source Table (V5)\n");
fprintf(file, "\nSource Table: Local Source DB\n");
fprintf(file, "Light Units: SI\n");
fprintf(file, "\tSource: .AreaDefault\n");
fprintf(file, "\t\tType: Area\n");
fprintf(file, "\t\tFlags: 0x2\n");
fprintf(file, "\t\tFilter: 1 1 1\n");
fprintf(file, "\t\tColor: 1 1 1\n");
fprintf(file, "\t\tlid(diffuse) { flux = 1; }\n");
fprintf(file, "\tSource: .LinearDefault\n");
fprintf(file, "\t\tType: Linear\n");
fprintf(file, "\t\tFlags: 0x2\n");
fprintf(file, "\t\tFilter: 1 1 1\n");
fprintf(file, "\t\tColor: 1 1 1\n");
fprintf(file, "\t\tlid(isotropic) { flux = 1; }\n");
fprintf(file, "\tSource: .PointDefault\n");
fprintf(file, "\t\tType: Point\n");
fprintf(file, "\t\tFlags: 0x2\n");
fprintf(file, "\t\tFilter: 1 1 1\n");
fprintf(file, "\t\tColor: 1 1 1\n");
fprintf(file, "\t\tlid(isotropic) { flux = 1; }\n");
fprintf(file, "\nTable End\n");
}
/*******************************************************************************
* Function: writeBlock
* Class: LsWriter
* Access: Private
* Description: Writes a Block record
* Input:
* Output:
*******************************************************************************/
void LsWriter::writeBlock(char *name)
{
char bname[80];
strcpy(bname, rootname);
if (name != NULL)
{
strcat(bname, "_");
strcat(bname, name);
}
// Add code to handle luminaires later
fprintf(file, "\tBlock: %s\n", bname);
fprintf(file, "\t\tFlags: 0x0\n"); // Defaults to not a luminaire
fprintf(file, "\t\tBase Point: 0 0 0\n");
fprintf(file, "\t\tFocus Point: 0 0 0\n");
}
/******************************************************************************
* Function: decodeAttribute
* Class: LsWriter
* Access: Private
* Description: Decodes a polygon color table index
* Input:
* Output:
*******************************************************************************/
short LsWriter::decodeAttribute(short color)
{
int att=0, counter=0;
attribute *ptratt;
for (ptratt = attribList; ptratt != NULL; ptratt = ptratt->next)
{
if (color == ptratt->index)
att = counter;
counter++;
}
return att;
}
/*******************************************************************************
* Function: decodeTextureAttribute
* Class: LsWriter
* Access: Private
* Description: Decodes a polygon texture index
* Input:
* Output:
*******************************************************************************/
char *LsWriter::decodeTextureAttribute(short texture, char *attribute)
{
int counter=0;
TextureBead *tbead;
for (tbead = texList; tbead != NULL; tbead = (TextureBead *)tbead->right())
{
if (texture == tbead->Index())
{
sprintf(attribute, "%s_image%d", rootname, counter);
return attribute;
}
counter++;
}
strcpy(attribute, ".Default");
fprintf(stderr, "Warning: Couldn't find texture indexed by: %d\n",
texture);
fprintf(stderr, "\tReturning .Default as attribute\n");
return NULL;
}
/*******************************************************************************
* Function: updateAvgTextureScale
* Class: LsWriter
* Access: Private
* Description: keeps a running average of u,v scale values for each texture
* Input:
* Output:
*******************************************************************************/
int LsWriter::updateAvgTextureScale(short texture, double Uscale,
double Vscale)
{
TexInfo *tinfo;
for (tinfo = textureInfo; tinfo != NULL; tinfo = tinfo->next)
{
if (texture == tinfo->index)
{
tinfo->avgsizeU += Uscale;
tinfo->avgsizeV += Vscale;
tinfo->number++;
return (1);
}
}
fprintf(stderr, "Warning: Couldn't find texture indexed by: %d\n",
texture);
return (0);
}
/*******************************************************************************
* Function: writeEntity
* Class: LsWriter
* Access: Private
* Description: Writes an Entity record
* Input:
* Output:
*******************************************************************************/
void LsWriter::writeEntity(int flags, char *attribute)
{
char fstring[12], shortname[80];
strcpy(shortname, rootname);
sprintf(fstring, "0x%x", flags);
indent();
fprintf(file, "Entity:\n");
indent();
fprintf(file, "\tFlags: %s\n", fstring);
indent();
fprintf(file, "\tLayer: %s\n",shortname); // Only one layer for now
// Only write attributes for polygons, not instances
if (attribute != NULL)
{
indent();
fprintf(file, "\tAttr: %s\n", attribute);
}
}
/*******************************************************************************
* Function: V_normalize
* Class: LsWriter
* Access: Private
* Description:
* Input:
* Output:
*******************************************************************************/
void LsWriter::V_normalize(double v[])
{
float length;
length = sqrt((v[0]*v[0]) + (v[1]*v[1]) + (v[2]*v[2]));
if (length > FUZZ_VALUE)
{
v[0] /= length;
v[1] /= length;
v[2] /= length;
}
}
/*******************************************************************************
* Function: writePolygon
* Class: LsWriter
* Access: Private
* Description: Writes a Polygon record
* Input: Triangle
* Output:
*******************************************************************************/
void LsWriter::writePolygon(double p0[3], double p1[3], double p2[3],
char drawType, int textured)
{
double vec0[3], vec1[3], norm[3];
double absx, absy, absz, D;
// this writePolygon handles triangles
int flag = POLY_TRIANGLE_BIT;
if (drawType == 1) // indicates a two-sided polygon
flag |= POLY_TWO_SIDED_BIT;
if (textured)
flag |= POLY_TEXATTR_INSTALLED;
VEC3MINUS(p2, p0, vec0);
V_normalize(vec0);
VEC3MINUS(p1, p0, vec1);
V_normalize(vec1);
CROSSPRODUCT(vec1, vec0, norm);
V_normalize(norm);
// compute the -D component of the equation of the plane
DOTPRODUCT(norm, p0, D);
// then compute which axis is largest component of normal
int largeaxis = POLY_X_AXIS;
absx = fabs(norm[0]); absy = fabs(norm[1]); absz = fabs(norm[2]);
if ((absy > absx) && (absy > absz)) largeaxis = POLY_Y_AXIS;
else if ((absz > absx) && (absz > absy)) largeaxis = POLY_Z_AXIS;
flag |= largeaxis;
indent();
fprintf(file, "\tPolygon:\n");
indent();
fprintf(file, "\t\tFlags: 0x%x\n", flag);
indent();
fprintf(file, "\t\tSub Factor: 1\n");
indent();
fprintf(file, "\t\tPnt1: %lf %lf %lf\n", p0[0], p0[1], p0[2]);
indent();
fprintf(file, "\t\tPnt2: %lf %lf %lf\n", p1[0], p1[1], p1[2]);
indent();
fprintf(file, "\t\tPnt3: %lf %lf %lf\n", p2[0], p2[1], p2[2]);
indent();
fprintf(file, "\t\tPlane EQ: %lf %lf %lf %lf\n", norm[0], norm[1],
norm[2], -D);
}
/*******************************************************************************
* Function: writePolygon
* Class: LsWriter
* Access: Private
* Description: Writes a Polygon record
* Input: Quadrilateral
* Output:
*******************************************************************************/
void LsWriter::writePolygon(double p0[3], double p1[3], double p2[3],
double p3[3], char drawType, int textured)
{
double vec0[3], vec1[3], norm[3];
double absx, absy, absz, D;
// this writePolygon handles quads
int flag = 0x0000;
if (drawType == 1) // indicates a two-sided polygon
flag |= POLY_TWO_SIDED_BIT;
if (textured)
flag |= POLY_TEXATTR_INSTALLED;
VEC3MINUS(p2, p0, vec0);
V_normalize(vec0);
VEC3MINUS(p1, p0, vec1);
V_normalize(vec1);
CROSSPRODUCT(vec1, vec0, norm);
V_normalize(norm);
// compute the -D component of the equation of the plane
DOTPRODUCT(norm, p0, D);
// then compute which axis is largest component of normal
int largeaxis = POLY_X_AXIS;
absx = fabs(norm[0]); absy = fabs(norm[1]); absz = fabs(norm[2]);
if ((absy > absx) && (absy > absz)) largeaxis = POLY_Y_AXIS;
else if ((absz > absx) && (absz > absy)) largeaxis = POLY_Z_AXIS;
flag |= largeaxis;
indent();
fprintf(file, "\tPolygon:\n");
indent();
fprintf(file, "\t\tFlags: 0x%x\n", flag);
indent();
fprintf(file, "\t\tSub Factor: 1\n");
indent();
fprintf(file, "\t\tPnt1: %lf %lf %lf\n", p0[0], p0[1], p0[2]);
indent();
fprintf(file, "\t\tPnt2: %lf %lf %lf\n", p1[0], p1[1], p1[2]);
indent();
fprintf(file, "\t\tPnt3: %lf %lf %lf\n", p2[0], p2[1], p2[2]);
indent();
fprintf(file, "\t\tPnt4: %lf %lf %lf\n", p3[0], p3[1], p3[2]);
indent();
fprintf(file, "\t\tPlane EQ: %lf %lf %lf %lf\n", norm[0], norm[1],
norm[2], -D);
}
/*******************************************************************************
* Function: writeInstance
* Class: LsWriter
* Access: Private
* Description: Writes an instance record
* Input:
* Output:
*******************************************************************************/
void LsWriter::writeInstance(char *blockname)
{
char bname[80];
strcpy(bname, rootname);
if (blockname != NULL)
{
strcat(bname, "_");
strcat(bname, blockname);
}
indent();
fprintf(file, "\tInstance:\n");
indent();
fprintf(file, "\t\tFlags: 0x0\n"); // could be TRACE_DIRECT 0x0010 or
// NO_SHADOWS 0x0040
indent();
fprintf(file, "\t\tBlock: %s\n", bname);
indent();
fprintf(file, "\t\tMatrix: 1 0 0 0\n");
indent();
fprintf(file, "\t 0 1 0 0\n");
indent();
fprintf(file, "\t 0 0 1 0\n");
indent();
fprintf(file, "\t 0 0 0 1\n");
}
/*******************************************************************************
* Function: writeHierarchyBlock
* Class: LsWriter
* Access: Private
* Description: Allows preservation of hierarchy structure in blocks
* Input:
* Output:
*******************************************************************************/
void LsWriter::writeGroupBlock(Bead *bead)
{
Bead *temp;
char id[32];
temp = bead;
writeBlock(((GroupBead *)bead)->Name());
if (temp->down() != NULL)
{
temp = temp->down();
while (temp != NULL)
{
if (temp->Type() == MG_GROUP)
strcpy(id, ((GroupBead *)temp)->Name());
else
strcpy(id, ((ObjectBead *)temp)->Name());
writeEntity(ENTITY_INSTANCE);
writeInstance(id);
temp = temp->right();
}
}
}
/*******************************************************************************
* Function: verifyPolygon
* Class: LsWriter
* Access: Private
* Description: Checks for degenerate polygons
* Input:
* Output:
*******************************************************************************/
uint LsWriter::verifyPolygon(double vertices[MAX_VERTICES_PER_POLYGON][3],
int vtxcount)
{
int i, j, distinctverts=2*vtxcount;
double x0, x1, y0, y1, z0, z1;
if (vtxcount < 3)
{
fprintf(stderr, "\tDiscarding degenerate polygon: Only <%d> vertices\n",
vtxcount);
return 0;
}
for (i=0; i<vtxcount; i++)
{
for (j=0; j<vtxcount; j++)
{
if (j != i)
{
x0 = vertices[i][0]; x1 = vertices[j][0];
y0 = vertices[i][1]; y1 = vertices[j][1];
z0 = vertices[i][2]; z1 = vertices[j][2];
if ((fabs(x0-x1) < FUZZ_VALUE) && (fabs(y0-y1) < FUZZ_VALUE) &&
(fabs(z0-z1) < FUZZ_VALUE))
distinctverts--;
}
}
}
distinctverts /= 2;
if (distinctverts < 3)
{
fprintf(stderr, "\tDiscarding degenerate polygon: <%d> vertices\n",
vtxcount-distinctverts);
fprintf(stderr, "\tare duplicated:\n");
for (i=0; i<vtxcount; i++)
fprintf(stderr, "\t\tvertex[%d] = %lf %lf %lf\n", i, vertices[i][0],
vertices[i][1], vertices[i][2]);
return 0;
}
return 1;
}
/*******************************************************************************
* Function: computeTexCoords
* Class: LsWriter
* Access: Private
* Description: Computes the texture orienation vector for a polygon
* Input:
* Output:
*******************************************************************************/
void LsWriter::computeTexCoords(double verts[MAX_VERTICES_PER_POLYGON][3],
float tex[MAX_VERTICES_PER_POLYGON][2],
int vtxcount, double *v0, double *v1,
double& Uscale, double& Vscale)
{
#define EPSILON 1e-6
double pA[3], pB[3];
double tA[2], tB[2];
double detA, detAInv, alpha, beta;
VEC3MINUS(verts[1], verts[0], pA);
VEC2MINUS(tex[1], tex[0], tA);
VEC3MINUS(verts[vtxcount-1], verts[0], pB);
VEC2MINUS(tex[vtxcount-1], tex[0], tB);
detA = tA[0] * tB[1] - tA[1] * tB[0];
if (fabs(detA) < EPSILON)
{
fprintf(stderr, "ERROR: collinear points in computeTexCoords\n");
return;
}
detAInv = 1.0 / detA;
alpha = (tex[0][1] * tB[0] - tex[0][0] * tB[1]) * detAInv;
beta = (tex[0][0] * tA[1] - tex[0][1] * tA[0]) * detAInv;
// first output point
v0[0] = verts[0][0] + alpha * pA[0] + beta * pB[0];
v0[1] = verts[0][1] + alpha * pA[1] + beta * pB[1];
v0[2] = verts[0][2] + alpha * pA[2] + beta * pB[2];
alpha = ((1 - tex[0][0]) * tB[1] + tex[0][1] * tB[0]) * detAInv;
beta = ((tex[0][0] - 1) * tA[1] - tex[0][1] * tB[0]) * detAInv;
// second output point
v1[0] = verts[0][0] + alpha * pA[0] + beta * pB[0];
v1[1] = verts[0][1] + alpha * pA[1] + beta * pB[1];
v1[2] = verts[0][2] + alpha * pA[2] + beta * pB[2];
// compute the texture scale for size field in texture table
double v2[3], dist[3];
alpha = ((tex[0][1] - 1) * tB[0] - tex[0][0] * tB[1]) * detAInv;
beta = ((1 - tex[0][1]) * tA[0] + tex[0][0] * tA[1]) * detAInv;
v2[0] = verts[0][0] + alpha * pA[0] + beta * pB[0];
v2[1] = verts[0][1] + alpha * pA[1] + beta * pB[1];
v2[2] = verts[0][2] + alpha * pA[2] + beta * pB[2];
VEC3MINUS(v1, v0, dist);
DOTPRODUCT(dist, dist, Uscale);
Uscale = sqrt(Uscale);
VEC3MINUS(v2, v0, dist);
DOTPRODUCT(dist, dist, Vscale);
Vscale = sqrt(Vscale);
}
/*******************************************************************************
* Function: writeTextureOrientation
* Class: LsWriter
* Access: Private
* Description:
* Input:
* Output:
*******************************************************************************/
void LsWriter::writeTextureOrientation(double v0[3], double v1[3])
{
fprintf(file, "\t\t\t\tTexture Orientation:\n");
fprintf(file, "\t\t\t\t\tFlags: 0x0\n");
fprintf(file, "\t\t\t\t\tv0: %lf %lf %lf\n", v0[0], v0[1], v0[2]);
fprintf(file, "\t\t\t\t\tv1: %lf %lf %lf\n", v1[0], v1[1], v1[2]);
}
/*******************************************************************************
* Function: procBead
* Class: LsWriter
* Access: Private
* Description: Process bead and write appropriate data to .lp file
* Input:
* Output:
*******************************************************************************/
void LsWriter::procBead(Bead *bead)
{
size_t size=512;
char path[512];
switch (bead->Type())
{
case MG_HEADER:
if (travType == BLOCK_TRAVERSAL)
{
// write signature
fprintf(file, "\nLightscape Prep (V4)\n");
// write model file name and complete path
getcwd(path, size);
strcat(path, "/");
strcat(path, fname);
fprintf(file, "\nModel File: %s\n", path);
writeDefaults();
writeView();
// write model extents
fprintf(file, "\nMinExtents: %f %f %f\n", minExt[0],
minExt[1], minExt[2]);
fprintf(file, "MaxExtents: %lf %lf %lf\n", maxExt[0],
maxExt[1], maxExt[2]);
writeNaturalLight();
writeTextureTable();
writeAttributeTable();
writeLayerTable();
writeSourceTable();
// write block table header
fprintf(file, "\nLightscape Block Table (V4)\n");
fprintf(file, "\nBlock Table:\n");
}
break;
case MG_COLOR_TABLE:
if (travType == ATTRIBUTE_TRAVERSAL)
colTable = ((ColorTableBead *)bead)->fltStruct();
break;
case MG_MATERIAL_TABLE:
break;
case MG_TEXTURE_REF:
if (travType == ATTRIBUTE_TRAVERSAL)
{
texList = (TextureBead *)bead;
// create auxiliary texture data list for u,v avg scale values
TextureBead *tbead;
TexInfo *tinfo, *tcurrent;
for (tbead = texList; tbead != NULL;
tbead = (TextureBead *)tbead->right())
{
tinfo = (TexInfo *)malloc(sizeof(TexInfo));
tinfo->index = tbead->Index();
tinfo->number = 0;
tinfo->avgsizeU = 0;
tinfo->avgsizeV = 0;
tinfo->next = NULL;
if (textureInfo == NULL)
{
textureInfo = tinfo;
tcurrent = textureInfo;
}
else
{
tcurrent->next = tinfo;
tcurrent = tinfo;
}
}
}
break;
case MG_VTXTABLE:
if (vtxpool == NULL)
vtxpool = (VertexTableBead *)bead;
if (travType == BLOCK_TRAVERSAL)
{
Bead *temp = bead;
if (temp->down() != NULL)
{
char *rt = NULL;
writeBlock(rt);
temp = temp->down();
while (temp != NULL)
{
writeEntity(ENTITY_INSTANCE);
writeInstance(((GroupBead *)temp)->Name());
temp = temp->right();
}
}
}
break;
case MG_GROUP:
if (travType == BLOCK_TRAVERSAL)
writeGroupBlock(bead);
break;
case MG_LOD:
case MG_LODFLOAT:
case MG_DOF:
break;
case MG_OBJECT:
if (travType == BLOCK_TRAVERSAL)
{
mgObject obj = ((ObjectBead *)bead)->fltStruct();
writeBlock(obj.id);
}
break;
case MG_POLYGON:
attribute *tempAtt, *prev;
int found=0;
mgPolygon poly = ((PolygonBead *)bead)->fltStruct();
if (travType == ATTRIBUTE_TRAVERSAL)
{
tempAtt = attribList;
while ((tempAtt != NULL) && (!found))
{
if (tempAtt->index == poly.color1)
found = 1;
prev = tempAtt;
tempAtt = tempAtt->next;
}
if (!found) // tempAtt == NULL
{
tempAtt = (attribute *)malloc(sizeof(attribute));
tempAtt->index = poly.color1;
tempAtt->next = NULL;
if (attribList == NULL)
attribList = tempAtt;
else prev->next = tempAtt;
}
}
break;
case MG_VTXLIST:
{
int vtxcount, i;
double vertices[MAX_VERTICES_PER_POLYGON][3];
float texcoords[MAX_VERTICES_PER_POLYGON][2];
double point[3];
VertexOffsetBead *voffset;
VertexRecordBead *vrecord;
mgVertList vlist;
int polyTextured=0;
char texatt[80];
double texV0[3], texV1[3], s1, s2;
if (travType == ATTRIBUTE_TRAVERSAL)
{
// determine parameters of the vertex list
vlist = ((VertexListBead *)bead)->fltStruct();
vtxcount = (vlist.length - 4)/4;
// move pointer to first offset
voffset = (VertexOffsetBead *)bead->right();
// obtain vertex points for each offset
for (i=0; i<vtxcount; i++)
{
vrecord = vtxpool->getVertex(*voffset);
if (vrecord != NULL)
{
vrecord->getPoint(vertices[i]);
polyTextured = vrecord->getUV(texcoords[i]);
}
VEC3COPY(point, vertices[i]);
// Update global bounding box extents
if (point[0] < minExt[0]) minExt[0] = point[0];
if (point[1] < minExt[1]) minExt[1] = point[1];
if (point[2] < minExt[2]) minExt[2] = point[2];
if (point[0] > maxExt[0]) maxExt[0] = point[0];
if (point[1] > maxExt[1]) maxExt[1] = point[1];
if (point[2] > maxExt[2]) maxExt[2] = point[2];
voffset = (VertexOffsetBead *)voffset->right();
}
// get polygon data associated with the vertices
PolygonBead *poly = (PolygonBead *)bead->up();
mgPolygon pdata = poly->fltStruct();
// compute texture size values
if (polyTextured)
{
computeTexCoords(vertices, texcoords, vtxcount, &texV0[0],
&texV1[0], s1, s2);
updateAvgTextureScale(pdata.texture, s1, s2);
}
}
else if (travType == BLOCK_TRAVERSAL)
{
// get polygon data associated with the vertices
PolygonBead *poly = (PolygonBead *)bead->up();
mgPolygon pdata = poly->fltStruct();
// determine parameters of the vertex list
vlist = ((VertexListBead *)bead)->fltStruct();
vtxcount = (vlist.length - 4)/4;
// move pointer to first offset
voffset = (VertexOffsetBead *)bead->right();
// obtain vertex points for each offset
for (i=0; i<vtxcount; i++)
{
vrecord = vtxpool->getVertex(*voffset);
if (vrecord != NULL)
{
vrecord->getPoint(vertices[i]);
polyTextured = vrecord->getUV(texcoords[i]);
}
voffset = (VertexOffsetBead *)voffset->right();
}
// compute texture orientation coords
if (polyTextured)
{
decodeTextureAttribute(pdata.texture, texatt);
computeTexCoords(vertices, texcoords, vtxcount, &texV0[0],
&texV1[0], s1, s2);
}
// Check for degenerate polygons first
if (verifyPolygon(vertices, vtxcount) == 1)
{
char attribute[24];
if (!polyTextured)
sprintf(attribute, "%s_%d", rootname,
decodeAttribute(pdata.color1));
if (vtxcount == 3)
{
if (polyTextured)
writeEntity(ENTITY_POLY, texatt);
else
writeEntity(ENTITY_POLY, attribute);
writePolygon(vertices[0], vertices[1], vertices[2],
pdata.drawType, polyTextured);
if (polyTextured)
writeTextureOrientation(texV0, texV1);
}
else if (vtxcount == 4)
{
if (polyTextured)
writeEntity(ENTITY_POLY, texatt);
else
writeEntity(ENTITY_POLY, attribute);
writePolygon(vertices[0], vertices[1], vertices[2],
vertices[3], pdata.drawType, polyTextured);
if (polyTextured)
writeTextureOrientation(texV0, texV1);
}
else if (vtxcount > 4)
// Lightscape assumes tris or quads only
{
for (i=1; i<vtxcount-1; i++)
{
if (polyTextured)
writeEntity(ENTITY_POLY, texatt);
else
writeEntity(ENTITY_POLY, attribute);
writePolygon(vertices[0], vertices[i],
vertices[i+1], pdata.drawType,
polyTextured);
if (polyTextured)
writeTextureOrientation(texV0, texV1);
}
}
}
}
}
break;
case MG_VTXOFFSET:
break;
default:
fprintf(stderr, "procBead: unknown opcode < %d >\n", bead->Type());
exit(1);
break;
}
}
/*******************************************************************************
* Function: traverse
* Class: LsWriter
* Access: Private
* Description: Does an in-order, depth-first traversal of the database
* Input:
* Output:
*******************************************************************************/
void LsWriter::traverse(Bead *bead)
{
Bead *next;
ushort type;
procBead(bead);
type = bead->Type();
if (bead->down() != NULL)
{
next = bead->down();
traverse(next);
}
if ((type != MG_VTXTABLE) && (type != MG_TEXTURE_REF) &&
(bead->right() != NULL))
{
next = bead->right();
traverse(next);
}
}
/*******************************************************************************
* Function: write
* Class: LsWriter
* Access: Public
* Description:
* Input:
* Output:
*******************************************************************************/
uint LsWriter::Write(char *_fname)
{
strcpy(fname, _fname);
if (openFile() == 0)
{
fprintf(stderr, "openFile: Could not open file < %s >\n", fname);
return (0);
}
rootname = strtok(_fname, ".");
dbase->reset();
// Traverse the database to construct Lightscape Attribute Table
travType = ATTRIBUTE_TRAVERSAL;
traverse(dbase->data());
// Traverse the database to construct Lightscape Block Table
travType = BLOCK_TRAVERSAL;
traverse(dbase->data());
fprintf(file, "\nTable End\n\n");
// Write out the root Entity
travType = 0;
writeEntity(ENTITY_INSTANCE);
char *rt=NULL;
writeInstance(rt);
fclose(file);
return (1);
}