home *** CD-ROM | disk | FTP | other *** search
/ Altsys Virtuoso 2.0K / virtuoso_20k.iso / DemoApps / Graphics / 2D_3D / Tester3D / Source / Context3D.m < prev    next >
Encoding:
Text File  |  1990-07-18  |  2.6 KB  |  122 lines

  1. /* Context3D.m */
  2.  
  3. #import "Context3D.h"
  4. #import "View3D.h"
  5. #import <dpsclient/wraps.h>
  6.  
  7. @implementation Context3D
  8. + newFrame:(NXRect *)frameRect
  9. {
  10.     self=[super newFrame:frameRect];    
  11.     pictureDistance=1500.0;
  12.     clippingDistance=1.0;
  13.     currentPoint.x=0.0;
  14.     currentPoint.y=0.0;
  15.     currentPoint.z=0.0;
  16.     [self setContentView:[View3D new]];
  17.     return self;
  18. }
  19.  
  20. - moveto:(vector3D *)where
  21. {
  22.     currentPoint=*where;
  23.     return self;
  24. }
  25.  
  26. - lineto:(const vector3D *)where
  27. {
  28.     NXPoint start,end;
  29.     vector3D current,to;
  30.     if(currentPoint.z > -clippingDistance) {        // we make to be the point
  31.         if(where->z > -clippingDistance) {            // less than clippingDistance
  32.             currentPoint=*where;                            // if there is one.
  33.             return self;                                    // if both to and currentPoint
  34.         }                                                        // are closer than 
  35.         to=currentPoint;                                    // clippingDistance, we return
  36.         current=*where;                                    // out.
  37.     }
  38.     else {
  39.         to=*where;
  40.         current=currentPoint;
  41.     }
  42.     if(to.z > -clippingDistance) {                    // if to is closer than
  43.         float temp;                                            // clippingDistance, we
  44.         vector3D tempVect;                                // know from the above
  45.         temp=current.z+clippingDistance;                // steps that current
  46.         temp=temp/(current.z-to.z);                    // is not.
  47.         tempVect.x=to.x-current.x;                        // We recalculate to such that
  48.         tempVect.y=to.y-current.y;                        // it is at the intersection
  49.         tempVect.z=to.z-current.z;                        // of the line segment and the
  50.         to.x=current.x + tempVect.x*temp;            // clipping plane.
  51.         to.y=current.y + tempVect.y*temp;
  52.         to.z=current.z + tempVect.z*temp;                
  53.     }
  54.     
  55.     // here we do a simple projection, since we now know that both current and
  56.     // to are in front of the clipping plane.
  57.         
  58.     start.x=current.x*(-pictureDistance)/current.z;
  59.     start.y=current.y*(-pictureDistance)/current.z;
  60.     end.x=to.x*(-pictureDistance)/to.z;
  61.     end.y=to.y*(-pictureDistance)/to.z;
  62.     PSmoveto(start.x,start.y);
  63.     PSlineto(end.x,end.y);
  64.     PSstroke();
  65.     currentPoint=*where;
  66.     return self;    
  67. }
  68.  
  69. - polygon:(vector3D *)vertices howMany:(int)count
  70. {
  71.     int ctr;
  72.     [self moveto:&vertices[0]];
  73.     for(ctr=1;ctr<count;++ctr)
  74.         [self lineto:&vertices[ctr]];
  75.     [self lineto:&vertices[0]];
  76.     return self;
  77. }
  78.  
  79. - contentView
  80. {
  81.     return contentView;
  82. }
  83.  
  84. - setContentView:anObject
  85. {
  86.     contentView=anObject;
  87.     [anObject setSuperView:self];
  88.     return self;
  89. }
  90.  
  91. - setPictureDistance:(float)dist
  92. {
  93.     if(dist > 0.0) {
  94.         pictureDistance=dist;
  95.         return self;
  96.     }
  97.     return nil;
  98. }
  99.  
  100. - setClippingDistance:(float)dist
  101. {
  102.     if(dist > 0.0) {
  103.         clippingDistance=dist;
  104.         return self;
  105.     }
  106.     return nil;
  107. }
  108.  
  109. - drawSelf:(const NXRect *) rects:(int)rectCount
  110. {
  111.     [contentView display];
  112.     return self;
  113. }
  114.  
  115. - free
  116. {
  117.     [contentView free];
  118.     return [super free];
  119. }
  120.  
  121. @end
  122.