home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Homebrewer's Handbook / vr.iso / 3dgraph / pascal / chap2_5.pas < prev    next >
Pascal/Delphi Source File  |  1996-03-19  |  3KB  |  113 lines

  1. program chap2_5;
  2.  
  3. uses graph, crt;
  4.  
  5. const xoffset=640/2;
  6.       yoffset=480/2;
  7.       xscale=1;
  8.       yscale=-1;
  9.       persp=500;
  10.  
  11. type point=record
  12.        xw, yw, zw : real;
  13.        xs, ys, zs : integer;
  14.      end;
  15.  
  16. var vertex : array[1..18] of point;
  17.     edges : array[1..27,1..27] of integer;
  18.     f : text;
  19.     c : integer;
  20.     ch : char;
  21.     xe,ye,ze : real;
  22.  
  23. procedure Initialise_Graphics;
  24. var Graphdriver, Graphmode, Errorcode : integer;
  25. begin
  26.      Graphdriver:=Detect;
  27.      InitGraph(Graphdriver, Graphmode,'');
  28.      Errorcode:=graphresult;
  29.      if Errorcode<>grOk then
  30.      begin
  31.           writeln('Graphics Error: ',GraphErrorMsg(ErrorCode));
  32.           writeln('Program Aborted');
  33.           Halt(1);
  34.      end;
  35.      SetLineStyle(0,0,3);
  36. end;
  37.  
  38. procedure draw_picture;
  39. begin
  40.      ClearDevice;
  41.      for c:=1 to 27 do
  42.           line(vertex[edges[c,1]].xs,vertex[edges[c,1]].ys,vertex[edges[c,2]].xs,vertex[edges[c,2]].ys);
  43. end;
  44.  
  45. procedure transform(x,y,z : real);
  46. begin
  47.      for c:=1 to 18 do
  48.      begin
  49.          vertex[c].zs:=round(vertex[c].zw-z);
  50.          vertex[c].xs:=round(((vertex[c].xw-x)*(persp/vertex[c].zs)*xscale)+xoffset);
  51.          vertex[c].ys:=round(((vertex[c].yw-y)*(persp/vertex[c].zs)*yscale)+yoffset);
  52.      end;
  53. end;
  54.  
  55.  
  56. begin
  57.      clrscr;
  58.      writeln; writeln; writeln; writeln; writeln; writeln;
  59.      writeln('This is from Chapter 2 Step 5 of the Virtual Reality Homebrewer''s Handbook and');
  60.      writeln('shows how to simply move (no turning) around a virtual shape drawn in wireframe.');
  61.      writeln;
  62.      writeln;
  63.      writeln('Controls:       8/2  - move forwards/back');
  64.      writeln('                4/6  - move left/back');
  65.      writeln('                7/1  - move up/down');
  66.      writeln;
  67.      writeln('                q    - quit');
  68.      writeln;
  69.      writeln('                Press any key to continue');
  70.      ch:=ReadKey;
  71.      {Read in vertices}
  72.      assign(f,'points.dat');
  73.      reset(f);
  74.      for c:=1 to 18 do
  75.      begin
  76.           readln(f,vertex[c].xw,vertex[c].yw,vertex[c].zw);
  77.      end;
  78.      close(f);
  79.  
  80.      {Move to z=500}
  81.      for c:=1 to 18 do
  82.      begin
  83.           vertex[c].zw:=vertex[c].zw+500;
  84.      end;
  85.  
  86.      {Read in edges}
  87.      assign(f,'edges.dat');
  88.      reset(f);
  89.      for c:=1 to 27 do
  90.          readln(f,edges[c,1], edges[c,2]);
  91.      close(f);
  92.  
  93.      Initialise_Graphics;
  94.  
  95.      xe:=0; ye:=0; ze:=0;
  96.      transform(xe,ye,ze);
  97.      draw_picture;
  98.  
  99.      repeat
  100.            ch:=Readkey;
  101.            case ch of
  102.                 '8': ze:=ze+10;
  103.                 '2': ze:=ze-10;
  104.                 '4': xe:=xe-10;
  105.                 '6': xe:=xe+10;
  106.                 '1': ye:=ye-10;
  107.                 '7': ye:=ye+10;
  108.            end;
  109.            transform(xe,ye,ze);
  110.            draw_picture;
  111.      until ch='q';
  112.      CloseGraph;
  113. end.