# Moving camera along generated terrain?

I'm generating my terrain on the GPU, using diamond square subdivision (no height map involved). How can I anchor the camera's position to always be on top of the generated terrain?

Thanks

• You'll have to use the inputs for the terrain data to generate a heightmap or algorithm for finding the height at a specific location. – MichaelHouse May 12 '13 at 1:41

The most expeditious way to extract height information might be to draw it:

1. Set draw parameters to an orthographic projection directly down toward the terrain.
2. Draw to a render target with a depth-map shader.
3. Store render target contents to a 2D array, sample it for heights, and save it for as long as it is useful (no need to reproduce every frame).

I think this method will give you everything you need. It won't require a non-GPU implementation of your algorithm. It can be re-run if your camera moves to a new area of terrain. It can be low resolution; allowing you to interpolate between its "pixels".

cameraPos_Y = desiredDistanceFromGround + currentTerrainVertex_Y.

As the camera moves, keep updating cameraPosY checking the height of which vertex the camera is over or closest to. Use the the camera's X and Z coordinates to help single out which vertex the camera is over.

In case you you have big faces for your terrain, you may have to use a bounding box and get the average height of all vertices detected.

cameraPos_Y = desiredDistanceFromGround + ((vertex1 + vertex2 + vertex3) / 3).

Lets say your bounding box for your camera at whatever current position detects 3 vertices with heights of 3.5, 4.2 and 2.9. You also want to camera to stay at a distance of 2 off the ground. The average height will be 3.53, so roughly, cameraPosY = 2.0 + 3.53.

From here, you may need to incorporate change in height smoothly.

• Since the geometry is generated on the GPU I don't have the vertexes stored on the CPU side of things, so I cannot locate something like this. – Afiefh May 12 '13 at 12:55