fltReader.C 30.5 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
/******************************************************************************\  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:     fltReader.C
* CLASS:        FltReader
* DESCRIPTION:  MultiGen flight file reader class
* AUTHOR:       Mike Goslin
* CREATED:      2/7/95
* REVISIONS:
*
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <math.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include "fileReader.H"

static char *opcodeNames[MG_OPCODENAMES_MAX] = {
                        "unknown",              /*  0 */
                        "header",               /*  1 */
                        "group",                /*  2 */
                        "lod",                  /*  3 */
                        "object",               /*  4 */
                        "poly",                 /*  5 */
                        "unknown",              /*  6 */
                        "vert_abs",             /*  7 */
                        "vert_shad",            /*  8 */
                        "vert_norm",            /*  9 */
                        "push",                 /* 10 */
                        "pop",                  /* 11 */
                        "unknown",              /* 12 */
                        "unknown",              /* 13 */
                        "unknown",              /* 14 */
                        "unknown",              /* 15 */
                        "unknown",              /* 16 */
                        "unknown",              /* 17 */
                        "unknown",              /* 18 */
                        "push_sf",              /* 19 */
                        "pop_sf",               /* 20 */
                        "unknown",              /* 21 */
                        "unknown",              /* 22 */
                        "unknown",              /* 23 */
                        "unknown",              /* 24 */
                        "unknown",              /* 25 */
                        "unknown",              /* 26 */
                        "unknown",              /* 27 */
                        "unknown",              /* 28 */
                        "unknown",              /* 29 */
                        "unknown",              /* 30 */
                        "comment",              /* 31 */
                        "color_table",          /* 32 */
                        "unknown",              /* 33 */          
			"unknown",              /* 34 */
                        "unknown",              /* 35 */
                        "unknown",              /* 36 */
                        "unknown",              /* 37 */
                        "unknown",              /* 38 */
                        "unknown",              /* 39 */
                        "unknown_matrix",       /* 40 */
                        "unknown_matrix",       /* 41 */
                        "unknown_matrix",       /* 42 */
                        "unknown_matrix",       /* 43 */
                        "unknown_matrix",       /* 44 Translate */
                        "unknown_matrix",       /* 45 Scale */
                        "unknown_matrix",       /* 46 Rotate */
                        "unknown_matrix",       /* 47 */
                        "unknown_matrix",       /* 48 */
                        "matrix",               /* 49 */
                        "vector",               /* 50 */
                        "bbox",                 /* 51 */
                        "unknown",              /* 52 */
                        "unknown",              /* 53 */
                        "unknown",              /* 54 */
                        "unknown",              /* 55 */
                        "unknown",              /* 56 */
                        "unknown",              /* 57 */
                        "unknown",              /* 58 */
                        "unknown",              /* 59 */
                        "replicate",            /* 60 */
                        "instance_ref",         /* 61 */
                        "instance_def",         /* 62 */
                        "external_ref",         /* 63 */
                        "texture_ref",          /* 64 */
                        "eyepoints",            /* 65 */
                        "material_table",       /* 66 */
                        "vertex_table",         /* 67 */
                        "dvert_abs",            /* 68 */
                        "dvert_norm",           /* 69 */
                        "dvert_norm_tex",       /* 70 */
                        "dvert_tex",            /* 71 */
                        "unknown",              /* 72 */
                        "dlod",                 /* 73 */
                        "unknown",              /* 74 */
                        "unknown",              /* 75 */
                        "unknown_matrix",       /* 76 */
                        "unknown_matrix",       /* 77 */
                        "unknown_matrix",       /* 78 */
                        "unknown_matrix",       /* 79 */
                        "unknown_matrix",       /* 80 */
                        "unknown_matrix",       /* 81 */
                        "unknown_matrix",       /* 82 */
                        "unknown",              /* 83 */
                        "unknown",              /* 84 */
                        "unknown",              /* 85 */
                        "unknown",              /* 86 */
                        "road",                 /* 87 */
                        "road_zone",            /* 88 */
                        "morph_vertices",       /* 89 */
                        "linkage",              /* 90 */
                        "sound",                /* 91 */
                        "road_path",            /* 92 */
                        "sound_table",          /* 93 */
                        "general_matrix",       /* 94 */
                        "unknown",              /* 95 */
                        "switch" };             /* 96 */

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

