home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / 3 / 3dlib15.zip / PROJECT3.PAS < prev    next >
Pascal/Delphi Source File  |  1992-10-27  |  8KB  |  151 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. (******************************************************************************
  71. *                                 MyExitProc                                  *
  72. ******************************************************************************)
  73. procedure MyExitProc; far;
  74. Begin
  75.   ExitProc := OldExitProc; { Restore exit procedure address }
  76.   CloseGraph;              { Shut down the graphics system }
  77. End; { MyExitProc }
  78.  
  79. (*******************************************************************************
  80. *                                  StartGraph                                  *
  81. *******************************************************************************)
  82. Procedure StartGraph;
  83. var
  84.   ErrorCode : integer;
  85. Begin
  86.   { when using Crt and graphics, turn off Crt's memory-mapped writes }
  87.   OldExitProc := ExitProc;                { save previous exit proc }
  88.   ExitProc := @MyExitProc;                { insert our exit proc in chain }
  89.   GraphDriver := Detect;                  { use autodetection }
  90.   InitGraph(GraphDriver, GraphMode, '');  { activate graphics }
  91.   ErrorCode := GraphResult;               { error? }
  92.   if ErrorCode <> grOk then
  93.   Begin
  94.     Writeln('Graphics error: ', GraphErrorMsg(ErrorCode));
  95.     Halt(1);
  96.   End;
  97.   MaxColor := GetMaxColor;  { Get the maximum allowable drawing color }
  98.   MaxX := GetMaxX;          { Get screen resolution values }
  99.   MaxY := GetMaxY;
  100. End; { Initialize }
  101.  
  102. (******************************************************************************
  103. *                                  CalcPoint                                  *
  104. ******************************************************************************)
  105. procedure CalcPoint;
  106.  {Calculate screen cordinates of 3d location into screen}
  107.  
  108. Begin with p3d, psc do begin
  109.      if Perspective then begin
  110.           z  := z + HalfWidth;
  111.           If z < 0 Then z := 0;
  112.           sX := round((x * z / HalfWidth + HalfWidth) * (MaxX / ScreenWidth));
  113.           sY := round((HalfWidth - y * z / HalfWidth) * (MaxY / ScreenWidth));
  114.      end else begin
  115.           sX := round((HalfWidth + cosine_x * x - cosine_y * y) * (MaxX / ScreenWidth));
  116.           sY := round((HalfWidth - z + sine_x * x + sine_y * y) * (MaxY / ScreenWidth));
  117.      end;
  118.      end; {with}
  119. End; { calcPoint }
  120.  
  121. (******************************************************************************
  122. *                               setPerspective                                *
  123. ******************************************************************************)
  124. procedure setPerspective;
  125. begin
  126.         perspective := true;
  127. end; {setPerspective}
  128.  
  129. (******************************************************************************
  130. *                              resetPerspective                               *
  131. ******************************************************************************)
  132. procedure resetPerspective;
  133. begin
  134.         perspective := false;
  135. end; {resetPerspective}
  136.  
  137. (******************************************************************************
  138. *                              togglePerspective                              *
  139. ******************************************************************************)
  140. procedure togglePerspective;
  141. begin
  142.         perspective := not(perspective);
  143. end; {togglePerspective}
  144.  
  145. (******************************************************************************
  146. *                                    end.                                     *
  147. ******************************************************************************)
  148. begin
  149.     StartGraph;
  150. end.
  151.