fltWriter.C 22 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
/******************************************************************************\  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:     fltWriter.C
* CLASS:        FltWriter 
* DESCRIPTION:  MultiGen flight file writer class 
* AUTHOR:       Mike Goslin
* CREATED:      2/7/95
* REVISIONS:
*
*******************************************************************************/
#include <stdlib.h>
#include <iostream.h>
#include <math.h>
#include <string.h>

#include "fileWriter.H"

/*******************************************************************************
*     Function: Constructor 
*        Class: FltWriter
*       Access: Public 
*  Description: 
*        Input:
*       Output:
*******************************************************************************/
FltWriter::FltWriter(DataBase *_dbase) 
{
    file = NULL;
    strcpy(fname, "DEFAULT");
    dbase = _dbase;
}

/*******************************************************************************
*     Function: writeHeader 
*        Class: FltWriter
*       Access: Private
*  Description: Writes mgHeader
*        Input:
*       Output:
*******************************************************************************/
void FltWriter::writeHeader(const mgHeader& header)
{
    fwrite(&header, sizeof(header), 1, file);

/*
    fwrite(&header.opcode, sizeof(header.opcode), 1, file);
    fwrite(&header.length, sizeof(header.length), 1, file);
    fwrite(&header.id, FLT_HEADER_ID_LENGTH, 1, file);
    fwrite(&header.frev, sizeof(header.frev), 1, file);
    fwrite(&header.dbrev, sizeof(header.dbrev), 1, file);
    fwrite(&header.revDate, FLT_HEADER_REVDATE_LENGTH, 1, file);
    fwrite(&header.nextGroupId, sizeof(header.nextGroupId), 1, file);
    fwrite(&header.nextLodId, sizeof(header.nextLodId), 1, file);
    fwrite(&header.nextObjId, sizeof(header.nextObjId), 1, file);
    fwrite(&header.nextPolyId, sizeof(header.nextPolyId), 1, file);
    fwrite(&header.unitMulDiv, sizeof(header.unitMulDiv), 1, file);
    fwrite(&header.unitType, sizeof(header.unitType), 1, file);
    fwrite(&header.texWhite, sizeof(header.texWhite), 1, file);
    fwrite(&header.flags, sizeof(header.flags), 1, file);
    fwrite(&header.SWLat, sizeof(header.SWLat), 1, file);
    fwrite(&header.SWLong, sizeof(header.SWLong), 1, file);
    fwrite(&header.NELat, sizeof(header.NELat), 1, file);
    fwrite(&header.NELong, sizeof(header.NELong), 1, file);
    fwrite(&header.latOrig, sizeof(header.latOrig), 1, file);
    fwrite(&header.longOrig, sizeof(header.longOrig), 1, file);
    fwrite(&header.projType, sizeof(header.projType), 1, file);
    fwrite(&header.oswIfmt[0], sizeof(header.oswIfmt[0]), 1, file);
    fwrite(&header.oswIfmt[1], sizeof(header.oswIfmt[1]), 1, file);
    fwrite(&header.oterTrans[0], sizeof(header.oterTrans[0]), 1, file);
    fwrite(&header.oterTrans[1], sizeof(header.oterTrans[1]), 1, file);
    fwrite(&header.upperLat, sizeof(header.upperLat), 1, file);
    fwrite(&header.lowerLat, sizeof(header.lowerLat), 1, file);
    fwrite(&header.nlHash, sizeof(header.nlHash), 1, file);
    fwrite(&header.nextDofId, sizeof(header.nextDofId), 1, file);
    fwrite(&header.vtxFormat, sizeof(header.vtxFormat), 1, file);
    fwrite(&header.dblOrig, sizeof(header.dblOrig), 1, file);
    fwrite(&header.swIfmt[0], sizeof(header.swIfmt[0]), 1, file);
    fwrite(&header.swIfmt[1], sizeof(header.swIfmt[1]), 1, file);
    fwrite(&header.terTrans[0], sizeof(header.terTrans[0]), 1, file);
    fwrite(&header.terTrans[1], sizeof(header.terTrans[1]), 1, file);
    fwrite(&header.soundId, sizeof(header.soundId), 1, file);
    fwrite(&header.pathId, sizeof(header.pathId), 1, file);
    fwrite(&header.reserved[0], sizeof(header.reserved[0]), 1, file);
    fwrite(&header.reserved[1], sizeof(header.reserved[1]), 1, file);
    fwrite(&header.clipId, sizeof(header.clipId), 1, file);
    fwrite(&header.textId, sizeof(header.textId), 1, file);
    fwrite(&header.switchId, sizeof(header.switchId), 1, file);
    fwrite(&header.SWLat1, sizeof(header.SWLat1), 1, file);
    fwrite(&header.SWLong1, sizeof(header.SWLong1), 1, file);
    fwrite(&header.NELat1, sizeof(header.NELat1), 1, file);
    fwrite(&header.NELong1, sizeof(header.NELong1), 1, file);
    fwrite(&header.latOrig1, sizeof(header.latOrig1), 1, file);
    fwrite(&header.longOrig1, sizeof(header.longOrig1), 1, file);
    fwrite(&header.upperLat1, sizeof(header.upperLat1), 1, file);
    fwrite(&header.lowerLat1, sizeof(header.lowerLat1), 1, file);
*/
}