/*******************************************************************************
*     Function: loadFltFile 
*        Class: FltReader
*       Access: Public
*  Description: Reads the entire flight file into memory pointed to by buffer
*        Input:
*       Output:
*******************************************************************************/
uint FltReader::loadFltFile(const char* name)
{
    int             ifd;
    struct stat     stbuf;

#ifdef DEBUG
    cerr << "Loading Flight File\n";
#endif

    if (name == NULL)
        return 0;

    if ((ifd = open(name, O_RDONLY)) == -1)
    {
	fprintf(stderr, "LoadFltFile: Could not access < %s >\n", name);
        return 0;
    }

    fstat(ifd, &stbuf);             /* find the size of the file */
    if (stbuf.st_size == 0)
    {
	fprintf(stderr, "LoadFltFile: File < %s > is empty\n", file);
        close(ifd);
        return 0;
    }

    buffer = (char *)malloc((uint)stbuf.st_size);
    if (read(ifd, buffer, (uint)stbuf.st_size) != stbuf.st_size)
    {
	cerr << "loadFltFile: Couldn't read in entire file\n";
	exit(1);
    }  
    close(ifd);

    if (((mgBead *)buffer)->opcode != MG_HEADER)
    {
	cerr << "LoadFltFile: Bogus FLT header in < %s >\n", file;
        return 0;
    }
    bufsize = stbuf.st_size;
    bufend = buffer + bufsize;
    return 1;
}

/*******************************************************************************
*     Function: LookAhead 
*        Class: FltReader
*       Access: Public
*  Description: Look ahead in flight hierarchy 
*        Input:
*       Output:
*******************************************************************************/
void FltReader::LookAhead(char *ptr, mgComment **c, mgMatrix **m, 
			         mgReplicate **r, mgLongID **hLongID)
{
    *c = NULL;
    *hLongID = NULL;
    *m = NULL;
    *r = NULL;

    while(1)
    {
        switch(((mgBead*)ptr)->opcode)
        {
            case MG_COMMENT:
                *c = (mgComment*)ptr;
                break;
            case MG_LONGID:
                *hLongID = (mgLongID *) ptr;
                break;
            case MG_MATRIX:
                *m = (mgMatrix*)ptr;
                break;
            case MG_REPLICATE:
                *r = (mgReplicate*)ptr;
                break;
            case 42:
            case 43:
            case 44:
            case 45:
            case 46:
            case 47:
            case 48:
            case 76:
            case 77:
            case 78:
            case 79:
            case 80:
            case 81:
            case 82:
            case 94:
                break;
            default:
                return;
        }
        ptr += ((mgBead*)ptr)->length;
    }
}

