dataBase.C 16 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
/******************************************************************************\
  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:     dataBase.C
* CLASS:        DataBase
* DESCRIPTION:  A graphical database representation to serve as intermediary
*               for translations between various file formats.
* AUTHOR:       Mike Goslin
* CREATED:      2/7/95
* REVISIONS:
*
*******************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <iostream.h>
#include <time.h>
#include <math.h>

#include "dataBase.H"

/*******************************************************************************
*     Function: Constructor
*        Class: DataBase
*       Access: Public
*  Description: Constructor that provides flight format default header 
*        Input:
*       Output:
*******************************************************************************/
DataBase::DataBase(void)
{
    current = NULL;
    dbase = NULL;
    vpool = NULL;
    hdr = NULL;
    vlist = NULL;
    texlist = NULL;
    ctable = NULL;
    ctableflag = 0;
    addVert = 0;
    pastFirstPush = 0;
}

/*******************************************************************************
*     Function: Constructor
*        Class: DataBase
*       Access: Public
*  Description: Constructor that provides flight format default header 
*        Input:
*       Output:
*******************************************************************************/
DataBase::~DataBase()
{
}

/*******************************************************************************
*     Function: addSibling 
*        Class: DataBase
*       Access: Public 
*  Description: Adds a bead as sibling of current. Current is set to input bead 
*        Input: 
*       Output:
*******************************************************************************/
void DataBase::addSibling(Bead *bead)
{
    current->right(bead);
    bead->left(current);
    bead->up(current->up());
    current = bead;
}

/*******************************************************************************
*     Function: addChild 
*        Class: DataBase
*       Access: Public 
*  Description: Adds a bead as child of current.  Current is set to input bead 
*        Input: 
*       Output:
*******************************************************************************/
void DataBase::addChild(Bead *bead)
{
    current->down(bead);
    bead->up(current);
    current = bead;
}

