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

  1. program chap2_4;
  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.  
  22. procedure Initialise_Graphics;
  23. var Graphdriver, Graphmode, Errorcode : integer;
  24. begin
  25.      Graphdriver:=Detect;
  26.      InitGraph(Graphdriver, Graphmode,'');
  27.      Errorcode:=graphresult;
  28.      if Errorcode<>grOk then
  29.      begin
  30.           writeln('Graphics Error: ',GraphErrorMsg(ErrorCode));
  31.           writeln('Program Aborted');
  32.           Halt(1);
  33.      end;
  34.      SetLineStyle(0,0,3);
  35. end;
  36.  
  37. procedure draw_picture;
  38. var c : integer;
  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;
  46. var c : integer;
  47. begin
  48.      for c:=1 to 18 do
  49.      begin
  50.          vertex[c].zs:=round(vertex[c].zw);
  51.          vertex[c].xs:=round((vertex[c].xw*(persp/vertex[c].zs)*xscale)+xoffset);
  52.          vertex[c].ys:=round((vertex[c].yw*(persp/vertex[c].zs)*yscale)+yoffset);
  53.      end;
  54. end;
  55.  
  56. begin
  57.      clrscr;
  58.      writeln; writeln; writeln; writeln; writeln; writeln; writeln;
  59.      writeln('This is from Chapter 2 Step 4 of the Virtual Reality Homebrewer''s Handbook and');
  60.      writeln('shows how to draw a virtual shape in wireframe with perspective.');
  61.      writeln;
  62.      writeln;
  63.      writeln;
  64.      writeln('                Press any key to continue');
  65.      ch:=ReadKey;
  66.      {Read in vertices}
  67.      assign(f,'points.dat');
  68.      reset(f);
  69.      for c:=1 to 18 do
  70.      begin
  71.           readln(f,vertex[c].xw,vertex[c].yw,vertex[c].zw);
  72.      end;
  73.      close(f);
  74.  
  75.      {Move to z=500}
  76.      for c:=1 to 18 do
  77.      begin
  78.           vertex[c].zw:=vertex[c].zw+500;
  79.      end;
  80.  
  81.      {Read in edges}
  82.      assign(f,'edges.dat');
  83.      reset(f);
  84.      for c:=1 to 27 do
  85.          readln(f,edges[c,1], edges[c,2]);
  86.      close(f);
  87.  
  88.      Initialise_Graphics;
  89.  
  90.      transform;
  91.      draw_picture;
  92.      ch:=ReadKey;
  93.      CloseGraph;
  94. end.