/*******************************************************************************
*     Function: writeColorTable 
*        Class: FltWriter
*       Access: Private
*  Description: Writes mgColorTable
*        Input:
*       Output:
*******************************************************************************/
void FltWriter::writeColorTable(const mgColorTable& coltab)
{
//    int i, j;
    
    fwrite(&coltab, sizeof(coltab), 1, file);

    // write each field separately to avoid byte-alignment problems
/*
    fwrite(&coltab.opcode, sizeof(coltab.opcode), 1, file);
    fwrite(&coltab.length, sizeof(coltab.length), 1, file);
    for (i=0; i<MG_CT_SHADED; i++)
    {
	for (j=0; j<3; j++)
    	    fwrite(&coltab.color[i][j], sizeof(coltab.color[i][j]), 1, file);
    }
    for (i=0; i<4; i++)
    {
	for (j=0; j<3; j++)
    	    fwrite(&coltab.spare[i][j], sizeof(coltab.spare[i][j]), 1, file);
    }
    for (i=0; i<MG_CT_FIXED; i++)
    {
	for (j=0; j<3; j++)
    	    fwrite(&coltab.fixedColor[i][j], sizeof(coltab.fixedColor[i][j]), 
	 	   1, file);
    }
*/
}

/*******************************************************************************
*     Function: writeMaterialTable 
*        Class: FltWriter
*       Access: Private
*  Description: Writes mgMaterialTable
*        Input:
*       Output:
*******************************************************************************/
void FltWriter::writeMaterialTable(const mgMaterialTable& mattab)
{
//    int i, j;

    fwrite(&mattab, sizeof(mattab), 1, file);
/*
    fwrite(&mattab.opcode, sizeof(mattab.opcode), 1, file);
    fwrite(&mattab.length, sizeof(mattab.length), 1, file);
    for (i=0; i<64; i++)
    {
	for (j=0; j<3; j++)
	    fwrite(&mattab.mat[i].ambient[j], sizeof(mattab.mat[i].ambient[j]),
		    1, file); 
	for (j=0; j<3; j++)
	    fwrite(&mattab.mat[i].diffuse[j], sizeof(mattab.mat[i].diffuse[j]),
		    1, file); 
	for (j=0; j<3; j++)
	    fwrite(&mattab.mat[i].specular[j],sizeof(mattab.mat[i].specular[j]),		      1,file); 
	for (j=0; j<3; j++)
	    fwrite(&mattab.mat[i].emission[j],sizeof(mattab.mat[i].emission[j]),		    1,file); 
       fwrite(&mattab.mat[i].shininess,sizeof(mattab.mat[i].shininess),1,file); 
	fwrite(&mattab.mat[i].alpha, sizeof(mattab.mat[i].alpha), 1, file); 
	for (j=0; j<32; j++)
	    fwrite(&mattab.mat[i].spare1[j], sizeof(mattab.mat[i].spare1[j]), 
		    1, file); 
    }
*/
}

