Hi there,
I have an application that renders some terrain from a height map and I have a ‘camera’ class which allows rotation (using spherical coordinates) around a point and movement in the XZ plane. There are, of course, times when the camera can be positioned below my defined terrain. The way I’ve come up with to handle this situation is:
Vec3glf increment = position / 100;
for(int i=100; i>0; i--){
Vec3glf putativePosition = increment * i;
if(putativePosition.y()-0.1 > landscape->height(round(putativePosition.x(), round(putativePosition.z()))){
gluLookAt(putativePosition.x(), putativePosition.y(), putativePosition.z(),
focus.x(), focus.y(), focus.z(),
up.x(), up.y(), up.z());
break;
}
}
where:
Vec3glf is a class that handles my vector math.
position is a Vec3glf representing the camera’s position.
100 is an arbitrary division of my position vector.
-0.1 is my near clipping plane (is there a way of getting this information out of the GL - I don’t really want to have to pass this to my camera class if I’m not going to need it anywhere else).
landscape->height gives the terrain’s height at an x,z coordinate.
focus is the point at which the camera is looking
up is the up-vector.
So, the code just starts at the actual camera position and checks if it’s under the terrain and keeps moving in by 100th of itself towards the focus if it isn’t until it finds a position above the terrain that’s suitable to place the camera upon.
My question is whether there is a better way of doing this? Where better is more efficient and more precise. I’ll be wanting to do the same thing when objects occlude my focus and I can imagine that this is going to become quite costly when it’s not just terrain I’m concerned with. Also, the division is very arbitrary and guarantee the smoothest animation when moving over a surface of terrain.
Thanks, Dan.