CameraPersp CameraPersp::getFrameSphere( const Sphere &worldSpaceSphere, int maxIterations ) const { CameraPersp result = *this; result.setEyePoint( worldSpaceSphere.getCenter() - result.mViewDirection * getCenterOfInterest() ); float minDistance = 0.01f, maxDistance = 100000.0f; float curDistance = getCenterOfInterest(); for( int i = 0; i < maxIterations; ++i ) { float curRadius = result.getScreenRadius( worldSpaceSphere, 2.0f, 2.0f ); if( curRadius < 1.0f ) { // we should get closer maxDistance = curDistance; curDistance = ( curDistance + minDistance ) * 0.5f; } else { // we should get farther minDistance = curDistance; curDistance = ( curDistance + maxDistance ) * 0.5f; } result.setEyePoint( worldSpaceSphere.getCenter() - result.mViewDirection * curDistance ); } result.setCenterOfInterest( result.getEyePoint().distance( worldSpaceSphere.getCenter() ) ); return result; }