**3D System**

by: cprogramming.com

þ Introduction

Hi there! ASPHYXIA is BACK with our first MegaDemo, Psycho Neurosis! Apaltry 1.3MB download is all it takes to see the group from Durbs firstmajor production! We are quite proud of it, and think you should see it;)

Secondly, I released a small little trainer (a trainerette ;-)) onRsaPROG and Connexctix BBS mail, also on the ASPHYXIA BBS as COPPERS.ZIPIt is a small Pascal program demonstrating how to display copper bars intext mode. Also includes a check for horizontal retrace (A lot of peoplewanted it, that is why I wrote the program) (ASPHYXIA ... first with thetrainer goodies ;-) aargh, sorry, had to be done ))

Thirdly, sorry about the problems with Tut 8! If you had all thechecking on, the tutorial would probably die on the first points. Thereason is this : in the first loop, we have DrawPoints thenRotatePoints. The variables used in DrawPoints are set in RotatePoints,so if you put RotatePoints before DrawPoints, the program should workfine. Alternatively, turn off error checking 8-)

Fourthly, I have had a surprisingly large number of people saying that"I get this, like, strange '286 instructions not enabled' message!What's wrong with your code, dude?" To all of you, get into Pascal, hitAlt-O (for options), hit enter and a 2 (for Enable 286 instructions). Hardhey? Doesn't anyone EVER set up their version of Pascal?

Now, on to todays tutorial! 3D solids. That is what the people wanted,that is what the people get! This tutorial is mainly on how to draw thepolygon on screen. For details on how the 3D stuff works, check out tut8.

If you would like to contact me, or the team, there are many ways youcan do it :

1) Write a message to Grant Smith/Denthor/Asphyxia in private mail on the ASPHYXIA BBS.

2) Write to Denthor, EzE or Goth on Connectix.

3) Write to : Grant Smith / P.O.Box 270 Kloof / 3640 / Natal

4) Call me (Grant Smith) at (031) 73 2129 (leave a message if you call during varsity)

5) Write to mcphail@beastie.cs.und.ac.za on InterNet, and mention the word Denthor near the top of the letter.

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!

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

þ How to draw a polygon

Sounds easy enough, right? WRONG! There are many, many different ways togo about this, and today I'll only be showing you one. Please don't takewhat is written here as anything approaching the best method, it is justhere to get you on your way...

The procedure I will be using here is based on something most of uslearned in standard eight ... I think. I seem to recall doing somethinglike this in Mrs. Reids maths class all those years ago ;)Take two points, x1,y1 and x2,y2. Draw them :

+ (x1,y1)
\
\ <-- Point a somewhere along the line
\
+ (x2,y2)

Right, so what we have to do is this : if we know the y-coord of a, whatis it's x-coord? To prove the method we will give the points randomvalues.

+ (2,10)
\
\ <-- a.y = 12
\
+ (15,30)

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

(a.y-y1) = (12 - 10) {to get a.y as though y1 was zero}
*(x2-x1) = *(15 - 2) {the total x-length of the line}
/(y2-y1) = /(30 - 10) {the total y-length of the line}
+x1 = +2 { to get the equation back to real coords}
So our equation is : (a.y-y1)*(x2-x1)/(y2-y1)+x4 or
(12-10)*(15-2)/(30-10)+2
which gives you :
2*13/20+2 = 26/20+2
= 3.3

That means that along the line with y=12, x is equal to 3.3. Since weare 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 welland good, I hear you cry, but what does this have to do with life andhow it relates to polygons in general. The answer is simple. For each ofthe four sides of the polygon we do the above test for each y line. Westore the smallest and the largest x values into separate variables foreach line, and draw a horizontal line between them. Ta-Dah! We have acool polygon!

For example : Two lines going down :

+ +
/ <-x1 x2->| <--For this y line
/ |
+ +

Find x1 and x2 for that y, then draw a line between them. Repeat for ally values.

Of course, it's not as simple as that. We have to make sure we onlycheck those y lines that contain the polygon (a simple min y, max y testfor all the points). We also have to check that the line we arecalculating actually extends as far as where our current y is (checkthat the point is between both y's). We have to compare each x to seeweather it is smaller then the minimum x value so far, or bigger thenthe maximum (the original x min is set as a high number, and the x maxis set as a small number). We must also check that we only draw to theplace that we can see ( 0-319 on the x ; 0-199 on the y (the size of theMCGA screen))

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

Okay, that's it! What's that? How do you draw a vertical line? Thatssimple ...

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

þ Drawing a vertical line

