home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 5 / MA_Cover_5.iso / ppc / mesa / openstep / mesaview / mesaview.m < prev    next >
Encoding:
Text File  |  1998-01-31  |  3.7 KB  |  189 lines

  1. #import <MesaView.h>
  2.  
  3. @interface MesaView(Private)
  4. -(void)_renderImage;
  5. -(void)_createViewport;
  6. -(void)_startDrag;
  7. -(void)_stopDrag:(NSTimer*)the_timer;
  8. @end
  9.  
  10. @implementation MesaView
  11.  
  12. - initWithFrame:(NSRect)frameRect
  13. {
  14.     [super initWithFrame: frameRect];
  15.     ctx = NULL;
  16.     buffer = NULL;
  17.     zoomDist=150;
  18.     spinAngle=0;
  19.     elevAngle=30;
  20.     draggingBase=NO;
  21.     averagingNormals=0;
  22.     whiteBackground=NO;
  23.     timer_object=nil;
  24.     [self _createViewport];
  25.     make_matrix();
  26.     return self;
  27. }
  28.  
  29. - (void)awakeFromNib
  30. {
  31.     [zoomSlider setFloatValue:zoomDist];
  32.     [spinSlider setFloatValue:spinAngle];
  33.     [elevSlider setFloatValue:elevAngle];
  34. }
  35.  
  36.  
  37. - (void)dealloc
  38. {
  39.     OSMesaDestroyContext(ctx);
  40.     free(buffer);
  41.     [timer_object invalidate];
  42.     [timer_object release];
  43.     [super dealloc];
  44. }
  45.  
  46.  
  47. - (BOOL)isOpaque
  48. {
  49.     return YES;
  50. }
  51.  
  52. - (void)_createViewport
  53. {
  54.     if(buffer)
  55.       free(buffer);
  56.     buffer=malloc([self bounds].size.width*[self bounds].size.height*4);
  57.  
  58.     if(ctx)
  59.       OSMesaDestroyContext(ctx);
  60.     ctx = OSMesaCreateContext( GL_RGBA, NULL );
  61.     OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE,
  62.                        [self bounds].size.width, [self bounds].size.height );
  63.     OSMesaPixelStore( OSMESA_Y_UP, 0 );
  64.  
  65.  
  66.     my_init([self bounds].size.width,[self bounds].size.height);
  67. }
  68.  
  69. - (void)resizeWithOldSuperviewSize:(NSSize)old
  70. {
  71.     [super resizeWithOldSuperviewSize:old];
  72.     [self _createViewport];
  73.     [self setNeedsDisplay:YES];
  74. }
  75.  
  76. - (void)drawRect:(NSRect)rect
  77. {
  78.     set_viewpoint(zoomDist, elevAngle, spinAngle, whiteBackground);
  79.     if(draggingBase)
  80.     {
  81.       if(whiteBackground)
  82.        {
  83.         PSsetgray(1);
  84.         NSRectFill(rect);
  85.         PSsetgray(0);
  86.        }
  87.       else
  88.        {
  89.         PSsetgray(0);
  90.         NSRectFill(rect);
  91.         PSsetgray(1);
  92.        }
  93.        outline_scene();
  94.        
  95.     }
  96.     else
  97.     {
  98.       [self _renderImage];
  99.     }
  100. }
  101.  
  102. - (void)_renderImage
  103. {
  104.     NSBitmapImageRep *bitmap;
  105.  
  106.     draw_scene(averagingNormals);
  107.  
  108.     bitmap = [[ NSBitmapImageRep alloc] initWithBitmapDataPlanes:&buffer
  109.                                         pixelsWide:[self bounds].size.width
  110.                                         pixelsHigh:[self bounds].size.height
  111.                                         bitsPerSample:8 samplesPerPixel:4
  112.                                         hasAlpha:YES isPlanar:NO
  113.                                         colorSpaceName:NSDeviceRGBColorSpace
  114.                                         bytesPerRow:0 bitsPerPixel:0];
  115.     [bitmap autorelease];
  116.     [bitmap draw];
  117. }
  118.  
  119. - (void)_startDrag
  120. {
  121.     draggingBase=YES;
  122.     timer_object = [NSTimer
  123.                     scheduledTimerWithTimeInterval:(NSTimeInterval)0.0
  124.                     target:self selector:@selector(_stopDrag:)
  125.                     userInfo:nil repeats:NO];
  126.     [timer_object retain];
  127. }
  128.  
  129. - (void)_stopDrag:(NSTimer*)the_timer
  130. {
  131.     [timer_object invalidate];
  132.     [timer_object release];
  133.     timer_object=nil;
  134.     draggingBase=NO;
  135.     [self setNeedsDisplay:YES];
  136. }
  137.  
  138. - changeZoom:sender
  139. {
  140.     if(!draggingBase)
  141.        [self _startDrag];
  142.     zoomDist = [zoomSlider floatValue];
  143.     [self display];
  144.     return self;
  145. }
  146.  
  147. - changeSpin:sender
  148. {
  149.     if(!draggingBase)
  150.        [self _startDrag];
  151.     spinAngle = [spinSlider floatValue];
  152.     [self display];
  153.     return self;
  154. }
  155.  
  156. - changeElev:sender
  157. {
  158.     if(!draggingBase)
  159.        [self _startDrag];
  160.     elevAngle = [elevSlider floatValue];
  161.     [self display];
  162.     return self;
  163. }
  164.  
  165. - changeNormal:sender
  166. {
  167.     averagingNormals=[sender state];
  168.     [self setNeedsDisplay:YES];
  169.     return self;
  170. }
  171.  
  172. - remakeSurface:sender
  173. {
  174.     make_matrix();
  175.     [self setNeedsDisplay:YES];
  176.     return self;
  177. }
  178.  
  179. - changeBackground:sender
  180. {
  181.     whiteBackground=[sender state];
  182.     [self setNeedsDisplay:YES];
  183.     return self;
  184. }
  185.  
  186.  
  187.  
  188. @end
  189.