home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / PCGPEV10.ZIP / TUT9.TXT < prev    next >
Text File  |  1994-05-05  |  30KB  |  824 lines

  1.                    ╒═══════════════════════════════╕
  2.                    │         W E L C O M E         │
  3.                    │  To the VGA Trainer Program   │ │
  4.                    │              By               │ │
  5.                    │      DENTHOR of ASPHYXIA      │ │ │
  6.                    ╘═══════════════════════════════╛ │ │
  7.                      ────────────────────────────────┘ │
  8.                        ────────────────────────────────┘
  9.  
  10.                            --==[ PART 9 ]==--
  11.  
  12.  
  13.  
  14. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  15. ■ Introduction
  16.  
  17. Hi there! ASPHYXIA is BACK with our first MegaDemo, Psycho Neurosis! A
  18. paltry 1.3MB download is all it takes to see the group from Durbs first
  19. major production! We are quite proud of it, and think you should see it
  20. ;)
  21.  
  22. Secondly, I released a small little trainer (a trainerette ;-)) on
  23. RsaPROG and Connexctix BBS mail, also on the ASPHYXIA BBS as COPPERS.ZIP
  24. It is a small Pascal program demonstrating how to display copper bars in
  25. text mode. Also includes a check for horizontal retrace (A lot of people
  26. wanted it, that is why I wrote the program) (ASPHYXIA ... first with the
  27. trainer goodies ;-)  aargh, sorry, had to be done ))
  28.  
  29. Thirdly, sorry about the problems with Tut 8! If you had all the
  30. checking on, the tutorial would probably die on the first points. The
  31. reason is this : in the first loop, we have DrawPoints then
  32. RotatePoints. The variables used in DrawPoints are set in RotatePoints,
  33. so if you put RotatePoints before DrawPoints, the program should work
  34. fine. Alternatively, turn off error checking 8-)
  35.  
  36. Fourthly, I have had a surprisingly large number of people saying that
  37. "I get this, like, strange '286 instructions not enabled' message!
  38. What's wrong with your code, dude?"  To all of you, get into Pascal, hit
  39. Alt-O (for options), hit enter and a 2 (for Enable 286 instructions). Hard
  40. hey? Doesn't anyone EVER set up their version of Pascal?
  41.  
  42. Now, on to todays tutorial! 3D solids. That is what the people wanted,
  43. that is what the people get! This tutorial is mainly on how to draw the
  44. polygon on screen. For details on how the 3D stuff works, check out tut
  45. 8.
  46.  
  47.  
  48.  
  49. If you would like to contact me, or the team, there are many ways you
  50. can do it : 1) Write a message to Grant Smith/Denthor/Asphyxia in private mail
  51.                   on the ASPHYXIA BBS.
  52.             2) Write to Denthor, EzE or Goth on Connectix.
  53.             3) Write to :  Grant Smith
  54.                            P.O.Box 270 Kloof
  55.                            3640
  56.                            Natal
  57.             4) Call me (Grant Smith) at (031) 73 2129 (leave a message if you
  58.                   call during varsity)
  59.             5) Write to mcphail@beastie.cs.und.ac.za on InterNet, and
  60.                   mention the word Denthor near the top of the letter.
  61.  
  62. NB : If you are a representative of a company or BBS, and want ASPHYXIA
  63.        to do you a demo, leave mail to me; we can discuss it.
  64. NNB : If you have done/attempted a demo, SEND IT TO ME! We are feeling
  65.         quite lonely and want to meet/help out/exchange code with other demo
  66.         groups. What do you have to lose? Leave a message here and we can work
  67.         out how to transfer it. We really want to hear from you!
  68.  
  69.  
  70.  
  71. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  72. ■ How to draw a polygon
  73.  
  74. Sounds easy enough, right? WRONG! There are many, many different ways to
  75. go about this, and today I'll only be showing you one. Please don't take
  76. what is written here as anything approaching the best method, it is just
  77. here to get you on your way...
  78.  
  79. The procedure I will be using here is based on something most of us
  80. learned in standard eight ... I think. I seem to recall doing something
  81. like this in Mrs. Reids maths class all those years ago ;)
  82.  
  83. Take two points, x1,y1 and x2,y2. Draw them :
  84.  
  85.                   + (x1,y1)
  86.                    \
  87.                      \  <-- Point a somewhere along the line
  88.                        \
  89.                          + (x2,y2)
  90.  
  91. Right, so what we have to do is this : if we know the y-coord of a, what
  92. is it's x-coord? To prove the method we will give the points random
  93. values.
  94.  
  95.                  + (2,10)
  96.                   \
  97.                     \  <-- a.y = 12
  98.                       \
  99.                         +  (15,30)
  100.  
  101. Right. Simple enough problem. This is how we do it :
  102.    (a.y-y1) = (12 - 10)  {to get a.y as though y1 was zero}
  103.    *(x2-x1) = *(15 - 2)  {the total x-length of the line}
  104.    /(y2-y1) = /(30 - 10) {the total y-length of the line}
  105.         +x1 = +2         { to get the equation back to real coords}
  106.  
  107. So our equation is :  (a.y-y1)*(x2-x1)/(y2-y1)+x4    or
  108.                       (12-10)*(15-2)/(30-10)+2
  109.       which gives you :
  110.                       2*13/20+2 = 26/20+2
  111.                                 = 3.3
  112.  
  113. That means that along the line with y=12, x is equal to 3.3. Since we
  114. are not concerned with the decimal place, we replace the  /  with a div,
  115. which in Pascal gives us an integer result, and is faster too. All well
  116. and good, I hear you cry, but what does this have to do with life and
  117. how it relates to polygons in general. The answer is simple. For each of
  118. the four sides of the polygon we do the above test for each y line. We
  119. store the smallest and the largest x values into separate variables for
  120. each line, and draw a horizontal line between them. Ta-Dah! We have a
  121. cool polygon!
  122.  
  123. For example : Two lines going down :
  124.     
  125.                 +             +
  126.                / <-x1     x2->|   <--For this y line
  127.              /                |
  128.            +                  +
  129.  
  130. Find x1 and x2 for that y, then draw a line between them. Repeat for all
  131. y values.
  132.  
  133. Of course, it's not as simple as that. We have to make sure we only
  134. check those y lines that contain the polygon (a simple min y, max y test
  135. for all the points). We also have to check that the line we are
  136. calculating actually extends as far as where our current y is (check
  137. that the point is between both y's). We have to compare each x to see
  138. weather it is smaller then the minimum x value so far, or bigger then
  139. the maximum (the original x min is set as a high number, and the x max
  140. is set as a small number). We must also check that we only draw to the
  141. place that we can see ( 0-319 on the x ; 0-199 on the y (the size of the
  142. MCGA screen))
  143.  
  144. To see how this looks in practice, have a look at the sample code
  145. provided. (Mrs. Reid would probably kill me for the above explanation,
  146. so when you learn it in school, split it up into thousands of smaller
  147. equations to get the same answer ;))
  148.  
  149. Okay, that's it! What's that? How do you draw a vertical line? Thats
  150. simple ...
  151.  
  152. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  153. ■ Drawing a vertical line
  154.  
  155. Right, this is a lot easier than drawing a normal line (Tut 5 .. I
  156. think), because you stay on the same y value. So, what you do is you set
  157. ES to the screen you want to write to, and get DI to the start of the
  158. y-line (see earlier trainers for a description of how SEGMENT:OFFSET
  159. works.
  160.  
  161. IN   : x1 , x2, y, color, where
  162.  
  163.            asm
  164.              mov    ax,where
  165.              mov    es,ax
  166.              mov    di,y
  167.              mov    ax,y
  168.              shl    di,8   { di:=di*256 }
  169.              shl    ax,6   { ax:=ax*64 }
  170.              add    di,ax  { di := (y*256)+(y*64) := y*320 Faster then a
  171.                              straight multiplication }
  172.  
  173. Right, now you add the first x value to get your startoff.
  174.              add    di,x1
  175. Move the color to store into ah and al
  176.              mov    al,color
  177.              mov    ah,al       { ah:=al:=color }
  178. then get CX equal to how many pixels across you want to go
  179.              mov    cx,x2
  180.              sub    cx,x1   { cx:=x2-x1 }
  181. Okay, as we all know, moving a word is a lot faster then moving a byte,
  182. so we halve CX
  183.              shr    cx,1    { cx:=cx/2 }
  184. but what happens if CX was an odd number. After a shift, the value of
  185. the last number is placed in the carry flag, so what we do is jump over
  186. a single byte move if the carry flag is zero, or execute it if it is
  187. one.
  188.             jnc     @Start  { If there is no carry, jump to label Start }
  189.             stosb           { ES:[DI]:=al ; increment DI }
  190.         @Start :            { Label Start }
  191.             rep     stosw   { ES:[DI]:=ax ; DI:=DI+2; repeat CX times }
  192.  
  193. Right, the finished product looks like this :
  194.  
  195. Procedure Hline (x1,x2,y:word;col:byte;where:word); assembler;
  196.   { This draws a horizontal line from x1 to x2 on line y in color col }
  197. asm
  198.   mov   ax,where
  199.   mov   es,ax
  200.   mov   ax,y
  201.   mov   di,ax
  202.   shl   ax,8
  203.   shl   di,6
  204.   add   di,ax
  205.   add   di,x1
  206.  
  207.   mov   al,col
  208.   mov   ah,al
  209.   mov   cx,x2
  210.   sub   cx,x1
  211.   shr   cx,1
  212.   jnc   @start
  213.   stosb
  214. @Start :
  215.   rep   stosw
  216. end;
  217.  
  218. Done!
  219.  
  220. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  221. ■  In closing
  222.  
  223. This 3D system is still not perfect. It needs to be faster, and now I
  224. have also dumped the problem of face-sorting on you! Nyahahahaha!
  225.  
  226.            [ My sister and I were driving along the other day when she
  227.                asked me, what would I like for my computer.
  228.              I thought long and hard about it, and came up with the
  229.                following hypothesis. When a girl gets a Barbie doll, she
  230.                then wants the extra ballgown for the doll, then the
  231.                hairbrush, and the car, and the house, and the friends
  232.                etc.
  233.              When a guy gets a computer, he wants the extra memory, the
  234.                bigger hard drive, the maths co-pro, the better
  235.                motherboard, the latest software, and the bigger monitor
  236.                etc.
  237.              I told my sister all of this, and finished up with : "So as
  238.                you can see, computers are Barbie dolls for MEN!"
  239.              She called me a chauvinist. And hit me. Hard.
  240.                                                                    ]
  241.                                                        - Grant Smith
  242.                                                            19:24
  243.                                                              26/2/94
  244.  
  245. See you next time!
  246.   - Denthor
  247.  
  248. These fine BBS's carry the ASPHYXIA DEMO TRAINER SERIES : (alphabetical)
  249.  
  250. ╔══════════════════════════╦════════════════╦═════╦═══╦════╦════╗
  251. ║BBS Name                  ║Telephone No.   ║Open ║Msg║File║Past║
  252. ╠══════════════════════════╬════════════════╬═════╬═══╬════╬════╣
  253. ║ASPHYXIA BBS #1           ║(031) 765-5312  ║ALL  ║ * ║ *  ║ *  ║
  254. ║ASPHYXIA BBS #2           ║(031) 765-6293  ║ALL  ║ * ║ *  ║ *  ║
  255. ║Connectix BBS             ║(031) 266-9992  ║ALL  ║   ║ *  ║ *  ║
  256. ╚══════════════════════════╩════════════════╩═════╩═══╩════╩════╝
  257.  
  258. Open = Open at all times or only A/H
  259. Msg  = Available in message base
  260. File = Available in file base
  261. Past = Previous Parts available
  262.  
  263. Does no other BBS's ANYWHERE carry the trainer? Am I writing this for
  264. three people who get it from one of these BBS's each week? Should I go
  265. on? (Hehehehe ... I was pleased to note that Tut 8 was THE most
  266. downloaded file from ASPHYXIA BBS last month ... )       
  267.  
  268.  
  269. ┌──────────────┬─────────────────────────────────────────────────────────────
  270. │ TUTPROG9.PAS │
  271. └──────────────┘
  272.  
  273. {$X+}
  274. USES Crt;
  275.  
  276. CONST VGA = $A000;
  277.       maxpolys = 5;
  278.       A : Array [1..maxpolys,1..4,1..3] of integer =
  279.         (
  280.          ((-10,10,0),(-2,-10,0),(0,-10,0),(-5,10,0)),
  281.          ((10,10,0),(2,-10,0),(0,-10,0),(5,10,0)),
  282.          ((-2,-10,0),(2,-10,0),(2,-5,0),(-2,-5,0)),
  283.          ((-6,0,0),(6,0,0),(7,5,0),(-7,5,0)),
  284.          ((0,0,0),(0,0,0),(0,0,0),(0,0,0))
  285.         );  { The 3-D coordinates of our object ... stored as (X1,Y1,Z1), }
  286.             { (X2,Y2,Z2) ... for the 4 points of a poly }
  287.      S : Array [1..maxpolys,1..4,1..3] of integer =
  288.         (
  289.          ((-10,-10,0),(10,-10,0),(10,-7,0),(-10,-7,0)),
  290.          ((-10,10,0),(10,10,0),(10,7,0),(-10,7,0)),
  291.          ((-10,1,0),(10,1,0),(10,-2,0),(-10,-2,0)),
  292.          ((-10,-8,0),(-7,-8,0),(-7,0,0),(-10,0,0)),
  293.          ((10,8,0),(7,8,0),(7,0,0),(10,0,0))
  294.         );  { The 3-D coordinates of our object ... stored as (X1,Y1,Z1), }
  295.             { (X2,Y2,Z2) ... for the 4 points of a poly }
  296.      P : Array [1..maxpolys,1..4,1..3] of integer =
  297.         (
  298.          ((-10,-10,0),(-7,-10,0),(-7,10,0),(-10,10,0)),
  299.          ((10,-10,0),(7,-10,0),(7,0,0),(10,0,0)),
  300.          ((-9,-10,0),(9,-10,0),(9,-7,0),(-9,-7,0)),
  301.          ((-9,-1,0),(9,-1,0),(9,2,0),(-9,2,0)),
  302.          ((0,0,0),(0,0,0),(0,0,0),(0,0,0))
  303.         );  { The 3-D coordinates of our object ... stored as (X1,Y1,Z1), }
  304.             { (X2,Y2,Z2) ... for the 4 points of a poly }
  305.      H : Array [1..maxpolys,1..4,1..3] of integer =
  306.         (
  307.          ((-10,-10,0),(-7,-10,0),(-7,10,0),(-10,10,0)),
  308.          ((10,-10,0),(7,-10,0),(7,10,0),(10,10,0)),
  309.          ((-9,-1,0),(9,-1,0),(9,2,0),(-9,2,0)),
  310.          ((0,0,0),(0,0,0),(0,0,0),(0,0,0)),
  311.          ((0,0,0),(0,0,0),(0,0,0),(0,0,0))
  312.         );  { The 3-D coordinates of our object ... stored as (X1,Y1,Z1), }
  313.             { (X2,Y2,Z2) ... for the 4 points of a poly }
  314.      Y : Array [1..maxpolys,1..4,1..3] of integer =
  315.         (
  316.          ((-7,-10,0),(0,-3,0),(0,0,0),(-10,-7,0)),
  317.          ((7,-10,0),(0,-3,0),(0,0,0),(10,-7,0)),
  318.          ((-2,-3,0),(2,-3,0),(2,10,0),(-2,10,0)),
  319.          ((0,0,0),(0,0,0),(0,0,0),(0,0,0)),
  320.          ((0,0,0),(0,0,0),(0,0,0),(0,0,0))
  321.         );  { The 3-D coordinates of our object ... stored as (X1,Y1,Z1), }
  322.             { (X2,Y2,Z2) ... for the 4 points of a poly }
  323.      X : Array [1..maxpolys,1..4,1..3] of integer =
  324.         (
  325.          ((-7,-10,0),(10,7,0),(7,10,0),(-10,-7,0)),
  326.          ((7,-10,0),(-10,7,0),(-7,10,0),(10,-7,0)),
  327.          ((0,0,0),(0,0,0),(0,0,0),(0,0,0)),
  328.          ((0,0,0),(0,0,0),(0,0,0),(0,0,0)),
  329.          ((0,0,0),(0,0,0),(0,0,0),(0,0,0))
  330.         );  { The 3-D coordinates of our object ... stored as (X1,Y1,Z1), }
  331.             { (X2,Y2,Z2) ... for the 4 points of a poly }
  332.      I : Array [1..maxpolys,1..4,1..3] of integer =
  333.         (
  334.          ((-10,-10,0),(10,-10,0),(10,-7,0),(-10,-7,0)),
  335.          ((-10,10,0),(10,10,0),(10,7,0),(-10,7,0)),
  336.          ((-2,-9,0),(2,-9,0),(2,9,0),(-2,9,0)),
  337.          ((0,0,0),(0,0,0),(0,0,0),(0,0,0)),
  338.          ((0,0,0),(0,0,0),(0,0,0),(0,0,0))
  339.         );  { The 3-D coordinates of our object ... stored as (X1,Y1,Z1), }
  340.             { (X2,Y2,Z2) ... for the 4 points of a poly }
  341.  
  342.  
  343. Type Point = Record
  344.                x,y,z:real;                { The data on every point we rotate}
  345.              END;
  346.      Virtual = Array [1..64000] of byte;  { The size of our Virtual Screen }
  347.      VirtPtr = ^Virtual;                  { Pointer to the virtual screen }
  348.  
  349.  
  350. VAR Lines : Array [1..maxpolys,1..4] of Point;  { The base object rotated }
  351.     Translated : Array [1..maxpolys,1..4] of Point; { The rotated object }
  352.     Xoff,Yoff,Zoff:Integer;               { Used for movement of the object }
  353.     lookup : Array [0..360,1..2] of real; { Our sin and cos lookup table }
  354.     Virscr : VirtPtr;                     { Our first Virtual screen }
  355.     Vaddr  : word;                        { The segment of our virtual screen}
  356.  
  357.  
  358. {──────────────────────────────────────────────────────────────────────────}
  359. Procedure SetMCGA;  { This procedure gets you into 320x200x256 mode. }
  360. BEGIN
  361.   asm
  362.      mov        ax,0013h
  363.      int        10h
  364.   end;
  365. END;
  366.  
  367.  
  368. {──────────────────────────────────────────────────────────────────────────}
  369. Procedure SetText;  { This procedure returns you to text mode.  }
  370. BEGIN
  371.   asm
  372.      mov        ax,0003h
  373.      int        10h
  374.   end;
  375. END;
  376.  
  377. {──────────────────────────────────────────────────────────────────────────}
  378. Procedure Cls (Where:word;Col : Byte);
  379.    { This clears the screen to the specified color }
  380. BEGIN
  381.      asm
  382.         push    es
  383.         mov     cx, 32000;
  384.         mov     es,[where]
  385.         xor     di,di
  386.         mov     al,[col]
  387.         mov     ah,al
  388.         rep     stosw
  389.         pop     es
  390.      End;
  391. END;
  392.  
  393. {──────────────────────────────────────────────────────────────────────────}
  394. Procedure SetUpVirtual;
  395.    { This sets up the memory needed for the virtual screen }
  396. BEGIN
  397.   GetMem (VirScr,64000);
  398.   vaddr := seg (virscr^);
  399. END;
  400.  
  401.  
  402. {──────────────────────────────────────────────────────────────────────────}
  403. Procedure ShutDown;
  404.    { This frees the memory used by the virtual screen }
  405. BEGIN
  406.   FreeMem (VirScr,64000);
  407. END;
  408.  
  409.  
  410. {──────────────────────────────────────────────────────────────────────────}
  411. procedure flip(source,dest:Word);
  412.   { This copies the entire screen at "source" to destination }
  413. begin
  414.   asm
  415.     push    ds
  416.     mov     ax, [Dest]
  417.     mov     es, ax
  418.     mov     ax, [Source]
  419.     mov     ds, ax
  420.     xor     si, si
  421.     xor     di, di
  422.     mov     cx, 32000
  423.     rep     movsw
  424.     pop     ds
  425.   end;
  426. end;
  427.  
  428.  
  429. {──────────────────────────────────────────────────────────────────────────}
  430. Procedure Pal(Col,R,G,B : Byte);
  431.   { This sets the Red, Green and Blue values of a certain color }
  432. Begin
  433.    asm
  434.       mov    dx,3c8h
  435.       mov    al,[col]
  436.       out    dx,al
  437.       inc    dx
  438.       mov    al,[r]
  439.       out    dx,al
  440.       mov    al,[g]
  441.       out    dx,al
  442.       mov    al,[b]
  443.       out    dx,al
  444.    end;
  445. End;
  446.  
  447.  
  448. {──────────────────────────────────────────────────────────────────────────}
  449. Procedure Hline (x1,x2,y:word;col:byte;where:word); assembler;
  450.   { This draws a horizontal line from x1 to x2 on line y in color col }
  451. asm
  452.   mov   ax,where
  453.   mov   es,ax
  454.   mov   ax,y
  455.   mov   di,ax
  456.   shl   ax,8
  457.   shl   di,6
  458.   add   di,ax
  459.   add   di,x1
  460.  
  461.   mov   al,col
  462.   mov   ah,al
  463.   mov   cx,x2
  464.   sub   cx,x1
  465.   shr   cx,1
  466.   jnc   @start
  467.   stosb
  468. @Start :
  469.   rep   stosw
  470. end;
  471.  
  472.  
  473. {──────────────────────────────────────────────────────────────────────────}
  474. Procedure DrawPoly(x1,y1,x2,y2,x3,y3,x4,y4:integer;color:byte;where:word);
  475.   { This draw a polygon with 4 points at x1,y1 , x2,y2 , x3,y3 , x4,y4
  476.     in color col }
  477. var
  478.   x:integer;
  479.   mny,mxy:integer;
  480.   mnx,mxx,yc:integer;
  481.   mul1,div1,
  482.   mul2,div2,
  483.   mul3,div3,
  484.   mul4,div4:integer;
  485.  
  486. begin
  487.   mny:=y1; mxy:=y1;
  488.   if y2<mny then mny:=y2;
  489.   if y2>mxy then mxy:=y2;
  490.   if y3<mny then mny:=y3;
  491.   if y3>mxy then mxy:=y3;    { Choose the min y mny and max y mxy }
  492.   if y4<mny then mny:=y4;
  493.   if y4>mxy then mxy:=y4;
  494.  
  495.   if mny<0 then mny:=0;
  496.   if mxy>199 then mxy:=199;
  497.   if mny>199 then exit;
  498.   if mxy<0 then exit;        { Verticle range checking }
  499.  
  500.   mul1:=x1-x4; div1:=y1-y4;
  501.   mul2:=x2-x1; div2:=y2-y1;
  502.   mul3:=x3-x2; div3:=y3-y2;
  503.   mul4:=x4-x3; div4:=y4-y3;  { Constansts needed for intersection calc }
  504.  
  505.   for yc:=mny to mxy do
  506.     begin
  507.       mnx:=320;
  508.       mxx:=-1;
  509.       if (y4>=yc) or (y1>=yc) then
  510.         if (y4<=yc) or (y1<=yc) then   { Check that yc is between y1 and y4 }
  511.           if not(y4=y1) then
  512.             begin
  513.               x:=(yc-y4)*mul1 div div1+x4; { Point of intersection on x axis }
  514.               if x<mnx then
  515.                 mnx:=x;
  516.               if x>mxx then
  517.                 mxx:=x;       { Set point as start or end of horiz line }
  518.             end;
  519.       if (y1>=yc) or (y2>=yc) then
  520.         if (y1<=yc) or (y2<=yc) then   { Check that yc is between y1 and y2 }
  521.           if not(y1=y2) then
  522.             begin
  523.               x:=(yc-y1)*mul2 div div2+x1; { Point of intersection on x axis }
  524.               if x<mnx then
  525.                 mnx:=x;
  526.               if x>mxx then
  527.                 mxx:=x;       { Set point as start or end of horiz line }
  528.             end;
  529.       if (y2>=yc) or (y3>=yc) then
  530.         if (y2<=yc) or (y3<=yc) then   { Check that yc is between y2 and y3 }
  531.           if not(y2=y3) then
  532.             begin
  533.               x:=(yc-y2)*mul3 div div3+x2; { Point of intersection on x axis }
  534.               if x<mnx then
  535.                 mnx:=x;
  536.               if x>mxx then
  537.                 mxx:=x;       { Set point as start or end of horiz line }
  538.             end;
  539.       if (y3>=yc) or (y4>=yc) then
  540.         if (y3<=yc) or (y4<=yc) then   { Check that yc is between y3 and y4 }
  541.           if not(y3=y4) then
  542.             begin
  543.               x:=(yc-y3)*mul4 div div4+x3; { Point of intersection on x axis }
  544.               if x<mnx then
  545.                 mnx:=x;
  546.               if x>mxx then
  547.                 mxx:=x;       { Set point as start or end of horiz line }
  548.             end;
  549.       if mnx<0 then
  550.         mnx:=0;
  551.       if mxx>319 then
  552.         mxx:=319;          { Range checking on horizontal line }
  553.       if mnx<=mxx then
  554.         hline (mnx,mxx,yc,color,where);   { Draw the horizontal line }
  555.     end;
  556.   end;
  557.  
  558.  
  559.  
  560. {──────────────────────────────────────────────────────────────────────────}
  561. Function rad (theta : real) : real;
  562.   {  This calculates the degrees of an angle }
  563. BEGIN
  564.   rad := theta * pi / 180
  565. END;
  566.  
  567.  
  568. {──────────────────────────────────────────────────────────────────────────}
  569. Procedure SetUpPoints;
  570.   { This creates the lookup table }
  571. VAR loop1,loop2:integer;
  572. BEGIN
  573.   For loop1:=0 to 360 do BEGIN
  574.     lookup [loop1,1]:=sin (rad (loop1));
  575.     lookup [loop1,2]:=cos (rad (loop1));
  576.   END;
  577. END;
  578.  
  579.  
  580. {──────────────────────────────────────────────────────────────────────────}
  581. Procedure Putpixel (X,Y : Integer; Col : Byte; where:word);
  582.   { This puts a pixel on the screen by writing directly to memory. }
  583. BEGIN
  584.   Asm
  585.     mov     ax,[where]
  586.     mov     es,ax
  587.     mov     bx,[X]
  588.     mov     dx,[Y]
  589.     mov     di,bx
  590.     mov     bx, dx                  {; bx = dx}
  591.     shl     dx, 8
  592.     shl     bx, 6
  593.     add     dx, bx                  {; dx = dx + bx (ie y*320)}
  594.     add     di, dx                  {; finalise location}
  595.     mov     al, [Col]
  596.     stosb
  597.   End;
  598. END;
  599.  
  600.  
  601.  
  602. {──────────────────────────────────────────────────────────────────────────}
  603. Procedure RotatePoints (X,Y,Z:Integer);
  604.   { This rotates object lines by X,Y and Z; then places the result in
  605.     TRANSLATED }
  606. VAR loop1,loop2:integer;
  607.     temp:point;
  608. BEGIN
  609.   For loop1:=1 to maxpolys do BEGIN
  610.     For loop2:=1 to 4 do BEGIN
  611.       temp.x:=lines[loop1,loop2].x;
  612.       temp.y:=lookup[x,2]*lines[loop1,loop2].y - lookup[x,1]*lines[loop1,loop2].z;
  613.       temp.z:=lookup[x,1]*lines[loop1,loop2].y + lookup[x,2]*lines[loop1,loop2].z;
  614.  
  615.       translated[loop1,loop2]:=temp;
  616.  
  617.       If y>0 then BEGIN
  618.         temp.x:=lookup[y,2]*translated[loop1,loop2].x - lookup[y,1]*translated[loop1,loop2].y;
  619.         temp.y:=lookup[y,1]*translated[loop1,loop2].x + lookup[y,2]*translated[loop1,loop2].y;
  620.         temp.z:=translated[loop1,loop2].z;
  621.         translated[loop1,loop2]:=temp;
  622.       END;
  623.  
  624.       If z>0 then BEGIN
  625.         temp.x:=lookup[z,2]*translated[loop1,loop2].x + lookup[z,1]*translated[loop1,loop2].z;
  626.         temp.y:=translated[loop1,loop2].y;
  627.         temp.z:=-lookup[z,1]*translated[loop1,loop2].x + lookup[z,2]*translated[loop1,loop2].z;
  628.         translated[loop1,loop2]:=temp;
  629.       END;
  630.     END;
  631.   END;
  632. END;
  633.  
  634.  
  635.  
  636. {──────────────────────────────────────────────────────────────────────────}
  637. Procedure DrawPoints;
  638.   { This draws the translated object to the virtual screen }
  639. VAR loop1:Integer;
  640.     nx,ny,nx2,ny2,nx3,ny3,nx4,ny4:integer;
  641.     temp:integer;
  642. BEGIN
  643.   For loop1:=1 to maxpolys do BEGIN
  644.     If (translated[loop1,1].z+zoff<0) and (translated[loop1,2].z+zoff<0) and
  645.        (translated[loop1,3].z+zoff<0) and (translated[loop1,4].z+zoff<0) then BEGIN
  646.       temp:=round (translated[loop1,1].z+zoff);
  647.       nx :=round (256*translated[loop1,1].X) div temp+xoff;
  648.       ny :=round (256*translated[loop1,1].Y) div temp+yoff;
  649.       temp:=round (translated[loop1,2].z+zoff);
  650.       nx2:=round (256*translated[loop1,2].X) div temp+xoff;
  651.       ny2:=round (256*translated[loop1,2].Y) div temp+yoff;
  652.       temp:=round (translated[loop1,3].z+zoff);
  653.       nx3:=round (256*translated[loop1,3].X) div temp+xoff;
  654.       ny3:=round (256*translated[loop1,3].Y) div temp+yoff;
  655.       temp:=round (translated[loop1,4].z+zoff);
  656.       nx4:=round (256*translated[loop1,4].X) div temp+xoff;
  657.       ny4:=round (256*translated[loop1,4].Y) div temp+yoff;
  658.       drawpoly (nx,ny,nx2,ny2,nx3,ny3,nx4,ny4,13,vaddr);
  659.     END;
  660.   END;
  661. END;
  662.  
  663.  
  664.  
  665. {──────────────────────────────────────────────────────────────────────────}
  666. Procedure MoveAround;
  667.   { This is the main display procedure. Firstly it brings the object towards
  668.     the viewer by increasing the Zoff, then passes control to the user }
  669. VAR deg,loop1,loop2:integer;
  670.     ch:char;
  671.  
  672.   Procedure Whizz (sub:boolean);
  673.   VAR loop1:integer;
  674.   BEGIN
  675.     For loop1:=-64 to -5 do BEGIN
  676.       zoff:=loop1*8;
  677.       if sub then xoff:=xoff-7 else xoff:=xoff+7;
  678.       RotatePoints (deg,deg,deg);
  679.       DrawPoints;
  680.       flip (vaddr,vga);
  681.       Cls (vaddr,0);
  682.       deg:=(deg+5) mod 360;
  683.     END;
  684.   END;
  685.  
  686. BEGIN
  687.   deg:=0;
  688.   ch:=#0;
  689.   Yoff:=100;
  690.   Xoff:=350;
  691.   Cls (vaddr,0);
  692.   For loop1:=1 to maxpolys do
  693.     For loop2:=1 to 4 do BEGIN
  694.       Lines [loop1,loop2].x:=a [loop1,loop2,1];
  695.       Lines [loop1,loop2].y:=a [loop1,loop2,2];
  696.       Lines [loop1,loop2].z:=a [loop1,loop2,3];
  697.     END;
  698.   Whizz (TRUE);
  699.  
  700.   For loop1:=1 to maxpolys do
  701.     For loop2:=1 to 4 do BEGIN
  702.       Lines [loop1,loop2].x:=s [loop1,loop2,1];
  703.       Lines [loop1,loop2].y:=s [loop1,loop2,2];
  704.       Lines [loop1,loop2].z:=s [loop1,loop2,3];
  705.     END;
  706.   Whizz (FALSE);
  707.  
  708.   For loop1:=1 to maxpolys do
  709.     For loop2:=1 to 4 do BEGIN
  710.       Lines [loop1,loop2].x:=p [loop1,loop2,1];
  711.       Lines [loop1,loop2].y:=p [loop1,loop2,2];
  712.       Lines [loop1,loop2].z:=p [loop1,loop2,3];
  713.     END;
  714.   Whizz (TRUE);
  715.  
  716.   For loop1:=1 to maxpolys do
  717.     For loop2:=1 to 4 do BEGIN
  718.       Lines [loop1,loop2].x:=h [loop1,loop2,1];
  719.       Lines [loop1,loop2].y:=h [loop1,loop2,2];
  720.       Lines [loop1,loop2].z:=h [loop1,loop2,3];
  721.     END;
  722.   Whizz (FALSE);
  723.  
  724.   For loop1:=1 to maxpolys do
  725.     For loop2:=1 to 4 do BEGIN
  726.       Lines [loop1,loop2].x:=y [loop1,loop2,1];
  727.       Lines [loop1,loop2].y:=y [loop1,loop2,2];
  728.       Lines [loop1,loop2].z:=y [loop1,loop2,3];
  729.     END;
  730.   Whizz (TRUE);
  731.  
  732.   For loop1:=1 to maxpolys do
  733.     For loop2:=1 to 4 do BEGIN
  734.       Lines [loop1,loop2].x:=x [loop1,loop2,1];
  735.       Lines [loop1,loop2].y:=x [loop1,loop2,2];
  736.       Lines [loop1,loop2].z:=x [loop1,loop2,3];
  737.     END;
  738.   Whizz (FALSE);
  739.  
  740.   For loop1:=1 to maxpolys do
  741.     For loop2:=1 to 4 do BEGIN
  742.       Lines [loop1,loop2].x:=i [loop1,loop2,1];
  743.       Lines [loop1,loop2].y:=i [loop1,loop2,2];
  744.       Lines [loop1,loop2].z:=i [loop1,loop2,3];
  745.     END;
  746.   Whizz (TRUE);
  747.  
  748.   For loop1:=1 to maxpolys do
  749.     For loop2:=1 to 4 do BEGIN
  750.       Lines [loop1,loop2].x:=a [loop1,loop2,1];
  751.       Lines [loop1,loop2].y:=a [loop1,loop2,2];
  752.       Lines [loop1,loop2].z:=a [loop1,loop2,3];
  753.     END;
  754.   Whizz (FALSE);
  755.  
  756.   cls (vaddr,0);
  757.   cls (vga,0);
  758.   Xoff := 160;
  759.  
  760.   Repeat
  761.     if keypressed then BEGIN
  762.       ch:=upcase (Readkey);
  763.       Case ch of 'A' : zoff:=zoff+5;
  764.                  'Z' : zoff:=zoff-5;
  765.                  ',' : xoff:=xoff-5;
  766.                  '.' : xoff:=xoff+5;
  767.                  'S' : yoff:=yoff-5;
  768.                  'X' : yoff:=yoff+5;
  769.       END;
  770.     END;
  771.     DrawPoints;
  772.     flip (vaddr,vga);
  773.     cls (vaddr,0);
  774.     RotatePoints (deg,deg,deg);
  775.     deg:=(deg+5) mod 360;
  776.   Until ch=#27;
  777. END;
  778.  
  779.  
  780. BEGIN
  781.   SetUpVirtual;
  782.   clrscr;
  783.   Writeln ('Hello there! Varsity has begun once again, so it is once again');
  784.   Writeln ('back to the grindstone ;-) ... anyway, this tutorial is, by');
  785.   Writeln ('popular demand, on poly-filling, in relation to 3-D solids.');
  786.   Writeln;
  787.   Writeln ('In this program, the letters of ASPHYXIA will fly past you. As you');
  788.   Writeln ('will see, they are solid, not wireframe. After the last letter has');
  789.   Writeln ('flown by, a large A will be left in the middle of the screen.');
  790.   Writeln;
  791.   Writeln ('You will be able to move it around the screen, and you will notice');
  792.   Writeln ('that it may have bits only half on the screen, i.e. clipping is');
  793.   Writeln ('perfomed. To control it use the following : "A" and "Z" control the Z');
  794.   Writeln ('movement, "," and "." control the X movement, and "S" and "X"');
  795.   Writeln ('control the Y movement. I have not included rotation control, but');
  796.   Writeln ('it should be easy enough to put in yourself ... if you have any');
  797.   Writeln ('hassles, leave me mail.');
  798.   Writeln;
  799.   Writeln ('I hope this is what you wanted...leave me mail for new ideas.');
  800.   writeln;
  801.   writeln;
  802.   Write ('  Hit any key to contine ...');
  803.   Readkey;
  804.   SetMCGA;
  805.   SetUpPoints;
  806.   MoveAround;
  807.   SetText;
  808.   ShutDown;
  809.   Writeln ('All done. This concludes the ninth sample program in the ASPHYXIA');
  810.   Writeln ('Training series. You may reach DENTHOR under the names of GRANT');
  811.   Writeln ('SMITH/DENTHOR/ASPHYXIA on the ASPHYXIA BBS. I am also an avid');
  812.   Writeln ('Connectix BBS user, and occasionally read RSAProg.');
  813.   Writeln ('The numbers are available in the main text. You may also write to me at:');
  814.   Writeln ('             Grant Smith');
  815.   Writeln ('             P.O. Box 270');
  816.   Writeln ('             Kloof');
  817.   Writeln ('             3640');
  818.   Writeln ('I hope to hear from you soon!');
  819.   Writeln; Writeln;
  820.   Write   ('Hit any key to exit ...');
  821.   Readkey;
  822. END.
  823.  
  824.