Right, this is a lot easier than drawing a normal line (Tut 5 .. Ithink), because you stay on the same y value. So, what you do is you setES to the screen you want to write to, and get DI to the start of they-line (see earlier trainers for a description of how SEGMENT:OFFSETworks.

IN : x1 , x2, y, color, where
asm
mov ax,where
mov es,ax
mov di,y
mov ax,y
shl di,8 { di:=di*256 }
shl ax,6 { ax:=ax*64 }
add di,ax { di := (y*256)+(y*64) := y*320 Faster then a
straight multiplication }

Right, now you add the first x value to get your startoff.

add di,x1

Move the color to store into ah and al

mov al,color
mov ah,al { ah:=al:=color }

then get CX equal to how many pixels across you want to go

mov cx,x2
sub cx,x1 { cx:=x2-x1 }

Okay, as we all know, moving a word is a lot faster then moving a byte,so we halve CX

shr cx,1 { cx:=cx/2 }

but what happens if CX was an odd number. After a shift, the value ofthe last number is placed in the carry flag, so what we do is jump overa single byte move if the carry flag is zero, or execute it if it isone.

jnc @Start { If there is no carry, jump to label Start }
stosb { ES:[DI]:=al ; increment DI }
@Start : { Label Start }
rep stosw { ES:[DI]:=ax ; DI:=DI+2; repeat CX times }

Right, the finished product looks like this :

[Pascal]
Procedure Hline (x1,x2,y:word;col:byte;where:word); assembler;
{ This draws a horizontal line from x1 to x2 on line y in color col }
asm
mov ax,where
mov es,ax
mov ax,y
mov di,ax
shl ax,8
shl di,6
add di,ax
add di,x1
mov al,col
mov ah,al
mov cx,x2
sub cx,x1
shr cx,1
jnc @start
stosb
@Start :
rep stosw
end;
[C++]
void Hline (word X1, word X2, word Y, byte Col, word Where) {
asm {
mov ax, [Where] // move segment of Where to AX
mov es, ax // set ES to segment of Where
mov ax, [Y] // set AX to Y
mov di, ax // set DI to Y
shl ax, 8 // shift AX left 8 places (multiply Y by 256)
shl di, 6 // shift DI left 6 places (multiply Y by 64)
add di, ax // add AX to DI (Y*64 + Y*256 = Y*320)
add di, [X1] // add the X1 offset to DI
mov al, [Col] // move Col to AL
mov ah, al // move Col to AH (we want 2 copies for word moving)
mov cx, [X2] // move X2 to CX
sub cx, [X1] // move the change in X to CX
shr cx, 1 // divide change in X by 2 (for word moving)
jnc Start // if we have an even number of moves, go to Start
stosb // otherwise, move one byte more
}
Start: asm {
rep stosw // do it!
}
}

Done!

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

þ In closing

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

[ My sister and I were driving along the other day when she asked me, what would I like for my computer. I thought long and hard about it, and came up with the following hypothesis. When a girl gets a Barbie doll, she then wants the extra ballgown for the doll, then the hairbrush, and the car, and the house, and the friends etc. When a guy gets a computer, he wants the extra memory, the bigger hard drive, the maths co-pro, the better motherboard, the latest software, and the bigger monitor etc. I told my sister all of this, and finished up with : "So as you can see, computers are Barbie dolls for MEN!" She called me a chauvinist. And hit me. Hard. ]

- Grant Smith

19:24

26/2/94

See you next time!

- Denthor

These fine BBS's carry the ASPHYXIA DEMO TRAINER SERIES : (alphabetical)

ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍËÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍËÍÍÍÍÍËÍÍÍËÍÍÍÍËÍÍÍÍ»
ºBBS Name ºTelephone No. ºOpen ºMsgºFileºPastº
ÌÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÎÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÎÍÍÍÍÍÎÍÍÍÎÍÍÍÍÎÍÍÍÍ¹
ºASPHYXIA BBS #1 º(031) 765-5312 ºALL º * º * º * º
ºASPHYXIA BBS #2 º(031) 765-6293 ºALL º * º * º * º
ºConnectix BBS º(031) 266-9992 ºALL º º * º * º
ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÊÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÊÍÍÍÍÍÊÍÍÍÊÍÍÍÍÊÍÍÍÍ¼
Open = Open at all times or only A/H
Msg = Available in message base
File = Available in file base
Past = Previous Parts available
Does no other BBS's ANYWHERE carry the trainer? Am I writing this for
three people who get it from one of these BBS's each week? Should I go
on? (Hehehehe ... I was pleased to note that Tut 8 was THE most
downloaded file from ASPHYXIA BBS last month ... )

**DISCLAIMER:** The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |

More Web Development Articles

More By Developer Shed