Initializes given matrix as perspective projection matrix. - Android java.lang

Android examples for java.lang:Math Matrix

Description

Initializes given matrix as perspective projection matrix.

Demo Code

/*//  ww  w .  java 2 s  .c  o m
   Copyright 2012 Harri Smatt

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
//package com.java2s;
import android.opengl.Matrix;

public class Main {
    /**
     * Initializes given matrix as perspective projection matrix.
     * 
     * @param m
     *            Matrix for writing, should be float[16], or bigger.
     * @param fovy
     *            Field of view in degrees.
     * @param aspect
     *            Aspect ratio.
     * @param zNear
     *            Near clipping plane.
     * @param zFar
     *            Far clipping plane.
     */
    public static void setPerspectiveM(float[] m, float fovy, float aspect,
            float zNear, float zFar) {

        Matrix.setIdentityM(m, 0);
        float h = zNear * (float) Math.tan(fovy * Math.PI / 360);
        float w = h * aspect;
        float d = zFar - zNear;

        m[0] = zNear / w;
        m[5] = zNear / h;
        m[10] = -(zFar + zNear) / d;
        m[11] = -1;
        m[14] = (-2 * zNear * zFar) / d;
        m[15] = 0;
    }
}

Related Tutorials