home   drawings   cartoons   stories   waffles   projects   virtual worlds   files   blog

Should be displaying a VRML world here

- Drag your mouse up to move forward, drag it down to move back, and right or left to turn.
- Hold down the shift key while dragging the mouse to move faster.
- Drag the middle mouse button to slide up or down, left or right.
- Roll mouse scroll wheel to look up or down. - PageUp and PageDown keys to jump through the viewpoints.
- Right-click with the mouse to popup the menu.
- The menu lets you choose between fly or walk, and also the preset viewpoints.

- If you are using a phone or tablet, touch and drag in the direction you want to move.
- Use 2 fingers to look up or down.
- Long touch in one spot to popup the menu for various settings, like viewpoints to jump to, and type of movement (fly, or walk).

This is an experiment in generating random terrain. The world you see above (if everything is working properly) is a square of land 25 km by 25 km. I added sea to make it a little less boring.

I used a simple awk program to write the landscape's heightfield as a text .pgm image. This is a neat trick because it lets me view the heightfield as a picture before pasting it into the VRML world.

Why use the awk computer language? Because it is fast, tiny, doesn't need any supporting files, and it is included with virtually all installations of Linux or Unix (it is also in Mac since OSX, because your expensive Mac is actually a free version of BSD Unix underneath). Whatever kind of computer you use, it is almost certain you can get awk for free. There are a few versions of awk, but the one I use is Gnu awk, known as gawk. This program should work on any of them (though the awk in busybox might not have math functions compiled in).

The pgm format is largely forgotten these days. If you want to learn more about it, look up NetPBM Tools It is an early, but still incredibly useful, suite of about 400 image manipulation tools. Internally, it uses its own special image formats:

pbm - for black and white images
pgm - for grayscale images
ppm - for color images
pam - any of the above and can include transparency
Each of those formats can store pixel values as binary data, which uses very little space, or as ASCII text (except for pam, which only has binary format -- a huge shortcoming, in my opinion). The beauty of using text is that it is extremely easy to operate on using programs that handle text. And awk excels at working on text. And being an image format, I can see immediately how well my program worked.

I use the grayscale pgm format because many 3D programs allow heightfields, which are simply lists of height values, convenient for describing a landscape. Simply copy the list of text values in a pgm image into the heightfield of a 3D virtual world and it can be displayed as a 3D landscape. In VRML, the heightfield is inside the ElevationGrid, specifically the height numbers.

To see how nice this is, here is the text of a very simple pgm image of 9 pixels, arranged 3 by 3:

P2
3 3
1
0  0  0
0 1 0
0  0  0
The first line "P2" tells us that it is an ASCII text pgm file. The next line tells us that the picture is 3 pixels across, by 3 pixels down. The last line of the header tells us that the pixels range in value from 0 (black) to the maximum, 1 (white). Grayscale images will more often have a maximum of 255 (though it can be any number), but to simplify things here I'm just using two levels -- black, or white. Then there are 9 text values for the pixels. I could have written them in a single line (0 0 0 0 1 0 0 0 0), but it is easier to understand arranged as a set of three lines.

A mere 3x3 pixel image is tiny:
So here it is enlarged five times:
See how it is 3 pixels across, by 3 pixels down, all black except the center pixel, which is white?

I can copy those values from the pgm file directly to the list of heights in a simple VRML wrl file. The 3D file requires setting up with more information, as you can imagine. We need to tell it how we light the world, how we move, where we are viewing it from, aand what color the object is (remember the heightfield gives only the height, not the color.

#VRML V2.0 utf8

NavigationInfo {
	type "EXAMINE"
	headlight TRUE
}

Viewpoint {
	position	1.5  1.8  4.4
	orientation	-0.2 0.05 0  0.5
}

Shape {
	appearance Appearance {
		material Material { diffuseColor 0.6 0.6 0.6 }
	}
	geometry ElevationGrid {
		height [
			0 0 0
			0 1 0
			0 0 0
		]
		xDimension 3
		zDimension 3
	}
}
You can read a commented version here if you want to understand what the parts do.

And here is how that simple 3x3 heightfield looks in 3D:

Should be displaying a simple VRML world here

Drag your mouse to rotate it.

Getting back to the world at the top of the page...

To make the heightfield that represents the land, I used this simple awk program: random_terrain.awk.

I like to program in awk because it is fast and flexible, however I got ChatGPT-5 to assist me with this program because I wasn't confident of my maths in working out how to make the spot fade out from its center to its edge. We'll get to that in a moment.

If you download that awk program and run it like this:
awk -f random_terrain.awk >random_terrain.pgm
In just several seconds you'll get a pgm image file that looks like this:

I've converted it to a png file to show it here because your web browser probably can't display pgm images, but you can download the actual pgm file here: random_terrain.pgm. If you compare it with the one you get by running the awk program it should look the same. (If it doesn't, please send me a comment telling me what kind of awk you're using and what operating system.)

Now the long list of numbers in the main body of the pgm file can be pasted into the skeleton of a VRML file. I've made one that has different settings than the one I showed earlier.

The VRML world file can be downloaded and loaded into a text editor to examine and alter at your leisure. If you install a program capable of viewing VRML files you'll be able to explore your own changes too.