/*******************************************************************************
*     Function: add 
*        Class: DataBase
*       Access: Public
*  Description: Adds a bead to the database 
*        Input: 
*       Output:
*******************************************************************************/
uint DataBase::add(Bead& bead, short pushflag)
{
    short type;

    if (current != NULL)
    	type = current->Type();

    switch (bead.Type())
    {
	case MG_HEADER:
	    hdr = (HeaderBead *)&bead;
	    // We impose the ordering of header->coltable->mattable->texlist
	    // ->vtxtable
	    if (dbase == NULL)
	    {
		dbase = &bead;
		current = dbase;
	    }
	    else 
	    {
		current = dbase;
		if (current->Type() == MG_HEADER)
		{
		    cerr << "dbase add: tried to add two header beads\n";
		    return 0;
		}
		current->up(&bead);
		bead.down(current);
		dbase = &bead;
	    }	
	    // current remains at lowest spot
	    while (current->down() != NULL)
		current = current->down();
	break;
	case MG_COLOR_TABLE:
	    // We impose the ordering of header->coltable->mattable->texlist
	    // ->vtxtable
	    // Order of insertion must be before first group, but doesn't 
	    // matter otherwise
	    current = dbase;
	    if (current->Type() != MG_HEADER)
	    {
		cerr << "database add: tried to add colortable w/ no header\n";
		return 0;
	    }
	    if (current->down() != NULL)
	    {
		Bead *temp;
		temp = current->down();
		bead.down(temp);
		temp->up(&bead);
	    }
	    current->down(&bead);
	    bead.up(current);
	    // current remains at lowest spot
	    while (current->down() != NULL)
		current = current->down();
	    ctable = (ColorTableBead *)&bead;
	    ctableflag = 1;
	break;
	case MG_MATERIAL_TABLE:
	    // We impose the ordering of header->coltable->mattable->texlist
	    // ->vtxtable
	    // Order of insertion must be before first group, but doesn't 
	    // matter otherwise
	    current = dbase;
	    if (current->Type() != MG_HEADER)
	    {
		cerr << "database add: tried to add a mattable w/ no header\n";
		return 0;
	    }
	    if (current->down() != NULL)
	    {
		Bead *temp;
		temp = current->down();
		if (temp->Type() == MG_COLOR_TABLE)
		{
		    current = temp;
		    if (current->down() != NULL)
		    {
			temp = current->down();
			temp->up(&bead);
			bead.down(temp);
		    }
		}
		else
		{
		    temp->up(&bead);
		    bead.down(temp);
		} 
	    }
	    current->down(&bead);
	    bead.up(current);
	    // current remains at lowest spot
	    while (current->down() != NULL)
		current = current->down();
	break;
	case MG_TEXTURE_REF:
	    // We impose the ordering of header->coltable->mattable->texlist
	    // ->vtxtable
	    // Order of insertion must be before first group, but doesn't 
	    // matter otherwise
	    Bead *thold;
	    if (pastFirstPush) thold = current;
	    current = dbase;
	    if (current->Type() != MG_HEADER)
	    {
		cerr << "database add: tried to add a texref w/ no header\n";
		return 0;
	    }
	    if (texlist == NULL) // Create texture list
	    {
		texlist = (TextureBead *)&bead;
		// Insert texlist into hierarchy 
		Bead *temp;
		temp = current;
		while ((temp->down() != NULL) && (temp->Type() != MG_VTXTABLE))
		{
		    current = temp;
		    temp = temp->down();
		}
		if (temp->Type() != MG_VTXTABLE) // Insert below current
		{
		    temp->down(&bead);
		    bead.up(temp);
		}
		else // Insert above vertex table and below current
		{
		    current->down(&bead);
		    temp->up(&bead);
		    bead.down(temp);
		    bead.up(current);
		}
	    }
	    else
	    {
		TextureBead *tbead = texlist;
		while (tbead->right() != NULL)
		    tbead = (TextureBead *)tbead->right();
		tbead->right(&bead);
	    } 
	    // current remains at lowest spot
	    while (current->down() != NULL)
		current = current->down();
	    if (pastFirstPush) current = thold;
	break;
	case MG_VTXTABLE:
	    // We impose the ordering of header->coltable->mattable->texlist
	    // ->vtxtable
	    // Order of insertion must be before first group, but doesn't 
	    // matter otherwise
	    Bead *hold;
	    if (addVert) hold = current;
	    current = dbase;
	    if (current->Type() != MG_HEADER)
	    {
		cerr << "database add: tried to add a vtxtable w/ no header\n";
		return 0;
	    }
	    while (current->down() != NULL)
		current = current->down();
	    current->down(&bead);
	    bead.up(current);
	    // current remains at vpool
	    current = &bead;
	    vpool = (VertexTableBead *)&bead;
	    if (addVert) current = hold;
	break;
	case MG_DVERT_ABS:
	case MG_DVERT_NORM:
	case MG_DVERT_NORM_TEX:
	case MG_DVERT_ABS_TEX:
	    // current remains at vpool
	    if (vpool == NULL)  // Create the vertex table
	    {
		VertexTableBead vtable;
		addVert = 1;
		if (add(vtable, 0) == 0)
		    return 0;
		addVert = 0;
	    }		
	    VertexRecordBead *vrec;
	    vrec = (VertexRecordBead *)&bead;
	    vpool->addVertex(vrec->Type(), vrec->fltStruct());
	break;
	case MG_GROUP:
	    pastFirstPush = 1;
	    if (!ctableflag)  // Need to insert default color table
	    {
		cerr << "dbase add: No color table - creating default\n";
	    	ColorTableBead ctab;
		Bead *hold = current;
	    	if (add(ctab, 0) == 0) //Add default color table recursively
		    return 0;
		current = hold;
		ctableflag = 1;	
	    }
	    if (vpool == NULL)  // Create the vertex table
	    {
		cerr << "dbase add: No vertex table - creating default\n";
		VertexTableBead vtable;
		Bead *h = current;
		if (add(vtable, 0) == 0)
		    return 0;
		current = h;
	    }		
	    type = current->Type();
	    if (type == MG_VTXTABLE) // Implied push
		pushflag = MG_PUSH;
	    if (pushflag == MG_PUSH) // Allowed parents for group
	    {
		if ((type == MG_GROUP) ||	(type == MG_DOF) ||
		    (type == MG_LOD) ||		(type == MG_LODFLOAT) ||
		    (type == MG_VTXTABLE))
			addChild(&bead);
		else
		{
		    cerr<<"dbase add:tried to add group below invalid parent\n";
		    return 0;
		}
	    }
	    else if ((type == MG_GROUP) ||	(type == MG_OBJECT) ||
		     (type == MG_DOF) ||	(type == MG_LODFLOAT) ||
		     (type == MG_LOD))
	        addSibling(&bead);
	    else
	    {
		cerr << "database add: failed to insert group bead\n";
		return 0;
	    }
	break;
	case MG_LOD:
	case MG_LODFLOAT:
	    // Add these later
	break;
	case MG_OBJECT:
 	    if (pushflag == MG_PUSH)
	    {
		if ((type == MG_GROUP) ||	(type == MG_DOF) ||
		    (type == MG_LOD) ||		(type == MG_LODFLOAT)) 
		    addChild(&bead);
		else
		{
		    cerr << "dbase add: tried to add obj below invalid\n";
		    fprintf(stderr, "type: %d\n", type);
		    return 0;
		}
	    }
	    else if ((type == MG_GROUP) ||	(type == MG_OBJECT) ||
		     (type == MG_DOF) ||	(type == MG_LODFLOAT) ||
		     (type == MG_LOD))
		addSibling(&bead);
 	    else
	    {
		cerr << "dbase add: invalid add of object\n";
		return 0;
	    }
	break;
 	case MG_POLYGON:
 	    if (((pushflag == MG_PUSH) && (type == MG_OBJECT)) ||
		((pushflag == MG_PUSH_SF) && (type == MG_POLYGON)) ||
		 (type == MG_OBJECT))
	   	addChild(&bead);
	    else if (type == MG_POLYGON)
		addSibling(&bead);
 	    else
	    {
		cerr << "dbase add: invalid add of polygon\n";
		return 0;
	    }
	break;
	case MG_VTXLIST:
	    vlist = (VertexListBead *)&bead;
	    if (type == MG_POLYGON)
		addChild(&bead);
	    else
	    {
		cerr << "database add: couldn't insert vtxlist bead\n";
		return 0;
	    }
	break;
	case MG_VTXOFFSET:
       	    vlist->addOffset();  // Increment size of vertex list
	    if ((type == MG_VTXLIST) || (type == MG_VTXOFFSET))
		addSibling(&bead);
	    else
	    {
		cerr << "database add: couldn't insert vtxoffset bead\n";
		return 0;
	    }
	break;
	default:
	    fprintf(stderr, "database add: unknown opcode %ld\n",bead.Type());
	    return 0;
    }
    return 1;
}