/*******************************************************************************
*     Function: writeTextureRef 
*        Class: FltWriter
*       Access: Private
*  Description: Writes mgTextureRef
*        Input:
*       Output:
*******************************************************************************/
void FltWriter::writeTextureRef(const mgTextureRef& texref)
{
    int i;


    fwrite(&(texref.opcode), sizeof(texref.opcode), 1, file);
// Objectcenter thinks this field is a char - produces a run-time error 
//    fwrite(&(texref.length), sizeof(texref.length), 1, file);
// MPG hack for large models
    short len=96;
    fwrite(&len, sizeof(short), 1, file);
    for (i=0; i<80; i++)
	fwrite(&texref.file[i], sizeof(char), 1, file);
    fwrite(&texref.index, sizeof(texref.index), 1, file);
    fwrite(&texref.x, sizeof(texref.x), 1, file);
    fwrite(&texref.y, sizeof(texref.y), 1, file);
}

/*******************************************************************************
*     Function: writeVertex 
*        Class: FltWriter
*       Access: Private
*  Description: One for each type of flight vertex 
*		*** write each field separately to avoid byte-alignment
*		    problems
*        Input:	The appropriate type of flight vertex structure
*       Output:
*******************************************************************************/
void FltWriter::writeVertex(mgVertSharedAbs* vert)
{
    int i;

    fwrite(&vert->opcode, sizeof(vert->opcode), 1, file);
    fwrite(&vert->length, sizeof(vert->length), 1, file);
    fwrite(&vert->vcolor, sizeof(vert->vcolor), 1, file);
    fwrite(&vert->flags, sizeof(vert->flags), 1, file);
    for (i=0; i<3; i++)
	fwrite(&vert->p[i], sizeof(vert->p[0]), 1, file);
}

void FltWriter::writeVertex(mgVertSharedNorm* vert)
{
    int i;

    fwrite(&vert->opcode, sizeof(vert->opcode), 1, file);
    fwrite(&vert->length, sizeof(vert->length), 1, file);
    fwrite(&vert->vcolor, sizeof(vert->vcolor), 1, file);
    fwrite(&vert->flags, sizeof(vert->flags), 1, file);
    for (i=0; i<3; i++)
    	fwrite(&vert->p[i], sizeof(vert->p[0]), 1, file);
    for (i=0; i<3; i++)
    	fwrite(&vert->n[i], sizeof(vert->n[0]), 1, file);
    fwrite(&vert->spare, sizeof(vert->spare), 1, file);
}

void FltWriter::writeVertex(mgVertSharedNormTex* vert)
{
    int i;

    fwrite(&vert->opcode, sizeof(vert->opcode), 1, file);
    fwrite(&vert->length, sizeof(vert->length), 1, file);
    fwrite(&vert->vcolor, sizeof(vert->vcolor), 1, file);
    fwrite(&vert->flags, sizeof(vert->flags), 1, file);
    for (i=0; i<3; i++)
    	fwrite(&vert->p[i], sizeof(vert->p[0]), 1, file);
    for (i=0; i<3; i++)
    	fwrite(&vert->n[i], sizeof(vert->n[0]), 1, file);
    fwrite(&vert->t[0], sizeof(vert->t[0]), 1, file);
    fwrite(&vert->t[1], sizeof(vert->t[0]), 1, file);
    fwrite(&vert->spare, sizeof(vert->spare), 1, file);
}

void FltWriter::writeVertex(mgVertSharedAbsTex* vert)
{
    int i;

    fwrite(&vert->opcode, sizeof(vert->opcode), 1, file);
    fwrite(&vert->length, sizeof(vert->length), 1, file);
    fwrite(&vert->vcolor, sizeof(vert->vcolor), 1, file);
    fwrite(&vert->flags, sizeof(vert->flags), 1, file);
    for (i=0; i<3; i++)
    	fwrite(&vert->p[i], sizeof(vert->p[0]), 1, file);
    fwrite(&vert->t[0], sizeof(vert->t[0]), 1, file);
    fwrite(&vert->t[1], sizeof(vert->t[0]), 1, file);
}

