-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvformat.cpp
More file actions
890 lines (700 loc) · 22.1 KB
/
vformat.cpp
File metadata and controls
890 lines (700 loc) · 22.1 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
//############################################################################
//## ##
//## VFORMAT.CPP ##
//## ##
//## Defines a LightMapVertex, which is a vertex with two U/V channels. ##
//## Also defines a class to organize a triangle soup into an ordered mesh ##
//## ##
//## OpenSourced 12/5/2000 by John W. Ratcliff ##
//## ##
//## No warranty expressed or implied. ##
//## ##
//## Part of the Q3BSP project, which converts a Quake 3 BSP file into a ##
//## polygon mesh. ##
//############################################################################
//## ##
//## Contact John W. Ratcliff at jratcliff@verant.com ##
//############################################################################
#include "q3shader.h"
#include "vformat.h"
#include "stb_image.h"
#include "stb_image_write.h"
// does the file exists ?
bool ExistsFile(const char *path)
{
FILE *f = fopen(path,"rb");
if (!f)
return false;
fclose(f);
return true;
}
// check basename for texture, and try to return jpg or png file
void CheckTexture(const char *baseName, String &textureFileName)
{
const char *ext = strrchr(baseName,'.');
// check if jpg exists
textureFileName=baseName;
if (!ext) {
textureFileName+=".jpg";
if (ExistsFile(textureFileName.c_str()))
return;
}
// check if png exists
textureFileName=baseName;
if (ext) // erase ext
textureFileName.erase(textureFileName.size()-strlen(ext),strlen(ext));
textureFileName+=".png";
if (ExistsFile(textureFileName.c_str()))
return;
// check if tga exists
textureFileName=baseName;
if (ext) // erase ext
textureFileName.erase(textureFileName.size()-strlen(ext),strlen(ext));
textureFileName+=".tga";
if (ExistsFile(textureFileName.c_str())) {
// convert tga to png
String outFileName=baseName;
if (ext) // erase ext
outFileName.erase(outFileName.size()-strlen(ext),strlen(ext));
outFileName+=".png";
// bmp.TGAToPNG(textureFileName.c_str(),outFileName.c_str());
int width, height, comp;
unsigned char *data = stbi_load(textureFileName.c_str(), &width, &height, &comp , 0);
stbi_write_png(outFileName.c_str(), width, height, comp, data, width*comp);
stbi_image_free(data);
textureFileName = outFileName;
return;
}
// check if jpg exists
textureFileName=baseName;
if (ext) // erase ext
textureFileName.erase(textureFileName.size()-strlen(ext),strlen(ext));
textureFileName+=".jpg";
if (ExistsFile(textureFileName.c_str()))
return;
printf("Texture file not found:'%s'\n",baseName);
}
LightMapVertex VertexLess::mFind;
VertexVector * VertexLess::mList=0;
bool VertexLess::operator()(int v1,int v2) const
{
const LightMapVertex& a = Get(v1);
const LightMapVertex& b = Get(v2);
if ( a.GetX() < b.GetX() ) return true;
if ( a.GetX() > b.GetX() ) return false;
if ( a.GetY() < b.GetY() ) return true;
if ( a.GetY() > b.GetY() ) return false;
if ( a.GetZ() < b.GetZ() ) return true;
if ( a.GetZ() > b.GetZ() ) return false;
if ( a.mTexel1.x < b.mTexel1.x ) return true;
if ( a.mTexel1.x > b.mTexel1.x ) return false;
if ( a.mTexel1.y < b.mTexel1.y ) return true;
if ( a.mTexel1.y > b.mTexel1.y ) return false;
if ( a.mTexel2.x < b.mTexel2.x ) return true;
if ( a.mTexel2.x > b.mTexel2.x ) return false;
if ( a.mTexel2.y < b.mTexel2.y ) return true;
if ( a.mTexel2.y > b.mTexel2.y ) return false;
return false;
};
VertexMesh::~VertexMesh(void)
{
VertexSectionMap::iterator i;
for (i=mSections.begin(); i!=mSections.end(); ++i)
{
VertexSection *section = (*i).second;
delete section;
}
}
void VertexMesh::AddTri(const StringRef &name,const LightMapVertex &v1,const LightMapVertex &v2,const LightMapVertex &v3)
{
VertexSection *section;
if ( name == mLastName )
{
section = mLastSection;
}
else
{
VertexSectionMap::iterator found;
found = mSections.find( name );
if ( found != mSections.end() )
{
section = mLastSection = (*found).second;
mLastName = name;
}
else
{
mLastSection = section = new VertexSection( name );
mSections[name] = section;
mLastName = name;
}
}
assert( section );
section->AddTri( v1, v2, v3 );
mBound.MinMax( v1.mPos );
mBound.MinMax( v2.mPos );
mBound.MinMax( v3.mPos );
}
void VertexSection::AddTri(const LightMapVertex &v1,
const LightMapVertex &v2,
const LightMapVertex &v3)
{
mBound.MinMax(v1.mPos);
mBound.MinMax(v2.mPos);
mBound.MinMax(v3.mPos);
AddPoint(v1);
AddPoint(v2);
AddPoint(v3);
}
void VertexSection::AddPoint(const LightMapVertex &p)
{
unsigned short idx = (unsigned short)mPoints.GetVertex(p);
mIndices.push_back(idx);
};
void VertexMesh::SaveVRML2(
FILE *fph,
VFormatOptions &options) const
{
if ( mSections.size() )
{
if ( fph )
{
//fprintf(fph,"#VRML V2.0 utf8 generated by QBSP \n");
if (mSections.size() >0) {
fprintf(fph,"Group {\n");
fprintf(fph,"children [\n");
}
VertexSectionMap::const_iterator i;
for (i=mSections.begin(); i!=mSections.end(); ++i)
{
(*i).second->SaveVRML2(fph,options);
}
if (mSections.size() >0) {
fprintf(fph,"\n]\n}\n");
}
}
}
}
void VertexMesh::SaveVRML(const String &name, // base file name
bool tex1) const // texture channel 1=(true)
{
if ( mSections.size() )
{
String oname = name+".wrl";
FILE *fph = fopen(oname.c_str(),"wb");
if ( fph )
{
fprintf(fph,"#VRML V1.0 ascii\n");
fprintf(fph,"Separator {\n");
fprintf(fph," ShapeHints {\n");
fprintf(fph," shapeType SOLID\n");
fprintf(fph," vertexOrdering COUNTERCLOCKWISE\n");
fprintf(fph," faceType CONVEX\n");
fprintf(fph," }\n");
VertexSectionMap::const_iterator i;
for (i=mSections.begin(); i!=mSections.end(); ++i)
{
(*i).second->SaveVRML(fph,tex1);
}
fprintf(fph,"}\n");
fclose(fph);
}
}
}
void VertexSection::SaveVRML(FILE *fph,bool tex1)
{
// save it into a VRML file!
static int itemcount=1;
fprintf(fph,"DEF item%d Separator {\n",itemcount++);
fprintf(fph,"Translation { translation 0 0 0 }\n");
fprintf(fph,"Material {\n");
fprintf(fph," ambientColor 0.1791 0.06536 0.06536\n");
fprintf(fph," diffuseColor 0.5373 0.1961 0.1961\n");
fprintf(fph," specularColor 0.9 0.9 0.9\n");
fprintf(fph," shininess 0.25\n");
fprintf(fph," transparency 0\n");
fprintf(fph,"}\n");
fprintf(fph,"Texture2 {\n");
const char *foo = mName;
char scratch[256];
if ( !tex1 )
{
while ( *foo && *foo != '+' ) foo++;
char *dest = scratch;
if ( *foo == '+' )
{
foo++;
while ( *foo ) *dest++ = *foo++;
}
*dest = 0;
}
else
{
char *dest = scratch;
while ( *foo && *foo != '+' ) *dest++ = *foo++;
*dest = 0;
}
if ( tex1 )
fprintf(fph," filename %c%s.tga%c\n",0x22,scratch,0x22);
else
fprintf(fph," filename %c%s.bmp%c\n",0x22,scratch,0x22);
fprintf(fph,"}\n");
mPoints.SaveVRML(fph,tex1);
int tcount = mIndices.size()/3;
if ( 1 )
{
fprintf(fph," IndexedFaceSet {\ncoordIndex [\n");
UShortVector::iterator j= mIndices.begin();
for (int i=0; i<tcount; i++)
{
int i1 = *j;
j++;
int i2 = *j;
j++;
int i3 = *j;
j++;
if ( i == (tcount-1) )
fprintf(fph," %d, %d, %d, -1]\n",i1,i2,i3);
else
fprintf(fph," %d, %d, %d, -1,\n",i1,i2,i3);
}
}
if ( 1 )
{
fprintf(fph," textureCoordIndex [\n");
UShortVector::iterator j= mIndices.begin();
for (int i=0; i<tcount; i++)
{
int i1 = *j;
j++;
int i2 = *j;
j++;
int i3 = *j;
j++;
if ( i == (tcount-1) )
fprintf(fph," %d, %d, %d, -1]\n",i1,i2,i3);
else
fprintf(fph," %d, %d, %d, -1,\n",i1,i2,i3);
}
}
fprintf(fph," }\n");
fprintf(fph,"}\n");
};
void VertexPool::SaveVRML(FILE *fph,bool tex1)
{
if ( 1 )
{
fprintf(fph," Coordinate3 {\npoint [\n");
int count = mVtxs.size();
for (int i=0; i<count; i++)
{
const LightMapVertex &vtx = mVtxs[i];
if ( i == (count-1) )
fprintf(fph," %f %f %f\n]\n",vtx.mPos.x,vtx.mPos.y,vtx.mPos.z);
else
fprintf(fph," %f %f %f,\n",vtx.mPos.x,vtx.mPos.y,vtx.mPos.z);
}
fprintf(fph," }\n");
}
if ( 1 )
{
fprintf(fph," TextureCoordinate2 {\npoint [\n");
int count = mVtxs.size();
for (int i=0; i<count; i++)
{
const LightMapVertex &vtx = mVtxs[i];
if ( !tex1 ) // if saving second U/V channel.
{
if ( i == (count-1) )
fprintf(fph," %f %f\n]\n",vtx.mTexel2.x,1.0f-vtx.mTexel2.y);
else
fprintf(fph," %f %f,\n",vtx.mTexel2.x,1.0f-vtx.mTexel2.y);
}
else
{
if ( i == (count-1) )
fprintf(fph," %f %f\n]\n",vtx.mTexel1.x,1.0f-vtx.mTexel1.y);
else
fprintf(fph," %f %f,\n",vtx.mTexel1.x,1.0f-vtx.mTexel1.y);
}
}
fprintf(fph," }\n");
}
}
// write a single VRML ImageTexture node
void WriteImageTexture(FILE *fph,const StringRef &name,
VFormatOptions &options,bool lightmap, bool clamp= false)
{
// texture node already defined ?
TextureDefMap::iterator found;
found = options.textureDefMap.find(name);
if ( found != options.textureDefMap.end()) { // simply use it
fprintf(fph,"USE %s\n",(const char *)(*found).second );
}
else
{ // need to define new imageTexture node
char buf[60];
sprintf(buf,"_T%d",options.textureCount);
options.textureCount++;
fprintf(fph,"DEF %s ImageTexture {\n",(const char *)buf);
if (clamp) fprintf(fph,"\trepeatS FALSE repeatT FALSE\n");
const char *ext="bmp";
if (options.usePng)
ext = "png";
if (!lightmap) {
String textureFileName;
// check and return jpg, png ..
CheckTexture(name, textureFileName);
fprintf(fph," url %c%s%c\n",0x22,textureFileName.c_str(),0x22);
} else
fprintf(fph," url %c%s.%s%c\n",0x22,name.Get(),ext,0x22);
fprintf(fph,"}\n");
// insert new node into map
options.textureDefMap.insert(TextureDefMap::value_type(name,buf));
}
}
// save it into a VRML 2 file
void VertexSection::SaveVRML2(FILE *fph,VFormatOptions &options)
{
static int itemcount=1;
bool hasLightMap = true;
int numStages=2;
int lightMapStage = 0; // place lightmap coord into this texture stage
bool vertexColor = true;
//lightMapStage=1;
// name of lightmap texture
StringRef lightMap;
// parse mName into base texture + light map
const char *foo = mName;
char scratch[256];
char *dest = scratch;
while ( *foo && *foo != '+' ) *dest++ = *foo++;
if (*foo == '+' && foo[1]) {
lightMap = foo+1;
hasLightMap = true;
}
else {
hasLightMap = false;
numStages = 1;
lightMapStage = -1;
}
*dest = 0;
StringRef name(scratch);
if (mShader) {
numStages =mShader->GetNumStages();
if (mShader->mNoLightMap) {
hasLightMap = false;
lightMapStage = -1;
} else {
lightMapStage=mShader->mLightMapStage;
}
//if (rgbGen == Identity)
// vertexColor = false;
} else {
}
//fprintf(fph,"DEF item%d Shape {\n",itemcount++);
fprintf(fph,"Shape {\n");
fprintf(fph,"appearance ");
// appearance node already defined ?
AppearanceDefMap::iterator found;
found = options.appearanceDefMap.find(mName);
if ( found != options.appearanceDefMap.end()) { // simply use it
//found.r
fprintf(fph,"USE %s\n",(const char *)(*found).second);
}
else
{ // need to define new Appearance node
if (options.verbose) {
fprintf(fph," #%s\n",(const char *) mName);
if (mShader) {
fprintf(fph," #shader %s\n",(const char *) mShader->GetName());
}
}
fprintf(fph,"DEF A%d Appearance {\n",options.appearanceCount);
char buf[60];
sprintf(buf,"_A%d",options.appearanceCount);
// insert new node into map
options.textureDefMap.insert(AppearanceDefMap::value_type(StringRef(mName),StringRef(buf)));
options.appearanceCount++;
if (options.useLighting) {
fprintf(fph,"material Material {\n");
fprintf(fph," ambientIntensity 0.06536 \n");
fprintf(fph," diffuseColor 0.5373 0.1961 0.1961\n");
fprintf(fph," specularColor 0.9 0.9 0.9\n");
fprintf(fph," shininess 0.25\n");
//fprintf(fph," transparency 0\n");
fprintf(fph,"}\n");
}
else {
if (options.useMat) {
if (!options.matDefined) {
fprintf(fph,"material DEF MAT Material {\n");
//fprintf(fph," diffuseColor 0 0 0\n");
fprintf(fph," diffuseColor 1 1 1\n");
//fprintf(fph," emissiveColor 0.5 0.5 0.5\n"); // overall brightness
fprintf(fph," emissiveColor 0.2 0.2 0.2\n"); // overall brightness
fprintf(fph,"}\n");
options.matDefined = true;
} else
fprintf(fph,"material USE MAT \n");
}
}
fprintf(fph,"texture ");
bool tex1 = options.tex1;
if (options.useMultiTexturing) {
fprintf(fph,"MultiTexture {\nmaterialColor TRUE texture [\n");
tex1 = true;
if (mShader) {
hasLightMap = false;
numStages =mShader->GetNumStages();
for (int i=0; i<numStages; i++) {
//if (i>0 && numStages >2) // we can do only 2 stages
// i=numStages-1;
const ShaderStage& stage = mShader->GetStage(i);
if (stage.isAnimMap) {
if (options.useEffects) {
fprintf(fph,"DEF MAP AnimMap {\nfrequency %f textures [\n",stage.animMapFrequency);
for (unsigned int i=0; i<stage.animMap.size(); i++)
WriteImageTexture(fph,stage.animMap[i],options,false,stage.clamp);
//fprintf(fph,"] ROUTE TIMER.fraction_changed TO MAP.set_fraction \n");
fprintf(fph,"]ROUTE TIMER.time_changed TO MAP.set_time\n");
fprintf(fph,"}");
}else
WriteImageTexture(fph,stage.animMap[0],options,false,stage.clamp);
} else
if (stage.isLightMap) {
WriteImageTexture(fph,lightMap,options,true,stage.clamp);
hasLightMap = true;
mShader->mLightMapStage=lightMapStage=i;
} else
WriteImageTexture(fph,stage.map,options,false,stage.clamp);
}
}
else
{ // no shader
//WriteImageTexture(fph,name,options,false);
if (options.useMultiTexturing) {
if (hasLightMap) {
WriteImageTexture(fph,lightMap,options,true,false);
}
}
WriteImageTexture(fph,name,options,false);
}
}
else {
// not useMultiTexturing
if (tex1) {
if (mShader)
mShader ->GetBaseTexture(name);
WriteImageTexture(fph,name,options,false);
} else {
if (hasLightMap) {
WriteImageTexture(fph,lightMap,options,true,false);
}
else WriteImageTexture(fph,"nomap",options,true,false);
}
}
if (options.useMultiTexturing) {
fprintf(fph,"]");
// add makes it to bright
// with modulate currently to dark
//if (numStages>1)
if (mShader) {
fprintf(fph,"mode [ ");
bool hasTcMod = false;
for (int i=0; i<numStages; i++) {
//if (i>0 && numStages >2) // we can do only 2 stages
// i=numStages-1;
const ShaderStage& stage = mShader->GetStage(i);
const char *mode = stage.textureBlendMode;
if (stage.tcmod.length()>0) hasTcMod = true;
if (stage.tcmodOk.length()>0) hasTcMod = true;
if (i==0 && stage.textureBlendMode == String("ADD") && !stage.isLightMap)
mode = "MODULATE";
if (!mode) {
if (i==0 && !options.useMat)
mode = "REPLACE";
}
fprintf(fph,"\"%s\" ", mode ? mode : "ADD");
fprintf(fph,"# blend %s %s \n",(const char *) stage.blendFuncSrc,(const char *) stage.blendFuncDst);
}
fprintf(fph,"]\n");
// tcmod
if (hasTcMod) {
fprintf(fph,"textureTransform [ ");
for (int i=0; i<numStages; i++) {
//if (i>0 && numStages >2) // we can do only 2 stages
// i=numStages-1;
const ShaderStage& stage = mShader->GetStage(i);
const char *mode = stage.tcmod.c_str();
const char *modeOk = stage.tcmodOk.c_str();
if (stage.tcmod.length()>0 || stage.tcmodOk.length()>0 ) {
fprintf(fph,"DEF TC%d TcMod {\n",i);
if (stage.tcmodOk.length()>0)
fprintf(fph,"\t%s\n", modeOk);
if (stage.tcmod.length()>0)
fprintf(fph,"\tmode \"%s\" \n",mode ? mode : "" );
//fprintf(fph,"ROUTE TIMER.fraction_changed TO TC%d.set_fraction \n",i);
fprintf(fph,"ROUTE TIMER.time_changed TO TC%d.set_time\n",i);
fprintf(fph,"}\n");
}
else fprintf(fph,"NULL\n");
}
fprintf(fph,"]\n");
}
}
else {
if (hasLightMap)
fprintf(fph, "%s", options.blendMode);
else ; // MODULATE
}
fprintf(fph,"}");
} // multi texture
fprintf(fph,"}\n");
}
int tcount = mIndices.size()/3;
// write the indexed face set
if ( 1 )
{
fprintf(fph,"geometry IndexedFaceSet {\n");
fprintf(fph,"\tccw FALSE creaseAngle 3.14\n");
if (mShader && (mShader->mCull == StringRef("none") || mShader->mCull == StringRef("disable"))) {
fprintf(fph,"\tsolid FALSE\n");
}
if (mShader && (mShader->mCull == StringRef("back"))) {
fprintf(fph,"\tccw TRUE\n");
}
fprintf(fph,"\tcoordIndex [\n");
UShortVector::iterator j= mIndices.begin();
for (int i=0; i<tcount; i++)
{
int i1 = *j;
j++;
int i2 = *j;
j++;
int i3 = *j;
j++;
if ( i == (tcount-1) )
fprintf(fph,"\t%d,%d,%d,-1]\n",i1,i2,i3);
else
fprintf(fph,"\t%d,%d,%d,-1,\n",i1,i2,i3);
}
}
if ( 0 ) // if texCoord index == ccordIndex no need to export
{
fprintf(fph," textureCoordIndex [\n");
UShortVector::iterator j= mIndices.begin();
for (int i=0; i<tcount; i++)
{
int i1 = *j;
j++;
int i2 = *j;
j++;
int i3 = *j;
j++;
if ( i == (tcount-1) )
fprintf(fph,"\t%d,%d,%d,-1]\n",i1,i2,i3);
else
fprintf(fph,"\t%d,%d,%d,-1,\n",i1,i2,i3);
}
}
mPoints.SaveVRML2(fph,lightMapStage,options);
fprintf(fph," }\n");
fprintf(fph,"}\n");
};
void VertexPool::SaveVRML2(FILE *fph, int lightMapStage, VFormatOptions &options)
{
if ( 1 )
{
fprintf(fph,"coord Coordinate {\npoint [\n\t");
int count = mVtxs.size();
for (int i=0; i<count; i++)
{
const LightMapVertex &vtx = mVtxs[i];
if (i>0) {
fprintf(fph,",\n\t");
}
if (options.yzFlip)
fprintf(fph,options.VFORMAT,vtx.mPos.x,vtx.mPos.z,vtx.mPos.y);
else fprintf(fph,options.VFORMAT,vtx.mPos.x,vtx.mPos.y,vtx.mPos.z);
}
fprintf(fph,"\n]\n}\n");
}
if (options.useMultiTexturing) { // PROPOSAL MultiTextureCoodinate
fprintf(fph,"texCoord MultiTextureCoordinate {\ncoord [\n\t");
int count = mVtxs.size();
// channel 0
fprintf(fph,"\tTextureCoordinate {\npoint [\n\t");
for (int i=0; i<count; i++)
{
const LightMapVertex &vtx = mVtxs[i];
if (i>0) {
if ( (i%4) == 0) fprintf(fph,",\n\t");
else fprintf(fph,",");
}
if (lightMapStage == 0)
fprintf(fph,options.TFORMAT,vtx.mTexel2.x,1.0f-vtx.mTexel2.y);
else fprintf(fph,options.TFORMAT,vtx.mTexel1.x,1.0f-vtx.mTexel1.y);
}
fprintf(fph,"\n]\n}\n");
//if (lightMapStage == 1)
{
// channel 1
fprintf(fph,"\tTextureCoordinate {\npoint [\n\t");
for (int i=0; i<count; i++)
{
const LightMapVertex &vtx = mVtxs[i];
if (i>0) {
if ( (i%4) == 0) fprintf(fph,",\n\t");
else fprintf(fph,",");
}
if (lightMapStage == 1)
fprintf(fph,options.TFORMAT,vtx.mTexel2.x,1.0f-vtx.mTexel2.y);
else fprintf(fph,options.TFORMAT,vtx.mTexel1.x,1.0f-vtx.mTexel1.y);
}
fprintf(fph,"\n\t]\n}\n");
}
fprintf(fph,"\n]\n}\n");
} else
if ( options.noTextureCoordinates )
{
fprintf(fph,"texCoord TextureCoordinate {\npoint [\n\t");
int count = mVtxs.size();
for (int i=0; i<count; i++)
{
const LightMapVertex &vtx = mVtxs[i];
if (i>0) {
if ( (i%4) == 0) fprintf(fph,",\n\t");
else fprintf(fph,",");
}
if ( !options.tex1 ) // if saving second U/V channel.
{
fprintf(fph,options.TFORMAT,vtx.mTexel2.x,1.0f-vtx.mTexel2.y);
}
else
{
fprintf(fph,options.TFORMAT,vtx.mTexel1.x,1.0f-vtx.mTexel1.y);
}
}
fprintf(fph,"\n]\n}\n");
}
//if (options.useVertexColor)
{
int count = mVtxs.size();
// channel 0
fprintf(fph,"\tcolor Color {\ncolor [\n\t");
for (int i=0; i<count; i++)
{
const LightMapVertex &vtx = mVtxs[i];
if (i>0) {
if ( (i%4) == 0) fprintf(fph,",\n\t");
else fprintf(fph,",");
}
fprintf(fph,options.CFORMAT,vtx.mColor.x,vtx.mColor.y,vtx.mColor.z);
}
fprintf(fph,"\n]\n}\n");
}
}