-
Notifications
You must be signed in to change notification settings - Fork 0
/
q3bsp.cpp
955 lines (750 loc) · 21.3 KB
/
q3bsp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
//############################################################################
//## ##
//## Q3BSP.CPP ##
//## ##
//## Class to load Quake3 BSP files, interpret them, organize them into ##
//## a triangle mesh, and save them out in various ASCII file formats. ##
//## ##
//## 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 "q3bsp.h"
#include "q3shader.h"
#include "patch.h"
#include "fload.h"
#include "stb_image_write.h"
Quake3BSP::Quake3BSP(const StringRef &fname,
const StringRef &code)
{
mMesh = 0;
mOk = false;
mName = fname;
mCodeName = code;
mUsePng = true; // hg
String ifoo = fname;
String str = ifoo+".bsp";
// hg prefix for light map files
const char * del = strrchr(fname,'\\');
if (del) mLmPrefix = (del+1); // use file name part only
else mLmPrefix=fname.Get();
Fload data(str);
void *mem = data.GetData();
int len = data.GetLen();
if ( mem )
{
mOk = mHeader.SetHeader(mem);
if ( mOk )
{
ReadFaces(mem);
ReadElements(mem);
ReadVertices(mem);
ReadLightmaps(mem);
ReadShaders(mem);
BuildVertexBuffers();
ReadPlanes(mem);
ReadLeaves(mem);
ReadLeafSurfaces(mem);
ReadNodes(mem);
// brushes
ReadEntities(mem);
}
}
}
Quake3BSP::~Quake3BSP(void)
{
delete mMesh;
}
bool QuakeHeader::SetHeader(const void *mem) // returns true if valid quake header.
{
const int *ids = (const int *) mem;
#define BSPHEADERID (*(int*)"IBSP")
#define BSPVERSION 46
if ( ids[0] != BSPHEADERID ) return false;
if ( ids[1] != BSPVERSION ) return false;
mId = ids[0];
mVersion = ids[1];
const QuakeLump *lump = (const QuakeLump *) &ids[2];
for (int i=0; i<NUM_LUMPS; i++) mLumps[i] = *lump++;
return true;
}
void Quake3BSP::ReadShaders(const void *mem)
{
assert( mOk );
int lsize;
int lcount;
const unsigned char *smem = (const unsigned char *) mHeader.LumpInfo(Q3_SHADERREFS,mem,lsize,lcount);
mShaders.clear();
mShaders.reserve( lcount );
for (int i=0; i<lcount; i++)
{
ShaderReference s(smem);
mShaders.push_back(s);
smem+=lsize;
}
}
const void * QuakeHeader::LumpInfo(QuakeLumps lump,
const void *mem,
int &lsize,
int &lcount)
{
switch ( lump )
{
case LUMP_ENTITIES :
lsize = 1;
break;
case Q3_SHADERREFS: // LUMP_SHADERS
lsize = sizeof(dshader_t);
break;
case Q3_PLANES: // LUMP_PLANES
lsize = sizeof(float)*4; // size of a plane equations.
break;
case Q3_NODES: //LUMP_NODES
lsize = sizeof(dnode_t); // sizeof(int)*9;
break;
case Q3_LEAFS:
lsize = sizeof(dleaf_t); // sizeof(int)*12;
break;
case Q3_FACES:
lsize = sizeof(int)*26;
break;
case Q3_VERTS:
lsize = sizeof(int)*11;
break;
case Q3_LFACES:
lsize = sizeof(int);
break;
case Q3_ELEMS:
lsize = sizeof(int);
break;
case Q3_MODELS:
lsize = sizeof(int)*10;
break;
case Q3_LIGHTMAPS:
lsize = 1;
break;
case Q3_VISIBILITY:
lsize = 1;
break;
default:
assert( 0 ); // unsupported query type
}
int flen = mLumps[lump].GetFileLength();
assert( flen );
assert( (flen%lsize) == 0 ); // must be evenly divisible by lump size.
lcount = flen/lsize; // number of lump items.
const char *foo = (const char *)mem;
foo = &foo[ mLumps[lump].GetFileOffset() ];
return (const void *) foo;
}
QuakeNode::QuakeNode(const int *node)
{
const dnode_t *n = (const dnode_t*) node;
mPlane = node[0];
mLeftChild = node[1];
mRightChild = node[2];
mBound.r1.x = float( node[3] );
mBound.r1.y = float( node[4] );
mBound.r1.z = float( node[5] );
mBound.r2.x = float( node[6] );
mBound.r2.y = float( node[7] );
mBound.r2.z = float( node[8] );
}
QuakeLeaf::QuakeLeaf(const int *node)
{
const dleaf_t *n = (const dleaf_t*) node;
mCluster = node[0];
mArea = node[1];
mBound.r1.x = float( node[2] );
mBound.r1.y = float( node[3] );
mBound.r1.z = float( node[4] );
mBound.r2.x = float( node[5] );
mBound.r2.y = float( node[6] );
mBound.r2.z = float( node[7] );
mFirstFace = node[8];
mFaceCount = node[9];
mFirstUnknown = node[10];
mNumberUnknowns = node[11];
}
// read the array of planes
void Quake3BSP::ReadPlanes(const void *mem)
{
assert( mOk );
int lsize;
int lcount;
const dplane_t *planes = (const dplane_t *) mHeader.LumpInfo(Q3_PLANES,mem,lsize,lcount);
mPlanes.assign(planes,planes+lcount);
}
// read the dnode_t nodes
void Quake3BSP::ReadNodes(const void *mem)
{
assert( mOk );
int lsize;
int lcount;
const dnode_t *nodes= (const dnode_t *) mHeader.LumpInfo(Q3_NODES,mem,lsize,lcount);
mNodes.assign(nodes,nodes+lcount);
}
// read the dleaf_t nodes
void Quake3BSP::ReadLeaves(const void *mem)
{
assert( mOk );
int lsize;
int lcount;
const dleaf_t *nodes= (const dleaf_t *) mHeader.LumpInfo(Q3_LEAFS,mem,lsize,lcount);
mLeaves.assign(nodes,nodes+lcount);
assert(mLeaves.size() == lcount);
}
// read the leaf surface indices
void Quake3BSP::ReadLeafSurfaces(const void *mem)
{
assert( mOk );
int lsize;
int lcount;
const int *nodes= (const int *) mHeader.LumpInfo(Q3_LFACES,mem,lsize,lcount);
mLeafSurfaces.assign(nodes,nodes+lcount);
}
QuakeFace::QuakeFace(const int *face)
{
const dsurface_t *n = (const dsurface_t *) face;
mFrameNo = 0;
const float *fface = (const float *) face;
mShader = face[0];
mUnknown = face[1];
mType = (FaceType)face[2];
mFirstVertice = face[3];
mVcount = face[4];
mFirstElement = face[5];
mEcount = face[6];
mLightmap = face[7];
mOffsetX = face[8];
mOffsetY = face[9];
mSizeX = face[10];
mSizeY = face[11];
mOrig.x = fface[12];
mOrig.y = fface[13];
mOrig.z = fface[14];
mBound.r1.x = fface[15];
mBound.r1.y = fface[16];
mBound.r1.z = fface[17];
mBound.r2.x = fface[18];
mBound.r2.y = fface[19];
mBound.r2.z = fface[20];
mNormal.x = fface[21];
mNormal.y = fface[22];
mNormal.z = fface[23];
mControlX = face[24];
mControlY = face[25];
static int faceno=0;
if ( mType == FACETYPE_TRISURF )
{
printf("Face: %d\n",faceno++);
printf("Shader: %d\n",mShader);
printf("Unknown: %d\n",mUnknown);
printf("Type: %d\n",mType);
printf("FirstVertice: %d\n",mFirstVertice);
printf("Vcount: %d\n",mVcount);
printf("FirstElement: %d\n",mFirstElement);
printf("Ecount: %d\n",mEcount);
printf("Lightmap: %d\n",mLightmap);
printf("OffsetX: %d\n",mOffsetX);
printf("OffsetY: %d\n",mOffsetY);
printf("SizeX: %d\n",mSizeX);
printf("SizeY: %d\n",mSizeY);
printf("ControlX: %d\n",mControlX);
printf("ControlY: %d\n",mControlY);
}
}
QuakeFace::~QuakeFace(void)
{
}
QuakeVertex::QuakeVertex(const int *vert)
{
const drawVert_t *n = (const drawVert_t *) vert;
const float *fvert = (const float *) vert;
mPos.x = fvert[0];
mPos.y = fvert[1];
mPos.z = fvert[2];
mTexel1.x = fvert[3];
mTexel1.y = fvert[4];
mTexel2.x = fvert[5];
mTexel2.y = fvert[6];
mNormal.x = fvert[7];
mNormal.y = fvert[8];
mNormal.z = fvert[9];
mColor = (unsigned int) vert[10];
}
void Quake3BSP::ReadFaces(const void *mem)
{
assert( mOk );
int lsize;
int lcount;
const int *faces = (const int *) mHeader.LumpInfo(Q3_FACES,mem,lsize,lcount);
assert( lsize == sizeof(int)*26 );
mFaces.clear();
mFaces.reserve( lcount );
for (int i=0; i<lcount; i++)
{
QuakeFace f(faces);
mFaces.push_back(f);
faces+=26;
}
}
void Quake3BSP::ReadVertices(const void *mem)
{
assert( mOk );
int lsize;
int lcount;
const int *vertices = (const int *) mHeader.LumpInfo(Q3_VERTS,mem,lsize,lcount);
assert( lsize == sizeof(int)*11 );
mVertices.clear();
mVertices.reserve( lcount );
mBound.InitMinMax();
for (int i=0; i<lcount; i++)
{
QuakeVertex v(vertices);
#define RECIP (1.0f/45.0f)
v.mPos.x*=RECIP;
v.mPos.y*=-RECIP;
v.mPos.z*=RECIP;
mBound.MinMax(v.mPos);
mVertices.push_back(v);
vertices+=11;
}
}
void Quake3BSP::ReadLightmaps(const void *mem)
{
assert( mOk );
int lsize;
int lcount;
const unsigned char *maps = (const unsigned char *) mHeader.LumpInfo(Q3_LIGHTMAPS,mem,lsize,lcount);
unsigned char *lmaps = new unsigned char[lcount];
memcpy(lmaps,maps,lcount);
for (int j=0; j<lcount; j++)
{
unsigned int c = lmaps[j];
lmaps[j] = (unsigned char) c;
}
#define LMWID 128
#define LMHIT 128
#define LMSIZE (LMWID*LMHIT*3)
int texcount = lcount / LMSIZE;
unsigned char *map = lmaps;
for (int i=0; i<texcount; i++)
{
char scratch[256];
sprintf(scratch,"%slm%s%02d",mLmPrefix.Get(),mCodeName.Get(),i);
String sname = scratch;
if (mUsePng) {
String bname = sname+".png";
stbi_write_png(bname.c_str(), LMWID, LMHIT, 3, map, LMWID*3);
} else {
String bname = sname+".bmp";
stbi_write_bmp(bname.c_str(), LMWID, LMHIT, 3, map);
}
map+=LMSIZE;
}
delete lmaps;
}
void Quake3BSP::ReadElements(const void *mem)
{
assert( mOk );
int lsize;
int lcount;
// LUMP_DRAWINDEXES
const int *elements = (const int *) mHeader.LumpInfo(Q3_ELEMS,mem,lsize,lcount);
assert( lsize == sizeof(int) );
mElements.clear();
mElements.reserve( lcount );
for (int i=0; i<lcount; i++)
{
unsigned short ic = (unsigned short)(*elements);
mElements.push_back(ic);
elements++;
}
}
void QuakeVertex::Get(LightMapVertex &vtx) const
{
vtx.mPos.x = mPos.x;
vtx.mPos.y = mPos.y;
vtx.mPos.z = mPos.z;
vtx.mTexel1.x = mTexel1.x;
vtx.mTexel1.y = mTexel1.y;
vtx.mTexel2.x = mTexel2.x;
vtx.mTexel2.y = mTexel2.y;
vtx.mColor.x = float((mColor>>0) & 0xFF) / 255.0f;
vtx.mColor.y = float((mColor>>8) & 0xFF) / 255.0f;
vtx.mColor.z = float((mColor>>16) & 0xFF) / 255.0f;
}
void Quake3BSP::BuildVertexBuffers(void)
{
mMesh = 0;
mMesh = new VertexMesh;
QuakeFaceVector::iterator i;
for (i=mFaces.begin(); i!=mFaces.end(); ++i)
{
(*i).Build(mElements,mVertices,mShaders,mLmPrefix,mName,mCodeName,*mMesh);
}
}
void QuakeFace::Build(const UShortVector &elements,
const QuakeVertexVector &vertices,
ShaderReferenceVector &shaders,
const StringRef &lmPrefix,
const StringRef &sourcename,
const StringRef &code,
VertexMesh &mesh)
{
assert( mVcount < 1024 );
assert( mShader >= 0 && mShader < shaders.size() );
StringRef mat;
static StringRef oname = StringDict::gStringDict().Get("outside");
static StringRef inside = StringDict::gStringDict().Get("inside");
StringRef name = inside;
int type = 0;
LightMapVertex verts[1024];
for (int i=0; i<mVcount; i++)
{
vertices[ i+mFirstVertice ].Get(verts[i]);
}
char scratch[256];
char texname[256];
//shaders[ mShader ].GetTextureName(texname);
shaders[ mShader ].GetTextureFullName(texname); // get full name with path
/* hg why ?
if ( strcmp("toxicskytim_ctf1",texname) == 0 )
return;
*/
#if 0
if ( mLightmap < 0 )
return;
#endif
StringRef basetexture = StringDict::gStringDict().Get(texname);
QuakeShader *shader = QuakeShaderFactory::gQuakeShaderFactory().Locate(basetexture);
if ( shader )
{
//shader->GetBaseTexture(basetexture);
//printf("Shader uses texture: %s\n",basetexture);
}
else {
if ( strcmp("textures/liquids/lavahell_1000",basetexture) == 0 ) {
}
if ( strstr(basetexture,"lavahell") ) {
printf("shader for : %s\n",basetexture.Get());
}
// try to load a matching shader from disk
shaders[mShader].GetShaderFileName(scratch);
strcat(scratch,".shader");
mat = StringDict::gStringDict().Get(scratch);
if (!QuakeShaderFactory::gQuakeShaderFactory().ShaderFileLoaded(mat)
&& QuakeShaderFactory::gQuakeShaderFactory().AddShader(mat))
{
shader = QuakeShaderFactory::gQuakeShaderFactory().Locate(basetexture);
}
if (!shader) {
printf("No shader for : '%s'\n",basetexture.Get());
}
}
// geometry sorted by shader string
if ( mLightmap < 0 ) // no lightmap
sprintf(scratch,"%s+",basetexture.Get());
else
sprintf(scratch,"%s+%slm%s%02d",basetexture.Get(),lmPrefix.Get(),
code.Get(),
mLightmap);
mat = StringDict::gStringDict().Get(scratch);
char lmapspace[256];
sprintf(lmapspace,"lm%s%02d",code.Get(),mLightmap);
StringRef lmap = SGET(lmapspace);
switch ( mType )
{
case FACETYPE_NORMAL:
case FACETYPE_TRISURF:
if ( 1 )
{
assert( (mEcount%3) == 0 );
const unsigned short *idx = &elements[mFirstElement];
int tcount = mEcount/3;
for (int j=0; j<tcount; j++)
{
int i1 = idx[0];
int i2 = idx[1];
int i3 = idx[2];
mesh.AddTri(mat,verts[i1], verts[i2], verts[i3] );
idx+=3;
}
}
break;
case FACETYPE_MESH:
if ( 1 )
{
PatchSurface surface(verts,mVcount,mControlX,mControlY);
const LightMapVertex *vlist = surface.GetVerts();
const unsigned short *indices = surface.GetIndices();
int tcount = surface.GetIndiceCount()/3;
for (int j=0; j<tcount; j++)
{
int i1 = *indices++;
int i2 = *indices++;
int i3 = *indices++;
mesh.AddTri(mat,vlist[i1], vlist[i2], vlist[i3] );
}
}
break;
case FACETYPE_FLARE:
break;
default:
// assert( 0 );
break;
}
if (mesh.mLastSection && shader)
mesh.mLastSection->SetShader(shader);
}
void ShaderReference::GetTextureName(char *tname)
{
strcpy(tname,"null");
int len = strlen(mName);
if ( !len ) return;
const char *foo = &mName[len-1];
while ( *foo && *foo != '/' ) foo--;
if ( !*foo ) return;
foo++;
char *dest = tname;
while ( *foo )
{
*dest++ = *foo++;
}
*dest = 0;
}
void ShaderReference::GetShaderFileName(char *tname)
{
strcpy(tname,"");
int len = strlen(mName);
if ( !len )
return;
const char *foo = &mName[0];
while ( *foo && *foo != '/' ) foo++;
if ( !*foo ) return;
foo++;
char *dest = tname;
while ( *foo && *foo != '/' )
{
*dest++ = *foo++;
}
*dest = 0;
}
void Quake3BSP::ReadEntities(const void *mem)
{
assert( mOk );
int lsize;
int lcount;
const char *elements = (const char *) mHeader.LumpInfo(Q3_ENTITIES,mem,lsize,lcount);
assert( lsize == sizeof(char) );
mEntities.clear();
const char *estart=elements;
const char *p=estart;
int level=0;
for (int i=0; i<lcount; i++)
{
if (*p == '{') {
level++;
if (level == 1) estart = p+1;
} else if (*p == '}') {
level--;
if (level == 0) {
EntityReference *e=new EntityReference(estart,p-estart-1);
mEntities.push_back(*e);
estart = p+1;
}
}
p++;
}
}
// parse an entity
void Quake3BSP::SaveEntity(const EntityReference &entity,
FILE *f
,VFormatOptions &options
) const
{
std::string classname;
std::string targetname;
float angle = 0;
vec3_t origin;
// targetname
// model
// dmg
// spawnflags
//_color
//message
//music
ArgList args;
args.Set( entity.mBody.c_str(),false); // crunch it into arguments.
// now ready to process it as a series of arguments!
int argi=0;
while (argi < args.size())
{
String& key = args[argi];
argi++;
if (key == "classname") {
classname = args[argi];
argi++;
}
else if (key == "targetname") {
targetname = args[argi];
argi++;
}
else if (key == "origin") {
sscanf(args[argi].c_str(),"%f %f %f",&origin[0],&origin[1],&origin[2]);
options.MapVertex(origin);
argi++;
}
else if (key == "angle") {
sscanf(args[argi].c_str(),"%f",&angle);
angle -=90;
angle *= (float)Q_PI/180.0f;
argi++;
}
else {
argi += 1;
}
}
//
// info_player_intermission
if (classname == "target_position") {
fprintf(f,"DEF %s Viewpoint { position %g %g %g fieldOfView 1.0 jump TRUE orientation 0 1 0 %g\n\tdescription %c%s%c }\n",targetname.c_str(),origin[0],origin[1],origin[2],angle,'"',targetname.c_str(),'"');
}
if (classname == "info_player_deathmatch") {
fprintf(f,"Viewpoint { position %g %g %g fieldOfView 1.0 jump TRUE orientation 0 1 0 %g\n\tdescription %c%s%c }\n",origin[0],origin[1],origin[2],angle,'"',classname.c_str(),'"');
}
// worldspawn
//misc_model .. model
}
// save the entities
void Quake3BSP::SaveEntitiesVRML2(
FILE *fph,
VFormatOptions &options) const
{
if ( mEntities.size() )
{
if ( fph )
{
fprintf(fph,"DEF Entities Group {\n");
fprintf(fph,"children [\n");
EntityReferenceVector::const_iterator i;
for (i=mEntities.begin(); i!=mEntities.end(); ++i)
{
SaveEntity(*i,fph,options);
}
fprintf(fph,"]}\n");
}
}
}
void Quake3BSP::SaveNode(
int nodeNum,
FILE *fph,
VFormatOptions &options)
{
if (nodeNum<0) {
// negative numbers are -(leafs+1), not nodes
int leafNum = - (nodeNum+1);
SaveNodeBsp(&mLeaves[leafNum],fph,options);
}
else {
SaveNodeBsp(&mNodes[nodeNum],fph,options);
}
}
// save the BSP Node tree
void Quake3BSP::SaveNodesBsp(
FILE *fph,
VFormatOptions &options)
{
if (mNodes.size()>0)
SaveNodeBsp(&mNodes[0],fph,options);
}
// save a node
void Quake3BSP::SaveNodeBsp(
const dnode_t *node,
FILE *fph,
VFormatOptions &options)
{
const dplane_t *plane;
if (node->planeNum != PLANENUM_LEAF) {
plane = &mPlanes[node->planeNum];
//d = DotProduct (origin, plane->normal) - plane->dist;
// if (d >= 0) node = node->children[0];
vec3_t normal;
// is this correct ??
options.MapVector(plane->normal,normal) ;
fprintf(fph,"BspTree { plane %g %g %g %g \n",normal[0], normal[1],normal[2],plane->dist);
fprintf(fph,"front "); SaveNode(node->children[0],fph,options);
fprintf(fph,"back ");SaveNode(node->children[1],fph,options);
fprintf(fph,"}\n");
} else {
#if 0
portal_t **pp, *t;
pp = &node->portals;
while (1)
{
t = *pp;
if (!t)
break;
/*
if (t->nodes[0] == l)
pp = &t->next[0];
else if (t->nodes[1] == l)
pp = &t->next[1];
else
Error ("RemovePortalFromNode: portal not bounding leaf");
*/
}
if (!node->brushlist)
fprintf (fph,"NULL\n");
else
{
for (bb=node->brushlist ; bb ; bb=bb->next) {
//_printf ("%i ", bb->original->brushnum);
}
//_printf ("\n");
}
return;
#endif
}
}
// mm problems:
// save face references in same leaf node ??
// where are the portals ??
// save a node
void Quake3BSP::SaveNodeBsp(
const dleaf_t *node,
FILE *fph,
VFormatOptions &options)
{
// save the leaf surfaces & brushes
if (node->numLeafSurfaces == 0) {
fprintf(fph,"NULL ");
return;
}
if (node->numLeafSurfaces >1) {
//fprintf(fph,"Group { children [\n");
}
VertexMesh mesh;
// print all the surfaces
for (int i=0; i<node->numLeafSurfaces;i++) {
int surface = node->firstLeafSurface + i;
surface = mLeafSurfaces[surface];
fprintf(fph,"## surface %d \n",surface);
mFaces[surface].Build(mElements,mVertices,mShaders,mLmPrefix,mName,mCodeName,mesh);
}
mesh.SaveVRML2(fph,options);
if (node->numLeafSurfaces >1) {
//fprintf(fph,"]}");
}
}