Procedural Landscape Generation with Virtual Machines

One of the first 3d graphics programs I tried way back when was the Persistence of Vision raytracer. POV-Ray really appealed to me since I was a hard-core programmer and not much of an artist. I could make high-quality graphics, even though I could barely draw tolerable stick-figures. I’ve since moved on to using more advanced programs such as 3d Studio, but the idea of creating art with pure code has never left me.

Playing God

Recently, I’ve spent some time playing Minecraft, which generates unique randomized worlds that are created on the fly and are virtually infinite in extent. By using properly designed procedural algorithms, Minecraft creates a surface world of plains, mountains, and oceans populated by trees, plants, and villages with elaborate cave systems below. Granted, the worlds are still very blocky and simple, but the variety is still pretty amazing. One of my more ambitious projects is to extend this idea beyond blocky voxel worlds, and be able to create a full 3d world completely procedural.

“And God said ‘Let there be light!'” … *Click*

I recently started on the first step of this journey. You can’t have a world without ground, so for the past few months, I’ve been working on a procedural landscape generator. I already had modules for handling geometry meshes, so I just needed a method for creating procedural heightmaps. It’s easy enough to iterate over the vertices in a high resolution plane, and set the height as a function of the vertex’s x and y coordinates. However, I didn’t want to limit the landscape to a set of pre-canned functions. I wanted to be able to make whatever landscape I could think of without needed to add new equations to the program and re-compile it every time I had a new idea.

The Genesis Machine

The solution I decided on was to parse a Lisp-style domain specific language into opcodes and use a custom-built virtual machine to execute the landscape heightmap function. This was my first experience with virtual machine programming, and it was very enlightening. In future posts, I’ll describe the pieces of this prototype landscape generator. But in the meantime, here are a few samples of what it’s capable of right now.

A procedurally generated 3d crater


A procedurally generated volcano


A procedurally generated valley


Procedurally generated mountains


Minecraft to Max Converter (Part 2)

As I wrote about previously, I’m working on a utility to convert Minecraft levels into a Wavefront OBJ format so I can render my worlds in 3ds Max. Since my last post, I’ve added the following features:

  • You can now choose the region to render. This reduces the polygon count on the final object, especially if you’re just doing a surface render, and don’t need all of the cave polygons.
  • I exported the default texture set from Minecraft so I could apply the in-game textures to my final model.
  • Fixed some chunk boundary issues. Previously, if a block was at the edge of a chunk, it couldn’t tell if it should be rendered or not, since there was no next block to compare to. This resulted in holes in the final object. Now, the converter imports all of the chunks before doing the export, so it can check conditions across chunk boundaries.
  • The biggest feature I added was the ability to use custom geometry for certain blocks. So, now the exporter will automatically insert actual torches, doors, fences, flowers and steps where appropriate. It will also check the extra info for those objects in the level data, so the torches and doors are rotated correctly. There are many other custom geometries that I could add, but I don’t have any in the scenes I wanted to render, so I haven’t done them yet.

Rendering Minecraft scenes in 3ds Max

Once I have the level object imported into 3ds Max, I still need to hook up the materials and setup the lighting. I used the Mental Ray rendering engine with a dim skylight for ambient light. I also added a glowing material to the torch flames to get more dynamic lighting in the scene.


Still to-do

I still need to wrap the utility in a nice interface, and perhaps add some more custom geometry. Once I find the time to do that, I’ll package up the utility and release it here.

Minecraft to Max Converter (Part 1)

Minecraft house on a hill

Home Sweet Home

There’s a new computer game that has become rather popular lately.

Minecraft is the perfect game for people who enjoyed building with Lego bricks as a kid. The player wanders around an infinite natural landscape during the day gather natural resources such as wood from trees, and coal from caves. The player then uses these resources to build whatever they desire. However, when night falls, the player had better have made at least one shelter, because the darkness spawns zombies, skeletons, and other baddies whose sole purpose is to kill the player. You can only survive by crafting a secure shelter, or if that fails, a sword and armor.

Minecraft is a sandbox game, which means that there is no goal other than what the player decides to do. There are no levels to finish, bosses to defeat, or damsels to rescue. It is limited only by your imagination. Some Minecraft players have created very complex creations, including waterslides, the Mines of Moria, a recreation of the battle at Minas Tirith from the Lord of the Rings, and even a working computer! Yes, you read that right. Someone made a simulation of a computer inside a computer game.

Minecraft underground tree farm

Torches are just as good as sunlight, right?

I’m not quite that ambitious. My world consists of a modest house on a hillside, the start of a castle, and a few underground mines. The underground tree farm is pretty cool, though!

Beyond 8-bit graphics

The graphics for Minecraft are very simple compared to most modern computer games. Every piece of the terrain is a simple cube. Dynamic lighting is crude, and shadows are non-existent. Granted, these limitations are natural given the player’s ability to alter virtually anything in the environment, but as a hard-core 3D graphics snob, I thought it would be cool to see if I could render my world at a higher quality. Fortunately, notch, the author of Minecraft, decided to publish the file formats for the game’s level data. As a result, many people have created third-party utilities to visualize and interact with the game’s data. So, I decided to jump on the band-wagon, and write a Minecraft to 3D Studio converter.

I wrote up a nice C++ library to read notch’s NBT file format, and then created a test harness to iterate over the chunk files for my world, and convert the block data into a Wavefront Object, which I could then import into 3D Studio. After a few long nights of gnarly pointer shenanigans (death to the Big Endians!), scaling mismatches, and out-of-memory errors, I finally have a prototype. The gaps in the model are due to Minecraft’s level data being saved in strips. I stopped the conversion early once I could see that it was working correctly.

Minecraft world converted to 3D Studio

Woohoo! It works!

Future Plans

My next steps include building a base texture for the world, creating low-poly models for the in-game objects such as chests, workbenches, and furnaces, and lighting the world model with dynamically places torches and sunlight. I will also need an option to limit the region to convert. The region in the image above has over 1.6 million polygons, and is probably not even a quarter of my world. Then, once I wrap a nice interface around the converter, I’ll probably put it up for download here for everyone else to enjoy.