# Fitview for Perspective view

## Recommended Posts

spiffycrony    122
I am trying to fit the model for a perspective view. For the orthogonal view i was able to do this by getting the width and height of the model and passing this as the parameters to D3DXMatrixOrthoLH. In D3DXMatrixPerspectiveFovLH i dont see an equivalent?

##### Share on other sites
Sc4Freak    643
Isn't that because, with perspective, the size of the object decreases as a function of distance?

##### Share on other sites
Evil Steve    2017
Quote:
 Original post by Sc4FreakIsn't that because, with perspective, the size of the object decreases as a function of distance?
Exactly. It doesn't really make sense for a pespective view. You can still do it by hand if you want but it'll only work for a certain depth of object.

##### Share on other sites
spiffycrony    122
Does that mean you cannot do an automatic fitview for perspective view?

##### Share on other sites
Evil Steve    2017
Quote:
 Original post by spiffycronyDoes that mean you cannot do an automatic fitview for perspective view?
You can, but things will only fit at one Z-value, which means you're just as well using orthogonal anyway...

Why are you trying to do this? The point in a perspective transform is that things scale correctly, meaning that the view extents are smaller at Z=5 than they are at Z=100. A perspective transform is a truncated-pyramid shape, an orthogonal one is a box shape.

##### Share on other sites
PlayfulPuppy    419
It's possible to write your own function to do this. However, I won't give you a full answer as it's 1:30am and I don't really feel like getting my math cap on. :(

Anyway, what you need to do is get the bounding sphere of the object (A center-point and a radius). With this, you can tell how far away the object needs to be from the camera in order for that sphere to lie within the cameras frustum. Think of it as a right-angled triangle, like this:

\           / \         /  \   |---/  <- Sphere radius   \ X|  /    \ | /     \|/       V  <- Angle is FOV/2

What you need to find is X, which a site on trig should be able to help you with. Note that for the FOV you'll need to test either the horizontal FOV or the vertical FOV, depending on which will be smallest (Although if you're just going to stick with a standard 4:3 or 16:9 aspect ratio, you should be safe just testing the vertical FOV).

Once you have X, you just need to place the camera at objCenter + (X * -CameraDir), and it should fit in the camera within a tolerable margin of error.