QBXL Technical Articles

3D Tutorial

By Xerol

QBXL Extra
3d!? What good is that?
Really, if I have to tell you where 3d might be useful, you have some issues. Here we are though, a nice long list of applications!

Cutscene creation!
Racing games!
Puzzle games!
6D shooters(like descent!
Creepy playboy lookalike games!
Japanese dating games!
Retro 60s porno flicks!

I could go on all day, but if you can't think of an application for 3d, you should seek professional help!
LWell this program uses my 'generic' 3d engine and a BMP saving routine I found, uncredited, in an old QB program. (The date on the file is about 2 weeks after I downloaded Qb 4.5, so I probably got it just after I joined QB45.com for the first time).

Get the program here

The program "as-is" contains the code to produce a "road" scene, with cars traveling up and down it. With a low "stepping" (variable 's' at the end of the environment variable settings) the program produced 100 frames in about an hour(FPH). At full stepping the program would probably run about 20 FPH and therefore produce the intended 8 seconds of video in about 10 hours. Not too great, but I'd like to see YOU do better. (Really, I would. E-mail me  if you want to help me get this running faster.)

I really should re-write the program "barebones" with no render code in it so you can easily create your own but I figured an example might help too.

So, a quick summary of the program "as-is":

TYPE Car declaration: This used to create 'cars' to travel on the 'highway'. Not needed unless you want cars that only face one direction in your movie.

WINDOW (0, 0)-(639, 479)
OPEN "zbuf.3d" FOR RANDOM AS #3 LEN = 4
DIM SHARED zoomx, zoomy
SOUND 1000, .1

This section(above) basically "sets up" the program to render. None of the environment is set yet, but this does the following things: Gets the screen ready to put points down; Opens a ZBuffer file(1.2MB) to make sure points get "layered" right(although there is a few issues that still need to be resolved with this, certain camera angles 'confuse' it but i'll get into that later), Shares the zoom variables(which are needed by several functions) and then "clicks" to signal that the ZBuffer is
ready(it has to write 640*480 zero's to the file)

SetPal C, R, G, B: Sets the palette entries to whatever is needed for the scene.

The For n = 1 to 100 loop sets up the cars in random positions and makes sure none are intersecting.

For Iter = 0 to 200: Ok, finally getting into the REAL program now. This is the 201 'frames' that the program will generate.

The next lines set up the environment variables so that the camera moves correctly with the flow of the movie. Here's what needs to be set:
1) ViewX: X viewpoint of the camera
2) ViewY: Y viewpoint of the camera
3) ViewZ: Z viewpoint of the camera
4) Horcamrot: Horizontal camera rotation. Needs to be in radians, but I usually leave the conversion calculations sitting there anyway so that I can work in degrees. 0 = facing towards the positive Y axis from the origin. Positive = Rotating right, negative = rotating left but messes up the Z-Buffer. So to rotate left 45 degrees instead of using -45 use +315.
5) Vercamrot: Vertical Camera Rotation. 0 = Viewing directly horizontal. Positive = tilted down, negative = tilted up. No z-buffer problems with this, but using angles other than 'standard' ones(0, 30, 45, 60, 90 degrees) leads to 'skewed' points and a lot more holes than would normally appear, which means more points need to be rendered which means longer rendering times.
6) ZoomX and ZoomY: X and Y zoom of the camera. Usually you want this to be around 100 for a 'first test'. And you usually wants these to be equal. Unless you want a "cinematic" effect or something else.
7) s. Probably the most important variable, but when creating your own movies you need to remember to implement this yourself. I'll explain more below.

The next section of code does the actual rendering. The reason for the double blocks is that I used a 'larger' stepping for the 'farther' half of the road to reduce render time. The last block renders the cars and moves them along, the center lanes moving slightly faster than the outer ones.

The bottom block prepares the filename(you may have to change the path) so that the files are in sequential order(so the order isn't
1,10,11,12,13....19,2,20,21...etc, instead it goes 001, 002, 003, 004....)Then after saving the BMP, the program clears the ZBuffer, clears the screen, and goes up to the top again to do the next iteration.

After all 201 frames are done, the two loops at the bottom constantly "beep" until a key is pressed to alert you that the program is done. You can disable this if you like.

Now, to create your own movies:
1) Create the environment. Figure out where walls, floors, objects, etc. will be placed, what colors to use, and everything else about this. Paper works well. (I used 4 sheets just to design the cars, and they really don't look too good anyway)
2) Create code from this. I'm tired right now, and I don't feel like getting into detail right now, but I will explain later whether you like
it or not. Basically to create straight walls, do a set of nested For loops that do what you need to be done, and remember the STEP 1/s at the end of each For declaration. This way you can change the speed and detail of rendering easily, a low step for testing positions and the general look, and a high step for the final renderings.

Oh yeah, to put points actually on the screen, it's really simple.

pt3d x,y,z,c

That's all you have to do. The rest of the work is in the pt3d sub. The sub rotates the points depending on camera rotation, calculates screen coordinates, and checks the z-buffer so things appear in the right back-to-front order.

The BMPs take up 150kb each, so make sure you have enough free space on your main hard drive for this, or be ready to convert to jpgs as soon as they come off the 'assembly line'. I find it easier to wait for the end so that you can batch convert all the files.

Once again, I highly recommend IrfanView(www.irfanview.com) for the conversion of the files. I say JPG because they need to be in that format for Slide Show Movie Maker(www.joern-thiemann.de) to convert to a movie. Get the DivX 5.01(or whatever the latest version is) from any of a number of sites.

Load your JPGs into SSMM and hit "select all". Choose 0 for all settings except for "Frames for Displaying Picture". Set this to 1. Chose "Cross Dissolve" (NOT 2-WAY) as the effect for all frames. If you want each frame to display for more than one frame, choose "Frames for Fading to next picture" and set this to however many additional frames you want the frame to display for, as this creates a smoother effect than just re-displaying the same frames. Now, hit "Start Creation" and hit "Choose Video Codec". If you installed DivX 5.01 correctly it should be in the list. Now hit properties(it may come up automatically) and set a bitrate(1150 works fairly well while keeping filesizes relatively small).

If anyone needs help with any other features/projects feel free to
contact me.
Contact information can be found on the first page.

Have fun!

--Xerol Is QBXLs first contributing tech editor!

  Next issue: Creating objects.
Upcoming Tutorials: Textures: They are your friends.
  Modifying the basic functions.
 The Screen 13 RGB Palette;
 Colored Lightsourcing.
  Creating Real effects with only 16 colors(it CAN be done!)