Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 2 of 2 FirstFirst 12
Results 11 to 12 of 12

Thread: Zoom to fit screen

  1. #11
    Junior Member Newbie
    Join Date
    Feb 2010
    Location
    Arizona
    Posts
    1

    Re: Zoom to fit screen

    Is it possible to see the code that finally worked? I have the same problem to solve.

    Thanks much!
    Thanks,
    Sparks

    Hmmm, maybe I should think of something clever to say here! Nah...

  2. #12
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    1

    Re: Zoom to fit screen

    'sin' does not just give better results, it is the right trigonometric function to use.

    For the code you need to know:

    * width:height aspect ratio (width / height as floating point number as used by gluPerspective)
    * center and radius of the bounding sphere for the scene
    * direction of the camera (unit length)
    * fov in y (as used by gluPerspective)

    The code would be as follows:

    Code :
    half_min_fov_in_radians = 0.5 * (fov * PI / 180);
     
    if (aspect < 1.0)
    {
        // fov in x is smaller
        half_min_fov_in_radians = atan(aspect * tan(half_min_fov_in_radians));
    }
     
    distance_to_center = radius / sin(half_min_fov_in_radians);
    eye = center - dir * distance_to_center;

    You may also take advantage of that information to fit your zfar clipping plane (if you do not want to keep it too far).

    Code :
    zfar = distance_to_center + radius;
     
    if (zfar < 1.5 * znear)
    {
        // Keep zfar always bigger than znear
        zfar = 1.5 * znear;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •