home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / 3 / 3d120.zip / PROJECT3.PAS < prev    next >
Pascal/Delphi Source File  |  1992-08-08  |  7KB  |  154 lines

  1. (******************************************************************************
  2. *                                  project3                                   *
  3. ******************************************************************************)
  4. unit project3;
  5.  
  6. (*******************************************************************************
  7. *      this unit has 2 instances : the first one is used with the windows      *
  8. *               graphic user interface, and the other does not,                *
  9. *                                                                              *
  10. *             +-------------------------------------------------+              *
  11. *             |  this unit is NOT interfaced to the window GUI, |              *
  12. *             |   for use on a bare screen ONLY - for runTime!  |              *
  13. *             |                ****                             |              *
  14. *             +-------------------------------------------------+              *
  15. *                                                                              *
  16. *this unit handles the 3d -> 2d projections, we use 2 different methods        *
  17. *       of projections :                                                       *
  18. *                                                                              *
  19. *               A : axonometric projections, no perspective due to             *
  20. *                       distance is performed, the general way                 *
  21. *                       we can look at the coordinate system is as             *
  22. *                       follows :                                              *
  23. *                                                                              *
  24. *                               |  z axis                                      *
  25. *                               |                                              *
  26. *                              / \                                             *
  27. *                    x axis   /   \  y axis                                    *
  28. *                                                                              *
  29. *               B : perspective projections : the normal eye perspective       *
  30. *                       projection is performed, we can look at the 3d         *
  31. *                       universe we are refering to as a cube of               *
  32. *                       1000 x 1000 x 1000 integer locations, with             *
  33. *                       the x axis, and y axis parallel to the screen          *
  34. *                       x, y axis respectivly, and the z axis going into       *
  35. *                       the screen.                                            *
  36. *                                                                              *
  37. *                       we will look at the coordinate system as follows :     *
  38. *                                                                              *
  39. *                       │ Y axis                                               *
  40. *                       │                                                      *
  41. *                Z axis x------ X axis                                         *
  42. *                                                                              *
  43. *                                                                              *
  44. *******************************************************************************)
  45.  
  46. interface
  47.  
  48. uses    graph,
  49.         hdr3d
  50.         ;
  51.  
  52. const
  53.         perspective     : boolean = false; 
  54.        {true = perspective, else = axonometric}
  55. var
  56.     MaxX, MaxY : word;          { In pixels for graphics screen }
  57.     MaxColor   : word;
  58.     GraphDriver           : integer;
  59.     GraphMode             : integer;
  60.  
  61. procedure calcPoint(p3d : point3d; var psc : screenPoints);
  62. procedure setPerspective;
  63. procedure resetPerspective;
  64. procedure togglePerspective;
  65.  
  66. implementation
  67.  
  68. var OldExitProc           : Pointer;
  69.  
  70. {$F+}
  71. (******************************************************************************
  72. *                                 MyExitProc                                  *
  73. ******************************************************************************)
  74. procedure MyExitProc;
  75. Begin
  76.   ExitProc := OldExitProc; { Restore exit procedure address }
  77.   CloseGraph;              { Shut down the graphics system }
  78. End; { MyExitProc }
  79. {$F-}
  80.  
  81. (*******************************************************************************
  82. *                                  StartGraph                                  *
  83. *******************************************************************************)
  84. Procedure StartGraph;
  85. var
  86.   ErrorCode : integer;
  87. Begin
  88.   { when using Crt and graphics, turn off Crt's memory-mapped writes }
  89.   OldExitProc := ExitProc;                { save previous exit proc }
  90.   ExitProc := @MyExitProc;                { insert our exit proc in chain }
  91.   GraphDriver := Detect;                  { use autodetection }
  92.   {$ifdef ishai}
  93.       InitGraph(GraphDriver, GraphMode, 'c:/tc');  { activate graphics }
  94.   {$else}
  95.       InitGraph(GraphDriver, GraphMode, 'd:/tp/units');  { activate graphics }
  96.   {$endif}
  97.   ErrorCode := GraphResult;               { error? }
  98.   if ErrorCode <> grOk then
  99.   Begin
  100.     Writeln('Graphics error: ', GraphErrorMsg(ErrorCode));
  101.     Halt(1);
  102.   End;
  103.   MaxColor := GetMaxColor;  { Get the maximum allowable drawing color }
  104.   MaxX := GetMaxX;          { Get screen resolution values }
  105.   MaxY := GetMaxY;
  106. End; { Initialize }
  107.  
  108. (******************************************************************************
  109. *                                  CalcPoint                                  *
  110. ******************************************************************************)
  111. procedure CalcPoint;
  112.  {Calculate screen cordinates of 3d location into screen}
  113.  
  114. Begin with p3d, psc do begin
  115.      if Perspective then begin
  116.           z  := z + HalfWidth;
  117.           If z < 0 Then z := 0;
  118.           sX := round((x * z / HalfWidth + HalfWidth) * (MaxX / ScreenWidth));
  119.           sY := round((HalfWidth - y * z / HalfWidth) * (MaxY / ScreenWidth));
  120.      end else begin
  121.           sX := round((HalfWidth + cosine_x * x - cosine_y * y) * (MaxX / ScreenWidth));
  122.           sY := round((HalfWidth - z + sine_x * x + sine_y * y) * (MaxY / ScreenWidth));
  123.      end;
  124.      end; {with}
  125. End;
  126.  
  127. (******************************************************************************
  128. *                               setPerspective                                *
  129. ******************************************************************************)
  130. procedure setPerspective;
  131. begin
  132.         perspective := true;
  133. end;
  134.  
  135. (******************************************************************************
  136. *                              resetPerspective                               *
  137. ******************************************************************************)
  138. procedure resetPerspective;
  139. begin
  140.         perspective := false;
  141. end;
  142.  
  143. (******************************************************************************
  144. *                              togglePerspective                              *
  145. ******************************************************************************)
  146. procedure togglePerspective;
  147. begin
  148.         perspective := not(perspective);
  149. end;
  150.  
  151. begin
  152.     StartGraph;
  153. end.
  154.