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
|
/*
* Modification History
*
* 2001-January-9 Jason Rohrer
* Created.
*
* 2001-January-10 Jason Rohrer
* Made class serializable.
* Added a mMembersAllocated flag and made parameterless constructor
* public to help with deserialization.
*
* 2001-January-15 Jason Rohrer
* Fixed a bug in the deserialize() function.
*
* 2001-January-16 Jason Rohrer
* Fixed a bug in the anchor serialization code.
*
* 2001-January-19 Jason Rohrer
* Changed to support multi-texturing.
*
* 2001-January-24 Jason Rohrer
* Added a copy() function.
* Fixed a bug in deserialize().
* Made mMembersAllocated public for copy function.
*
* 2001-February-3 Jason Rohrer
* Updated serialization code to use new interfaces.
*
* 2001-March-11 Jason Rohrer
* Added support for paramatization and temporal animations.
*
* 2001-March-13 Jason Rohrer
* Added interface for getting the number of parameters and animations.
*
* 2001-April-1 Jason Rohrer
* Made copy function virtual. Added a getNewInstance function
* to make derived-class-specific copying easier.
*/
#ifndef PRIMITIVE_3D_INCLUDED
#define PRIMITIVE_3D_INCLUDED
#include <string.h>
#include "minorGems/graphics/RGBAImage.h"
#include "minorGems/math/geometry/Angle3D.h"
#include "minorGems/math/geometry/Vector3D.h"
#include "minorGems/math/geometry/Angle3D.h"
#include "minorGems/io/Serializable.h"
/**
* 3D primitive object.
*
* Comprised of a triangle mesh, texture map, and anchor points.
*
* @author Jason Rohrer
*/
class Primitive3D : public Serializable {
public:
/**
* Constructs a Primitive.
*
* No parameters are copied, so they should not be destroyed
* or re-accessed by caller. All are destroyed when the
* primitive is destroyed.
*
* @param inWide width of mesh in number of vertices.
* Must be even and at least 2.
* @param inHigh height of mesh in number of vertices.
* @param inVertices vertices in row-major order.
* @param inNumTextures number of multi-texture layers for
* this primitive.
* @param inTexture array of textures to map onto mesh.
* Note that no RGBAImage in this array should have a
* selection in it.
* @param inAnchorX x texture anchors for each texture and
* each vertex (indexed as inAnchorX[textNum][vertNum]),
* in range [0,1] (outside this range, texture will wrap).
* @param inAnchorY y texture anchors for each texture and
* each vertex (indexed as inAnchorY[textNum][vertNum]),
* in range [0,1] (outside this range, texture will wrap).
*/
Primitive3D( long inWide, long inHigh, Vector3D **inVertices,
long inNumTextures, RGBAImage **inTexture,
double **inAnchorX, double **inAnchorY );
// construct without initializing any members
// for use by subclasses and for deserialization.
Primitive3D();
char mMembersAllocated;
~Primitive3D();
/**
* Sets whether this primitive is transparent or not. Default
* is not transparent.
*
* @param inTransparent true if this primitive is transparent.
*/
void setTransparent( char inTransparent );
/**
* Gets whether this primitive is transparent or not.
*
* @return true iff this primitive is transparent.
*/
char isTransparent();
/**
* Sets whether this primitive's back face is visible. Default
* is not visible.
*
* @param inIsVisible true if the back face is visible.
*/
void setBackVisible( char inIsVisible );
/**
* Gets whether this primitive's back face is visible or not.
*
* @return true iff the back face is visible.
*/
char isBackVisible();
/**
* Gets a new instance of the derived class type.
*
* Should be equivalent to calling the default constructor
* for the derived class type.
*
* Should be overridden by all derived classes that
* have data members beyond those provided by Primitive3D.
* Note that if the extra data members require copying,
* then the copy() function should be overridden instead
* of simply overriding this function.
*
* Primitive3D::copy() will use this function to produce
* a class instance before doing a copy.
*
* Note that this functionality SHOULD be provided by
* the built-in RTTI, but it doesn't seem to be.
*
* @return an instance of the derived class.
*/
virtual Primitive3D *getNewInstance();
/**
* Copies this primitive.
*
* @return a copy of this primitive, which must be destroyed
* by the caller.
*/
virtual Primitive3D *copy();
/**
* Gets the number of parameters associated with this object.
*
* @return the number of parameters.
*/
virtual int getNumParameters();
/**
* Gets the number of animations associated with this object.
*
* @return the number of animations.
*/
virtual int getNumAnimations();
/*
* Note that the default implementations for all the parameter
* and temporal animation functions do nothing.
*/
/**
* Sets a parameter for this primative.
*
* @param inParameterIndex the index of the parameter to set.
* If an index for a non-existing parameter is specified,
* this call has no effect.
* @param inValue the value to set the parameter to, in [-1, 1].
* The default value for all parameters is 0.
*/
virtual void setParameter( int inParameterIndex, double inValue );
/**
* Gets a parameter for this primative.
*
* @param inParameterIndex the index of the parameter to get.
* If an index for a non-existing parameter is specified,
* 0 is returned.
*
* @return the value of the parameter, in [-1, 1].
* The default value for all parameters is 0.
*/
virtual double getParameter( int inParameterIndex );
/**
* Steps this primitive forward in time.
*
* @param inStepSize the size of the timestep to take.
*/
virtual void step( double inStepSize );
/**
* Starts a temporal animation of this primitive.
* The animation progresses as step() is called repeatedly.
* If called for a non-existent animation or for one that is
* already running, this function has no effect.
*/
virtual void startAnimation( int inAnimationIndex );
/**
* Stops a temporal animation of this primitive. If called
* for a non-existent animation or for one that is not currently
* running, this function has no effect.
*/
virtual void stopAnimation( int inAnimationIndex );
long mHigh, mWide;
long mNumVertices;
Vector3D **mVertices;
Vector3D **mNormals;
long mNumTextures;
// Note that no RGBAImage in this array should have a
// selection in it.
RGBAImage **mTexture;
double **mAnchorX;
double **mAnchorY;
// implement the Serializable interface
virtual int serialize( OutputStream *inOutputStream );
virtual int deserialize( InputStream *inInputStream );
protected:
/**
* Generates standard normals from the vertices.
*
* If subclass is not generating normals, this
* must be called before primitive is drawn, but after
* the vertices have been initialized.
*/
void generateNormals();
char mTransparent;
char mBackVisible;
};
inline Primitive3D::Primitive3D( long inWide, long inHigh,
Vector3D **inVertices, long inNumTextures, RGBAImage **inTexture,
double **inAnchorX, double **inAnchorY )
: mHigh( inHigh ), mWide( inWide ), mNumVertices( inHigh * inWide ),
mNumTextures( inNumTextures ), mVertices( inVertices ),
mTexture( inTexture ), mAnchorX( inAnchorX ), mAnchorY( inAnchorY ),
mTransparent( false ), mBackVisible( false ),
mMembersAllocated( true ) {
generateNormals();
}
inline Primitive3D::Primitive3D()
: mTransparent( false ), mBackVisible( false ),
mMembersAllocated( false ) {
}
inline Primitive3D::~Primitive3D() {
if( mMembersAllocated ) {
int i;
for( i=0; i<mNumVertices; i++ ) {
delete mVertices[i];
delete mNormals[i];
}
for( i=0; i<mNumTextures; i++ ) {
delete [] mAnchorX[i];
delete [] mAnchorY[i];
delete mTexture[i];
}
delete [] mVertices;
delete [] mNormals;
delete [] mAnchorX;
delete [] mAnchorY;
delete [] mTexture;
}
}
inline void Primitive3D::generateNormals() {
mNormals = new Vector3D*[ mNumVertices ];
// for each non-edge vertex
for( int y=0; y<mHigh; y++ ) {
for( int x=0; x<mWide; x++ ) {
int index = y * mWide + x;
// each point is adjacent to up to 4 other points, and thus
// is part of up to 4 edges (or 8 edge pairs)
// if we sum the normals generated by taking the cross of
// each edge pair, we'll have a good approximation of the
// normal at this point.
Vector3D *normalSum = new Vector3D( 0.0, 0.0, 0.0 );
// is there a more elegant way to do this?
Vector3D **edges = new Vector3D*[4];
// watch for boundary cases.
// use NULL for any non-existing edge (i.e., at edge of mesh)
if( x != 0 ) {
edges[0] = new Vector3D( mVertices[ index - 1 ] ); // x - 1
}
else {
edges[0] = NULL;
}
if( y != 0 ) {
edges[1] = new Vector3D( mVertices[ index - mWide ] ); // y - 1
}
else {
edges[1] = NULL;
}
if( x != mWide - 1 ) {
edges[2] = new Vector3D( mVertices[ index + 1 ] ); // x + 1
}
else {
edges[2] = NULL;
}
if( y != mHigh - 1 ) {
edges[3] = new Vector3D( mVertices[ index + mWide ] ); // y + 1
}
else {
edges[3] = NULL;
}
int e;
// subtract this vertex from each to get a directional vector
for( e=0; e<4; e++ ) {
if( edges[e] != NULL ) {
edges[e]->subtract( mVertices[ index ] );
}
}
// now cross and add into sum
for( e=0; e<4; e++ ) {
if( edges[e] != NULL && edges[ (e+1) % 4 ] != NULL ) {
// not that this order of crossing works
// because our cross product is right handed
Vector3D *normal = edges[e]->cross( edges[ (e+1) % 4 ] );
normal->normalize();
// add this normal to our sum
normalSum->add( normal );
delete normal;
}
}
// now delete edges
for( e=0; e<4; e++ ) {
if( edges[e] != NULL ) {
delete edges[e];
}
}
delete [] edges;
// save summed normal as normal for this point
normalSum->normalize();
mNormals[index] = normalSum;
}
}
}
inline void Primitive3D::setTransparent( char inTransparent ) {
mTransparent = inTransparent;
}
inline char Primitive3D::isTransparent() {
return mTransparent;
}
inline void Primitive3D::setBackVisible( char inIsVisible ) {
mBackVisible = inIsVisible;
}
inline char Primitive3D::isBackVisible() {
return mBackVisible;
}
inline Primitive3D *Primitive3D::getNewInstance() {
return new Primitive3D();
}
inline Primitive3D *Primitive3D::copy() {
// get an instance of the derived class, if they've
// overridden getNewInstance()
Primitive3D *primCopy = getNewInstance();
primCopy->mHigh = mHigh;
primCopy->mWide = mWide;
primCopy->mNumVertices = mNumVertices;
primCopy->mVertices = new Vector3D*[mNumVertices];
primCopy->mNormals = new Vector3D*[mNumVertices];
int i;
for( i=0; i<mNumVertices; i++ ) {
primCopy->mVertices[i] = new Vector3D( mVertices[i] );
primCopy->mNormals[i] = new Vector3D( mNormals[i] );
}
primCopy->mNumTextures = mNumTextures;
primCopy->mTexture = new RGBAImage*[mNumTextures];
primCopy->mAnchorX = new double*[mNumTextures];
primCopy->mAnchorY = new double*[mNumTextures];
for( i=0; i<mNumTextures; i++ ) {
primCopy->mTexture[i] = mTexture[i]->copy();
primCopy->mAnchorX[i] = new double[mNumVertices];
primCopy->mAnchorY[i] = new double[mNumVertices];
memcpy( primCopy->mAnchorX[i], mAnchorX[i],
sizeof( double ) * mNumVertices );
memcpy( primCopy->mAnchorY[i], mAnchorY[i],
sizeof( double ) * mNumVertices );
}
primCopy->mMembersAllocated = true;
primCopy->setBackVisible( isBackVisible() );
primCopy->setTransparent( isTransparent() );
return primCopy;
}
inline int Primitive3D::getNumParameters() {
return 0;
}
inline int Primitive3D::getNumAnimations() {
return 0;
}
// the base class versions of these functions do nothing
inline void Primitive3D::setParameter( int inParameterIndex, double inValue ) {
}
inline double Primitive3D::getParameter( int inParameterIndex ) {
return 0;
}
inline void Primitive3D::step( double inStepSize ) {
}
inline void Primitive3D::startAnimation( int inAnimationIndex ) {
}
inline void Primitive3D::stopAnimation( int inAnimationIndex ) {
}
inline int Primitive3D::serialize( OutputStream *inOutputStream ) {
int numBytes = 0;
numBytes += inOutputStream->writeLong( mWide );
numBytes += inOutputStream->writeLong( mHigh );
int i;
// output vertices and normals
for( i=0; i<mNumVertices; i++ ) {
numBytes += mVertices[i]->serialize( inOutputStream );
}
for( i=0; i<mNumVertices; i++ ) {
numBytes += mNormals[i]->serialize( inOutputStream );
}
numBytes += inOutputStream->writeLong( mNumTextures );
// output textures
for( i=0; i<mNumTextures; i++ ) {
numBytes += mTexture[i]->serialize( inOutputStream );
}
// output anchor arrays
for( i=0; i<mNumTextures; i++ ) {
for( int p=0; p<mNumVertices; p++ ) {
numBytes += inOutputStream->writeDouble( mAnchorX[i][p] );
}
}
for( i=0; i<mNumTextures; i++ ) {
for( int p=0; p<mNumVertices; p++ ) {
numBytes += inOutputStream->writeDouble( mAnchorY[i][p] );
}
}
numBytes +=
inOutputStream->write( (unsigned char *)&mTransparent, 1 );
numBytes +=
inOutputStream->write( (unsigned char *)&mBackVisible, 1 );
return numBytes;
}
inline int Primitive3D::deserialize( InputStream *inInputStream ) {
int numBytes = 0;
int i;
if( mMembersAllocated ) {
// delete the old vertices, normals, and anchors
for( i=0; i<mNumVertices; i++ ) {
delete mVertices[i];
delete mNormals[i];
}
for( i=0; i<mNumTextures; i++ ) {
delete [] mAnchorX[i];
delete [] mAnchorY[i];
delete mTexture[i];
}
delete [] mVertices;
delete [] mNormals;
delete [] mAnchorX;
delete [] mAnchorY;
delete [] mTexture;
}
int numBytesRead = 0;
unsigned char *intByteArray = new unsigned char[4];
numBytes += inInputStream->readLong( &mWide );
numBytes += inInputStream->readLong( &mHigh );
mNumVertices = mHigh * mWide;
mVertices = new Vector3D*[mNumVertices];
mNormals = new Vector3D*[mNumVertices];
// input vertices and normals
for( i=0; i<mNumVertices; i++ ) {
mVertices[i] = new Vector3D( 0, 0, 0 );
numBytes += mVertices[i]->deserialize( inInputStream );
}
for( i=0; i<mNumVertices; i++ ) {
mNormals[i] = new Vector3D( 0, 0, 0 );
numBytes += mNormals[i]->deserialize( inInputStream );
}
// input number of textures
numBytes += inInputStream->readLong( &mNumTextures );
mAnchorX = new double*[mNumTextures];
mAnchorY = new double*[mNumTextures];
mTexture = new RGBAImage*[mNumTextures];
// input textures
for( i=0; i<mNumTextures; i++ ) {
mTexture[i] = new RGBAImage( 0, 0 );
numBytes += mTexture[i]->deserialize( inInputStream );
}
// input anchor arrays
for( i=0; i<mNumTextures; i++ ) {
mAnchorX[i] = new double[mNumVertices];
for( int p=0; p<mNumVertices; p++ ) {
numBytes += inInputStream->readDouble( &( mAnchorX[i][p] ) );
}
}
for( i=0; i<mNumTextures; i++ ) {
mAnchorY[i] = new double[mNumVertices];
for( int p=0; p<mNumVertices; p++ ) {
numBytes += inInputStream->readDouble( &( mAnchorY[i][p] ) );
}
}
numBytes +=
inInputStream->read( (unsigned char *)&mTransparent, 1 );
numBytes +=
inInputStream->read( (unsigned char *)&mBackVisible, 1 );
mMembersAllocated = true;
return numBytesRead;
}
#endif
|