/*******************************************************************************
*     Function: traversePoly 
*        Class: FltReader
*       Access: Public
*  Description: Traverses the flight file structure 
*        Input:
*       Output:
*******************************************************************************/
char *FltReader::traversePoly(mgPolygon *mgPoly)
{
#if 0
    mgVertAbs           *aVert = NULL;
    mgVertNorm          *nVert = NULL;
    long                **repVerts = NULL;
#endif
    mgLongID            *pLongID = NULL;
    mgVertOffset        *voffset;
    long                numVerts, vertType;
    long                l;
    register char       *ptr;
    long                depth=0, done=0, mixed;
    mgComment           *comment = NULL;
    mgMatrix            *matrix = NULL;
    mgReplicate         *replicate = NULL;
    mgBead              *bead;
    long                vcount;
    ushort	        pushflag=0;

    mgPoly->drawType = 0;

    numVerts = 0;
    vertType = 0;
    mixed = 0;

    /*
     * Check for mixed vertex types. If a poly has mixed vertex types
     * we default all vertices to MG_VERT_ABS.
    */
    ptr = (char*)mgPoly + mgPoly->length;
    while(!done)
    {
        bead = (mgBead*)ptr;
        switch (bead->opcode)
        {
        case MG_VTXLISTS:
	    // morphing vertices
            break;
        case MG_VTXLIST:
            /* If double format, get the vertex ref */
            /* Get the number of vertices in list */
            vcount = (bead->length - sizeof(mgVertList))/sizeof(mgVertOffset);

	    // Create the vertex list bead
	    mgVertList* pvlist;
	    pvlist = (mgVertList *)bead;
	    VertexListBead *vlist;
	    vlist = new VertexListBead(*pvlist);
	    // Must always be a push before a vertex list
	    if ((dbase->add(*vlist, MG_PUSH)) == 0) 
	    	cerr << "traversePoly: Couldn't add vertex list bead\n";

            /* Get the first offset */
            voffset = (mgVertOffset *) ((char *)bead + sizeof(mgVertList));

            ptr += sizeof ( mgVertList );
            for ( l = 0; l < vcount; l++ ) 
	    // Get the remaining offsets
	    {
	        VertexOffsetBead *voff;
	     	voff = new VertexOffsetBead(*voffset);
	    	if ((dbase->add(*voff)) == 0)
		    cerr << "traversePoly: Couldn't add vertex list bead\n";

                ptr += sizeof ( mgVertOffset );
                voffset = ( mgVertOffset * ) ptr;
            }
	    pushflag = MG_POP; // Reset pushflag for polygons
            continue;
        case MG_PUSH:
            depth++;
	    pushflag = MG_PUSH;
            break;
        case MG_POP:
            depth--;
            if(depth <= 0)
                done = 1;
	    pushflag = MG_POP;
	    dbase->pop();
            break;
        case MG_COMMENT:
            comment = (mgComment*)bead;
            break;
        case MG_LONGID:
            pLongID = (mgLongID *) bead;
            break;
        case MG_MATRIX:
            matrix = (mgMatrix*)bead;
            break;
        case MG_REPLICATE:
            replicate = (mgReplicate*)bead;
            break;
        case MG_POLYGON:
	    fprintf(stderr, "traversePoly: poly found in poly id = %s\n");
            fprintf(stderr, "\tPossible vertex-less polygon\n",
		     ((mgPolygon *)bead)->id);
            return ptr;
        case MG_PUSH_SF:
	    fprintf(stderr, "traversePoly: unsupported bead %ld < %s >\n",
		     bead->opcode, 
		     opcodeNames[ (bead->opcode < MG_OPCODENAMES_MAX)
		     ? bead->opcode: 0 ]);
	    fprintf(stderr, "id = %s\n\t Possible vertex-less superface\n",
		    mgPoly->id);
            break;
        case MG_POP_SF:
	    pushflag = MG_POP_SF;
	    dbase->pop();
            break;
        default:
	    fprintf(stderr, "traversePoly: unsupported bead %ld < %s > found\n",
		     bead->opcode, 
		     opcodeNames[ (bead->opcode < MG_OPCODENAMES_MAX)
		     ? bead->opcode: 0 ]);
            break;
        }
        ptr += bead->length;
    }

    return ptr;
}