/*******************************************************************************
*     Function: writeVertexPool 
*        Class: FltWriter
*       Access: Private
*  Description:
*        Input:
*       Output:
*******************************************************************************/
uint FltWriter::writeVertexPool(const VertexTableBead *vtable)
{
    mgVertTable verthead = vtable->fltStruct(); 
    // write the vertex table header record
    fwrite(&verthead.opcode, sizeof(verthead.opcode), 1, file);
    fwrite(&verthead.length, sizeof(verthead.length), 1, file);
    fwrite(&verthead.vtxlen, sizeof(verthead.vtxlen), 1, file);

    const VertexRecordBead* vert = vtable->vPool();
    if (vert == NULL)
    {
	cerr << "writeVertexPool: no vertices in vertex table";
	return (0);
    }

    while (vert != NULL)
    {
	switch (vert->Type())
 	{
	    case MG_DVERT_ABS:
		writeVertex((mgVertSharedAbs *)vert->fltStruct());
	    break;
	    case MG_DVERT_NORM:
		writeVertex((mgVertSharedNorm *)vert->fltStruct());
	    break;
	    case MG_DVERT_NORM_TEX:
		writeVertex((mgVertSharedNormTex *)vert->fltStruct());
	    break;
	    case MG_DVERT_ABS_TEX:
		writeVertex((mgVertSharedAbsTex *)vert->fltStruct());
	    break;
	    default:
		fprintf(stderr, "writeVertexPool: Unknown opcode < %d >\n",
			vert->Type());
		return (0);
	}
	vert = (VertexRecordBead *)vert->right();
    }
    return (1);
}

