In order to get this feature to work I had to do two main things: Add a terrain class that I could use to sample the terrain height at a given location, and improve the camera class to function more like a fps camera.
My terrain class is a height map that is generated from an obj file. However, in order for it to really work, the obj file needs to have similar traits to the obj file created by my terrain generator. These traits are as follows: All vertex x and z locations must be zero or greater. Breaking this will cause an error. Vertices’ x and z values should make a grid from (0..width,0..depth) with a y value at each integer location. Breaking this will simply cause inaccurate sampling in areas adjacent to the invalid vertices. Finally, the obj file needs to be in the edited format that we have been using in class. This will be fixed at the same time I fix the mesh importing.
In order to provide accurate sampling between vertexes, I used bi-linear interpolation as shown below. I decided to allow sampling outside of the height map’s bounds. Instead of throwing an exception, I treat the height map as if the boundary values extend infinitely outward. For example, (-1, 1.5) == (0, 1.5), (-1, -1) == (0, 0), (width + 20, -1) == (width -1, 0), and so on.
The changes that I made to the camera class were to allow looking around with the mouse, setting its elevation, and keeping movement speed independent of where you are looking as seen below. The change to Advance is simply using ground_forward instead of forward, where ground_forward is forward with the y component zeroed out and then normalized. This is what keeps speed constant, even if you are looking almost straight down. SetElevation simply changes the y of from and to, while preserving the amount you are looking up/down. Yaw now needs to rotate the up vector as well so that turning right rotates around the y axis, not the up axis. Roll is similar to Yaw with the exception that it rotates around the right axis, not the y axis.
Implementing the controls looks like so:
Here is a video of me running around some red terrain: