CS40 Lab 5: Modeling terrain meshes

Due 11:59pm Friday 10 April 2009
You may work with one partner on this assignment. For this lab, you will be writing most of the code from scratch, but you can use the terrain.cpp example in labs/05 as a possible starting point. You will be modeling terrain as a triangular mesh given a gridded representation of a terrain. The grid.* code provided can read gridded data in a specific ASCII format.
Data
The file /usr/local/doc/pa_dem.asc contains the gridded data of the terrain you should model. The file is a few megabytes in size and you should not copy this file to your lab directory on the CS machines. You may however create a symlink to this file, or copy it to a personal machine if you are working remotely. The function read_grid in grid.h/.cpp imports .asc files in row major order. The fields north, south, east, and west in the grid struct give the bounding box of the terrain in meters (for this particular data set), from a particular offset. For the file pa_dem.asc, minval and maxval are the minimum and maximum elevations of terrain in meters. The value of pixels[row][col], gives the elevation of the terrain at a horizontal location west + ewres*col and a vertical location of north - nsres*row. You should model this data as a 3D triangular mesh in OpenGL.
Basic Requirements
You code should be written in a general enough manner, such that it could render any terrain given a file in a format similar to pa_dem.asc. To make viewing easier, you should scale all terrains to fit into a box of a common size and common center location. This can be done by applying an appropriate scale and translate transform to the original data. You may want to start by drawing a 1000x1000x100 box (or some other size) and then adjusting the lookat and perspective transforms to view the box correctly. Then transform the terrain to fit inside the box. You code should handle cases were the length and width of the terrain (in either pixels or meters) are not identical/square.

Allow the user to adjust the vertical exaggeration of the terrain, so that hills can be magnified or shrunk. Vertical exaggerations are typically in the range 0.5-10.0.

Your code should be able to toggle between drawing the terrain in filled or wireframe mode.

To speed the display of the terrain, your code should support rendering a coarse model of the terrain. This can be done by drawing only every kth point in the x or y directions.

You should assign normals to each vertex in the terrain for proper lighting. This should be done in two different ways. In the first way, each triangle has a single normal that is computed by taking a cross product of two vectors along the edges of the triangle. By assigning a single normal to each triangle, the lighting across a single triangle will be flat. In the second way of computing normals, you should assign each vertex in the terrain a normal that is the average of all normals of triangles adjacent to the vertex. This will give a more realistic view of the terrain at the price of increased computation cost.

Create a color map that assigns a color to each vertex that is a function of the height of the vertex. You can design how the colors change with elevation.

Use at least one display list to speed the display of the terrain.

Add keyboard controls to pan and zoom. You can decide how this is implemented. Remember you can change either the projection or model view.

Submit
Once you are satisfied with your programs, hand them in by typing handin40 at the unix prompt. You may run handin40 as many times as you like, and only the most recent submission will be recorded. This is useful if you realize after handing in some programs that you'd like to make a few more changes to them.