home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / VOGLE.ZIP / SRC / VIEWP.C < prev    next >
C/C++ Source or Header  |  2000-02-11  |  4KB  |  171 lines

  1. #include <stdio.h>
  2. #include "vogle.h"
  3.  
  4. static    Vstack    *vsfree = (Vstack *)NULL;
  5.  
  6. /*
  7.  * pushviewport
  8.  *
  9.  * pushes the current viewport on the viewport stack
  10.  *
  11.  */
  12. void
  13. pushviewport()
  14. {
  15.     Vstack    *nvport;
  16.     Token    *tok;
  17.  
  18.     if (!vdevice.initialised)
  19.         verror("pushviewport: vogle not initialised");
  20.     
  21.     if (vdevice.inobject) {
  22.         tok = newtokens(1);
  23.  
  24.         tok->i = PUSHVIEWPORT;
  25.  
  26.         return;
  27.     }
  28.  
  29.     if (vsfree != (Vstack *)NULL) {
  30.         nvport = vdevice.viewport;
  31.         vdevice.viewport = vsfree;
  32.         vsfree = vsfree->back;
  33.         vdevice.viewport->back = nvport;
  34.         vdevice.viewport->v.left = nvport->v.left;
  35.         vdevice.viewport->v.right = nvport->v.right;
  36.         vdevice.viewport->v.bottom = nvport->v.bottom;
  37.         vdevice.viewport->v.top = nvport->v.top;
  38.     } else {
  39.         nvport = (Vstack *)vallocate(sizeof(Vstack));
  40.         nvport->back = vdevice.viewport;
  41.         nvport->v.left = vdevice.viewport->v.left;
  42.         nvport->v.right = vdevice.viewport->v.right;
  43.         nvport->v.bottom = vdevice.viewport->v.bottom;
  44.         nvport->v.top = vdevice.viewport->v.top;
  45.         vdevice.viewport = nvport;
  46.     }
  47. }
  48.  
  49. /*
  50.  * popviewport
  51.  *
  52.  * pops the top viewport off the viewport stack.
  53.  *
  54.  */
  55. void
  56. popviewport()
  57. {
  58.     Token    *tok;
  59.     Vstack    *nvport;
  60.  
  61.     if (!vdevice.initialised)
  62.         verror("popviewport: vogle not initialised");
  63.     
  64.     if (vdevice.inobject) {
  65.         tok = newtokens(1);
  66.  
  67.         tok->i = POPVIEWPORT;
  68.  
  69.         return;
  70.     }
  71.  
  72.     if (vdevice.viewport->back == (Vstack *)NULL)
  73.         verror("popviewport: viewport stack underflow");
  74.     else {
  75.         nvport = vdevice.viewport;
  76.         vdevice.viewport = vdevice.viewport->back;
  77.         nvport->back = vsfree;
  78.         vsfree = nvport;
  79.     }
  80.  
  81.     vdevice.maxVx = vdevice.viewport->v.right * vdevice.sizeX;
  82.     vdevice.maxVy = vdevice.viewport->v.top * vdevice.sizeY;
  83.     vdevice.minVx = vdevice.viewport->v.left * vdevice.sizeX;
  84.     vdevice.minVy = vdevice.viewport->v.bottom * vdevice.sizeY;
  85.  
  86.     CalcW2Vcoeffs();
  87. }
  88.  
  89. /*
  90.  * viewport
  91.  *
  92.  * Define a Viewport in Normalized Device Coordinates
  93.  *
  94.  * The viewport defines that fraction of the screen that the window will
  95.  * be mapped onto.  The screen dimension is -1.0 -> 1.0 for both X & Y.
  96.  */
  97. void
  98. viewport(xlow, xhigh, ylow, yhigh)
  99.     float    xlow, xhigh, ylow, yhigh;
  100. {
  101.     Token    *tok;
  102.     char    buf[35];
  103.  
  104.     if (!vdevice.initialised) 
  105.         verror("viewport: vogle not initialised");
  106.  
  107.     /*
  108.      *    A few preliminary checks ....
  109.      */
  110.     
  111.     if (xlow >= xhigh) {
  112.         sprintf(buf,"viewport: xleft(%5.2f) >= xright(%5.2f)", xlow, xhigh);
  113.         verror(buf);
  114.     } 
  115.     if (ylow >= yhigh) {
  116.         sprintf(buf,"viewport: ybottom(%5.2f) >= ytop(%5.2f)", ylow, yhigh);
  117.         verror(buf);
  118.     } 
  119.  
  120.     if (vdevice.inobject) {
  121.         tok = newtokens(5);
  122.  
  123.         tok[0].i = VIEWPORT;
  124.         tok[1].f = xlow;
  125.         tok[2].f = xhigh;
  126.         tok[3].f = ylow;
  127.         tok[4].f = yhigh;
  128.  
  129.         return;
  130.     }
  131.  
  132.     /*
  133.      * convert to 0.0 to 1.0
  134.      */
  135.     xlow = xlow / 2 + 0.5;
  136.     xhigh = xhigh / 2 + 0.5;
  137.     ylow = ylow / 2 + 0.5;
  138.     yhigh = yhigh / 2 + 0.5;
  139.  
  140.     /*
  141.      * Make sure the viewport stack knows about us.....
  142.      */
  143.     vdevice.viewport->v.left = xlow;
  144.     vdevice.viewport->v.right = xhigh;
  145.     vdevice.viewport->v.bottom = ylow;
  146.     vdevice.viewport->v.top = yhigh;
  147.  
  148.     vdevice.maxVx = xhigh * vdevice.sizeX;
  149.     vdevice.maxVy = yhigh * vdevice.sizeY;
  150.     vdevice.minVx = xlow * vdevice.sizeX;
  151.     vdevice.minVy = ylow * vdevice.sizeY;
  152.  
  153.     CalcW2Vcoeffs();
  154. }
  155.  
  156. /*
  157.  * getviewport
  158.  *
  159.  *    Returns the left, right, bottom and top limits of the current
  160.  *    viewport.
  161.  */
  162. void
  163. getviewport(left, right, bottom, top)
  164.     float    *left, *right, *bottom, *top;
  165. {
  166.     *left = (vdevice.viewport->v.left - 0.5) * 2;
  167.     *right = (vdevice.viewport->v.right - 0.5) * 2;
  168.     *bottom = (vdevice.viewport->v.bottom - 0.5) * 2;
  169.     *top = (vdevice.viewport->v.top - 0.5) * 2;
  170. }
  171.