/*******************************************************************************
*     Function: traverseSubPoly
*        Class: FltReader
*       Access: Public
*  Description: Traverses the flight file structure 
*        Input:
*       Output:
*******************************************************************************/
void FltReader::traverseSubPoly(char **nptr, char *ptr, char *end, long depth)
{
    long        sfdepth = 0, done = 0;
    mgBead      *bead;
    ushort 	 pushflag=0;

    while(ptr < end && !done)
    {
        bead = (mgBead*)ptr;
        switch (bead->opcode)
        {
        case MG_POLYGON:
            if (sfdepth-1 == depth)
	    {
		mgPolygon *pptr = (mgPolygon *)bead;
		PolygonBead *poly;
		poly = new PolygonBead(*pptr);
		if ((dbase->add(*poly, pushflag)) == 0)
		    cerr << "traverseSubPoly: Couldn't add polygon bead\n";

                ptr = traversePoly((mgPolygon*)bead);
  	    }
            else        /* Skip over polygon */
            {
                mgBead  *tmpBead;

                tmpBead = (mgBead*)(ptr + bead->length);
                while(tmpBead->opcode != MG_POP)
                    tmpBead = (mgBead*)((char*)tmpBead + tmpBead->length);

                ptr = ((char*)tmpBead + tmpBead->length);
            }
	    pushflag = 0;
            break;
        case MG_PUSH_SF:
            sfdepth++;
            ptr += bead->length;
	    pushflag = MG_PUSH_SF;
            break;
        case MG_POP_SF:
            sfdepth--;
            if (sfdepth <= 0)
                done = 1;
            ptr += bead->length;
	    pushflag = MG_POP_SF;
            *nptr = ptr;
            break;
        case 40: case 41: case 42: case 43: case 44: case 45: case 46:
        case 47: case 48: case 76: case 77: case 78: case 79: case 80:
        case 81: case 82: case 94: /* Ignored transformation record types */
        case MG_MATRIX:
        case MG_COMMENT:
        case MG_LONGID:
            ptr += bead->length;
            break;
        default:
	    fprintf(stderr, "trav sub poly: unsupported node %ld < %s >found\n",
	  	    bead->opcode,
                    opcodeNames[ (bead->opcode < MG_OPCODENAMES_MAX) ? 
		    bead->opcode: 0 ]);
            ptr += bead->length;
            break;
        }
    }
}