/*******************************************************************************
*     Function: writeBead 
*        Class: FltWriter
*       Access: Private
*  Description: Writes a bead to a file 
*        Input:
*       Output:
*******************************************************************************/
void FltWriter::writeBead(Bead *bead)
{
    int i;
    switch (bead->Type())
    {
	case MG_HEADER:
	    writeHeader(((HeaderBead *)bead)->fltStruct());
	break;
	case MG_COLOR_TABLE:
	    writeColorTable(((ColorTableBead *)bead)->fltStruct());
	break;
	case MG_MATERIAL_TABLE:
	    writeMaterialTable(((MaterialTableBead *)bead)->fltStruct());
	break;
	case MG_TEXTURE_REF:
	    // Write out all texture references at once
	    Bead *temp;
	    temp = bead;
	    while (temp != NULL)
	    {
		writeTextureRef(((TextureBead *)temp)->fltStruct()); 
		temp = temp->right();
	    }
	break;
	case MG_VTXTABLE:
	    if (writeVertexPool((VertexTableBead *)bead) == 0)
	    {
		cerr << "Error in vtxpool";
		exit(1);
	    }
	break;
	case MG_GROUP:
	    {
	    mgGroup group;
	    groupAux aux;
	    group = ((GroupBead *)bead)->fltStruct();
            aux = ((GroupBead *)bead)->auxStruct();
	    fwrite(&group.opcode, sizeof(group.opcode), 1, file);
	    fwrite(&group.length, sizeof(group.length), 1, file);
	    fwrite(&group.id, sizeof(group.id), 1, file);
	    fwrite(&group.relPri, sizeof(group.relPri), 1, file);
	    fwrite(&group.spare1, sizeof(group.spare1), 1, file);
	    fwrite(&group.flags, sizeof(group.flags), 1, file);
	    fwrite(&group.fxId1, sizeof(group.fxId1), 1, file);
	    fwrite(&group.fxId2, sizeof(group.fxId2), 1, file);
	    fwrite(&group.signif, sizeof(group.signif), 1, file);
	    fwrite(&group.layer, sizeof(group.layer), 1, file);
	    fwrite(&group.spare2, sizeof(group.spare2), 1, file);

	    // write aux data
	    if (aux.id != NULL)
	    {
		fwrite(&aux.id->opcode,sizeof(aux.id->opcode),1,file);
		fwrite(&aux.id->length,sizeof(aux.id->length),1,file);
		for (i=0; i<aux.id->length-4; i++)
		    fwrite(&aux.id->text[i], sizeof(char), 1, file);
	    }
	    }
	break;
	case MG_LOD:
	    mgLod lod;
	    lod = ((LodBead *)bead)->fltStruct();
	    fwrite(&lod.opcode, sizeof(lod.opcode), 1, file);
	    fwrite(&lod.length, sizeof(lod.length), 1, file);
	    fwrite(&lod.id, sizeof(lod.id), 1, file);
	    fwrite(&lod.inDist, sizeof(lod.inDist), 1, file);
	    fwrite(&lod.outDist, sizeof(lod.outDist), 1, file);
	    fwrite(&lod.fxId1, sizeof(lod.fxId1), 1, file);
	    fwrite(&lod.fxId2, sizeof(lod.fxId2), 1, file);
	    fwrite(&lod.flags, sizeof(lod.flags), 1, file);
	    for (i=0; i<3; i++)
		fwrite(&lod.center[i], sizeof(lod.center[0]), 1, file);
	    for (i=0; i<11; i++)
		fwrite(&lod.spare1[i], sizeof(lod.spare1[0]), 1, file);
	break;
	case MG_LODFLOAT:
	    mgFLod flod;
	    flod = ((FLodBead *)bead)->fltStruct();
	    fwrite(&flod.opcode, sizeof(flod.opcode), 1, file);
	    fwrite(&flod.length, sizeof(flod.length), 1, file);
	    fwrite(&flod.id, sizeof(flod.id), 1, file);
	    fwrite(&flod.inDist, sizeof(flod.inDist), 1, file);
	    fwrite(&flod.outDist, sizeof(flod.outDist), 1, file);
	    fwrite(&flod.fxId1, sizeof(flod.fxId1), 1, file);
	    fwrite(&flod.fxId2, sizeof(flod.fxId2), 1, file);
	    fwrite(&flod.flags, sizeof(flod.flags), 1, file);
	    for (i=0; i<3; i++)
		fwrite(&flod.center[i], sizeof(flod.center[0]), 1, file);
	break;
	case MG_DOF:
	    // add later
	break;
	case MG_OBJECT:
	    {
	    mgObject obj;
	    objectAux aux;
	    obj = ((ObjectBead *)bead)->fltStruct();
            aux = ((ObjectBead *)bead)->auxStruct();
	    fwrite(&obj.opcode, sizeof(obj.opcode), 1, file);
	    fwrite(&obj.length, sizeof(obj.length), 1, file);
	    fwrite(&obj.id, sizeof(obj.id), 1, file);
	    fwrite(&obj.flags, sizeof(obj.flags), 1, file);
	    fwrite(&obj.relPri, sizeof(obj.relPri), 1, file);
	    fwrite(&obj.transp, sizeof(obj.transp), 1, file);
	    fwrite(&obj.fxId1, sizeof(obj.fxId1), 1, file);
	    fwrite(&obj.fxId2, sizeof(obj.fxId2), 1, file);
	    fwrite(&obj.signif, sizeof(obj.signif), 1, file);
	    // Assumes spare1 is a short (not array of shorts) as per flt14_1.h
	    fwrite(&obj.spare1, sizeof(obj.spare1), 1, file);

	    // write aux data
	    if (aux.id != NULL)
	    {
		fwrite(&aux.id->opcode,sizeof(aux.id->opcode),1,file);
		fwrite(&aux.id->length,sizeof(aux.id->length),1,file);
		for (i=0; i<aux.id->length-4; i++)
		    fwrite(&aux.id->text[i], sizeof(char), 1, file);
	    }
	    }
	break;
	case MG_POLYGON:
	    mgPolygon poly;
	    polygonAux aux;
            poly = ((PolygonBead *)bead)->fltStruct();
            aux = ((PolygonBead *)bead)->auxStruct();
	    fwrite(&poly.opcode, sizeof(poly.opcode), 1, file);
	    fwrite(&poly.length, sizeof(poly.length), 1, file);
	    fwrite(&poly.id, sizeof(poly.id), 1, file);
	    fwrite(&poly.IRCode, sizeof(poly.IRCode), 1, file);
	    fwrite(&poly.relPri, sizeof(poly.relPri), 1, file);
	    fwrite(&poly.drawType, sizeof(poly.drawType), 1, file);
	    fwrite(&poly.spare1, sizeof(poly.spare1), 1, file);
	    fwrite(&poly.color1, sizeof(poly.color1), 1, file);
	    fwrite(&poly.color2, sizeof(poly.color2), 1, file);
	    fwrite(&poly.spare2, sizeof(poly.spare2), 1, file);
	    fwrite(&poly.textureTemplate, sizeof(poly.textureTemplate), 
								1, file);
	    fwrite(&poly.detail, sizeof(poly.detail), 1, file);
	    fwrite(&poly.texture, sizeof(poly.texture), 1, file);
	    fwrite(&poly.material, sizeof(poly.material), 1, file);
	    fwrite(&poly.surfaceMaterialCode, 
				sizeof(poly.surfaceMaterialCode), 1, file);
	    fwrite(&poly.featureId, sizeof(poly.featureId), 1, file);
	    fwrite(&poly.IRMaterial, sizeof(poly.IRMaterial), 1, file);
	    fwrite(&poly.transp, sizeof(poly.transp), 1, file);
	    fwrite(&poly.spare4[0], sizeof(poly.spare4[0]), 1, file);
	    
	    // write aux data
	    if (aux.comment != NULL)
	    {
		fwrite(&aux.comment->opcode,sizeof(aux.comment->opcode),1,file);
		fwrite(&aux.comment->length,sizeof(aux.comment->length),1,file);
		for (i=0; i<aux.comment->length-4; i++)
		    fwrite(&aux.comment->text[i], sizeof(char), 1, file);
	    }
	break;
	case MG_VTXLIST:	
	    {
	    mgVertList vlist;
	    vlist = ((VertexListBead *)bead)->fltStruct();
	    fwrite(&vlist.opcode, sizeof(vlist.opcode), 1, file); 
	    fwrite(&vlist.length, sizeof(vlist.length), 1, file); 
	    }
	break;
	case MG_VTXOFFSET:
    	    mgVertOffset voff;
	    voff = ((VertexOffsetBead *)bead)->fltStruct();
	    fwrite(&voff.vtxoffset, sizeof(voff.vtxoffset), 1, file);
	    fflush(file);
	break;
	default:
	    fprintf(stderr, "writeBead: unknown opcode < %d >\n", bead->Type());
	    exit(1);
	break;
    }
}

