Zoom to fit model to screen?

Started by
2 comments, last by renman29 17 years, 7 months ago
I was thinking that in order to zoom to fit my model to screen, I'd need to constantly shrink or expand the model to fit into the viewing frustum. One other idea I had was: - center object around some central point inside viewing frustum (where?) using the object's bounding box center - loop through vertices - - - create a vector from "center" to vertex - - - find maximum vector length - find percent difference between vector and a version of the vector that's exactly long enough to touch the frustum. - multiply all vertices by that percentage to scale them Is this right? Any ideas? Any and all ideas are welcome. :)
Advertisement
I'm afraid that doesn't work - if you have a shape like a slightly 'bulging' cube, the vector you find will be to one of the corners of the cube, but scaling it up such that that vector is on the edge of the frustum will cause the 'bulges' to go outside it.

I'd do this in screenspace, I think. Transform all the vertices - or if you don't need a very close fit, the vertices of the bounding box - by the view and projection matrices, and then find the bounding rectangle of those vertices' transformed (X,Y) coordinates. Then work out how much you need to scale that rectangle by to have it touch the edges of the screen.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Ah yes. That's perfect - thanks! :)
Ah - only one problem I found. Some models load microscopic and some load absolutely huge, such that clipping is happening against near and far clipping planes. I guess I could combine the two methods. First do a geometry scale(keeping the scale amount for scaling back later), then use the screenspace trick. :)

This topic is closed to new replies.

Advertisement