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

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