topblock - xscreensaver hack

topBlock is my first foray into openGL and xscreensaver graphical hacks.

The program creates 3D blocks that fall, building up and up.
There are numerous options to make it behave differently and to your preference.

First off, this is pretty simple to watch so unless you want some information about how I created this, all the information you need is in the man file.

Frequently Asked Questions
Genesis of a hack


topblock screen shot (thumbnail) I had a dream about a 3D environment with falling blocks that settled on one another thus building up towers.

This simple idea wanted to be realised as 3D screensaver.

Being a linux user I looked at the most prevalent screensaver package, xscreensaver, which has loads and loads of different graphical hacks with full source code.

I created some 3D blocks that bare little resemblance to commercially available building blocks and dropped them on one another.


Why ?
Easy, I wanted to.

A dream ?
well a daydream, a mental itch I had to scratch

Windows version ?
Maybe, it all depends ... <shrug>
Windows version

Where is truck mode ?
truck mode become blob mode when I realised making a truck version of the blocks would not be as easy I had first thought.

Any more hacks coming ?
Maybe, look here for more xscreensaver hacks

Why did you add the override mode ?
Having played with the camera location in real time via the keyboard short cuts I had seen the result of looking straight up and it looked pretty cool.

Isn't it a bit simplistic ?
It is elegant, colourful and fun to watch.

Where are the shadows and reflections ?
As a screen saver topBlock is not designed to max out your cpu, and it still manges to look good.

Why building blocks ?
Being 30 I naturally play with commercial building blocks a lot :)

Why did you switch from using glut?
glut can not draw to the root window and thus can not be a screen saver hack, plus it is a bit heavy.

Where can I get it ?
Hopefuly it will be part of the xscreensaver distribution soon, but in the mean time look at resources

Did it really take over a year to complete ?
Well I started in 2004 but the actual coding number was done in three months with very few hours per day.

I am creating my own hack, can I ask you for help ?
you can ask.

This page is bit basic, do you not know HTML ?
This page would not be enhanced by any more of a complex layout, and yes I am well versed in HTML and other web technologies

Genesis of a hack
topblock screen shot (thumbnail) Since the days of the Amiga I have been interested in graphical demos/hacks but 3D has always been beyond me.
Some of the hacks distributed as part of screensaver are truly beautiful to behold and it got me interested in trying my own hand at coding one.
I did try a 2d hack called subliminal static but this had numerous problems, mainly that it was not very interesting.

Back in 2004 I had an idea about colourful falling blocks falling from the sky and simply building up and up.
But it was not until early 2005 I began to explore the possibility, starting with notes an sketches in a notepad while I was traveling on the train to and from work.
I had clear idea of what I wanted and how the program logic would work along with camera setup but no actual code and no openGL experience.

To start with I bought Interactive Computer Graphics : A Top-Down Approach Using OpenGL and read a lot of the tutorials at NeHe.
The book was pretty heavy but filled in the gaps left by the simplistic approach at the NeHe site and it also included some great ready to go examples using a library called GLUT,The OpenGL Utility Toolkit
This allows you to get on with the 3D and forget about creating a window to put it in, it has a number of other handy utilities that make your life easier.

topblock screen shot (thumbnail) So towards the end of the year I took apart the cube demo from the books examples and gained my feet but just playing around, changing values and adding little bits here and there.
It was simple to remove one of the planes making a six sided cube into an open box, then I changed the dimensions so the box was twice as long as it was wide and added nipples and udders.
i.e. the six nodules on the top, made from tubes capped with disks and three larger tubes on the inside of the block.

Originally I had expected to call the hack LinuxBlock but as xscreensaver is not linux exclusive I changed the name to topBlock.
I now had my basic block and as it was part of a glList I could reuse it at will, setting the colour each time before the glList item was called upon.

The green base carpet reused my experience generating the block and ensured I could create this at any size.

By this time all I had were the objects, nothing actually happened, they just sat there waiting to be animated.
I remembered from college days about the useful nature of linked lists and set about creating a simple C program that would create a linked list of information where at each point in the list an list element only knows about where the next element is.
This means you can not reefer to element 4 directly as you would in an array but instead you start with the first element an use its pointer the next element in the list and then use its next pointer and so on until you reached the element you require.
That was exactly what I wanted, to add to a list and then process all items in the list each frame.
OpenGL does not just accept objects into a world and then draw it for you, you must recreate the world from your list of objects for each frame generated.

