CS 455 Lecture 17 – Heightmaps
Agenda
- what ?s
- heightmap exercise
TODO
Heightmaps are grayscale images used to generate terrain. For example, to get a landscape with rolling hills, we can use a noisy but smooth image:
To produce a plateau (white) with a sudden chasm (black), we can use this image of starker contrast:
The are two significant advantages of a heightmap over a traditional 3D mesh:
- They can be generated and edited in many different tools.
- They are backed by a very simple and fast data structure: an array.
The one drawback to heightmaps is that they are not expressed as vertices and triangles. To render our terrain, we’ll first need to convert the heightmap into suitable geometry that we can ship to the GPU. Your task in lab, then, is to write class Heightmap
class that reads in an image and produces vertex attributes (position and normal) and connectivity information (the indices of the vertices forming each face).
- In the
Heightmap
constructor, read the Image at the specified path. Let the image’s width be the terrain’s width. Let the image’s height be the terrain’s depth. Let the grayscale intensity at index (x, z) be used as the height of the terrain at location (x, z). However, consider the grayscale value (which is in [0, 255]) as a proportion of themax
parameter. - Write a destructor.
- Write
GetWidth
andGetDepth
. - Write
operator()(int x, int z)
. Return as a reference the height at location (x, z) in the terrain. - Complete
ToAttributes
. I suggest tackling it in the following steps:- Determine the number of vertices.
- Generate the list of vertex positions in the terrain. Each pixel in the original image corresponds to a vertex. What is each pixel’s location? Its location in the image determines its xz-coordinates, and its grayscale intensity determines its y-coordinate.
- Prime the list of vertex normals to initially all have 0 length.
- Determine the number of faces.
- Generate the list of vertex indices that connect up to form the terrain’s triangles. When you have identified a face’s vertices, calculate its normal as the crossproduct of two of its sides. Don’t forget to normalize. Add this face normal to each of its three vertices’ normals.
ObjUtilities
contains code that might serve as a template. - Renormalize all vertex normals.
- Test your solution in the
heightmap
project, which uses yourHeightmap
to loadmodels/noise.pgm
.
Complete this lab exercise and send a screenshot.
Next week, we’ll look at automatically adjusting the camera’s height based on its position in the terrain.
Haiku
Mankind has evolved
We once thought the world was flat
Now we admit bumps