3D in VB Overview
This section is all about 3D computer graphics for visual basic. This is for a very basic level but will give a person new to the world of 3D a good start. At the end of this tutorial is a zip file that contains an example of the tutorial so that you can get an idea of how to apply what you've learned.
What is 3D?
Well we all live in the three dimensional world and should have a pretty good idea of what it is like, but what is it like in the computer, how does the computer think of it? I think it is best imagined as a 3D graph. It has x(horizontal) y(vertical) and z(depth). To put this into a program you need a set of 3 coordinates for each point. In VB you would create a type for this:
Public Type t3Dcoord
x as double
y as double
z as double
End Type
Dim Array() As t3Dcoord
This creates an array of 3D vectors. Using this you can create all sorts of 3D shapes, but then you reach the problem. How do I put it on my 2D screen?
This is actually very easy to do. All you do is use the formulas:
x=x/z
y=y/z
After this you will have to multiply this by the width of your screen because this assumes the width of your screen is 1. Otherwise your shapes will seem stretched or contracted.
Of course it isn't as simple as that because you will not be able to move at all and you will have behind you shown as well. To handle this you create a camera.
The camera will just be the point that you are looking from. You make the camera from a single 3D vector. To apply the camera to the points you simply take the camera's coordinates from the points coordinates. This is basically moving the points so tht the camera is at point 0,0,0. The coding for this would be:
x = x - cam.x
y = y - cam.y
z = z - cam.z
Now we need to remove anything behind the camera that you can't see. If a points z coordinate is less than 0 then we don't draw the point. This is best done when you are actually drawing the points because you can simply not draw it.
Thats a brief overview of 3D. To find out about more complex things like rortation take a look at some of the other instructional pages. Below are some useful formulas.
Sphere. This is a general formula it will not work directly in VB. Note that VB works in radian rather than degrees.
For a = 0 to 360 step 10
For b = 0 to 180 step 10
x(i) = sin(a) * sin(b)
y(i) = cos(a) * sin(b)
z(i) = cos(b)
i = i + 1
Next
Next
Download the source code for this tutorial.


beeen a while