Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
    
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. –  Byte56 May 12 '13 at 1:41

2 Answers 2

up vote 1 down vote accepted

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".

share|improve this answer
    
Thanks, that sounds like it might be the best approach –  Afiefh May 12 '13 at 12:55

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.

share|improve this answer
1  
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.