home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Modula / Source / Draw3D.MOD < prev    next >
Text File  |  1985-01-01  |  2KB  |  102 lines

  1. MODULE Draw3D;
  2.  
  3. (*
  4.   Draw and rotate a three dimensional wire-frame object
  5. *)
  6.  
  7. FROM ThreeDee   IMPORT Point3D, SetRot, SetScale, 
  8.                SetTranslation, SetPerspective, 
  9.                TransformSRT, Project;
  10. FROM QuickDrawTypes IMPORT Point;
  11. FROM MiniQD         IMPORT MoveTo, LineTo, ObscureCursor;
  12. FROM Terminal       IMPORT ClearScreen, BusyRead;
  13. FROM InOut          IMPORT OpenInput, CloseInput, 
  14.                ReadInt, WriteString, Done;
  15. FROM RealInOut      IMPORT ReadReal;
  16.  
  17. CONST
  18.   maxVertices = 150;
  19.   maxEdges    = 300;
  20.  
  21. VAR
  22.   vertices:          ARRAY[1..maxVertices] OF Point3D;
  23.   projectedVertices: ARRAY[1..maxVertices] OF Point;
  24.   edges: ARRAY[1..maxEdges] OF INTEGER;
  25.   numVertices, numEdges: INTEGER;
  26.  
  27. PROCEDURE DrawEdge( from, to: INTEGER );
  28. BEGIN
  29.   WITH projectedVertices[from] DO MoveTo( 256+h, 171+v ); END;
  30.   WITH projectedVertices[to]   DO LineTo( 256+h, 171+v ); END;
  31. END DrawEdge;
  32.  
  33. PROCEDURE DisplayList;
  34. VAR
  35.   edgeIndex: INTEGER;
  36. BEGIN
  37.   FOR edgeIndex:=1 TO numEdges DO
  38.     IF edges[edgeIndex] > 0 
  39.     THEN DrawEdge( ABS(edges[edgeIndex-1]), edges[edgeIndex] );
  40.     END; (*IF*)
  41.   END; (*FOR*)
  42. END DisplayList;
  43.  
  44. PROCEDURE ProjectList; (* rotate and project all vertices *)
  45. VAR
  46.   vertIndex: INTEGER;
  47.   rotVertex: Point3D;
  48. BEGIN
  49.   FOR vertIndex:=1 TO numVertices DO
  50.     TransformSRT( vertices[vertIndex], rotVertex );
  51.     Project( rotVertex, projectedVertices[vertIndex] );
  52.   END; (*FOR*)
  53. END ProjectList;
  54.   
  55. PROCEDURE ReadList;
  56. VAR
  57.   index, edge: INTEGER;
  58. BEGIN
  59.   ClearScreen;
  60.   WriteString( "Please enter the name of a 3-D data file:" );
  61.   OpenInput( "3D" );
  62.   ReadInt( numVertices );
  63.   FOR index:=1 TO numVertices DO
  64.     WITH vertices[index] DO
  65.       ReadReal( X ); ReadReal( Y ); ReadReal( Z );
  66.     END; (*WITH*)
  67.   END; (*FOR*)
  68.   numEdges:=0;
  69.   LOOP
  70.     ReadInt( edge );
  71.     IF NOT Done THEN EXIT; END;
  72.     INC( numEdges );
  73.     edges[numEdges]:=edge;
  74.   END; (*LOOP*)
  75.   CloseInput;
  76. END ReadList;
  77.  
  78. PROCEDURE KeyWasPressed(): BOOLEAN;
  79. VAR
  80.   ch: CHAR;
  81. BEGIN
  82.   BusyRead( ch );
  83.   RETURN ch <> 0C;
  84. END KeyWasPressed;
  85.  
  86. VAR
  87.   xR, yR, zR: REAL;
  88.  
  89. BEGIN
  90.   ReadList;
  91.   SetTranslation( 0.0, 0.0, 0.0 );
  92.   SetPerspective( 220.0, -180.0 );
  93.   xR:=0.0; yR:=0.0; zR:=0.0;
  94.   ObscureCursor;
  95.   REPEAT
  96.     SetRot( xR, yR, zR );
  97.     ProjectList;
  98.     ClearScreen;
  99.     DisplayList;
  100.     xR:=xR + 8.0; yR:=yR + 10.0; zR:=zR + 12.0;
  101.   UNTIL KeyWasPressed();
  102. END Draw3D.