home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / source / rrt.lha / RRT / RRT.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-31  |  9.8 KB  |  395 lines

  1. ;/* Realtime Ray Tracer
  2. lc -b1 -ccit -v -D RRT.c
  3. quit
  4. */
  5.  
  6. #include "rrt.h"
  7.  
  8. /***** Declarations for CBACK *****/
  9. long _BackGroundIO = 0;
  10. long _stack = 10000;
  11. char *_procname = "RRT";
  12. long _priority = 50;
  13.  
  14. int CXBRK(void) { return(0); }        /* Disable Lattice CTRL-C handling */
  15. int chkabort(void) { return(0); }        /* really */
  16.  
  17. /*****  Libraries and HardWare  *****/
  18. struct IntuitionBase *IntuitionBase=NULL;
  19. struct GfxBase       *GfxBase=NULL;
  20. extern struct Custom __far custom;
  21.  
  22. /*****  Graphics Data  *****/
  23. extern unsigned char far BM0[40000];
  24. extern unsigned char far BM1[32000];
  25.  
  26. unsigned char *BMraw[2]={&BM0[0],&BM1[0]};
  27.  
  28. char FastRead[8000];
  29. char *RStartPlane;
  30. ULONG output;
  31.  
  32. extern unsigned char far SILVERFOX[1];
  33. extern unsigned char far ImageDataBall[1];
  34. extern ULONG far ReflectPlane[62];
  35.  
  36. struct UCopList   usercopper;
  37.  
  38. /****
  39. struct BitMap
  40.   { UWORD BytesPerRow, Rows;
  41.     UBYTE Flags, Depth;
  42.     UWORD   pad;
  43.     PLANEPTR Planes[8]; }
  44. ****/
  45. struct BitMap BMap[] =
  46. {{ 40, 200, 0, 5, 0, &BM0[0], &BM0[8000], &BM0[16000], &BM0[24000],
  47.    &BM0[32000], NULL, NULL, NULL },
  48.  { 40, 200, 0, 5, 0, &BM1[0], &BM1[8000], &BM1[16000], &BM1[24000],
  49.    &BM0[32000], NULL, NULL, NULL },
  50.  { 40, 200, 0, 5, 0, &BM1[0], &BM1[0], &BM1[0], &BM1[0],
  51.    &BM1[0], NULL, NULL, NULL },
  52.  { 40, 200, 0, 1, 0, &BM0[32000], NULL, NULL, NULL, NULL, NULL, NULL, NULL }
  53. };
  54.  
  55. struct RasInfo  *DB_rinfo = NULL;
  56. struct ViewPort *vport = NULL;
  57. USHORT CurrentFront = 0;
  58. USHORT NextFront = 1;
  59.  
  60. struct NewScreen NewScreenStructure = {
  61.     0,0,    /* screen XY origin relative to View */
  62.     320,200,    /* screen width and height */
  63.     5,    /* screen depth (number of bitplanes) */
  64.     0,1,    /* detail and block pens */
  65.     SPRITES,    /* display modes for this screen */
  66.     CUSTOMSCREEN|CUSTOMBITMAP,    /* screen type */
  67.     NULL,    /* pointer to default screen font */
  68.     NULL,    /* screen title */
  69.     NULL,    /* first in list of custom screen gadgets */
  70.     &BMap[0]    /* pointer to custom BitMap structure */
  71. };
  72.  
  73. struct Screen *MyScreen=NULL;
  74.  
  75. struct NewWindow NewWindowStructure1 = {
  76.     0,0,    /* window XY origin relative to TopLeft of screen */
  77.     320,200,    /* window width and height */
  78.     0,1,    /* detail and block pens */
  79.     0,    /* IDCMP flags */
  80.     /* other window flags */
  81.     SIMPLE_REFRESH|BORDERLESS|RMBTRAP|NOCAREREFRESH|BACKDROP|ACTIVATE,
  82.     NULL,    /* first gadget in gadget list */
  83.     NULL,    /* custom CHECKMARK imagery */
  84.     NULL,    /* window title */
  85.     NULL,    /* custom screen pointer */
  86.     NULL,    /* custom bitmap */
  87.     1,1,    /* minimum width and height */
  88.     -1,-1,    /* maximum width and height */
  89.     CUSTOMSCREEN    /* destination screen type */
  90. };
  91.  
  92. struct Window *MyWindow=NULL;
  93.  
  94. UWORD ColorPalettes[][32] =
  95. {
  96.   { 0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
  97.     0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
  98.     0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
  99.     0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000 },
  100.   { 0x000,0xFFF,0xFEF,0xEDF,0xDCF,0xDBF,0xCAF,0xC9F, /* SilverFox Colors */
  101.     0xB8F,0xB6F,0xA5F,0xA4F,0x93F,0x92F,0x81F,0x80F,
  102.     0xF00,0xFFF,0xFEF,0xEDF,0xDCF,0xDBF,0xCAF,0xC9F,
  103.     0xB8F,0xB6F,0xA5F,0xA4F,0x93F,0x92F,0x81F,0x80F },
  104.   { /* Reflect Colors */
  105.     0x0F0,0xF00,0xD00,0xB00,0x900,0x800,0x600,0x400,
  106.     0x0F0,0xAAF,0x99D,0x88B,0x779,0x668,0x556,0x444,
  107.     0x03A,0x03A,0x03A,0x03A,0x03A,0x03A,0x03A,0x03A,
  108.     0x03A,0x03A,0x03A,0x03A,0x03A,0x03A,0x03A,0x03A
  109.  }
  110. };
  111.  
  112. short Xarray[31][32];
  113.  
  114. BLIT_PARMS BP={NULL};
  115. BLIT_PARMS BP_SCAN={NULL};
  116. BLIT_PARMS BP_CLEAR={NULL};
  117.  
  118. extern ULONG far NOMOUSE[];
  119.  
  120. /* * * * * * * * * * * EXTERNAL ROUTINES * * * * * * * * * */
  121. VOID __regargs UnPackByteRun(UBYTE *from, UBYTE *to);
  122. VOID __regargs ComputeInterColor(UWORD *from, UWORD *to, UWORD step);
  123. VOID __regargs WaitFRAMES(UWORD frames);
  124. VOID __regargs FTB(UBYTE *a0);
  125. VOID __regargs SLAM_BLITTER(BLIT_PARMS *a0);
  126.  
  127. VOID __regargs ReadPoint(long int sX_d0, long int sY_d1);
  128.  
  129. /***************  Beginning of code  ***************/
  130.  
  131. VOID __regargs SetView(USHORT which)
  132. {
  133.   CurrentFront=which;
  134.   NextFront=1-CurrentFront;
  135.  
  136.   DB_rinfo->BitMap = &BMap[which];
  137. //  WaitBOVP(vport); ScrollVPort(vport);
  138.   MakeScreen(MyScreen); RethinkDisplay();
  139. }
  140.  
  141. VOID CleanUp()
  142. {
  143.   if (DB_rinfo!=NULL)
  144.     { vport->UCopIns = NULL; SetView(0); WaitFRAMES(2); RemakeDisplay(); }
  145.   if (MyWindow!=NULL)
  146.     CloseWindow(MyWindow);
  147.   if (MyScreen!=NULL)
  148.     { FreeCopList(usercopper.CopList);
  149.       CloseScreen(MyScreen); }
  150.   if (IntuitionBase!=NULL)
  151.     CloseLibrary((struct Library *)IntuitionBase);
  152.   if (GfxBase!=NULL)
  153.     CloseLibrary((struct Library *)GfxBase);
  154.   LED_ON;  ON_SPRITE
  155.   exit(0);
  156. }
  157.  
  158. VOID GridZAP(short from,short to,short amount)
  159. {
  160.   BLIT_PARMS *myblit=&BP_SCAN;
  161.  
  162.   myblit->bpt=(USHORT *)(&BM1[from*40]);
  163.   myblit->dpt=(USHORT *)(&BM0[to*40]);
  164.   myblit->size=BLTSIZE(20,amount);
  165.   SLAM_BLITTER(myblit);
  166. }
  167.  
  168.  
  169. VOID InitStuff()
  170. {
  171.   register USHORT loop;
  172.   struct RastPort *RPort;
  173.  
  174.   LED_OFF;
  175.  
  176. /**** Open Libraries ****/
  177.   if (
  178.     ((GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",0))==NULL) ||
  179.     ((IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0))==NULL)
  180.      )
  181.     { CleanUp(); }
  182.  
  183.   if ((MyScreen=(struct Screen *) OpenScreen(&NewScreenStructure))==NULL)
  184.     CleanUp();
  185.   vport = &(MyScreen->ViewPort);
  186.   LoadRGB4(vport,ColorPalettes[0],32);
  187.  
  188. /*****  Set Up CopperList *****/
  189.   CINIT(&usercopper,200)
  190.   CWait(&usercopper,37,0); CBump(&usercopper);
  191.   for(loop=1;loop!=32;loop++)
  192.     {
  193.       CMove(&usercopper,(long)&custom.color[loop],ColorPalettes[2][loop]);
  194.       CBump(&usercopper);
  195.     }
  196.   CWait(&usercopper,161,0); CBump(&usercopper);
  197.   for(loop=1;loop!=32;loop++)
  198.     {
  199.       CMove(&usercopper,(long)&custom.color[loop],ColorPalettes[1][loop]);
  200.       CBump(&usercopper);
  201.     }
  202.   CEND(&usercopper)
  203.  
  204. /***** WINDOW OPEN *****/
  205.  
  206.   NewWindowStructure1.Screen = MyScreen;
  207.   if ((MyWindow=(struct Window *) OpenWindow(&NewWindowStructure1))==NULL)
  208.     CleanUp();
  209.  
  210.   ShowTitle(MyScreen,FALSE);
  211.   SetPointer(MyWindow,(short*)&NOMOUSE[0],1,16,0,0);
  212.   WaitFRAMES(1);
  213.  
  214. /*** Set Up the intuition data for using BM1 as double buffer to BM0 ***/
  215.   DB_rinfo = vport->RasInfo;
  216.  
  217. /*** Show the SilverFox Logo  ***/
  218.   UnPackByteRun(SILVERFOX,BM0);
  219.   for(loop=1; loop<=16; loop++)
  220.     { WaitFRAMES(3); ComputeInterColor(ColorPalettes[0],ColorPalettes[1],loop); }
  221.   BltBitMap(&BMap[0],0,0,&BMap[1],0,0,320,200,192,15,0);
  222.   for(loop=1; loop!=107; loop++)
  223.     { WaitFRAMES(1); GridZAP(loop,1,loop); }
  224.   for(loop=1; loop!=36; loop++)
  225.     { WaitFRAMES(2);
  226.       GridZAP(loop+107,loop,107);
  227.       GridZAP(177-loop,177-loop,20); }
  228.  
  229.  
  230.   vport->UCopIns = &usercopper; WaitFRAMES(2); RemakeDisplay();
  231.   RPort=MyWindow->RPort;
  232.   SetAPen(RPort,16);
  233.   for(loop=0;loop!=10;loop++)
  234.     {
  235.       Move(RPort,53+loop,50);
  236.       Draw(RPort,53+loop,150);
  237.       Move(RPort,155+loop,50);
  238.       Draw(RPort,155+loop,150);
  239.       Move(RPort,257+loop,50);
  240.       Draw(RPort,257+loop,150);
  241.       Move(RPort,0,95+loop);
  242.       Draw(RPort,319,95+loop);
  243.     }
  244.  
  245.   BltBitMap(&BMap[1],0,0,&BMap[2],0,0,320,200,0xE0,15,0);
  246.   BltBitMap(&BMap[2],2,0,&BMap[3],2,40,316,107,0x60,1,0);
  247.   BltClear(BM1,8000,0);
  248.   BltBitMap(&BMap[0],0,0,&BMap[2],0,0,320,200,0xE0,31,0);
  249.   BltBitMap(&BMap[2],0,0,&BMap[3],0,0,320,200,0xC0,1,0);
  250.   BltBitMap(&BMap[0],0,0,&BMap[1],0,0,320,200,0xC0,15,0);
  251.  
  252.   RStartPlane=FastRead; /* For ReadPoint */
  253.   memcpy(FastRead,&BM0[32000],8000);
  254. }
  255.  
  256. VOID __regargs DrawBall(USHORT x, USHORT y, USHORT whichBMAP)
  257. {
  258.   register BLIT_PARMS *myblit=&BP;
  259.   USHORT xshift;
  260.  
  261.   xshift=(x&15); y*=40; x>>=4; x<<=1; y+=x;
  262.  
  263.   myblit->con0=(ABC|ABNC|ANBC|ANBNC)|(DEST|SRCA)|(xshift<<ASHIFTSHIFT);
  264.   myblit->apt=(USHORT *)(ImageDataBall);
  265.   myblit->dpt=(USHORT *)(&(BMraw[whichBMAP][y]));
  266.  
  267.   SLAM_BLITTER(myblit);
  268. }
  269.  
  270. VOID InitBP()
  271. {
  272.   register BLIT_PARMS *myblit=&BP;
  273.  
  274.   myblit->dmod=40-6;
  275.   myblit->amod=4-6;
  276.   myblit->afwm=0xFFFF;
  277.   myblit->adda=124;
  278.   myblit->addcd=8000;
  279.   myblit->size=BLTSIZE(3,31);
  280.   myblit->planes=4;
  281.  
  282.   myblit=&BP_SCAN;
  283.  
  284.   myblit->bmod=-40;
  285.   myblit->con0=(ABC|ABNC|NABC|NABNC)|(DEST|SRCB);
  286.   myblit->addb=8000;
  287.   myblit->addcd=8000;
  288.   myblit->planes=4;
  289.  
  290.   myblit=&BP_CLEAR;
  291.  
  292.   myblit->bmod=0;
  293.   myblit->con0=(DEST);
  294.   myblit->addcd=8000;
  295.   myblit->planes=1;
  296.   myblit->size=BLTSIZE(20,31+3*4);
  297. }
  298.  
  299. VOID DoDemo()
  300. {
  301.   register short xx,yy;
  302.   short x,y,xdir,ydir,xxx,yyy;
  303.   ULONG *place;
  304.  
  305.   x=160; y=45;
  306.   xdir=-1; ydir=1;
  307.  
  308.   while((CurrentFront!=0)||(! LEFTMOUSE))
  309.     {
  310.       x+=xdir; if((x<10)||(x>278)) xdir=-xdir;
  311.       y+=ydir; if((y<40)||(y>123)) ydir=-ydir;
  312.       place=&ReflectPlane[0];
  313.       BP_CLEAR.dpt=(WORD *)&BMraw[NextFront][(y-6)*40];
  314.       for(yy=0;yy!=31;yy++)
  315.         {
  316.           if((yy&7)==0) SLAM_BLITTER(&BP_CLEAR);
  317.           output=0;
  318.           for(xx=0;xx!=31;xx++)
  319.             {
  320.               xxx=Xarray[yy][xx]+x;
  321.               if((xxx>0)&&(xxx<319))
  322.                 {
  323.                   yyy=Xarray[xx][yy]+y;
  324.                   if ((yyy>0)&&(yyy<199)) ReadPoint(xxx,yyy);
  325.                 }
  326.               output+=output;
  327.             }
  328.             *(place++)=output;
  329.         }
  330.       DrawBall(x,y,NextFront);
  331.       SetView(NextFront);
  332.     }
  333. }
  334.  
  335. VOID CalculateMap()
  336. {
  337.   short  y,y1,x,x1;
  338.   long   x2;
  339.   double r;
  340.  
  341.   for(y=0; y<31; y++)
  342.     {
  343.       y1=y-15;
  344.       for(x=0; x<32; x++)
  345.         {
  346.           x1=x-15;
  347.           x2=(256-(y1*y1)-(x1*x1));
  348.           if (x2>0)
  349.             {
  350.               r=32.0/sqrt((double)x2);
  351.               r*=((double)x1);
  352.               r+=15.5;
  353.               x2=(short)r;
  354.               Xarray[y][x]=x2;
  355.             }
  356.           else
  357.             {
  358.               Xarray[y][x]=9999;
  359.             }
  360.         }
  361.     }
  362. }
  363.  
  364. VOID Finish()
  365. {
  366.   short loop;
  367.   struct RastPort *rport;
  368.  
  369.   rport=MyWindow->RPort;
  370.   SetAPen(rport,0);
  371.   for(loop=0;loop!=65;loop++)
  372.     {
  373.       Move(rport,loop,36+loop);
  374.       Draw(rport,319-loop,36+loop);
  375.       Draw(rport,319-loop,163-loop);
  376.       Draw(rport,loop,163-loop);
  377.       Draw(rport,loop,36+loop);
  378.     }
  379.   vport->UCopIns = NULL; SetView(0); WaitFRAMES(2); RemakeDisplay();
  380.   for(loop=1; loop<=16; loop++)
  381.     { WaitFRAMES(3); ComputeInterColor(ColorPalettes[1],ColorPalettes[0],loop); }
  382.  
  383. }
  384.  
  385. main()
  386. {
  387.   InitBP();
  388.   CalculateMap();
  389.   InitStuff();
  390.   DoDemo();
  391.   Finish();
  392.   CleanUp();
  393. }
  394.  
  395.