# Re:fast 3-d Wireframe

``` BBS: Inland Empire Archive
Date: 04-23-92 (02:50)             Number: 141
From: RICH GELDREICH               Refer#: NONE
To: RICK PEDLEY                   Recvd: NO
Subj: Re:fast 3-d Wireframe          Conf: (2) Quik_Bas```
```>  On 04-18-92 RICH GELDREICH wrote to Rick Pedley...
>
>  RG> Well, here you go! The following 3 messages contain a FAST 3-D
> wireframe
>  RG>
>  RG> program. See the last message for more details...
>  RG>
>  RG> 'Start of part *1*
>  RG> '3DEXP.BAS By Rich Geldreich April 16th, 1992
>  RG> 'Note: This is only experimental! Use at your own risk...
>  RG> '(A Fast 3-D wireframe program... Compile it for maximum speed!)
>
> This is excellent stuff!  It really demonstrates to me what is
> possible in pure BASIC.  I must have fooled around with it for
> about an hour last night.  Now -- how about giving us a rundown
> on what the code does?  Something this good shouldn't pass by
> undocumented.  (BTW, the only glitch I found was the occasional
> overflow error when the object strays too far into 'sub space').
>
>
Ow ya, about that glich. It does hang up the compiled program once
in a while. I hate when that happens! Damn.

The program has its origons to June of '91- when I first tried to
rotate a simple, mediocre 3-D cube. Well, logically, there must be 3
seperate coordinates, not two. I knew how to rotate 2-D coordinates
about the origon- with sine and cosine.

To rotate a X,Y point in 2-D space, the following formulus can be
used:

Xnew= Xold * Cosine(Angle) + YOld * Sine(Angle)
Ynew= Xold * Sine(Angle) - YOld * Cosine(Angle)

(This formula may differ a bit from the program- I'm doing this off the

This will rotate any 2-D point around an origon of (0,0). Simple enough.

It gets a little hairy with 3-D. I came up with this solution...

Two identify any 3-D vector in the universe, you must have 3 angles, of
course. The first angle rotates about the X & Y axis. The second, the X
& Z and the third rotates about the Y & Z. (I've only implemented the X,
& Z and Y & Z in the program you got for speed). So, the new formulas
for rotating a 3-D point are:

Xnew= X * Cosine + Z * Sine
Ztemp= X * Sine - Z * Cosine

Ynew=Y * Cosine2 + Ztemp * Sine2
Znew=Y * Sine2 - Ztemp * Cosine2

Where Cosine and Sine are: cos(Angle1) & sin(Angle1)
Where Cosine2 and Sine2 are: cos(Angle2) & sin(Angle2)

(the above calculations will only rotate on 2 axis. The only axis left
is the X and Y)

After all that's done, it's time for perspective. It took me weeks to
derive that formula.

Here's a rundown on how the perspective formula works: Take the freshly
rotated, 3-D point and draw a line from it to where you are "standing".
Wherever this line crosses the "viewing screen" [which is always
directly in front of you] is where you have to plot the point on the
monitor. (Clear as mud, huh?)

To simplify things, I always assume that the user is "standing" directly
at the origon of the 3-D world, or (0,0,0).

Here's the formula:

Xnew=Xold+(-Xold*( (Spos - Zold) / (Mpos - Zold) ) )
Ynew=Yold+(-Yold*( (Spos - Zold) / (Mpos - Zold) ) )

Where Spos is the Z coordinate of the viewing screen and MPos is the
Z coordinate of the user. Interesting notes: If you make Mpos get
farther and farther away from the screen, you will be looking at
yourself! This is how flight simulators get an outside view of the
craft(or at least that's how they can...)

Most 3-D simulations use a much simple formula(which I forget offhand)
for placing their views in perspective. Ow ya, it's quicker than mine,
but it is only correct for points that are directly in front of the user
and that is not "cool" for a universe(if you see what I mean...).

To get the most speed, I only use integer math. In the very
begginning of the program, I calculate a big sine and cosine
table(that's why there's such a big delay). Remember: for maximum speed,
always use integer math over floating point ! !

If you liked that little program, then you'll love my first shareware
product, Warp Field Animator. It's written in QB and assembly- of
course. It rotates/warps/animates 2-D and 3-D fields from user selected
parameters. It can also load a .gif file and allow the user to warp,
rotate, "tornado", & destroy it as much as the user wants! It's awesome!

See ya around! Rich Geldreich

--- RBBSMAIL 17.2A
* Origin: Computer Co-Op RBBS HST, 609-784-9404 Voorhees
NJ (RBBS-PC 1:266/29)
```

Echo Basic Postings

Books at Amazon:

Back to BASIC: The History, Corruption, and Future of the Language

Hackers: Heroes of the Computer Revolution (including Tiny BASIC)

Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution

The Advent of the Algorithm: The Idea that Rules the World

Moths in the Machine: The Power and Perils of Programming

Mastering Visual Basic .NET