Relating 3D points to 2D Co-ordinates + A 3d PSET function.
===========================================================
Tutorial By Tom Rathbone. 22/8/99
========================= =======
E-mail: tom.rathbone@advanced-internet.co.uk
====== chilliwilli_99@yahoo.com
Website: http://come.to/chilliwilli/
=======
Requirements: A QB compiler (Preferably QBasic 4.5)
============
I've had alot of people asking me about 3D routines in QB. Instead of telling each person seperately i have decided to write some tutorials so that more people can benefit from what i have learnt. I don't claim to be esspecially good at programming though i do think my techniques are simple to understand. These tutorials should be a good starting point for anyone who wants to learn how to create 3D graphic effects in QB. As well as 3D effects i shall teach some useful graphic routines such as Anti-aliasing. I also hope that i will be able to help with the maths behind 3D as it is essential to have a good understanding of the maths involved in order to be able to apply these routines effectively. I don't ask that you credit me in any programs which result from what i may teach you (as frankly i don't deserve it) however i do ask that you send me a copy of anything you write so that i can see how well people have understood my writings. Anyway enough... lets begin.
In this tutorial i shall try to explain a way of converting 3D to 2D by way of a simply algorithm. I shall also provide a routine which can rotate points in 3d space.
Converting a 3D Co-ordinate to 2D
=================================
A 3D co-ordinate has the form (x,y,z). Hence three dimensions. However in order to display this on a 2D screen we need to know how each points relative depth alters it position on the screen.
There is a simply equation for converting these points which can be easily derived using a little trigonometry (this set of tutorials will contain alot of trigonometry, if you have no knowledge of trig then i suggest you borrow some maths books from a library and teach yourself a bit. I'm not a maths teacher so i have not desire to teach such basic maths over and over again.) though i don't think you would gain alot from it so i'll give you the formulas straight.
X2D=256*(X3D/(Z3D + Zoffset) + Xoffset
Y2D=256*(Y3D/(Z3D + Zoffset) + Yoffset
The values of the offsets will depend on the size
Now this formula alone is no good for us as we don't want to have to type this everytime we want to convert a point so i suggest you write a basic function to return the 2D values when it is fed a 3D co-ordinate. Alternatively you could do as i am about to show you and include this conversion algoritm in your subroutines so you only have to call one routine instead of two.
A 3D PSET Routine
=================
Okay in QB create a new subroutine and call it something like XPSET.
Be sure to include the line: DECLARE SUB XPSET (x3d!,y3d!,z3d!,col%)
The code for the subroutine is as follows:
-------------------------------------------------------
SUB XPSET (x3d!, y3d!, z3d!,col%)
zoom = 200
depth = 40
x2d% = zoom * (x3d! / (z3d! + depth)) + 160
y2d% = zoom * (y3d! / (z3d! + depth)) + 100
pset (x2d%, y2d%), col%
END SUB
-------------------------------------------------------
NB. You will want to play with the zoom and the depth to suit your models.
WARNING: If you have a negative depth of greater or equal size to the depth e.g. -40 then you will get a division by zero and your prog will crash. Later i shall teach a technique for avoiding this.
Over the course of these tutorials we shall build up a comprehensive collection of subroutines such as the one below for you to use as you wish.
Plotting the corners of a cube.
===============================
OK now to use our new routine.
This code should plot the corners of a red cube.
SCREEN 13
DECLARE SUB XPSET (x3d!,y3d!,z3d!,col%)
XPSET 10,10,10,4
XPSET 10,10,-10,4
XPSET 10,-10,-10,4
XPSET -10,-10,-10,4
XPSET -10,-10,10,4
XPSET -10,10,10,4
XPSET -10,10,-10,4
XPSET 10,-10,10,4
END
The value 10 could be replaced with a variable. and an offset in each direction.
Try to write a program with a variable for cube width and three variables for displacements in the directions X,Y and Z. I'm sure you wont find that too hard.
Coming Next Tutorial
====================
Antialiasing points, rotations and smooth animations.