home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / fermiVogle.tar.Z / fermiVogle.tar / devel / src / viewp.c < prev    next >
C/C++ Source or Header  |  1996-02-07  |  4KB  |  169 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(void)
  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(void)
  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(float xlow, float xhigh, float ylow, float yhigh)
  99. {
  100.     Token    *tok;
  101.     char    buf[35];
  102.  
  103.     if (!vdevice.initialised) 
  104.         verror("viewport: vogle not initialised");
  105.  
  106.     /*
  107.      *    A few preliminary checks ....
  108.      */
  109.     
  110.     if (xlow >= xhigh) {
  111.         sprintf(buf,"viewport: xleft(%5.2f) >= xright(%5.2f)", xlow, xhigh);
  112.         verror(buf);
  113.     } 
  114.     if (ylow >= yhigh) {
  115.         sprintf(buf,"viewport: ybottom(%5.2f) >= ytop(%5.2f)", ylow, yhigh);
  116.         verror(buf);
  117.     } 
  118.  
  119.     if (vdevice.inobject) {
  120.         tok = newtokens(5);
  121.  
  122.         tok[0].i = VIEWPORT;
  123.         tok[1].f = xlow;
  124.         tok[2].f = xhigh;
  125.         tok[3].f = ylow;
  126.         tok[4].f = yhigh;
  127.  
  128.         return;
  129.     }
  130.  
  131.     /*
  132.      * convert to 0.0 to 1.0
  133.      */
  134.     xlow = xlow / 2 + 0.5;
  135.     xhigh = xhigh / 2 + 0.5;
  136.     ylow = ylow / 2 + 0.5;
  137.     yhigh = yhigh / 2 + 0.5;
  138.  
  139.     /*
  140.      * Make sure the viewport stack knows about us.....
  141.      */
  142.     vdevice.viewport->v.left = xlow;
  143.     vdevice.viewport->v.right = xhigh;
  144.     vdevice.viewport->v.bottom = ylow;
  145.     vdevice.viewport->v.top = yhigh;
  146.  
  147.     vdevice.maxVx = xhigh * vdevice.sizeX;
  148.     vdevice.maxVy = yhigh * vdevice.sizeY;
  149.     vdevice.minVx = xlow * vdevice.sizeX;
  150.     vdevice.minVy = ylow * vdevice.sizeY;
  151.  
  152.     CalcW2Vcoeffs();
  153. }
  154.  
  155. /*
  156.  * getviewport
  157.  *
  158.  *    Returns the left, right, bottom and top limits of the current
  159.  *    viewport.
  160.  */
  161. void
  162. getviewport(float *left, float *right, float *bottom, float *top)
  163. {
  164.     *left = (vdevice.viewport->v.left - 0.5) * 2;
  165.     *right = (vdevice.viewport->v.right - 0.5) * 2;
  166.     *bottom = (vdevice.viewport->v.bottom - 0.5) * 2;
  167.     *top = (vdevice.viewport->v.top - 0.5) * 2;
  168. }
  169.