I am creating a zoom fit function for a 3d model. Using DirectX 9, VB.NET (C# and C++ OK), right hand coord system, and perspective view. I have reviewed

http://www4.experts-exchange.com/Programming/Game_Development/Game_Graphics/OpenGL/Q_21903389.html
but still have some problems.

Basically, I need some direction (psuedocode) on an overall approach. The above post was for ortho projections, and did not seem to work completely in my case. Currently, I can transform all my world space vertices to screen space using Vector3.TransformCoordinat

e(vertex(i),matView*matProj). This gives me a cloud of screen points, some of which are outside the current screen display (-1 to 1 in x and y directions). Next, I calculate the world space coordinate of the center of a 2D screen bounding box (bb) containing all the transformed points using Vector3.TransformCoordinate(bb_center,Inverse(matView*matProj)), and move my camera target to that world space location, while also moving the camera position the same relative amount to preserve the camera look direction. The effectivly "centers" the model, but I still need to determine how to adjust distance between camera position and camera target to effectivly zoom in/out such that the bound box of transformed points fills the viewable screen area.

Some issues are:

1. first, the camera centering I described works great in ortho mode, after which I can manually zoom in/out as necessary until the model is completely within the screen. In perspective mode, manually zooming does not end up with a model that fits the screen edge to edge. Probably because as the camera position changes, so does the matView matrix, which would change all my transformed screen verticies.

2. how to move the camera to do the zoom in/out automatically such that the model fits the screen

I would prefer not to have to do an itterative method for camera fitting which some have suggested.

There is little to no examples of this on the web, but if you find one, let me know!

Public Const FOV As Single = Math.PI / 4

Public Const NEARCLIP As Single = 1

Public Const FARCLIP As Single = 100000

matProj = Matrix.PerspectiveFieldOfViewRightHanded(FOV, AspectRatio, NEARCLIP, FARCLIP)

matView = Matrix.LookAtRightHanded(CameraPosition, CameraTarget, CameraUp)