/*******************************************************************************
*     Function: writeControl 
*        Class: FltWriter
*       Access: Private
*  Description: Writes push and pop control records 
*        Input:
*       Output:
*******************************************************************************/
void FltWriter::writeControl(ushort *type)
{
    ushort length = FLT_HEADER_LENGTH;
    fwrite(type, sizeof(short), 1, file);
    fwrite(&length, sizeof(length), 1, file);
    fflush(file);

#ifdef DEBUG
    char sout[24];
    if (*type == MG_PUSH)
	strcpy(sout, "MG_PUSH");
    else if (*type == MG_PUSH_SF)
	strcpy(sout, "MG_PUSH_SF");
    else if (*type == MG_POP_SF)
	strcpy(sout, "MG_POP_SF");
    else strcpy(sout, "MG_POP");
    printf("\n=======================\n  %s\n----------------------\n",
	    sout);
#endif
}

/*******************************************************************************
*     Function: traverse 
*        Class: FltWriter
*       Access: Private
*  Description: Does an in-order, depth-first traversal of the database 
*        Input:
*       Output:
*******************************************************************************/
void FltWriter::traverse(Bead *bead)
{
    Bead *next;
    ushort nextPop=MG_POP, nextPush=MG_PUSH;
    ushort type;

    writeBead(bead);
    type = bead->Type();
    if (bead->down() != NULL)
    {
	next = bead->down();
	// check to see if it's a sub-face polygon
	if ((type == MG_POLYGON) && (next->Type() == MG_POLYGON))
	{
	    nextPush = MG_PUSH_SF;
	    writeControl(&nextPush); 
	    nextPop = MG_POP_SF;
	}
	else if ((type != MG_HEADER) && (type != MG_COLOR_TABLE) &&
		 (type != MG_MATERIAL_TABLE) && (type != MG_TEXTURE_REF))
	// Only write pushes after vertex table
	{
	    writeControl(&nextPush);
	    nextPop = MG_POP;
	}
	traverse(next);
	writeControl(&nextPop);
    }
    if ((type != MG_VTXTABLE) && (type != MG_TEXTURE_REF) &&
	(bead->right() != NULL))
    {
	next = bead->right();
	traverse(next);
    }
}

/*******************************************************************************
*     Function: write 
*        Class: FltWriter
*       Access: Public
*  Description: 
*        Input:
*       Output:
*******************************************************************************/
uint FltWriter::Write(char *_fname) 
{
    strcpy(fname, _fname);
    if (openFile() == 0)
    {
	fprintf(stderr, "openFile: Could not open file < %s >\n", fname);
	return (0);
    }

    dbase->reset();
    traverse(dbase->data());
       
    fclose(file);
    return (1);
}