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
|
/*
* Modification History
*
* 2001-January-9 Jason Rohrer
* Created. Copied from ObjectGL.
*
* 2001-January-10 Jason Rohrer
* Made class serializable. Added a parameterless constructor
* to facilitate deserialization.
*
* 2001-January-15 Jason Rohrer
* Fixed several bugs in the deserialize() function, as well as in the
* destructor.
*
* 2001-January-16 Jason Rohrer
* Changed to use a Transform3D instead of Vectors, Angles, and scales for
* each primitive in the object.
*
* 2001-January-24 Jason Rohrer
* Added a copy() function.
* Fixed a bug in deserialize().
* Made mMembersAllocated public for copy function.
*
* 2001-January-26 Jason Rohrer
* Fixed a bug in copy().
*
* 2001-January-31 Jason Rohrer
* Fixed bugs in serialize() and deserialize().
*
* 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-March-14 Jason Rohrer
* Added use of Primitive3DFactory for typed subclass primitive
* de/serialization.
*/
#ifndef OBJECT_3D_INCLUDED
#define OBJECT_3D_INCLUDED
#include "Primitive3D.h"
#include "minorGems/math/geometry/Transform3D.h"
#include "minorGems/io/Serializable.h"
#include "Primitive3DFactory.h"
/**
* 3D object.
*
* Comprised of a collection of primitives.
*
* @author Jason Rohrer
*/
class Object3D : public Serializable {
public:
/**
* Constructs an Object.
*
* No parameters are copied, so they should not be destroyed
* or re-accessed by caller. All are destroyed when the
* object is destroyed.
*
* @param inNumPrimitives the number of primitives in this object.
* @param inPrimitives the primitives comprising this object.
* @param inTransform a transform for each object.
*/
Object3D( long inNumPrimitives, Primitive3D **inPrimitives,
Transform3D **inTransform );
Object3D();
~Object3D();
/**
* Copies this object.
*
* @return a copy of this object, which must be destroyed
* by the caller.
*/
Object3D *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 object.
*
* @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 object.
*
* @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 object forward in time.
*
* @param inStepSize the size of the timestep to take.
*/
virtual void step( double inStepSize );
/**
* Starts a temporal animation of this object.
* 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 object. 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 mNumPrimitives;
Primitive3D **mPrimitives;
Transform3D **mTransform;
// implement the Serializable interface
virtual int serialize( OutputStream *inOutputStream );
virtual int deserialize( InputStream *inInputStream );
char mMembersAllocated;
};
inline Object3D::Object3D( long inNumPrimitives, Primitive3D **inPrimitives,
Transform3D **inTransform )
: mNumPrimitives( inNumPrimitives ), mPrimitives( inPrimitives ),
mTransform( inTransform ), mMembersAllocated( true ) {
}
inline Object3D::Object3D()
: mMembersAllocated( false ) {
}
inline Object3D::~Object3D() {
if( mMembersAllocated ) {
for( int i=0; i<mNumPrimitives; i++ ) {
delete mPrimitives[i];
delete mTransform[i];
}
delete [] mTransform;
}
}
inline Object3D *Object3D::copy() {
Object3D *objCopy = new Object3D();
objCopy->mNumPrimitives = mNumPrimitives;
objCopy->mPrimitives = new Primitive3D*[ mNumPrimitives ];
objCopy->mTransform = new Transform3D*[ mNumPrimitives ];
int i;
for( i=0; i<mNumPrimitives; i++ ) {
objCopy->mPrimitives[i] = mPrimitives[i]->copy();
objCopy->mTransform[i] = new Transform3D( mTransform[i] );
}
objCopy->mMembersAllocated = true;
return objCopy;
}
inline int Object3D::getNumParameters() {
return 0;
}
inline int Object3D::getNumAnimations() {
return 0;
}
// the base class versions of these functions do nothing
inline void Object3D::setParameter( int inParameterIndex, double inValue ) {
}
inline double Object3D::getParameter( int inParameterIndex ) {
return 0;
}
inline void Object3D::step( double inStepSize ) {
}
inline void Object3D::startAnimation( int inAnimationIndex ) {
}
inline void Object3D::stopAnimation( int inAnimationIndex ) {
}
inline int Object3D::serialize( OutputStream *inOutputStream ) {
int i;
int numBytes = 0;
numBytes += inOutputStream->writeLong( mNumPrimitives );
// write each primitive
for( i=0; i<mNumPrimitives; i++ ) {
// write a type flag for each primitive
long typeFlag =
Primitive3DFactory::primitive3DToInt( mPrimitives[i] );
numBytes += inOutputStream->writeLong( typeFlag );
// write the primitive
numBytes += mPrimitives[i]->serialize( inOutputStream );
}
// write each primitive's transform
for( i=0; i<mNumPrimitives; i++ ) {
numBytes += mTransform[i]->serialize( inOutputStream );
}
return numBytes;
}
inline int Object3D::deserialize( InputStream *inInputStream ) {
if( mMembersAllocated ) {
// first, delete current contents of object
for( int i=0; i<mNumPrimitives; i++ ) {
delete mPrimitives[i];
delete mTransform[i];
}
delete [] mPrimitives;
delete [] mTransform;
}
int i;
int numBytes = 0;
numBytes += inInputStream->readLong( &mNumPrimitives );
printf( "receiving %d primitives\n", mNumPrimitives );
mPrimitives = new Primitive3D*[mNumPrimitives];
for( i=0; i<mNumPrimitives; i++ ) {
// read the type flag for this primitive
long typeFlag;
numBytes += inInputStream->readLong( &typeFlag );
// construct a new object based on the type flag
mPrimitives[i] =
Primitive3DFactory::intToPrimitive3D( typeFlag );
// deserialize it
numBytes += mPrimitives[i]->deserialize( inInputStream );
}
mTransform = new Transform3D*[mNumPrimitives];
for( i=0; i<mNumPrimitives; i++ ) {
mTransform[i] = new Transform3D();
numBytes += mTransform[i]->deserialize( inInputStream );
}
mMembersAllocated = true;
return numBytes;
}
#endif
|