views:

412

answers:

1

I have a GLScene object of varying, but known size. It is completely surrounded by a TGLDummyCube.

I want to position the GLCamera (CameraStyle: glPerspective) so that the object is completely visible on screen. I got this running basically - the object is visible, but the distance is sometimes too far, or the object is larger than the screen and clipped.

How can I do that? I suppose that this can be done by a clever combination of camera distance and focal length, but I have not been successful so far. This seems to be different in GLScene compared to OpenGL.

PS: I'm using GLScene and Delphi 2007.

Thanks for your help!

+1  A: 

Although varying the camera distance and focal length will change the object's visual size, it has the drawback of also changing the perspecitve, thus leading to a somewhat distorted view. I suggest to use the camera's SceneScale property instead.

Alas, I have no valid steps to calculate the correct value for that. In my case I have to scale to a cube with varying size, while the window size of the viewer is constant. So I placed two dummycubes at the position of the target cube, each sized to fit either the width or the height of the viewer with appropriate values for SceneScale, camera distance and FoclaLength. During runtime I calculate the new SceneScale by the ratio of the target cube size in respect to the dummycube sizes. This works quite well in my case.

Edit: Here is some code I make for the calculations. ZoomRefX and ZoomRefY are those DummyCubes, TargetDimX and TargetDimY give the size of the current object, DesignWidth and DesignHeight are the size of MyGLView at design time, DesignSceneScale is the camera's SceneScale at design time.

  ScaleX := (ZoomRefX.CubeSize*MyGLView.Width)/(DesignWidth*TargetDimX);
  ScaleY := (ZoomRefY.CubeSize*MyGLView.Height)/(DesignHeight*TargetDimY);
  NewSceneScale := Min(ScaleX, ScaleY)*DesignSceneScale;

The DummyCubes ZoomRefX and ZoomRefY are sized so that they have a small margin to either the left-right or top-bottom edges of the viewing window. The are both positioned so that the front faces match. Also the target object is positioned to match its front face with those of these DummyCubes.

The formulas above allow the window size to be different from design time, but I actually didn't test this feature.

Uwe Raabe
Thanks for the tip - I've been playing with SceneScale as well, using it in my OnMouseWheel event handler. Do I understand correctly that you set those dummycubes up during development, and the fit the cube accordingly at run-time? Sounds like a clever trick. Change the object, not the camera.
Andreas
I set the dummycubes at during development to a size matching the window size of the viewer. Two cubes because the screen is not quadratic, but the dummycubes are. Now when my real object has say double the size of the dummycube, I know I have to halfen the SceneScale to make its visible size like the dummycube at design time. Doing these calculations separately for the widht and the height I can calculate the SceneScale that makes the target object fit into the window. This only works because the window size is fixed. And I don't change the object - I only change the camera's SceneScale.
Uwe Raabe