home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / VOGLE.ZIP / EXAMPLES / PTETRA.P < prev    next >
Text File  |  2000-02-11  |  4KB  |  213 lines

  1. program tetra;
  2. (*
  3.  * Demonstrate a rotating translating tetrahedron.
  4.  *)
  5.  
  6. #include 'Vogle.h'
  7.  
  8. const
  9.     TETRAHEDRON =    1;
  10.     NSIDES =    3;
  11.     NFACES =    4;
  12.     NPNTS =        4;
  13.  
  14. var
  15.     
  16.     pnts: array[1..NPNTS, 1..3] of real;
  17.     faces: array[1..NFACES, 1..NSIDES] of integer;
  18.     colface: array[1..4] of integer;
  19.  
  20.     device, dev: string_t;
  21.     i: integer;
  22.     rotval, drotval, zeye, R, tx, tz: real;
  23.     do_backface, do_backface_dir, do_fill: boolean;
  24.     c: integer;
  25.  
  26.     procedure init;
  27.     begin
  28.         rotval := 0.0;
  29.         drotval := 5.0;
  30.         zeye := 5.0;
  31.         R := 1.6;
  32.         tx := 0.0;
  33.         tz := R;
  34.         do_backface := false;
  35.         do_backface_dir := false;
  36.         do_fill := false;
  37.         pnts[1, 1] := -0.5;
  38.         pnts[1, 2] :=  0.866;
  39.         pnts[1, 3] := -0.667;
  40.         pnts[2, 1] := -0.5;
  41.         pnts[2, 2] := -0.866;
  42.         pnts[2, 3] := -0.667;
  43.         pnts[3, 1] :=  1.0;
  44.         pnts[3, 2] :=  0.0;
  45.         pnts[3, 3] := -0.667;
  46.         pnts[4, 1] :=  0.0;
  47.         pnts[4, 2] :=  0.0;
  48.         pnts[4, 3] :=  1.334;
  49.  
  50.         faces[1, 1] := 3;
  51.         faces[1, 2] := 2;
  52.         faces[1, 3] := 1;
  53.         faces[2, 1] := 1;
  54.         faces[2, 2] := 2;
  55.         faces[2, 3] := 4;
  56.         faces[3, 1] := 2;
  57.         faces[3, 2] := 3;
  58.         faces[3, 3] := 4;
  59.         faces[4, 1] := 3;
  60.         faces[4, 2] := 1;
  61.         faces[4, 3] := 4;
  62.  
  63.         colface[1] := GREEN;
  64.         colface[2] := YELLOW;
  65.         colface[3] := CYAN;
  66.         colface[4] := MAGENTA;
  67.     end;
  68.  
  69.     (*
  70.      * maketheobject
  71.      *
  72.      *    generate a tetraedron as a series of move draws
  73.      *)
  74.     procedure maketheobject;
  75.     var
  76.         i, j: integer;
  77.         x, y, z: real;
  78.     begin
  79.  
  80.         MakeObj(TETRAHEDRON);
  81.  
  82.             for i := 1 to NFACES do
  83.             begin
  84.                 Color(colface[i]);
  85.                 MakePoly;
  86.                     x := pnts[faces[i, 1], 1];
  87.                     y := pnts[faces[i, 1], 2];
  88.                     z := pnts[faces[i, 1], 3];
  89.                     Move(x, y, z);
  90.                     for j := 2 to NSIDES do
  91.                     begin
  92.                         x := pnts[faces[i, j], 1];
  93.                         y := pnts[faces[i, j], 2];
  94.                         z := pnts[faces[i, j], 3];
  95.                         Draw(x, y, z);
  96.                     end;
  97.                 ClosePoly;
  98.             end;
  99.  
  100.         CloseObj;
  101.     end;
  102.  
  103. begin
  104.  
  105.     init;
  106.     PrefSize(400, 400);
  107.  
  108.     Vinit(device);          (* set up device *)
  109.  
  110.     do_fill := true;
  111.     do_backface := true;
  112.     do_backface_dir := true;
  113.  
  114.     Font('small');
  115.  
  116.     (*
  117.      * Make the tetrahedral object
  118.      *)
  119.     maketheobject;
  120.  
  121.     (*
  122.      * See what to do with the thingo...
  123.      *)
  124.     PolyFill(do_fill);
  125.     BackFace(do_backface);
  126.     BackFaceDir(do_backface_dir);
  127.  
  128.     (*
  129.      * set up a perspective projection with a field of view of
  130.      * 40.0 degrees, aspect ratio of 1.0, near clipping plane 0.1,
  131.      * and the far clipping plane at 1000.0.
  132.      *)
  133.     Perspective(40.0, 1.0, 0.001, 15.0);
  134.     LookAt(0.0, 0.0, zeye, 0.0, 0.0, 0.0, 0.0);
  135.  
  136.     (*
  137.      * Setup drawing into the backbuffer....
  138.      *)
  139.  
  140.     if (BackBuffer < 0) then
  141.     begin
  142.         VgetDev(dev);
  143.         Vexit;
  144.         writeln(, 'The device ', dev, ' can''t support doublebuffering');
  145.         halt;
  146.     end;
  147.  
  148.     repeat
  149.         for i:= 0 to 72 do
  150.         begin
  151.             rotval := i * drotval;
  152.             Color(BLACK);
  153.             Clear;
  154.  
  155.             (*
  156.              * Rotate the whole scene...(this acumulates - hence
  157.              * drotval)
  158.              *)
  159.             Rotate(drotval * 0.1, 'x');
  160.             Rotate(drotval * 0.1, 'z');
  161.  
  162.             Color(RED);
  163.             PushMatrix;
  164.                 PolyFill(false);
  165.                 Rotate(90.0, 'x');
  166.                 Circle(0.0, 0.0, R);
  167.                 PolyFill(do_fill);
  168.             PopMatrix;
  169.  
  170.             Color(BLUE);
  171.             Move(0.0, 0.0, 0.0);
  172.             Draw(tx, 0.0, tz);
  173.             
  174.             (*
  175.              * Remember! The order of the transformations is
  176.              * the reverse of what is specified here in between
  177.              * the pushmatrix and the popmatrix. These ones don't
  178.              * accumulate because of the push and pop.
  179.              *)
  180.             PushMatrix;
  181.                 Translate(tx, 0.0, tz);
  182.                 Rotate(rotval, 'x');
  183.                 Rotate(rotval, 'y');
  184.                 Rotate(rotval, 'z');
  185.                 Scale(0.4, 0.4, 0.4);
  186.                 CallObj(TETRAHEDRON);
  187.             PopMatrix;
  188.  
  189.             tz := R * cos((rotval * 3.1415926535 / 180));
  190.             tx := R * sin((rotval * 3.1415926535 / 180));
  191.  
  192.             SwapBuffers;
  193.  
  194.             c := CheckKey;
  195.             if c = ord('b') then begin
  196.                 do_backface := not do_backface;
  197.                 BackFace(do_backface)
  198.             end else if c = ord('d') then begin
  199.                 do_backface_dir := not do_backface_dir;
  200.                 BackFaceDir(do_backface_dir)
  201.             end else if c = ord('f') then begin
  202.                 do_fill := not do_fill;
  203.                 PolyFill(do_fill)
  204.             end else if c <> 0 then begin
  205.                 Vexit;
  206.                 halt;
  207.             end
  208.         end;
  209.  
  210.     until false;
  211.  
  212. end.
  213.