TUT14.TXT

                   ╒═══════════════════════════════╕
                   │         W E L C O M E         │
                   │  To the VGA Trainer Program   │ │
                   │              By               │ │
                   │      DENTHOR of ASPHYXIA      │ │ │
                   ╘═══════════════════════════════╛ │ │
                     ────────────────────────────────┘ │
                       ────────────────────────────────┘

                           --==[ PART 14 ]==--



=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
■ Introduction

Hello there. Exams are just around the corner (again :( ), so I thought 
I better get round to doing the next trainer. As usual, there seems to 
have been a big delay between this one and the last one... sorry about 
that ;-)

Well, this trainer is mainly on four things : Glenzing, faster polys,
fixed point and assembler. The sample program is basically tut 9
rewritten to include the above.

I'll go through them in order, and hopefully you won't have any hassles
grasping the concepts. By the way, do any of you read the text files? I
find myself answering questions via E-Mail etc. that were discussed in
the text sections of the trainers ... oh well, I'll just ramble along
anyway ;-)

Please dont send any mail to smith9@batis.bis.und.ac.za anymore ... I 
don't know for how much longer the account will be valid (How can a 
non-BIS person get onto the BIS UNIX machine in the BIS2 directory? If 
his name is Denthor I suppose ;-) Oh well, I got about 8 months use out 
of it. The account expires on Christmas day anyway...) So anyway, please
leave all messages to denthor@beastie.cs.und.ac.za
                                                  

If you would like to contact me, or the team, there are many ways you
can do it : 1) Write a message to Grant Smith/Denthor/Asphyxia in private mail
                  on the ASPHYXIA BBS.
            2) Write to :  Grant Smith
                           P.O.Box 270 Kloof
                           3640
                           Natal
                           South Africa
            3) Call me (Grant Smith) at (031) 73 2129 (leave a message if you
                  call during varsity). Call +27-31-73-2129 if you call
                  from outside South Africa. (It's YOUR phone bill ;-))
            4) Write to denthor@beastie.cs.und.ac.za in E-Mail.
            5) Write to asphyxia@beastie.cs.und.ac.za to get to all of
               us at once.

NB : If you are a representative of a company or BBS, and want ASPHYXIA
       to do you a demo, leave mail to me; we can discuss it.
NNB : If you have done/attempted a demo, SEND IT TO ME! We are feeling
        quite lonely and want to meet/help out/exchange code with other demo
        groups. What do you have to lose? Leave a message here and we can work
        out how to transfer it. We really want to hear from you!



=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
■ What is glenzing?

This is an easy one. Imagine, in a 3D object, that all the sides are
made out of colored glass. That means that every time you look through
that side, everything behind it is tinged in a certain color.

In ascii ...
          +---------+
          |      <--|---Light blue
          |         |
     +--------+     |
     |    | <-|-----|---Dark blue
     |    +---|-----+
     |     <--|---------Light blue
     +--------+

So where the two sides overlap, the color values of the two sides are 
added. Easy huh? It is also easy to code. This is how you do it :

Set up your pallette to be a nice run of colors.
Draw your first poly.
While drawing poly 1, instead of plonking down a set pixel color, grab the
  backgrond pixel, add 1 to it, then put the result down.
Draw your second poly.
While drawing poly 2, instead of plonking down a set pixel color, grab the
  backgrond pixel, add 2 to it, then put the result down.
and so forth.

So if the color behind poly 1 was 5, you would place pixel 6 down
instead.

If you do this for every single pixel of every single side of your 3d 
object, you then have glenzing going. This is obviously slightly slower
then just drawing an item straight, but in the sample program it goes
quite quickly ... this is because of the following sections...
  

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
■ Faster Polygons

In Tut 9, you probably noticed that we were using a multiply for every
single line of the poly that we drew. This is not good. Let's find out
how to speed it up, shall we...

With the multiply method, we went through every line, to find out the
minimum x and maximum x value for that line.

                           +
                   ------/---\------- Find min x and max x, draw a line
                       /       \        between them.
                     +           +
                       \       /
                         \   /
                           +

How about if we found out all the min and max x's for every line first, 
then just went through an array drawing them. We could do it by 
"scanning" each side in turn. Here is how we do it :

                          + 1
                        /
                      /
                  2 +

We go from point one to point two. For every single y we go down, we 
move a constant x value. This value is found like this :

           xchange := (x1-x2)/(y1-y2)

Remember gradients? This is how you calulated the slope of a line waaay
back in school. You never thought it would be any use, didn't you ;-)

Anyway, with this value, we can do the following :

    For loop1:=y1 to y2 do BEGIN
      [ Put clever stuff here ]
      x:=x+xchange;
    END;
    
and we will go through all the x-values we need for that line. Clever, 
huh?

Now for the clever bit. You have an array, from 0 to 199 (which is all
the possible y-values your onscreen poly can have). Inside this is two 
values, which will be your min x and your max x. You start off with the 
min x being a huge number, and the max x being a low number. Then you 
scan a side. For each y, check to see if one of the following has 
happened :    If the x value is smaller then the xmin value in your
                array, make the xmin value equal to the x value
              If the x value is larger then the xmax value in your
                array, make the xmax value equal to the x value

The loop now looks like this :

    For loop1:=y1 to y2 do BEGIN
      if x>poly[loop1,1] then poly[loop1,1]:=x;
      if x