Generating terrain wasn’t too hard; the biggest problems I had were simple things that shouldn’t have been problems. I decided to write the diamond square algorithm in c++ because I’m way more familiar with it than I am with ruby, and there were a few things I wanted to try and play around with when generating the terrain. I made a terrain_generator class that generates a 2d array of height values and then writes them to an obj file. There were a few mistakes I made early on, the biggest being that my terrain wouldn’t come out square. I realized I was using the size of the grid as the number of faces instead of size – 1, and changing this fixed the problem. I also had issues with data storage; I decided to use a vector of vectors for storing the heights in my grid instead of a standard 2D array, as I found it easier to work (and getting the size of the grid is easier that way). I also had some trouble getting the decaying of the terrain heights down. The following is my random height generation:
I ran into divide by zero problems, which I found came from the max height getting very small and being clipped to zero when turned to an integer.
When running through the actual algorithm, I wanted to customize the terrain a little bit. I did this by fixing the corners to a height of zero (which is done by not setting them, since I set all the heights to zero at the start). I also set the middle point to the max height and left the middles of the edges at zero as well. I really had to reason for doing this, it was just the way I wanted my terrain to look.
The last change I made was switching the y and z coordinates in the obj file; this way there were less transformations to do when inserting the terrain into my world. I wrote to the file as such:
The only references I used other than Dr. Johnson’s video were the following forum posts about certain aspects of c++ I wasn’t sure of.
And lastly, here’s a video of my terrain. The lighting is just a single source from above and i only put it in so you could see the terrain.