# DENTHOR/ASPHYXIA'S VGA TRAINERS 3D SOLIDS

On to today’s tutorial: **3D solids**. That is what the people wanted, that is what the people get! This tutorial is mainly on how to draw the polygon on screen. For details on how the 3D stuff works, check out part 8.

On to today’s tutorial: **3D solids**. That is what the people wanted, that is what the people get! This tutorial is mainly on how to draw the polygon on screen. For details on how the 3D stuff works, check out part 8.

DENTHOR, coder for ... _____ _____ ____ __ __ ___ ___ ___ ___ __ _____ / _ \ / ___> | _ \ | |_| | \ \/ / \ \/ / | | / _ \ | _ | \___ \ | __/ | _ | \ / > < | | | _ | \_/ \_/ <_____/ |__| |__| |__| |__| /__/\__\ |__| \_/ \_/ smith9@batis.bis.und.ac.za The great South African Demo Team! Contact us for info/code exchange!

Grant Smith, alias Denthor of Asphyxia, wrote up several articles on the creation of demo effects in the 90s. I reproduce them here, as they offer so much insight into the demo scene of the time.

These articles apply some formatting to Denthor's original ASCII files, plus a few typo fixes.

## How to draw a polygon

Sounds easy enough, right? *WRONG!* There are many, many different ways to go about this, and today I’ll only be showing you one. Please don’t take what is written here as anything approaching the best method, it is just here to get you on your way…

The procedure I will be using here is based on something most of us learned in standard eight … I think. I seem to recall doing something like this in Mrs. Reid’s maths class all those years ago ;)

Take two points, x1,y1 and x2,y2. Draw them:

Right, so what we have to do is this: if we know the y-coord of `a`

, what is its x-coord? To prove the method we will give the points random values.

Right. Simple enough problem. This is how we do it:

So our equation is:

which gives you:

That means that along the line with `y=12`

, `x`

is equal to 3.3. Since we are not concerned with the decimal place, we replace the `/`

with a `div`

, which in Pascal gives us an integer result, and is faster too. “All well and good,” I hear you cry, “but what does this have to do with life and how it relates to polygons in general?” The answer is simple. For each of the four sides of the polygon we do the above test for each y line. We store the smallest and the largest x values into separate variables for each line, and draw a horizontal line between them. Ta-Dah! We have a cool polygon!

For example - two lines going down:

Find `x1`

and `x2`

for that `y`

, then draw a line between them. Repeat for all `y`

values.

Of course, it’s not as simple as that. We have to make sure we only check those `y`

lines that contain the polygon (a simple `min y`

, `max y`

test for all the points). We also have to check that the line we are calculating actually extends as far as where our current `y`

is (check that the point is between both `y`

’s). We have to compare each `x`

to see whether it is smaller then the minimum `x`

value so far, or bigger then the maximum (the original `x min`

is set as a high number, and the `x max`

is set as a small number). We must also check that we only draw to the place that we can see (0-319 on the x; 0-199 on the y (the size of the MCGA screen)).

To see how this looks in practice, have a look at the sample code provided. (Mrs. Reid would probably kill me for the above explanation, so when you learn it in school, split it up into thousands of smaller equations to get the same answer ;))

Okay, that’s it! What’s that? How do you draw a vertical line? Thats simple…

## Drawing a vertical line

Right, this is a lot easier than drawing a normal line (part 5 .. I think), because you stay on the same y value. So, what you do is you set `ES`

to the screen you want to write to, and get `DI`

to the start of the y-line (see earlier trainers for a description of how SEGMENT:OFFSET works).

Right, now you add the first `x`

value to get your startoff.

Move the color to store into `ah`

and `al`

:

then get `CX`

equal to how many pixels across you want to go:

Okay, as we all know, moving a word is a lot faster thán moving a byte, so we halve `CX`

:

but what happens if `CX`

was an odd number? After a shift, the value of the last number is placed in the carry flag, so what we do is jump over a single byte move if the carry flag is zero, or execute it if it is one.

Right, the finished product looks like this:

Done!

## In closing

This 3D system is still not perfect. It needs to be faster, and now I have also dumped the problem of face-sorting on you! Nyahahahaha!