home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer) / NeXT_Developer-3.3.iso / NextDeveloper / Examples / AppKit / BackspaceViews / Teapot / TeapotView.m < prev    next >
Encoding:
Text File  |  1993-06-18  |  4.3 KB  |  212 lines

  1. #import <appkit/appkit.h>
  2. #import "TeapotView.h"
  3. #import "Teapot.h"
  4.  
  5. //    TeapotView -- by sam streeper 920704
  6. //    borrowing heavily from sources by Bill Bumgarner and Dave Springer.
  7.  
  8. @implementation TeapotView
  9.  
  10. - initFrame:(const NXRect *) theRect
  11. {
  12.     // camera position points
  13.     RtPoint fromP = {0,0,-5.0}, toP = {0,0,0};
  14.  
  15.     // light position points
  16.     RtPoint lFromP = {30,20,-20};
  17.     RtPoint lFromP2 = {-10,10,-10};
  18.     RtPoint lFromP3 = {0,-10,-10};
  19.  
  20.     // the various 3Dkit object id''s that we will initialize here
  21.     id ambientLight;
  22.     id aLight;
  23.  
  24.     [super initFrame:theRect];
  25.     [self setEyeAt:fromP toward:toP roll:0.0];
  26.     
  27.     theShader=[[N3DShader alloc] init];
  28.     [theShader setUseColor:YES];
  29.     [self getTeapotColor];
  30.     [(N3DShader *)theShader setShader:"plastic"];
  31.  
  32.     teapot=[[Teapot alloc] init];
  33.     teapotsBox=[[N3DShape alloc] init];
  34.     [(N3DShape *) teapot setShader:theShader];
  35.  
  36.     [[self worldShape] linkDescendant:teapotsBox];
  37.     [teapotsBox linkDescendant:teapot];
  38.  
  39.     ambientLight=[[N3DLight alloc] init];
  40.     [ambientLight makeAmbientWithIntensity:0.1];
  41.     [self addLight:ambientLight];
  42.     
  43.     aLight=[[N3DLight alloc] init];
  44.     [aLight makeDistantFrom:lFromP to:toP intensity:0.2];
  45.     [self addLight:aLight];
  46.  
  47.     aLight=[[N3DLight alloc] init];
  48.     [aLight makeDistantFrom:lFromP2 to:toP intensity:0.9];
  49.     [self addLight:aLight];
  50.  
  51.     aLight=[[N3DLight alloc] init];
  52.     [aLight makeDistantFrom:lFromP3 to:toP intensity:0.3];
  53.     [self addLight:aLight];
  54.  
  55.     [self getSurfaceType];
  56.  
  57.     dx = randBetween(.05, .2);
  58.     dy = randBetween(.05, .2);
  59.     dz = randBetween(.05, .2);
  60.     rotationAxis[0] = 0;
  61.     theta = 0;
  62.  
  63.     return self;
  64. }
  65.  
  66. #define PI 3.14159
  67.  
  68. - oneStep
  69. {
  70.     RtMatrix m;
  71.  
  72.     rotationAxis[1] = sin(theta);
  73.     rotationAxis[2] = cos(theta);
  74.     theta += (2.0 * PI / 180.0);
  75.  
  76.     [teapot rotateAngle:5 axis:rotationAxis];
  77.     [teapotsBox translate:dx :dy :dz];
  78.  
  79.     [teapotsBox getTransformMatrix:m];
  80.  
  81. // I can use these values directly only because teapot's box isn't rotated
  82.     if (m[3][0] < -1.5) dx = randBetween(.02, .1);
  83.     else if (m[3][0] > 1.5) dx = randBetween(-.02, -.1);
  84.     if (m[3][1] < -1.5) dy = randBetween(.02, .1);
  85.     else if (m[3][1] > 1.5) dy = randBetween(-.02, -.1);
  86.     if (m[3][2] < -2.25) dz = randBetween(.02, .1);
  87.     else if (m[3][2] > 4) dz = randBetween(-.02, -.1);
  88.  
  89.  
  90. //    [self render];
  91.     [self display];
  92.  
  93.     return self;
  94. }
  95.  
  96. - (BOOL) useBufferedWindow
  97. {
  98.     return YES;
  99. }
  100.  
  101. - (const char *)windowTitle
  102. {    return "I'm a little teapot...";
  103. }
  104.  
  105. - inspector:sender
  106. {
  107.     char buf[MAXPATHLEN];
  108.     
  109.     if (!inspectorPanel)
  110.     {
  111.         [NXBundle getPath:buf forResource:"teapot" ofType:"nib" inDirectory:[sender moduleDirectory:"Teapot"] withVersion:0];
  112.         [NXApp loadNibFile:buf owner:self withNames:NO];
  113.  
  114.         [surfaceMatrix selectCellAt:surfaceMatrixSelection :0];
  115.         [colorWell setColor:[theShader color]];
  116.     }
  117.     return inspectorPanel;
  118. }
  119.  
  120. - setResolutionFrom:sender
  121. {
  122.     [teapot setResolution:[sender floatValue]];
  123.     [resolutionTextField setIntValue:[sender floatValue]];
  124.     return self;
  125. }
  126.  
  127. - setColorFrom:sender
  128. {
  129.     float r,g,b;
  130.     char str[100];
  131.  
  132.     [theShader setColor:[sender color]];
  133.  
  134.     NXConvertColorToRGB([sender color], &r, &g, &b);
  135.     sprintf(str, "%5.3f %5.3f %5.3f", r, g, b );
  136.     NXWriteDefault([NXApp appName], "TeapotColor", str);
  137.  
  138.     return self;
  139. }
  140.  
  141. - getTeapotColor
  142. {
  143.     float r = .757, g = .875, b = .898;
  144.     const char *ptr;
  145.  
  146.     ptr = NXGetDefaultValue([NXApp appName], "TeapotColor");
  147.     if (ptr)  sscanf (ptr, "%f %f %f", &r, &g, &b );
  148.  
  149.     [theShader setColor:NXConvertRGBToColor(r,g,b)];
  150.     return self;
  151. }
  152.  
  153. - setSurfaceType:sender
  154. {
  155.     char str[100];
  156.     int type, val = [sender selectedRow];
  157.  
  158.     switch (val)
  159.     {
  160.     case 0:
  161.         type = N3D_WireFrame;            break;
  162.     case 1:
  163.         type = N3D_FacetedSolids;        break;
  164.     case 2:
  165.         type = N3D_SmoothSolids;        break;
  166.     default:
  167.         type = N3D_WireFrame;            break;
  168.     }
  169.  
  170.     surfaceMatrixSelection = val;
  171.  
  172.     sprintf(str,"%d", val);
  173.     NXWriteDefault([NXApp appName], "TeapotSurface", str);
  174.  
  175.     [self setSurfaceTypeForAll:type chooseHider:YES];
  176.     return self;
  177. }
  178.  
  179. - getSurfaceType
  180. {
  181.     const char *ptr;
  182.     int type, val = 0;
  183.  
  184.     ptr = NXGetDefaultValue([NXApp appName], "TeapotSurface");
  185.     if (ptr)
  186.     {
  187.         sscanf(ptr,"%d",&val);
  188.         if (val < 0 || val > 2) val = 0;
  189.     }
  190.  
  191.     surfaceMatrixSelection = val;
  192.  
  193.     switch (val)
  194.     {
  195.     case 0:
  196.         type = N3D_WireFrame;            break;
  197.     case 1:
  198.         type = N3D_FacetedSolids;        break;
  199.     case 2:
  200.         type = N3D_SmoothSolids;        break;
  201.     default:
  202.         type = N3D_WireFrame;            break;
  203.     }
  204.  
  205.     [self setSurfaceTypeForAll:type chooseHider:YES];
  206.  
  207.     return self;
  208. }
  209.  
  210.  
  211. @end
  212.