home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Stuff / 3D_Reality / 3D_Reality_API / Examples / Sphere.bproj / Sphere.m < prev    next >
Encoding:
Text File  |  1992-10-19  |  3.2 KB  |  147 lines

  1. #import "../3Dincludes.h"
  2. #import "Sphere.h"
  3. @implementation Sphere
  4.  
  5. static int shapeNumber = 1;
  6. - (int)shapeNumber
  7. {
  8.     return shapeNumber++;
  9. }
  10.  
  11. - calcBoundingBox
  12. {
  13. // xmin & xmax
  14.     boundingBox[0]=-radius;
  15.     boundingBox[1]=radius;
  16.     
  17. // ymin & ymax
  18.     boundingBox[2]=-radius; 
  19.     boundingBox[3]=radius;  
  20.     
  21. // zmin & zmax
  22.     boundingBox[4]=zMin;
  23.     boundingBox[5]=zMax;
  24.     return self;
  25. }
  26.  
  27. - init
  28. {
  29.     [super init];
  30.     radius = 1.;
  31.     zMax = 1.;
  32.     zMin = -1.;
  33.     thetaMax = 360.;
  34.     [self calcBoundingBox];
  35.     return self;
  36. }
  37.  
  38. - setRadius:(RtFloat *) aRad
  39. {
  40.     [super setRadius:aRad];
  41.     [self calcBoundingBox];
  42.     return self;
  43. }
  44.  
  45. - setZMax:(RtFloat *)z;
  46. {
  47.     [super setZMax:z];
  48.     [self calcBoundingBox];
  49.     return self;
  50. }
  51.  
  52. - setZMin:(RtFloat *)z;
  53. {
  54.     [super setZMin:z];
  55.     [self calcBoundingBox];
  56.     return self;
  57. }
  58.  
  59. /* SolidSphere: create a closed sphere */
  60. void
  61. SolidSphere(rad, zmin, zmax)
  62. RtFloat rad, zmin, zmax;
  63. {
  64.     RiSolidBegin(RI_PRIMITIVE);
  65.  
  66.         RiSphere(rad, zmin, zmax, 360.0, RI_NULL);
  67.  
  68.         if(fabs(zmax) < rad)        /* The top is chopped off       */
  69.             RiDisk(zmax, sqrt(rad*rad - zmax*zmax), 360.0, RI_NULL);
  70.         
  71.         if(fabs(zmin) < rad)        /* The bottom is chopped off    */ 
  72.             RiDisk(zmin, sqrt(rad*rad - zmin*zmin), 360.0, RI_NULL);
  73.         
  74.     RiSolidEnd();
  75. }
  76.  
  77.     /*
  78.      *  SolidCylinder() makes a solid cylinder of the given radius, extending
  79.      *  from zmin to zmax along the z axis.
  80.      */
  81. void
  82. SolidCylinder(rad, zmin, zmax)
  83. float rad, zmin, zmax;
  84. {
  85.     RiSolidBegin(RI_PRIMITIVE);
  86.  
  87.         RiCylinder(rad, zmin, zmax, 360.0, RI_NULL);
  88.  
  89.         RiDisk(zmax, rad, 360.0, RI_NULL);       /* Close the top    */
  90.         RiDisk(zmin, rad, 360.0, RI_NULL);       /* Close the bottom */
  91.  
  92.     RiSolidEnd(); 
  93. }
  94.  
  95.  
  96. /* SolidHemisphere: create a solid hemisphere from a sphere and a cylinder*/
  97. void
  98. SolidHemisphere(rad, zmin, zmax)
  99. float rad, zmin, zmax;
  100. {
  101.     RiSolidBegin(RI_INTERSECTION);
  102.         SolidSphere(rad, zmin, zmax);
  103.         /*
  104.          * A cylinder is defined with the same radius as the sphere and
  105.          * rotated so that its bottom bisects the sphere in the x/z plane.
  106.          */
  107.         RiRotate(90.0, 1.0, 0.0, 0.0); 
  108.         /* Rotate the cylinder 90 degrees in x */
  109.         SolidCylinder(rad, 0.0, rad);
  110.     RiSolidEnd();                 /* intersection */
  111. }
  112.  
  113. void
  114. SolidWedge(rad, zmin, zmax, thetamax)
  115. float rad, zmin, zmax, thetamax;
  116. {
  117.     if (thetamax == 180.0) {
  118.         SolidHemisphere(rad, zmin, zmax);
  119.     } else if (thetamax == 360.0) {
  120.         SolidSphere(rad, zmin, zmax);
  121.     } else if (thetamax < 180.0) {
  122.         RiSolidBegin(RI_INTERSECTION);
  123.             SolidHemisphere(rad, zmin, zmax);
  124.             RiRotate(thetamax-180.0, 0.0, 0.0, 1.0);
  125.             SolidHemisphere(rad, zmin, zmax);
  126.         RiSolidEnd();
  127.     } else if (thetamax < 360.0) {
  128.         RiSolidBegin(RI_UNION);
  129.             SolidHemisphere(rad, zmin, zmax);
  130.             RiRotate(thetamax, 0.0, 0.0, 1.0);
  131.             SolidHemisphere(rad, zmin, zmax);
  132.         RiSolidEnd();
  133.     }
  134. }
  135.  
  136. - doRenderSelf: (Camera *) camera
  137. {
  138.     if(![super doRenderSelf:camera]) return nil;
  139.     
  140.     if (shFlags.isSolid)
  141.     SolidWedge(radius,zMin,zMax,thetaMax);
  142.     else 
  143.     RiSphere(radius,zMin,zMax,thetaMax,RI_NULL);
  144.     return self;
  145. }
  146. @end
  147.