home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / IBPalettes / WW3DKit / RIBTranslate.m < prev    next >
Encoding:
Text File  |  1995-03-22  |  5.6 KB  |  248 lines

  1. // copyright 1993 Michael B. Johnson; some portions copyright 1994, MIT
  2. // see COPYRIGHT for reuse legalities
  3. //
  4.  
  5. #import "RIBTranslate.h"
  6.  
  7. @implementation RIBTranslate
  8.  
  9. + initialize { return [RIBTranslate setVersion:1], self; }
  10.  
  11. - init
  12. {
  13.   [super init];
  14.   dx = 1.0;
  15.   dy = 0.0;
  16.   dz = 0.0;
  17.  
  18.   N3D_CopyMatrix(N3DIdentityMatrix, myMatrix);
  19.   myMatrix[0][3] = dx;
  20.   myMatrix[1][3] = dy;
  21.   myMatrix[2][3] = dz;
  22.  
  23.   return self;
  24. }
  25.  
  26. - awake
  27. {
  28.   [super awake];
  29.   N3D_CopyMatrix(N3DIdentityMatrix, myMatrix);
  30.   myMatrix[3][0] = dx;
  31.   myMatrix[3][1] = dy;
  32.   myMatrix[3][2] = dz;
  33.  
  34.   return self;
  35. }
  36.  
  37. - setDX:(RtFloat)newDX dy:(RtFloat)newDY dz:(RtFloat)newDZ
  38. {
  39.   dx = newDX;
  40.   dy = newDY;
  41.   dz = newDZ;
  42.  
  43.   N3D_CopyMatrix(N3DIdentityMatrix, myMatrix);
  44.   myMatrix[3][0] = dx;
  45.   myMatrix[3][1] = dy;
  46.   myMatrix[3][2] = dz;
  47.  
  48.   return self;
  49. }
  50.  
  51. - getDX:(RtFloat *)dxPtr dy:(RtFloat *)dyPtr dz:(RtFloat *)dzPtr
  52. {
  53.   *dxPtr = dx;
  54.   *dyPtr = dy;
  55.   *dzPtr = dz;
  56.  
  57.   return self;
  58. }
  59.  
  60. - (BOOL)isLerpable { return YES; }
  61.  
  62. // note: because we've made the WWSampleList "safe" for having
  63. // multiple samples with the same data, it's perfectly valid to return
  64. // yourself or b
  65. - lerpWith:b by:(float)uValue
  66. {
  67.    id      newMe = nil;
  68.   RtFloat  dxA, dyA, dzA;
  69.   RtFloat  dxB, dyB, dzB;
  70.  
  71.    if (([self class] != [b class]) || (uValue <= 0.0))
  72.    {  return self;
  73.    }
  74.  
  75.    if (uValue >= 1.0)
  76.    {  return b;
  77.    }
  78.  
  79.    newMe = [super lerpWith:b by:uValue]; // this makes a copy for us
  80.  
  81.    // okay, now do the specific stuff for this class
  82.    [self getDX:&dxA dy:&dyA dz:&dzA];
  83.    [b getDX:&dxB dy:&dyB dz:&dzB];
  84.    [newMe setDX:(dxA + ((dxB - dxA) * uValue))
  85.              dy:(dyA + ((dyB - dyA) * uValue))
  86.              dz:(dzA + ((dzB - dzA) * uValue))];
  87.  
  88.    return newMe;
  89. }
  90.  
  91. - lerpSelfWith:b by:(float)uValue
  92. {
  93.   RtFloat  dxA, dyA, dzA;
  94.   RtFloat  dxB, dyB, dzB;
  95.  
  96.    if (([self class] != [b class]) || (uValue <= 0.0))
  97.    {  return self;
  98.    }
  99.  
  100.    if (uValue >= 1.0)
  101.    {  return b;
  102.    }
  103.  
  104.    [super lerpSelfWith:b by:uValue]; // this makes a copy for us
  105.  
  106.    // okay, now do the specific stuff for this class
  107.    [self getDX:&dxA dy:&dyA dz:&dzA];
  108.    [b getDX:&dxB dy:&dyB dz:&dzB];
  109.    [self setDX:(dxA + ((dxB - dxA) * uValue))
  110.             dy:(dyA + ((dyB - dyA) * uValue))
  111.             dz:(dzA + ((dzB - dzA) * uValue))];
  112.  
  113.    return self;
  114. }
  115.  
  116.  
  117.  
  118. - (BOOL)isMoot
  119. {
  120.   if (!dx && !dy && !dz)  {  return YES; }
  121.   return NO;
  122. }
  123.  
  124. - (BOOL)theSameAs:otherRIBCommand
  125. {
  126.   RtFloat  theDX, theDY, theDZ;
  127.  
  128.  
  129.   [otherRIBCommand getDX:&theDX dy:&theDY dz:&theDZ];
  130.   if (dx != theDX)
  131.   {  return NO;
  132.   }
  133.   if (dy != theDY)
  134.   {  return NO;
  135.   }
  136.   if (dz != theDZ)
  137.   {  return NO;
  138.   }
  139.  
  140.   return [super theSameAs:otherRIBCommand];
  141. }
  142.  
  143.  
  144.  
  145. - renderSelf:(WW3DCamera *)camera  startingAt:(RtFloat)shutterOpenTime endingAt:(RtFloat)shutterCloseTime
  146. {
  147.   //[myShape logTransformWithMsg:"RiTranslate renderSelf: before"];
  148.   RiTranslate(dx, dy, dz);
  149.   //[myShape logTransformWithMsg:"RiTranslate renderSelf: after"];
  150.  
  151.   return self;
  152. }
  153.  
  154. - transformCTM:(WW3DAttributeState *)attributeState startingAt:(RtFloat)shutterOpenTime endingAt:(RtFloat)shutterCloseTime
  155.   RtMatrix  aMatrix, tmpMatrix;
  156.  
  157.   //[myShape logTransformWithMsg:"RiTranslate transformCTM before"];
  158.  
  159.   [attributeState getTransformMatrix:aMatrix];
  160.   N3DMultiplyMatrix(myMatrix, aMatrix, tmpMatrix);
  161.   [attributeState setTransformMatrix:tmpMatrix];
  162.  
  163.   //[myShape logTransformWithMsg:"RiTranslate transformCTM after"];
  164.  
  165.   //NXLogError("RIBTranslate myMatrix (-transformCTM:startingAt:endingAt):\n");
  166.   //NXLogError("\t%f %f %f %f\n",  myMatrix[0][0], myMatrix[0][1], myMatrix[0][2], myMatrix[0][3]);
  167.   //NXLogError("\t%f %f %f %f\n",  myMatrix[1][0], myMatrix[1][1], myMatrix[1][2], myMatrix[1][3]);
  168.   //NXLogError("\t%f %f %f %f\n",  myMatrix[2][0], myMatrix[2][1], myMatrix[2][2], myMatrix[2][3]);
  169.   //NXLogError("\t%f %f %f %f\n", myMatrix[3][0], myMatrix[3][1], myMatrix[3][2], myMatrix[3][3]);
  170.   //NXLogError("RIBTranslate aMatrix (-transformCTM:startingAt:endingAt):\n");
  171.   //NXLogError("\t%f %f %f %f\n",  aMatrix[0][0], aMatrix[0][1], aMatrix[0][2], aMatrix[0][3]);
  172.   //NXLogError("\t%f %f %f %f\n",  aMatrix[1][0], aMatrix[1][1], aMatrix[1][2], aMatrix[1][3]);
  173.   //NXLogError("\t%f %f %f %f\n",  aMatrix[2][0], aMatrix[2][1], aMatrix[2][2], aMatrix[2][3]);
  174.   //NXLogError("\t%f %f %f %f\n", aMatrix[3][0], aMatrix[3][1], aMatrix[3][2], aMatrix[3][3]);
  175.  
  176.   return self; 
  177. }
  178.  
  179. - (BOOL)isMotionBlurrable { return YES; }
  180.  
  181. - writeEve:(NXStream *)stream atTabLevel:(int)tab
  182. {
  183.    int  i;
  184.  
  185.  
  186.    for (i = 0; i < tab; i++)
  187.    {  NXPrintf(stream, "\t");
  188.    }
  189.    NXPrintf(stream, "Translate %f %f %f;", dx, dy, dz); 
  190.    return self;
  191. }
  192.  
  193. - writeInventorAtTime:(float)currentTime to:(NXStream *)stream atTabLevel:(int)tab
  194. {
  195.    int  i;
  196.  
  197.  
  198.    for (i = 0; i < tab; i++)
  199.    {  NXPrintf(stream, "\t");
  200.    }
  201.    NXPrintf(stream, "Translation {\n");
  202.    for (i = 0; i < (tab+1); i++)
  203.    {  NXPrintf(stream, "\t");
  204.    }
  205.    NXPrintf(stream, "translation %f %f %f\n", dx, dy, dz);
  206.    for (i = 0; i < tab; i++)
  207.    {  NXPrintf(stream, "\t");
  208.    }
  209.    NXPrintf(stream, "}");
  210.  
  211.    return self;
  212.  
  213. }
  214.  
  215. #define typeVector "fff"
  216. #define typeValues &dx, &dy, &dz
  217.  
  218. - read:(NXTypedStream*)stream 
  219. {
  220.     int version;
  221.     [super read:stream];
  222.  
  223. NX_DURING
  224.     version = NXTypedStreamClassVersion(stream,"RIBTranslate");
  225.     if (version == 0) NXReadTypes(stream,"i",&version), version=1;
  226.     if (version == 1)
  227.     {  NXReadTypes(stream, typeVector, typeValues);
  228.     } 
  229. NX_HANDLER
  230.    NXLogError("in read: %s, exception [%d] raised.\n", 
  231.                  [[self class] name], NXLocalHandler.code);
  232.    return nil;
  233. NX_ENDHANDLER
  234.     return self;
  235. }
  236.  
  237. - write:(NXTypedStream*)stream 
  238. {
  239.     [super write:stream];
  240.  
  241.     NXWriteTypes(stream,typeVector, typeValues);
  242.  
  243.     return self;
  244. }
  245.  
  246. @end
  247.