topblock screen shot (thumbnail) I began to explore how the dangerball hack worked in the xscreensaver 4.23 distribution and quickly found I could copy and paste the majority of code from the glut demo.
(glut is not suited as a framework for a screensaver hack for many reasons but the conversion was mostly painless).

xscreensaver allows you to use any program that writes to the root window and there is a lot of framework code that helps you setup a basic hack that does most things you want.
(from building the window, handling full screen, command parameters, saving preferences and much more).

By now (late December 2005) I had the blocks being randomly generated and falling as if from gravity but the blocks were not landing on each other instead going through each other.
I had to track how each block could be related in 3D space and work out when a connection was made stopping the block from falling further and ensuring any more brick could fall and connect to it.

topblock screen shot (thumbnail) This took some time but was not insurmountable and I began notice that the lighting was not quite right, as the blocks were rotated some had light sides and other did not.

Turns out I needed normals which are used by openGL to determine which faces of an object are facing a light source (or not) and so which faces are lite (or not).

(I had ignored normals when working with the original glut code because I could not see how they worked)

Another problem I had was tracking a moving object when the reference area it was located upon was rotating, after a few hours with trig and Java I had a solution, detailed here.

topblock screen shot (thumbnail) It was now mid January 2006 and things were starting to fly along, the normals were working giving a nice consistently lit world, the blocks were happily piling up and up.

It was time for some fun. One option I had seen in nearly all the openGL graphical hacks was a wire frame mode, showing the raw 3D structure of the objects displayed. After just a 10 mins and about 6 lines of code I had a wire frame mode, although I did falter on the lighting and colours at first.

topblock screen shot (thumbnail) I now had to tidy up the code, create the necessary support files, man file, XML config file and patches to the and

The code tidy up brought quite a few things to light, such as unused variables, unnecessary globals and the correct way for a xscreensaver to handle itself.

It also gave a change to play with and tweak default values and how the program reacted to different supplied values.

topblock screen shot (thumbnail) Finally the code was packaged up and sent to the xscreensaver creator jwz on 26th of Feburary 2006.

Will he accept it ?

time will tell ...

Time has told he has been working on a version 5 xscreensaver (works with Mac).

A few changes to the source code plus a cleanup I had been planning anyway and I have submitted the revised v5 compatable version of topblock [29/04/2006].

The main change is to add a XSCREENSAVER_MODULE_2 ("topBlock", topblock, topBlock) line which then takes over from the old #defines at the top of the code and add ENTRYPOINT to each special function.
Which also gives me a release function previously not possible
ENTRYPOINT void release_topBlock(ModeInfo *mi) {

(code available below).

topBlock was released as part of xscreensaver version 5 [27/06/2006]

Opps, someone found a bug (and only one person reported it), when using the parameter dropSpeed set to 7 the blocks only pile up twice before falling through each other.
Appears some dodgy code had created that and it only affected values in a 0.000131 window.
I fixed the bug and tidied up the code a bit thanks to suggestion from Valdis Kletnieks and also added gltrackball support (so you can move the display with the mouse when running standalone). [19/06/2006]

(code available below).

I rejiggered the gltrackball code to produce an effect more consistent with the other xscreensaver hacks, patch available below[05/07/2006].


Windows version
minor patch for gltrackball (see notes above)
two patch files for xscreensaver 5, fixes dropSpeed = 7 bug (see notes above)
raw source for topBlock that can be applied to the xscreensaver-5.00b4 (beta) source code
raw source for topBlock that can be applied to the xscreensaver-4.24 source code
jwz's other stuff
NeHe site
Interactive Computer Graphics : A Top-Down Approach Using OpenGL
coding rotation.


more xscreensaver hacks


This page is by me for me, if you are not me then please be aware of the following
I am not responsible for anything that works or does not work including files and pages made available at I am also not responsible for any information(or what you or others do with it) available at
In fact I'm not responsible for anything ever, so there!

[Pay4Foss banner long]