Frank Bapple
Frank Bapple

Reputation: 31

OpenGL-ES change angle of vision in frustum

Let's see if I can explain myself.

When you set up the glFrustum view it will give the perspective effect. Near things near & big... far things far & small. Everything looks like it shrinks along its Z axis to create this effect.

Is there a way to make it NOT shrink that much? To approach perspective view to an orthographic view.... but not that much to lose perspective completely?

Thanks

Upvotes: 3

Views: 1109

Answers (2)

sam hocevar
sam hocevar

Reputation: 12129

The thing is to understand that orthographic view is a view with a FOV of zero and a camera position at infinity. So there is a way to approach orthographic view by reducing FOV and moving the camera far away.

I can suggest the following code that computes a near-orthographic projection camera from a given theta FOV value. I use it in a personal project, though it uses custom matrix classes rather than glOrtho and glFrustum, so it might be incorrect. I hope it gives a good general idea though.

void SetFov(int width, int height, float theta)
{
    float near = -(width + height);
    float far = width + height;

    /* Set the projection matrix */
    if (theta < 1e-4f)
    {
        /* The easy way: purely orthogonal projection. */
        glOrtho(0, width, 0, height, near, far);
        return;
    }

    /* Compute a view that approximates the glOrtho view when theta
     * approaches zero. This view ensures that the z=0 plane fills
     * the screen. */
    float t1 = tanf(theta / 2);
    float t2 = t1 * width / height;
    float dist = width / (2.0f * t1);

    near += dist;
    far += dist;

    if (near <= 0.0f)
    {
        far -= (near - 1.0f);
        near = 1.0f;
    }

    glTranslate3f(-0.5f * width, -0.5f * height, -dist);
    glFrustum(-near * t1, near * t1,  -near * t2, near * t2, near, far);
}

Upvotes: 1

The angle is conformed by two parameters: heigth of the nearest clipping plane (determined by top and bottom parameters), and the distance of the nearest clipping plane (determined by zNearest). To make a perspective matrix such that it doesn't shrink the image too much, you can set a smaller height or a further nearest clipping plane.

Upvotes: 2

Related Questions