/*******************************************************************************
*     Function: traverseObj 
*        Class: FltReader
*       Access: Public
*  Description: Traverses the flight file structure 
*        Input:
*       Output:
*******************************************************************************/
char *FltReader::traverseObj(char *ptr, char *end, long depth)
{
    mgBead      *bead;
    int         done = 0;
    int         foundObject = FALSE;
#if 0
    mgLongID    *pLongID = NULL;
#endif
    ushort	pushflag=0;

    while ( ptr < end && ! done )
    {
        bead = (mgBead*)ptr;
        switch (bead->opcode)
        {
        case MG_OBJECT:
            // watch out for previous empty object.  Leave ptr at object for 
	    // caller
            if ( ! foundObject )
            {
                mgObject *pobj = (mgObject *) ptr;
		ObjectBead *obj;
	 	obj = new ObjectBead(*pobj);
		if ((dbase->add(*obj, pushflag)) == 0)
		    cerr << "traverseObj: couldn't add object bead\n";	
		
                /* Set flat-shading flag for object */
                // FlatFlag = objectBead->flags & 0x08000000;
                ptr += bead->length;
                foundObject = TRUE;
            }
            else
            {
		cerr << "traverseObj: found empty object bead\n";
                done = 1;
            }
	    pushflag = 0;	// Reset push flag
            break;
        case MG_POLYGON:
            mgComment       *comment;
            mgLongID        *pLongID;
            mgMatrix        *matrix;
            mgReplicate     *replicate;

            LookAhead( ptr + bead->length, &comment, &matrix, &replicate,
 			   &pLongID );

	    if (comment != NULL)
	    {
	    	// Check to see if the polygon is a portal-for now throw out all
	    	// portals
		char string[128];

		if (sscanf(comment->text, "PORTAL %s", string) > 0)
		{
		    fprintf(stderr, "Warning: Throwing out portal: %s\n", 
									string);

		    // skip over polygon
                    mgBead  *tmpBead;

                    tmpBead = (mgBead*)(ptr + bead->length);
                    while(tmpBead->opcode != MG_POP)
                    	tmpBead = (mgBead*)((char*)tmpBead + tmpBead->length);

                    ptr = ((char*)tmpBead + tmpBead->length);
		    continue;
		}
	    }

	    mgBead *tmpBead;
	    // Look ahead to next bead
            tmpBead = (mgBead *)(ptr + bead->length);

            /* watch out for empty faces.  Leave ptr at pop for caller. */
            if ( tmpBead->opcode != MG_POP )
            {
                /* Look ahead for PUSH_SF to see if this poly is a base poly */
                while ( tmpBead->opcode != MG_POP )
                {
                    tmpBead = (mgBead *) ((char *) tmpBead + tmpBead->length);
                }

                tmpBead = (mgBead *)((char *) tmpBead + tmpBead->length);

                if ( tmpBead->opcode == MG_PUSH_SF )    /* It's a base poly */
                {
                    char        *nptr = NULL;
                    long        sfdepth = 0;

		    mgPolygon *pptr = (mgPolygon *)bead;
		    PolygonBead *poly;
		    poly = new PolygonBead(*pptr);
		    if (comment != NULL)
			poly->Comment(*comment);
	    	    if ((dbase->add(*poly, pushflag)) == 0)
		    	cerr << "traverseObj: Couldn't add polygon bead\n";

                    ptr = traversePoly(pptr);

                    if ( pLongID )
			poly->Name(pLongID->text);
                    else
			poly->Name(((mgPolygon *)bead)->id);

		    // Only handles one level of subfacing right now
		    traverseSubPoly(&nptr, ptr, end, sfdepth);

                    ptr = nptr;
                }
                else  // It's a polygon with no sub-faces
                {
		    mgPolygon *pp = (mgPolygon *)bead;
		    PolygonBead *ply;
		    ply = new PolygonBead(*pp);
		    if (comment != NULL)
			ply->Comment(*comment);
		    if ((dbase->add(*ply, pushflag)) == 0)
			cerr << "traverseObj: Couldn't add polygon bead\n";

		    ptr = traversePoly((mgPolygon *)bead);
                }
            }
            else
            {
		cerr << "traverseObj: found empty face bead\n";
                ptr += bead->length;
            }
	    pushflag = 0;
            break;
        case MG_PUSH:
	    pushflag = MG_PUSH;
            depth++;
            ptr += bead->length;
            break;
        case MG_POP:
            depth--;
            if ( depth <= 0 )
                done = 1;
            /* watch out for empty objects.  Leave ptr at pop for caller. */
            if ( depth >= 0 )
                ptr += bead->length;
	    pushflag = MG_POP;
	    dbase->pop();
            break;
        case MG_PUSH_SF:
        case MG_POP_SF:
	    fprintf(stderr, "traverseObj: shouldn't be here < %d >\n", 
		    bead->opcode);
            break;
        case 40: case 41: case 42: case 43: case 44: case 45: case 46:
        case 47: case 48: case 76: case 77: case 78: case 79: case 80:
        case 81: case 82: case 94: /* Ignored transformation record types */
        case MG_MATRIX:
        case MG_COMMENT:
        case MG_PATH:
            ptr += bead->length;
            break;
        case MG_LONGID:
            pLongID = (mgLongID *) ptr;
            ptr += bead->length;
            break;
        default:
	    fprintf(stderr, "traverseObj: unsupported node %ld < %s > found\n", 
		     bead->opcode, 
		     opcodeNames[ (bead->opcode < MG_OPCODENAMES_MAX)
		     ? bead->opcode: 0 ]);
            ptr += bead->length;
            break;
        }
    }

    return ptr;
}