/*******************************************************************************
*     Function: printBead
*        Class: DataBase 
*       Access: Public 
*  Description: 
*        Input:
*       Output:
*******************************************************************************/
void DataBase::printBead(const Bead *bead) const
{
    switch (bead->Type())
    {
        case MG_HEADER:
            ((HeaderBead *)bead)->print();
        break;
        case MG_COLOR_TABLE:
            ((ColorTableBead *)bead)->print();
        break;
        case MG_MATERIAL_TABLE:
            ((MaterialTableBead *)bead)->print();
        break;
	case MG_TEXTURE_REF:
	    const Bead *temp;
	    temp = bead;
	    while (temp != NULL)
	    {
	    	((TextureBead *)temp)->print();
		temp = temp->right();
	    }
	break;
        case MG_VTXTABLE:
            ((VertexTableBead *)bead)->printVertPool();
        break;
        case MG_GROUP:
            ((GroupBead *)bead)->print();
        break;
        case MG_LOD:
            ((LodBead *)bead)->print();
        break;
        case MG_LODFLOAT:
            ((FLodBead *)bead)->print();
        break;
        case MG_DOF:
            ((DofBead *)bead)->print();
        break;
        case MG_OBJECT:
            ((ObjectBead *)bead)->print();
        break;
        case MG_POLYGON:
            ((PolygonBead *)bead)->print();
        break;
        case MG_VTXLIST:
            ((VertexListBead *)bead)->print();
        break;
        case MG_VTXOFFSET:
            ((VertexOffsetBead *)bead)->print();
        break;
        default:
            fprintf(stderr, "printBead: unknown opcode < %d >\n", bead->Type());
            exit(1);
        break;
    }
}

/*******************************************************************************
*     Function: traverse
*        Class: DataBase 
*       Access: Private
*  Description: Does an in-order, depth-first traversal of the database
*        Input:
*       Output:
*******************************************************************************/
void DataBase::traverse(Bead *bead) const
{
    Bead *next;

    printBead(bead);
    if (bead->down() != NULL)
    {
        next = bead->down();
        traverse(next);
    }
    if ((bead->Type() != MG_VTXTABLE) && (bead->Type() != MG_TEXTURE_REF) &&
	(bead->right() != NULL))
    {
        next = bead->right();
        traverse(next);
    }
}

/*******************************************************************************
*     Function: getPolyTextureFile 
*        Class: DataBase 
*       Access: Public
*  Description: Given a polygon it returns the texture file applied to the
*		polygon or NULL if it is untextured
*        Input:
*       Output:
*******************************************************************************/
char *DataBase::getPolyTextureFile(PolygonBead *poly)
{
    TextureBead *texbead;
    mgTextureRef tref;
    int index;

    if ((index = poly->textureIndex()) == -1)
	return NULL;

    texbead = texlist;
    while (texbead != NULL) 
    {
	tref = texbead->fltStruct();
	if (tref.index == index)
	    return (tref.file);
    	texbead = (TextureBead *)texbead->right();
    }
    fprintf(stderr, "Couldn't find texture with index: %d\n", index);
    return NULL;
}

/*******************************************************************************
*     Function: print 
*        Class: DataBase
*       Access: Public
*  Description: Debugging tool 
*        Input: 
*       Output:
*******************************************************************************/
void DataBase::printTextures(void) const
{
    Bead *temp;

    temp = texlist;
    while (temp != NULL)
    {
	temp->print();
	temp = temp->right();
    }	
}

/*******************************************************************************
*     Function: print 
*        Class: DataBase
*       Access: Public
*  Description: Debugging tool 
*        Input: 
*       Output:
*******************************************************************************/
void DataBase::print(void) const
{
    traverse(dbase);
}

/*******************************************************************************
*     Function: addColor 
*        Class: DataBase
*       Access: Public
*  Description:  
*        Input: 
*       Output:
*******************************************************************************/
ulong DataBase::addColor(short r, short g, short b, int delta)
{
    short value[3];
	
    value[0] = r; value[1] = g; value[2] = b;
    return (ctable->add(value, delta));
}