I was looking into different literature for generating terrains and finally decided to start off with the basic one - using height fields. The following is an algorithm to generate a simple terrain to start off with.
- Get a heightfield (you can get it using an image or generate it using noise). I decided to use Perlin noise to create a heightfield.
- Create a flat triangular mesh (flat = y-coordinate is same for all the vertices).
- Perturb the vertices in the vertex shader using the heightfield values. The noise is generated in the vertex shader itself. More details on the noise generated below.
- Shade the mesh in the fragment shader. Can shade with different colors based on elevation too.
The noise function that I used was adapted from http://www.sci.utah.edu/~leenak/IndStudy_reportfall/PNoiseCode.txt. Perlin noise generates random values based on a Hermite curve. This gives a continuous value for the randomly generated values. It randomly generates values based on a frequency that you pass in and generate a curve based oin these values. THe intermediate values are then determined using this curve. I clamped the output a bit so that I have more flat areas in my terrain.
Shown below are outputs generated from my first stab at the algorithm. The images display the mesh instead of the shaded polygons as I haven't yet calculated normals for the primitives.
Basic Mesh Output
Color coded based on height
- Calculate normals for the primitives.
- Shade terrain using the normal values (diffuse shading).
- Tessellate the geometry using the Tessellation Shader.