This trainer is on starfields, which is by request of more than one person. This is quite an easy effect, and you should have no trouble grasping the concept behind it. I will be doing a 3D starfield; a horizontal starfield is very easy with you merely incrementing a x-value for each star for each frame.

DENTHOR, coder for ...
_____   _____   ____   __   __  ___  ___ ___  ___  __   _____
/  _  \ /  ___> |  _ \ |  |_|  | \  \/  / \  \/  / |  | /  _  \
|  _  | \___  \ |  __/ |   _   |  \    /   >    <  |  | |  _  |
\_/ \_/ <_____/ |__|   |__| |__|   |__|   /__/\__\ |__| \_/ \_/
smith9@batis.bis.und.ac.za
The great South African Demo Team! Contact us for info/code exchange!  

Grant Smith, alias Denthor of Asphyxia, wrote up several articles on the creation of demo effects in the 90s. I reproduce them here, as they offer so much insight into the demo scene of the time.

These articles apply some formatting to Denthor's original ASCII files, plus a few typo fixes.

What is a 3D starfield?

I am not even sure if I should do this bit. Go watch any episode of Star Trek, the movies, Star Wars, or just about any sci-fi movie. Somewhere there will be a scene where you can see stars whizzing past the viewscreen, with the ones that are further away moving slower than the ones that are passed quite close to.

This is a 3D starfield. If you look closely, you will see that all the stars seem to originate from a point, the point you are travelling towards. This is an illusion which thankfully happens automatically, you don’t have to code for it ;)

Starfields look very nice, and can make a big difference to an otherwise black background. It also makes a great screen saver ;-)

How do they work?

This is actually quite simple. Imagine if you will, each star in the heavens having an x,y and z coordinate, with you being at 0,0,0. Easy? Right. Now, if you were to say move forward, i.e. increase your z value, to you you will still be at 0,0,0 but all the stars z values would have appeared to decrease by the exact same amount.

In easier language, we decrease the z value of all the stars so that they come closer to you, and eventually whizz past.

This solves all our problems. Stars that are close to us on the x and y scales will pass us by faster than those that are very far from us on the x and y scales. The only thing we must watch out for is that no star is at 0,0 , i.e. exactly in front of us, otherwise there will be a collision which will not look good.

How do we code this?

The first thing to be done is to generate our starfield. This is quite easy, with us choosing x values between -160 and 160, and y values between -100 and 100 randomly. Each z is sequentially greater for each star so that we don’t get large areas with no stars. We must remember to check that there are no stars at 0,0!

Okay, now we start the actual viewing section. Here are the steps:

1) Convert our 3-d coordinates into their 2-d versions. Have a look at tut 8 to see how this is done, but basically we divide by z.

2) Clear away all old stars that may be on the screen.

3) Draw all our stars according to our 2-d values we have calculated in 1)

4) Move all the stars either closer to us or further away from us by decreasing or increasing their z values respectively.

5) If a star’s z value has passed into the negative, place it at the very back of our “queue” so that it will come around again

6) Jump back to 1) ad-infinitum.

That is, as they say, it. In our sample program the steps have been neatly placed into individual procedures for easy reading.

What next?

Okay, so now we have a cool looking starfield. What next? How about adding left and right motion? A menu or a scrolly in the foreground? How about figuring out how a star tunnel works? A cool 3d routine going in front of the stars?

A starfield can make just about any routine look just that much more professional, and can itself be improved to be a great effect all on its own.