开发者

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.开发者_C百科

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


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.


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