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

  1. // copyright 1993 Michael B. Johnson; some portions copyright 1994, MIT
  2. // see COPYRIGHT for reuse legalities
  3. //
  4.  
  5. #import "WW3DShape.h"
  6. #import "RIBPatch.h"
  7.  
  8. @implementation RIBPatch
  9.  
  10. + initialize { return [RIBPatch setVersion:1], self; }
  11.  
  12. - (BOOL)hasBoundingBox { return YES; }
  13.  
  14. - init
  15. {
  16.   [super init];
  17.  
  18.   type = NULL;
  19.   return self;
  20. }
  21.  
  22.  
  23. - free
  24. {
  25.   if (type) { free(type); }
  26.   return [super init];
  27. }
  28.  
  29. // this is used only internally, for setting a copy without freeing the original
  30. - _setType:(const char *)newType
  31. {
  32.   if (newType)
  33.   {  type = NXCopyStringBuffer(newType);
  34.   }
  35.   else
  36.   {  type = NULL;
  37.   }
  38.   return self;
  39. }
  40.  
  41. - copyFromZone:(NXZone *)zone
  42. {
  43.    id   newCopy = [super copyFromZone:zone];
  44.  
  45.   [newCopy _setType:type];
  46.   return newCopy;
  47. }
  48.  
  49.  
  50. - setType:(char *)newType
  51.      n:(int)newN tokens:(RtToken *)newTokens parms:(RtPointer *)newParms archiveVector:(char **)newArchiveVector
  52.      printfTypeVector:(int *)newPrintfTypeVector printfNVector:(int *)newPrintfNVector
  53. {  
  54.    type = NXCopyStringBuffer(newType);
  55.   
  56.    [self setN:newN tokens:newTokens parms:newParms archiveVector:newArchiveVector printfTypeVector:newPrintfTypeVector printfNVector:newPrintfNVector];
  57.  
  58.    dirtyBoundingBox = TRUE;
  59.   
  60.    return self;
  61. }
  62.  
  63. - setType:(char *)newType  {  if (type) { free(type); }  type = NXCopyStringBuffer(newType); return self; } 
  64. - (const char *)type { return type; }
  65. - (RtBasis *)uBasis  {  return &uBasis; }
  66. - (RtToken)uBasisToken  {  return uBasisToken; }
  67. - (RtInt)uStep  {  return uStep; }
  68. - (RtBasis *)vBasis  {  return &vBasis; }
  69. - (RtToken)vBasisToken  {  return vBasisToken; }
  70. - (RtInt)vStep  {  return vStep; }
  71.  
  72.  
  73. // note that the code in here is largely stolen from WW3DShape's updateBases 
  74. // function.  The reason we can't use it directly is because of the need to 
  75. // start (potentially) somewhere in the middle of the list of ribCommands.
  76. - brokenGetBases 
  77. {
  78.   int   ourPosition = -1, 
  79.         howManyPositions, i;
  80.   BOOL  foundIt;
  81.   id    cmd = nil, 
  82.         ancestor = [myShape ancestor];  
  83.  
  84.  
  85.   howManyPositions = [[myShape ribCommands] count];
  86.   i = 0;
  87.   foundIt = NO;
  88.   while (!foundIt && (i < howManyPositions))
  89.   {  if (self == [[myShape ribCommands] objectAt:i])
  90.      {  ourPosition = i;
  91.         foundIt = YES;
  92.      }
  93.      else
  94.      {  i++;
  95.      }
  96.   }
  97.   if (!foundIt)
  98.   {  NXLogError("Yikes! couldn't find myself inside myShape (%s)", [myShape shapeName]);
  99.      return nil;
  100.   }
  101.  
  102.   i = (ourPosition - 1);
  103.   foundIt = NO;
  104.   while (!foundIt && (i > 0))
  105.   {  cmd = [[myShape ribCommands] objectAt:i];
  106.      if ([cmd respondsTo:@selector(uBasis)])
  107.      {  foundIt = YES;
  108.      }
  109.      else
  110.      {  i--;
  111.      }
  112.   }
  113.  
  114.   if (foundIt)
  115.   {  N3D_CopyMatrix(*([cmd uBasis]), uBasis);
  116.      uStep = [cmd uStep];
  117.      N3D_CopyMatrix(*([cmd vBasis]), vBasis);
  118.      vStep = [cmd vStep];
  119.   }
  120.   else
  121.   {  if (ancestor)  // get it from our ancestor
  122.      {  N3D_CopyMatrix(*([ancestor uBasis]), uBasis);
  123.         uStep = [ancestor uStep];
  124.         N3D_CopyMatrix(*([ancestor vBasis]), vBasis);
  125.         vStep = [ancestor vStep];
  126.      }
  127.      else // the shape we are in is the root; fill in bezier bases for both u and v
  128.      {  N3D_CopyMatrix(RiBezierBasis, uBasis);
  129.         uStep = 3;
  130.         N3D_CopyMatrix(RiBezierBasis, vBasis);
  131.         vStep = 3;
  132.      }
  133.   }
  134.  
  135.  
  136.   return self;
  137. }
  138.  
  139.  
  140. - getBases 
  141. {
  142.   //WAVE - fix ME!!! boge for now
  143.   N3D_CopyMatrix(RiBezierBasis, uBasis);
  144.   uStep = 3;
  145.   N3D_CopyMatrix(RiBezierBasis, vBasis);
  146.   vStep = 3;
  147.  
  148.   return self;
  149. }
  150.  
  151.  
  152. - calculateBoundingBoxStartingAt:(RtFloat)shutterOpenTime endingAt:(RtFloat)shutterCloseTime
  153. {
  154.   if (![self getBases])
  155.   {  return nil;
  156.   }
  157.  
  158.   // if both are bezier, just call our super's routine, since we 
  159.   // can just check against the points.  
  160.  
  161.   // ya know, we might want to have a string tag along with the bases,
  162.   // just so we don't have to deal with this epsilon bullshit...
  163.  
  164.   if (   (uBasisToken && vBasisToken) 
  165.       && (   ((!strcmp(uBasisToken, RI_BEZIER)) && (!strcmp(vBasisToken, RI_BEZIER)))
  166.           || ((!strcmp(uBasisToken, RI_BSPLINE)) && (!strcmp(vBasisToken, RI_BSPLINE)))
  167.           || ((!strcmp(uBasisToken, RI_BEZIER)) && (!strcmp(vBasisToken, RI_BSPLINE)))
  168.           || ((!strcmp(uBasisToken, RI_BSPLINE)) && (!strcmp(vBasisToken, RI_BEZIER)))))
  169.   {
  170.     [super calculateBoundingBoxStartingAt:shutterOpenTime endingAt:shutterCloseTime];
  171.     dirtyBoundingBox = FALSE; 
  172.     return self;
  173.   }
  174.  
  175.   // otherwise, need to transform one or both...
  176.   // for now, we'll still bail, and just call the super version...
  177.   [super calculateBoundingBoxStartingAt:shutterOpenTime endingAt:shutterCloseTime];
  178.   dirtyBoundingBox = FALSE; 
  179.  
  180.   return self;
  181. }
  182.  
  183.  
  184. - (BOOL)theSameAs:otherRIBCommand 
  185.   if ([self class] != [otherRIBCommand class])
  186.   {  return NO;
  187.   }
  188.   if (type != [(RIBPatch *)otherRIBCommand type])  // if they're tokens, they're the same string pointer
  189.   {  if (strcmp(type, [(RIBPatch *)otherRIBCommand type])) // just make sure by comparing the actual strings....
  190.      {  return NO; // okay, they really are different
  191.      }
  192.   }
  193.  
  194.   // this will check all the parameters...
  195.   return [super theSameAs:otherRIBCommand];
  196. }
  197.  
  198.  
  199. - (BOOL)similarTo:otherRIBCommand 
  200. {
  201.   if ([self class] != [otherRIBCommand class])
  202.   {  return NO;
  203.   }
  204.   if (type != [(RIBPatch *)otherRIBCommand type])  // if they're tokens, they're the same string pointer
  205.   {  if (strcmp(type, [(RIBPatch *)otherRIBCommand type])) // just make sure by comparing the actual strings....
  206.      {  return NO; // okay, they really are different
  207.      }
  208.   }
  209.   return YES;
  210. }
  211.  
  212.  
  213. - renderSelf:(WW3DCamera *)camera startingAt:(RtFloat)shutterOpenTime endingAt:(RtFloat)shutterCloseTime
  214. {
  215.   RiPatchV(type, n, tokens, parms);
  216.  
  217.   return self;
  218. }
  219.  
  220. - (BOOL)isMotionBlurrable { return YES; }
  221.  
  222. - writeEve:(NXStream *)stream atTabLevel:(int)tab
  223. {
  224.    int  i;
  225.  
  226.  
  227.    for (i = 0; i < tab; i++)
  228.    {  NXPrintf(stream, "\t");
  229.    }
  230.    NXPrintf(stream, "Patch %s ", type);
  231.    [super writeParameterList:stream];
  232.    return self;
  233. }
  234.  
  235. #define typeVector "*"
  236. #define typeValues &type
  237.  
  238. - read:(NXTypedStream*)stream 
  239. {
  240.     int version;
  241.     [super read:stream];
  242.  
  243.     version = NXTypedStreamClassVersion(stream,"RIBPatch");
  244.     if (version == 0) NXReadTypes(stream,"i",&version), version=1;
  245.     if (version == 1)
  246.     {  NXReadTypes(stream,typeVector,typeValues);
  247.     } 
  248.     return self;
  249. }
  250.  
  251. - write:(NXTypedStream*)stream 
  252. {
  253.     [super write:stream];
  254.  
  255.     NXWriteTypes(stream,typeVector, typeValues);
  256.  
  257.     return self;
  258. }
  259.  
  260.  
  261. @end
  262.