home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / raytrace / radiance / nextrad.lha / NeXtRad / NeXtInterface.m < prev    next >
Encoding:
Text File  |  1993-02-22  |  8.8 KB  |  309 lines

  1. /* NeXtInterface.m */
  2. /* Written by : Jason R. Wilson   2/21/93 */
  3.  
  4. /* this package (along with CView) provides a NeXtStep interface
  5.     for NeXtRad */
  6.  
  7. #import <appkit/Application.h>
  8. #import <appkit/Window.h>
  9. #import "CView.h"
  10. #import <appkit/Menu.h>
  11. #import <appkit/MenuCell.h>
  12. #import <appkit/NXBitmapImageRep.h>
  13. #import <appkit/graphics.h>
  14. #import <stdlib.h>
  15. #import <stdio.h>
  16. #import "NeXtInterface.h"
  17. #import "engine.h"
  18. #import <appkit/Button.h>
  19. #import <appkit/ButtonCell.h>
  20.  
  21. char SceneFileName[256]; /* the name of the scene file */
  22. id myWindow, myView, myMenu, myImage, myButtonCellf, myButtonf,myButtonCellb,myButtonb;
  23. id myButtonCelll,myButtonl,myButtonCellr,myButtonr,myButtonCellu,myButtonu,myButtonCelld,myButtond;
  24. id myButtonCellfast,myButtonfast,myButtonCellslow,myButtonslow;
  25. /* id myOutputImage; */
  26. unsigned char *ScreenBuf;
  27. /* unsigned char *OutputBuf; */
  28. NXRect graphicsRect;
  29. /* NXRect outputRect; */
  30. NXRect buttonRectf,buttonRectb,buttonRectl,buttonRectr,buttonRectu,buttonRectd,buttonRectfast,buttonRectslow;
  31.  
  32. void SetWidth (int width)
  33. {
  34.   winWidth = width;
  35. }
  36.  
  37. void SetHeight (int height)
  38. {
  39.   winHeight = height;
  40. }
  41.  
  42.  
  43. void InitGraphics ()
  44. {
  45.  
  46.   /* create the scene window */
  47.   
  48.   NXSetRect(&graphicsRect,100.0,300.0,(NXCoord)winWidth+100,(NXCoord)winHeight);
  49.   myWindow = [[Window alloc] initContent:&graphicsRect
  50.         style:NX_TITLEDSTYLE
  51.         backing:NX_BUFFERED
  52.         buttonMask:NX_MINIATURIZEBUTTONMASK
  53.         defer:NO];
  54.   [myWindow setTitle:SceneFileName];
  55.   [myWindow display];
  56.   
  57.   /* create the object to store and display the bitmap */
  58.   
  59.   myImage =  [[NXBitmapImageRep alloc]    initData:ScreenBuf
  60.         pixelsWide:winWidth
  61.         pixelsHigh:winHeight
  62.         bitsPerSample:8
  63.         samplesPerPixel:3
  64.         hasAlpha:NO
  65.         isPlanar:NO
  66.         colorSpace:2
  67.         bytesPerRow:winWidth*3
  68.         bitsPerPixel:24];
  69.   
  70.   /* draw the bitmap */
  71.   
  72.   [myImage draw];
  73.   
  74.   /* set up the view object (an instance of CView) */
  75.   
  76.   myView = [[CView alloc] initFrame:&graphicsRect];
  77.   
  78.   /* set up the buttons */
  79.   
  80.   NXSetRect(&buttonRectf,(NXCoord)winWidth+40,(NXCoord)winHeight-100,20,20);  
  81.   myButtonf = [[Button alloc] initFrame:&buttonRectf
  82.                title:"F"
  83.             tag:1
  84.                target:myView
  85.                action:@selector(forward:)
  86.            key:0
  87.             enabled:YES];
  88.   [myView addSubview:myButtonf];
  89.   myButtonCellf = [[ButtonCell alloc] initTextCell:"F"];
  90.   [myButtonCellf drawSelf:&buttonRectf
  91.                 inView:myView];
  92.  
  93.   NXSetRect(&buttonRectb,(NXCoord)winWidth+40,(NXCoord)winHeight-140,20,20);
  94.   myButtonb = [[Button alloc] initFrame:&buttonRectb
  95.            title:"B"
  96.                tag:2
  97.                target:myView
  98.                action:@selector(backward:)
  99.                key:0
  100.                enabled:YES];
  101.   [myView addSubview:myButtonb];
  102.   myButtonCellb = [[ButtonCell alloc] initTextCell:"B"];
  103.   [myButtonCellb drawSelf:&buttonRectb
  104.           inView:myView];
  105.  
  106.   NXSetRect(&buttonRectl,(NXCoord)winWidth+20,(NXCoord)winHeight-120,20,20);
  107.   myButtonl = [[Button alloc] initFrame:&buttonRectl
  108.            title:"L"
  109.                tag:3
  110.                target:myView
  111.                action:@selector(turnleft:)
  112.                key:0
  113.                enabled:YES];
  114.   [myView addSubview:myButtonl];
  115.   myButtonCelll = [[ButtonCell alloc] initTextCell:"L"];
  116.   [myButtonCelll drawSelf:&buttonRectl
  117.           inView:myView];
  118.  
  119.   NXSetRect(&buttonRectr,(NXCoord)winWidth+60,(NXCoord)winHeight-120,20,20);
  120.   myButtonr = [[Button alloc] initFrame:&buttonRectr
  121.            title:"R"
  122.                tag:4
  123.                target:myView
  124.                action:@selector(turnright:)
  125.                key:0
  126.                enabled:YES];
  127.   [myView addSubview:myButtonr];
  128.   myButtonCellr = [[ButtonCell alloc] initTextCell:"R"];
  129.   [myButtonCellr drawSelf:&buttonRectr
  130.           inView:myView];
  131.  
  132.   NXSetRect(&buttonRectu,(NXCoord)winWidth+40,(NXCoord)winHeight-200,20,20);
  133.   myButtonu = [[Button alloc] initFrame:&buttonRectu
  134.            title:"U"
  135.                tag:5
  136.                target:myView
  137.                action:@selector(tiltup:)
  138.                key:0
  139.                enabled:YES];
  140.   [myView addSubview:myButtonu];
  141.   myButtonCellu = [[ButtonCell alloc] initTextCell:"U"];
  142.   [myButtonCellu drawSelf:&buttonRectu
  143.           inView:myView];
  144.  
  145.   NXSetRect(&buttonRectd,(NXCoord)winWidth+40,(NXCoord)winHeight-240,20,20);
  146.   myButtond = [[Button alloc] initFrame:&buttonRectd
  147.            title:"D"
  148.                tag:6
  149.                target:myView
  150.                action:@selector(tiltdown:)
  151.                key:0
  152.                enabled:YES];
  153.   [myView addSubview:myButtond];
  154.   myButtonCelld = [[ButtonCell alloc] initTextCell:"D"];
  155.   [myButtonCelld drawSelf:&buttonRectd
  156.           inView:myView];
  157.  
  158.   NXSetRect(&buttonRectfast,(NXCoord)winWidth+20,(NXCoord)winHeight-320,60,20);
  159.   myButtonfast = [[Button alloc] initFrame:&buttonRectfast
  160.                    title:"Faster"
  161.                tag:7
  162.                target:myView
  163.                action:@selector(movefaster:)
  164.                key:0
  165.                    enabled:YES];
  166.   [myView addSubview:myButtonfast];
  167.   myButtonCellfast = [[ButtonCell alloc] initTextCell:"Faster"];
  168.   [myButtonCellfast drawSelf:&buttonRectfast
  169.         inView:myView];
  170.  
  171.  
  172.   NXSetRect(&buttonRectslow,(NXCoord)winWidth+20,(NXCoord)winHeight-360,60,20);
  173.   myButtonslow = [[Button alloc] initFrame:&buttonRectslow
  174.                    title:"Slower"
  175.                tag:8
  176.                target:myView
  177.                action:@selector(moveslower:)
  178.                key:0
  179.                    enabled:YES];
  180.   [myView addSubview:myButtonslow];
  181.   myButtonCellslow = [[ButtonCell alloc] initTextCell:"Slower"];
  182.   [myButtonCellslow drawSelf:&buttonRectslow
  183.         inView:myView];
  184.  
  185.  
  186.   [myView setOpaque:YES];
  187.   [myWindow setContentView:myView];
  188.   [myWindow makeKeyAndOrderFront:nil];              
  189.   /* set up the menus */
  190.   
  191.   myMenu = [[Menu alloc] initTitle:"NeXtRad"];
  192.   [[myMenu addItem:"clear" action:@selector(clear:) keyEquivalent:'k']
  193.    setTarget:myView];
  194.   [[myMenu addItem:"wireframe" action:@selector(wireframe:) keyEquivalent:'w']
  195.    setTarget:myView];
  196.   [[myMenu addItem:"render" action:@selector(refresh:) keyEquivalent:'r']
  197.    setTarget:myView];
  198.   [[myMenu addItem:"subdivide" action:@selector(subdivide:) keyEquivalent:'s']
  199.    setTarget:myView];
  200.   [[myMenu addItem:"outputPPM" action:@selector(outputPPM:) keyEquivalent:'o']
  201.    setTarget:myView];
  202.   [myMenu addItem:"Quit" action:@selector(terminate:)
  203.     keyEquivalent:'q'];
  204.   [myMenu sizeToFit];
  205.   [NXApp setMainMenu:myMenu];
  206.  
  207.  
  208.  
  209. }
  210.  
  211. void OutputPPM ()
  212. /* write current view area to a .ppm file */
  213.  
  214. {
  215.   FILE *ppmfp;
  216.   char filename[256];
  217.   int loop,loop2;
  218.   int address;
  219.   /* for some reason, the code that is commentted out only works
  220.      on the NeXTDimension ??? */
  221. /*  [myView lockFocus]; */
  222. /*  NXSetRect(&outputRect,0.0,0.0,(NXCoord)winWidth,(NXCoord)winHeight); */
  223. /*  OutputBuf = NULL; */
  224. /*  myOutputImage = [[NXBitmapImageRep alloc]  initData:OutputBuf
  225.     fromRect:&outputRect];    load data into OutputBuf */
  226. /*  OutputBuf = [myOutputImage data]; */
  227.   printf ("Please enter the name of the ppm file that you wish to output:\n");
  228.   scanf ("%s",filename);
  229.   ppmfp = fopen (filename,"w"); /* open ppm output file */
  230.   printf ("writing .ppm file ...\n");
  231.   /* write .ppm file */
  232.   fprintf (ppmfp,"%s\n","P6");  
  233.   fprintf (ppmfp,"%d %d\n",winWidth,winHeight);
  234.   fprintf (ppmfp,"%d\n",255);
  235.   for (loop = 0;loop < winHeight;loop++)
  236.      for (loop2 = 0;loop2 < winWidth;loop2++)
  237.        {
  238.          address = 3*(loop2 + winWidth*loop);
  239.          fprintf (ppmfp,"%c%c%c",ScreenBuf[address],
  240.                       ScreenBuf[address+1],
  241.                                  ScreenBuf[address+2]);
  242.        }
  243.   close (ppmfp);
  244. /*  [myOutputImage free]; */
  245.   printf ("Finished writing .ppm file\n");
  246. /*  [myView unlockFocus]; */
  247. }
  248.  
  249. /***********************************************************************/
  250.  
  251.  
  252. void SetPixel (int x, int y,unsigned char r,unsigned char g,unsigned char b)
  253. /* update a particular pixel in ScreenBuf */
  254. {
  255.   int address;
  256.   address = 3*(x + winWidth*y);
  257.   ScreenBuf[address] = r;
  258.   ScreenBuf[address+1] = g;
  259.   ScreenBuf[address+2] = b;
  260. }
  261.  
  262. void Update (void)
  263. /* redraw the view area */
  264. {
  265.   [myView lockFocus];
  266.   [myImage draw];
  267.   PSflushgraphics(); 
  268.   [myView unlockFocus];
  269. }
  270.  
  271. void DrawButtons (void)
  272. {
  273. /* redraw the buttons */
  274.  
  275.   [myButtonCellf drawSelf:&buttonRectf
  276.     inView:myView];
  277.   [myButtonCellb drawSelf:&buttonRectb
  278.     inView:myView];
  279.   [myButtonCelll drawSelf:&buttonRectl
  280.     inView:myView];
  281.   [myButtonCellr drawSelf:&buttonRectr
  282.     inView:myView];
  283.   [myButtonCellu drawSelf:&buttonRectu
  284.     inView:myView];      
  285.   [myButtonCelld drawSelf:&buttonRectd
  286.     inView:myView];
  287.   [myButtonCellfast drawSelf:&buttonRectfast
  288.     inView:myView];
  289.   [myButtonCellslow drawSelf:&buttonRectslow
  290.     inView:myView];
  291. }
  292.  
  293. /***********************************************************************/
  294.  
  295. void main (void)
  296. /* set-up and start event loop rolling */
  297. {
  298.   [Application new];
  299.   SetWidth (640);
  300.   SetHeight (480);
  301.   ScreenBuf = (unsigned char *)(malloc(winWidth*winHeight*3)); 
  302.   engine(SceneFileName);
  303.   InitGraphics(); 
  304.   [NXApp run];
  305.   [NXApp free];
  306. }
  307.     
  308.  
  309.