/*******************************************************************************
*     Function: traverse 
*        Class: FltReader
*       Access: Public
*  Description: Traverses the flight file structure 
*        Input:
*       Output:
*******************************************************************************/
char *FltReader::traverse(Bead *parent, char *tree, char *end, long depth)
{
    register char       *ptr;
    long                done;
    mgBead              *bead;
    Bead                *newParent = parent;
    ushort		pushflag=MG_PUSH;  

    done = FALSE;

    ptr = tree;

    while(buffer < bufend && !done)
    {
        bead = (mgBead *) ptr;
        switch (bead->opcode)
        {
        case MG_HEADER:
            mgComment       *comment;
            mgLongID        *pLongID;
            mgMatrix        *matrix;
            mgReplicate     *replicate;

            LookAhead( ptr + bead->length, &comment, &matrix, &replicate,
			   &pLongID );

	    mgHeader *phead;
	    phead = (mgHeader *)bead;
	    HeaderBead *hdr;
	    hdr = new HeaderBead(*phead);
	    if ((dbase->add(*hdr)) == 0)
	    	cerr << "traverse: Couldn't add header bead\n";
            ptr += bead->length;
            break;
        case MG_COLOR_TABLE:
	    mgColorTable *pctb;
	    pctb = (mgColorTable *)bead;
	    ColorTableBead *ctab;
            ctab = new ColorTableBead(*pctb);
	    if ((dbase->add(*ctab)) == 0)
		cerr << "traverse: Couldn't add color table bead\n";
            ptr += bead->length;
            break;
        case MG_MATERIAL_TABLE:
	    mgMaterialTable *pmtb;
	    pmtb = (mgMaterialTable *)bead;
	    MaterialTableBead *mtab;
	    mtab = new MaterialTableBead(*pmtb);
	    if ((dbase->add(*mtab)) == 0)
		cerr << "traverse: Couldn't add material table bead\n";
            ptr += bead->length;
            break;
        case MG_TEXTURE_REF:
	    mgTextureRef *ptrtex;
	    ptrtex = (mgTextureRef *)bead;
	    TextureBead *tex;
	    tex = new TextureBead(*ptrtex);
	    if ((dbase->add(*tex)) == 0)
		cerr << "traverse: Couldn't add texture reference bead\n"; 
            ptr += bead->length;
            break;
        case MG_VTXTABLE:
            vtable = (mgVertTable *) ptr;
	    VertexTableBead *vtab;
	    vtab = new VertexTableBead(*vtable);
	    if ((dbase->add(*vtab)) == 0)
		cerr << "traverse: Couldn't add vertex table bead\n";
	    register char *vend;
	    vend = ptr + vtable->vtxlen;
	    ptr += vtable->length;
	    // Add all the vertices in the table
	    VertexRecordBead *vert;
	    while (ptr < vend)
	    {
		bead = (mgBead *)ptr;
		vert = new VertexRecordBead(bead->opcode, (void *)ptr);
		if ((dbase->add(*vert)) == 0)
		    cerr << "traverse: Couldn't add vertex record bead\n";	
		ptr += bead->length;
	    }	
	    pushflag = 0;
            break;
        case MG_PUSH:
	    pushflag = MG_PUSH;
            depth++;
            ptr = traverse(newParent, ptr + bead->length, end, depth);
            depth--;    // Decrement depth because previous convTree got POP
	    pushflag = MG_POP;
            if (depth == 0)
                done = TRUE;
            break;
        case MG_POP:
	    // Insert stuff to handle switch beads here 
            ptr += bead->length;
	    pushflag = MG_POP;
	    dbase->pop();
            return ptr;
        case MG_GROUP:
	    {
            mgComment   *comment;
            mgLongID    *pLongID;
            mgMatrix    *matrix;
            mgReplicate *replicate;

            LookAhead( ptr + bead->length, &comment, &matrix, &replicate, 
		       &pLongID );

            mgGroup *mggroup;
	    mggroup = (mgGroup *) bead;
	    GroupBead *grp;
            grp = new GroupBead(*mggroup);
            if ( pLongID )
		grp->Name(pLongID->text);	
            else
		grp->Name(((mgGroup *)bead)->id);
	    if (dbase->add(*grp, pushflag) == 0)
		cerr << "traverse: Couldn't add group bead\n";
            newParent = grp;
            ptr += bead->length;
  	    pushflag = 0;
	    }
            break;
        case MG_DOF:
	    ptr += bead->length;
	    pushflag = 0;
            break;
        case MG_DOFFLOAT:
	    ptr += bead->length;
	    pushflag = 0;
            break;
        case MG_LOD:
	    {
            mgComment       *comment;
            mgLongID        *pLongID;
            mgMatrix        *matrix;
            mgReplicate     *replicate;

            LookAhead( ptr + bead->length, &comment, &matrix, &replicate,
		  	   &pLongID );

	    mgLod *fltlod;
            fltlod = (mgLod *)bead;
	    LodBead *lod;
            lod = new LodBead(*fltlod);
            if ( pLongID )
	   	lod->Name(pLongID->text);
            else
	    	lod->Name(((mgLod *)bead)->id);
	    if ((dbase->add(*lod, pushflag)) == 0)
	    	cerr << "traverse: Couldn't add Lod bead\n";
            newParent = lod;
            ptr += bead->length;
	    pushflag = 0;
	    }
            break;
        case MG_LODFLOAT:
	    {
            mgComment       *comment;
            mgLongID        *pLongID;
            mgMatrix        *matrix;
            mgReplicate     *replicate;

            LookAhead( ptr + bead->length, &comment, &matrix, &replicate,
                           &pLongID );

            mgFLod *fltlod;
            fltlod = (mgFLod *)bead;
	    FLodBead *lod;
            lod = new FLodBead(*fltlod);
            if ( pLongID )
                lod->Name(pLongID->text);
            else
                lod->Name(((mgFLod *)bead)->id);
            if ((dbase->add(*lod, pushflag)) == 0)
                cerr << "traverse: Couldn't add Lod bead\n";
            newParent = lod;
            ptr += bead->length;
	    pushflag = 0;
	    }
            break;
        case MG_OBJECT:
	    {
            mgComment       *comment;
            mgLongID        *pLongID;
            mgMatrix        *matrix;
            mgReplicate     *replicate;

            LookAhead( ptr + bead->length, &comment, &matrix, &replicate,
 			   &pLongID );

            mgObject *mgobj = (mgObject *) bead;
            ObjectBead *obj;
	    obj = new ObjectBead(*mgobj);
            if ( pLongID )
                obj->Name(pLongID->text);
            else
                obj->Name(((mgObject *)bead)->id);
            if ((dbase->add(*obj, pushflag)) == 0)
                cerr << "traverse: Couldn't add object bead\n";
            newParent = obj;
	    ptr += bead->length;
            ptr = traverseObj(ptr, end, 0);
	    pushflag = 0;
	    }
            break;
        case 40: case 41: case 42: case 43: case 44: case 45: case 46:
        case 47: case 48: case 76: case 77: case 78: case 79: case 80:
        case 81: case 82: case 94: /* Ignored transformation record types */
        case MG_COMMENT:
        case MG_LONGID:
        case MG_MATRIX:
        case MG_REPLICATE:
        case MG_INSTANCE_DEF:
        case MG_INSTANCE_REF:
        case MG_EXTERNAL_REF:
        case MG_EYEPOINTS:
        case MG_DEYEPOINTS:
        case MG_BBOX:
        case MG_BOXFLOAT:
        case MG_ROAD:           /* road bead */
        case MG_ZONE_REF:               /* road zone reference bead */
        case MG_LINK:           /* linkage bead */
        case MG_SOUND:          /* sound bead */
        case MG_PATH:           /* path bead */
        case MG_SOUNDS:         /* sound palette */
            ptr += bead->length;
	    pushflag = 0;
	    break;
        case MG_SWITCH:         /* switch bead */
            ptr += bead->length;
	    pushflag = 0;
            break;
        default:
	    fprintf(stderr,"traverse: opcode %ld < %s > not implemented\n",
		     bead->opcode, opcodeNames[ (bead->opcode < 
		     MG_OPCODENAMES_MAX) ? bead->opcode: 0 ]);
            ptr += bead->length;
            break;
        }
    }

    return ptr;
}

/*******************************************************************************
*     Function: Read 
*        Class: FltReader
*       Access: Public
*  Description:
*        Input:
*       Output:
*******************************************************************************/
DataBase *FltReader::Read(char *_fname)
{
    if (_fname == NULL)
    {
	cerr << "read: NULL file name\n";
	return NULL;
    }
    strcpy(fname, _fname);
    if (loadFltFile(fname) == 0)
    {
	cerr << "Error in loadFltFile\n";
	return NULL;
    }
    if (buffer == NULL)
    {
	cerr << "read: file < %s > not loaded\n", fname;
	return NULL;
    }
    if (traverse(NULL, buffer, bufend, 0) == NULL)
    {
	cerr << "traverse failed\n";
	return NULL;
    }
    return (dbase);
}