Joey
Joey

Reputation: 10965

Convert FOV angles to perspective projection parameters

I have a program that gets the FOV angle of the device's rear-facing camera, which returns the horizontal and vertical angle in degrees like so:

54.8
42.5

Is there an easy conversion to apply to get right "left, right, bottom, top" parameters to pass into OpenGL's Matrix.frustrumM()?

Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);

I'm overlaying OpenGL shapes onto a CameraView and want to make sure they are drawn correctly relative to real world objects in the camera feed, for all devices regardless of the camera's FOV.

Much thanks!


Edit: Here's a solution, in case anyone comes across this:

private float[] perspectiveM(float fovY, float fovX, float zNear, float zFar)
{
    float[] projMatrix = new float[16];
    float aspect = fovX / fovY;
    fovY = (float) ((fovY /180.0) * Math.PI); // degrees to radians
    float g = (float) (1 / Math.tan(fovY / 2));

    for(int i=0; i<16; i++) {
        switch (i) {

        case 0:
            projMatrix[i] = g / aspect;
            break;
        case 5:
            projMatrix[i] = g;
            break;
        case 10:
            projMatrix[i] = (zFar + zNear)/(zNear - zFar);
            break;
        case 11:
            projMatrix[i] = -1.0f;
            break;
        case 14:
            projMatrix[i] = (2 * zFar * zNear)/(zNear - zFar);
            break;
        default:
            projMatrix[i] = 0.0f;
        }
    }

    return projMatrix;
}

Then assign the return result to your projection matrix.

Upvotes: 3

Views: 2725

Answers (1)

Tim
Tim

Reputation: 35933

I didn't realize that perspectiveM was limited to a certain API level, but you can easily recreate the function yourself. The formulas are on the man page for gluPerspective, which works the same way.

You can just build the float matrix yourself using these equations:

http://www.opengl.org/sdk/docs/man/xhtml/gluPerspective.xml

Upvotes: 2

Related Questions