home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C++ / Snippets / QD3D Juggler / Juggler Sources / CProp.cp < prev    next >
Encoding:
Text File  |  1995-12-27  |  3.3 KB  |  145 lines  |  [TEXT/CWIE]

  1. //
  2. //    CProp.cp
  3. //
  4. //    class CProp
  5. //    Class for manipulating a QD3D object that may be
  6. //    spun and tossed independently.
  7. //
  8. //    by James Jennings
  9. //    Started November 23, 1995
  10. //
  11.  
  12. #include "CProp.h"
  13. #include "CBoxMaker.h"
  14. #include "CClubMaker.h"
  15. #include "QD3D Debug Macros.h"
  16. #include "StQ3Disposer.h"
  17.  
  18. //
  19. //    class CProp
  20. //
  21. CProp::CProp()
  22.     : mModel(0), mThing(0), mTranslation(0), mSpin(0), mTilt(0)
  23. {
  24.     // Does nothing: Be sure to call MakeModel after construction.
  25. }
  26.  
  27. CProp::CProp(const CProp & prop)
  28.     : mModel(0), mThing(0), mTranslation(0), mSpin(0), mTilt(0)
  29. {
  30.     SignalPStr_("\pHaven't written CProp copy constructor yet.");
  31. }
  32.                 //    CProp::CProp(const TQ3FileObject & source);
  33. CProp::~CProp()
  34. {
  35.     Dispose();
  36. }
  37.  
  38. void
  39. CProp::Dispose()
  40. {    // Safely dispose of everything.
  41.     if (mModel)
  42.         ::Q3Object_Dispose(mModel);
  43.     mModel = 0;
  44.     if (mThing)
  45.         ::Q3Object_Dispose(mThing);
  46.     mThing = 0;
  47.     if (mTranslation)
  48.         ::Q3Object_Dispose(mTranslation);
  49.     mTranslation = 0;
  50.     if (mSpin)
  51.         ::Q3Object_Dispose(mSpin);
  52.     mSpin = 0;
  53.     if (mTilt)
  54.         ::Q3Object_Dispose(mTilt);
  55.     mTilt = 0;
  56. }
  57.  
  58. void
  59. CProp::SetPosition(TQ3Point3D &where)
  60. {    // Set the position of the prop.
  61.     // Note: TQ3Point3D and TQ3Vector3D have the same "structure" and can be cast.
  62.     TQ3Status status = ::Q3TranslateTransform_Set(mTranslation,(TQ3Vector3D*) &where);
  63.     ThrowIfQ3Fail_(status);
  64. }
  65.  
  66. void
  67. CProp::SetSpinAngle(float theta)
  68. {
  69.     TQ3Status status = ::Q3RotateAboutPointTransform_SetAngle(mSpin, theta);
  70.     ThrowIfQ3Fail_(status);
  71. }
  72.  
  73. void
  74. CProp::SetTiltAngle(float theta)
  75. {
  76.     TQ3Status status = ::Q3RotateAboutPointTransform_SetAngle(mTilt, theta);
  77.     ThrowIfQ3Fail_(status);
  78. }
  79.  
  80. void
  81. CProp::MakeModel(CObjectMaker<TQ3GeometryObject> &aThing, TQ3Point3D &anOrigin)
  82. {
  83.     try {
  84.         // create the transforms
  85.         TQ3Vector3D moveOrigin;
  86.         ::Q3Vector3D_Set(&moveOrigin, -anOrigin.x, -anOrigin.y, -anOrigin.z );
  87.         TQ3TransformObject origin = ::Q3TranslateTransform_New(&moveOrigin);
  88.         ThrowIfNil_(origin);
  89.         StQ3Disposer ori(origin);
  90.         
  91.         TQ3Vector3D translate    = {0,0,0};
  92.         mTranslation = ::Q3TranslateTransform_New(&translate);
  93.         ThrowIfNil_(mTranslation);
  94.         
  95.         TQ3Point3D    center        = {0,0,0};
  96.         TQ3RotateAboutPointTransformData    spin;
  97.         spin.axis = kQ3AxisX;
  98.         spin.radians = 0.0;
  99.         spin.about = center;
  100.         mSpin = ::Q3RotateAboutPointTransform_New(&spin);
  101.         ThrowIfNil_(mSpin);
  102.         
  103.         spin.axis = kQ3AxisZ;
  104.         mTilt = ::Q3RotateAboutPointTransform_New(&spin);
  105.         ThrowIfNil_(mTilt);
  106.         
  107.         // Define a shading type for the group
  108.         TQ3ShaderObject        illuminationShader;
  109.         illuminationShader = ::Q3PhongIllumination_New();
  110.         StQ3Disposer ill(illuminationShader);
  111.     
  112.         MakeThing(aThing);
  113.         
  114.         // Make a group to put it all in
  115.         mModel = ::Q3DisplayGroup_New();
  116.         ThrowIfNil_(mModel);
  117.         
  118.         TQ3GroupPosition    pos;
  119.         pos = ::Q3Group_AddObject(mModel, illuminationShader);
  120.         ThrowIf_(pos==0);
  121.         // Note the order of the transforms. The mThing is last.
  122.         pos = ::Q3Group_AddObject(mModel, mTranslation);
  123.         ThrowIf_(pos==0);
  124.         pos = ::Q3Group_AddObject(mModel, mTilt);
  125.         ThrowIf_(pos==0);
  126.         pos = ::Q3Group_AddObject(mModel, mSpin);
  127.         ThrowIf_(pos==0);
  128.         pos = ::Q3Group_AddObject(mModel, origin);    // center it
  129.         ThrowIf_(pos==0);
  130.         pos = ::Q3Group_AddObject(mModel, mThing);    // the prop
  131.         ThrowIf_(pos==0);
  132.     } 
  133.     catch (...) {
  134.         Dispose();
  135.         throw;
  136.     }
  137. }
  138.  
  139. void
  140. CProp::MakeThing(CObjectMaker<TQ3GeometryObject> &aThing)
  141. {
  142.     mThing = ::Q3Shared_GetReference(aThing.Get());
  143.     ThrowIfNil_(mThing);
  144. }
  145.