home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / voglew.zip / DISTRT.C < prev    next >
C/C++ Source or Header  |  1993-05-28  |  2KB  |  111 lines

  1. #include "vogle.h"
  2.  
  3. /*
  4.  * Demonstrate how to use non-square viewports, the associated
  5.  * distortion caused and how to fix it.
  6.  */
  7. main()
  8. {
  9.     float    xfact, yfact;
  10.     char    buf[120];
  11.  
  12.     vinit("mswin");
  13.     color(BLACK);
  14.     clear();
  15.  
  16.     /* 
  17.      * Make the viewport the same size as the screen/window.
  18.       */
  19.     getfactors(&xfact, &yfact);
  20.     viewport(-1.0, xfact, -1.0, yfact);
  21.  
  22.     /*
  23.      * Draw a square. (Looks like a rectangle, if the viewport
  24.      * wasn't "accidentally" square)
  25.      */
  26.     color(1);
  27.     rect(-0.5, -0.5, 0.5, 0.5);
  28.  
  29.     /* 
  30.      * Tell them what it is.
  31.      */
  32.     move2(-1.0, 0.9);
  33.     sprintf(buf,"Distorted square (viewport(-1, %7.3f, -1, %7.3f))",
  34.     xfact, yfact);
  35.     drawstr(buf);
  36.     getkey();
  37.  
  38.  
  39.     /*
  40.      * Fix up the distortion (The actual formula to fix
  41.      * the distortion is (viewport.xmax * (1 + xfact) / 2.0),
  42.      * and similar for the y axis.
  43.      */
  44.     ortho2(-1.0, xfact, -1.0, yfact);
  45.  
  46.     /*
  47.      * Draw another square (Really is square this time)
  48.      */
  49.     color(3);
  50.     rect(-0.5, -0.5, 0.5, 0.5);
  51.  
  52.     /* 
  53.      * Tell them what it is.
  54.      */
  55.     move2(-1.0, -0.9);
  56.     sprintf(buf,"Fixed up square with ortho2(-1, %7.3f, -1, %7.3f)", 
  57.     xfact, yfact);
  58.     drawstr(buf);
  59.     getkey();
  60.  
  61.     /*
  62.      * Do it with world coords going from 0 - 5, 0 - 5.
  63.      * Reset square viewport.
  64.      */
  65.     color(0);
  66.     clear();
  67.  
  68.     viewport(-1.0, 1.0, -1.0, 1.0);
  69.     ortho2(0.0, 5.0, 0.0, 5.0);
  70.     textsize(0.1, 0.1);
  71.  
  72.     /*
  73.      * Square from 1 to 3. (Really is square)
  74.      */
  75.     color(2);
  76.     rect(1.0, 1.0, 3.0, 3.0);
  77.  
  78.     move2(0.0, 4.5);
  79.     drawstr("Square from 0 - 3, 0 - 3");
  80.  
  81.     getkey();
  82.  
  83.     /*
  84.      * Distort it with a non-square viewport.
  85.      */
  86.     viewport(-1.0, xfact, -1.0, yfact);
  87.  
  88.     color(4);
  89.     rect(1.0, 1.0, 3.0, 3.0);
  90.  
  91.     move2(0.0, 0.5);
  92.     drawstr("Distorted square from 0 - 3, 0 - 3");
  93.  
  94.     getkey();
  95.  
  96.     /*
  97.      * Fix the distortion.
  98.      */
  99.     ortho2(0.0, 5.0 * (1.0 + xfact) / 2.0, 0.0, 5.0 * (1.0 + yfact) / 2.0);
  100.     
  101.     color(5);
  102.     rect(1.0, 1.0, 3.0, 3.0);
  103.  
  104.     move2(0.0, 2.5);
  105.     drawstr("Fixed up  square from 0 - 3, 0 - 3");
  106.  
  107.     getkey();
  108.  
  109.     vexit();
  110. }
  111.