home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Graphics / graphics-16000.iso / general / raytrace / renaisnc / delaunay.lha / Delaunay / starbase.c < prev    next >
C/C++ Source or Header  |  1992-01-04  |  7KB  |  314 lines

  1. /*
  2.  *  Null device for animation.  Provides stubs but does nothing.
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include "k-Dtree.h"
  7. #include "Delaunay.h"
  8. #include <starbase.c.h>
  9. #include <sbdl.c.h>
  10.  
  11. #ifndef FALSE
  12. #  define FALSE 0
  13. #endif
  14. #ifndef TRUE
  15. #  define TRUE 1
  16. #endif
  17.  
  18.             /* Options that can be changed by the user */
  19. static int ShowTriangles = 1;
  20. static int ShowCells = 0;
  21. static int AnimationFrozen = 0;
  22. static int filedes;
  23. static int fdmouse;
  24. static int Outline=false;
  25.  
  26. AnimateInitialize( XL,YL,XH,YH) 
  27.      double XL, XH, YL, YH;
  28. {
  29.   /* Set up the user coordinate system.
  30.    *  XL is the x minimum
  31.    *  XH is the x maximum
  32.    *  ...
  33.    *
  34.    *  These are in the range [-1,1] as I recall (don't quote me)
  35.    */
  36.   float pl[2][3];
  37.   float r[3];
  38.   float p1[3];
  39.   float p2[3];
  40.   int cmap_size;
  41.   float xl,xh,yl,yh;
  42.   float size;
  43.  
  44.   inquire_sizes(filedes,pl,r,p1,p2,&cmap_size);
  45.   xl = pl[0][0]; yl = pl[0][1];
  46.   xh = pl[1][0]; yh = pl[1][1];
  47.   if ( (xh-xl)*r[0] < (yh-yl)*r[1] )
  48.     size = fabs(xh-xl)*r[0];
  49.   else
  50.     size = fabs(yh-yl)*r[1];
  51.   set_p1_p2(filedes, METRIC, 10., 10., 10., size-10.0, size-10.0, 20.);
  52.   vdc_extent(filedes, -1.0, -1.0, -1.0,   1.0, 1.0, 1.0);
  53. }
  54.  
  55. static void SetPoint(i, cl)
  56. int i;
  57. float cl[];
  58. {
  59.   float clist[20];
  60.   perPoint* pd;
  61.   Point* p;
  62.  
  63.   pd = PointData + i;
  64.   p = Points + i;
  65.   cl[0] = p->x;
  66.   cl[1] = p->y;
  67.   cl[2] = pd->r; cl[2] /= 255.;
  68.   cl[3] = pd->g; cl[3] /= 255.;
  69.   cl[4] = pd->b; cl[4] /= 255.;
  70. }
  71.  
  72. static void SetPoint2(x,y,r,g,b,cl)
  73. float x,y;
  74. short r,g,b;
  75. float cl[];
  76. {
  77.   cl[0] = x;
  78.   cl[1] = y;
  79.   cl[2] = r; cl[2] /= 255.;
  80.   cl[3] = g; cl[3] /= 255.;
  81.   cl[4] = b; cl[4] /= 255.;
  82. }
  83.  
  84. AnimateShadeTriangle( i, j, k )
  85.      register i,j,k;
  86. {
  87.   /* Shade a single triangle
  88.   ** i,j,k are vertex indicies.
  89.   **   Points[ i|j|k ] contains the vertex coordinates
  90.   **   PointData[ i|j|k ] contains the colors
  91.   */
  92.   float clist[20];
  93.  
  94.   if (!ANIMATING || AnimationFrozen || !ShowTriangles) return;
  95.  
  96.   SetPoint(i, clist);
  97.   SetPoint(j, clist+5);
  98.   SetPoint(k, clist+10);
  99.  
  100.   polygon2d(filedes, clist, 3, 0 );
  101.  
  102.   if ( Outline ) {
  103.     SetPoint(i, clist+15);
  104.     clist[2] = clist[3] = clist[4] = 
  105.     clist[7] = clist[8] = clist[9] = 
  106.     clist[12] = clist[13] = clist[14] =
  107.     clist[17] = clist[18] = clist[19] = 1.;
  108.     polyline2d(filedes,clist,4,0);
  109.   }
  110. }
  111.  
  112.  
  113. AnimateShadeCell( Boundary, Sample )
  114.      RectType *Boundary;
  115.      SampleData *Sample;
  116. {
  117.  
  118.   /* Fill a rectangular cell with a single color value */
  119.   /* PointData[ Sample->index ] contains the color. */
  120.   /* Boundary->{xL,yL,xH,yH} contain the rectangle bounds. */
  121.   float clist[20];
  122.   perPoint* pd;
  123.  
  124.   if (!ANIMATING || AnimationFrozen || !ShowCells) return;
  125.   pd = PointData + Sample->index;
  126.  
  127.   SetPoint2(Boundary->xL,Boundary->yL,pd->r,pd->g,pd->b,clist);
  128.   SetPoint2(Boundary->xL,Boundary->yH,pd->r,pd->g,pd->b,clist+5);
  129.   SetPoint2(Boundary->xH,Boundary->yH,pd->r,pd->g,pd->b,clist+10);
  130.   SetPoint2(Boundary->xH,Boundary->yL,pd->r,pd->g,pd->b,clist+15);
  131.   polygon2d(filedes, clist, 4, 0 );
  132. }
  133.  
  134. void Animate (Animation)
  135.      char *Animation;
  136. {
  137.   char* indev="/dev/hil_0.7";
  138.   char* s;
  139.  
  140.   ANIMATING = true;
  141.  
  142.   /* Initialize the graphics hardware */
  143.   filedes = gopen("/dev/crt0",OUTDEV,"hp98731",INIT);
  144.   if ( filedes < 0 ) {
  145.     fprintf(stderr,"gopen failed.\n");
  146.     exit(1);
  147.   }
  148.   shade_mode(filedes, CMAP_FULL|INIT, FALSE);
  149.   mapping_mode(filedes, ISOTROPIC);
  150.   double_buffer(filedes, 0, 24);
  151.   vertex_format(filedes, 3, 3, 1, 0, 1);
  152.  
  153.   if(s=getenv("SB_INDEV")) indev=s;
  154.   fdmouse = gopen(indev,INDEV,"hp-hil",0);
  155.   if ( fdmouse < 0 ) {
  156.     fprintf(stderr,"Can't open mouse.\n");
  157.   }
  158.  
  159.  
  160.  
  161. static void TrianglesReDraw()
  162. {
  163.   register TrianglePointer T;
  164.  
  165.   /* Step through all the triangles calling Animate Shade Triangle
  166.    * for each one.  These gives us a way to redraw on an expose event.
  167.    */
  168.   T = Triangles;
  169.   do
  170.     {
  171.       AnimateShadeTriangle( T->P1, T->P2, T->P3 );
  172.       T = T->Next;
  173.     }  
  174.   while (T != Triangles);
  175. }
  176.  
  177.  
  178. static void CellsReDraw()
  179. {
  180.   /*
  181.    *  Step through the kD-tree redrawing all leaf cells.
  182.    */
  183.   extern HierarchicalRegion Root;
  184.   extern RectType RootBoundary;
  185.   register HierarchicalRegion *Node;
  186.   RectType LeftBoundary, RightBoundary, Boundary, Overlap;
  187.  
  188.   /* This stack is used to avoid the expense of recursion */
  189.   HierarchicalRegion *NodeStack[MAX_DEPTH];
  190.   RectType BoundaryStack[MAX_DEPTH];
  191.   int levelStack[MAX_DEPTH];
  192.   int nStack, level;
  193.  
  194.   /* Start with the root node on the stack */
  195.   nStack = 0;
  196.   NodeStack[nStack] = &Root;
  197.   BoundaryStack[nStack] = RootBoundary;
  198.   levelStack[nStack++] = 0;
  199.  
  200.   while (nStack > 0) {
  201.  
  202.     /* Get the next node to process from the stack */
  203.     Node = NodeStack[--nStack];
  204.     Boundary = BoundaryStack[nStack];
  205.     level    = levelStack[nStack];
  206.     
  207.     /* Follow the tree down a leaf, pushing the side branches.
  208.      */
  209.     while (! IsLeaf(Node)) {
  210.  
  211.       SplitBoundary( TRUE, Node->splitDirection, Node->splitValue,
  212.             &Boundary, &LeftBoundary );
  213.       if (Node->Left) {
  214.     NodeStack[nStack] = Node->Left;
  215.     BoundaryStack[nStack] = LeftBoundary;
  216.     levelStack[nStack++] = level+1;
  217.       }
  218.       SplitBoundary( FALSE, Node->splitDirection, Node->splitValue, 
  219.             &Boundary, &RightBoundary );
  220.       if (Node->Right) {
  221.     if (nStack >= MAX_DEPTH) {
  222.       fprintf( stderr, "Filter stack overflow!\n"  );
  223.       abort();
  224.     }
  225.     NodeStack[nStack] = Node->Right;
  226.     BoundaryStack[nStack] = RightBoundary;
  227.     levelStack[nStack++] = level+1;
  228.       }
  229.  
  230.       /* Get the next node to process from the stack */
  231.       Node = NodeStack[--nStack];
  232.       Boundary = BoundaryStack[nStack];
  233.       level    = levelStack[nStack];
  234.     }
  235.   
  236.     if (Node == NULL)
  237.       continue;        /* Dead end, go back to the stack loop */
  238.   
  239.     AnimateShadeCell( &Boundary, Node->Sample );
  240.   }
  241. }
  242.  
  243.  
  244. static void AnimateReDraw()
  245. {
  246.   
  247.   /* Force a redraw */
  248.   if (ShowTriangles)    TrianglesReDraw();
  249.   if (ShowCells)        CellsReDraw();
  250. }
  251.  
  252.  
  253. AnimateDoEvent()
  254. {
  255.   double range;
  256.  
  257.   if (!ANIMATING) return;
  258.  
  259.   /* Respond to input events  */
  260. }
  261.  
  262.  
  263. int AnimateCheckInput()
  264. {
  265.   int valid;
  266.   int button;
  267.   int update=0;
  268.   static int last=0;
  269.  
  270.   /* Poll event queue and call AnimateDo Event if one is pending */
  271.   /* Look at mouse buttons */
  272.   if ( fdmouse >= 0 ) {
  273.       sample_choice(fdmouse,1,&valid,&button);
  274.       if ( button != 0 ) {
  275.         switch(button){
  276.         case 1:/* left */
  277.         if ( button != last )
  278.             Outline = !Outline;
  279.         break;
  280.         case 2:/* middle */
  281.         if ( button != last )
  282.             AnimationFrozen = !AnimationFrozen;
  283.         break;
  284.         case 3:/* right */
  285.         if ( button != last ) {
  286.             ShowCells = !ShowCells;
  287.             ShowTriangles = !ShowTriangles;
  288.             clear_view_surface(filedes);
  289.         }
  290.         break;
  291.         default:
  292.         break;
  293.         }
  294.         update = 1;
  295.       }
  296.       last = button;
  297.   }
  298.   if ( update && !AnimationFrozen ) {
  299.     AnimateReDraw();
  300.   }
  301. }
  302.  
  303.  
  304. AnimateExit()
  305. {
  306.   /* Call AnimateDoEvent until the user chooses to stop by ^C or picking
  307.      QUIT from the menu.
  308.   */
  309.   do {AnimateCheckInput();} while (1);
  310.   gclose(filedes);
  311. }
  312.  
  313.