teaching machines

Feature 2 Post Mortem

February 25, 2013 by . Filed under cs455, postmortems, spring 2013.

Generating terrain from a height map was not necessarily a very difficult task, but of course, I managed to make it difficult anyway. Honestly, the craziest part for me was simply figuring out how to actually access the pixel color values of an image. I scoured the web for simple ways to read in a .png file or a .raw file, because I thought that those were uncompressed formats, and that I would be able to read the values for each pixel easily. What I found was that, without learning to use a complicated external library or reading the file bit by bit and decoding everything, everything that I wanted to do was impossible. Of course, I was thinking much to hard about this. As it turns out, Gimp 2 can convert images into the .pgm format, which contains a nice list of pixel values in ASCII format. I did in ten minutes what I had been unable to do in three hours. After that, most everything was smooth sailing. I used the algorithm from the perl script that Dr. Johnson introduced in the terrain generating video to create a .obj file from the pixel values that I read in. However, I quickly found out that writing lines one by one to a file is incredibly slow in C++(at least when using the << operator). My program would run for 30 minutes before the file was written. Instead, I just concatenated each line together into one long string and wrote the string to the file afterwards. This cut the writing process of a 1048 x 1048 image to a .obj file down to about 30 seconds. I also added an option to proceed without writing a new .obj file, in case you didn’t need to. The lighting isn’t truly working yet, but that wasn’t the point of this feature. I only had to add it so that the hills and crests of the landscape would be visible.

I won’t be including any of the Web sites that I used when I was searching for a way to open .png files, since I was still heading completely in the wrong direction at this point. I found the desert height maps used in my video and screenshots at http://www.c4d-jack.de/php/textures/index.php?spgmGal=Heightmaps. Apart from this, all credit goes to Dr. Johnson for his terrain generator and his tip about using .pgm files.


Just so that you don’t need to trudge through the code, the generated .obj file path will is specified on line 172 of ObjUtilities.cpp, and the .pgm image file path is specified on line 45 on ProjectRenderer.cpp.


Here is a photo of the initial command line interface used to initiate my program.  The image on the right is the grey scale image that I am reading my height map from.

This is an image of the output displayed in the my initial interface, just before the landscape has been rendered.

This is an image of the initial window, just after the landscape has been rendered.

Here is a side-by-side view of the height map image and the generated terrain.

This is one more side-by-side view, but rotated just a bit (maybe unnecessary, but whatever).

As a final note, a video demo of my code running can be found at the following url:  http://youtu.be/aCs4wueR6eA


Leave a Reply

Your email address will not be published. Required fields are marked *