Home | History | Annotate | Download | only in graphics
      1 /*******************************************************************************
      2  * Copyright 2011 See AUTHORS file.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *   http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  ******************************************************************************/
     16 
     17 package com.badlogic.gdx.graphics;
     18 
     19 import com.badlogic.gdx.math.Matrix4;
     20 import com.badlogic.gdx.math.Vector3;
     21 
     22 /** A Camera with perspective projection.
     23  *
     24  * @author mzechner */
     25 public class PerspectiveCamera extends Camera {
     26 	/** the field of view of the height, in degrees **/
     27 	public float fieldOfView = 67;
     28 
     29 	public PerspectiveCamera () {
     30 	}
     31 
     32 	/** Constructs a new {@link PerspectiveCamera} with the given field of view and viewport size. The aspect ratio is derived from
     33 	 * the viewport size.
     34 	 *
     35 	 * @param fieldOfViewY the field of view of the height, in degrees, the field of view for the width will be calculated
     36 	 *           according to the aspect ratio.
     37 	 * @param viewportWidth the viewport width
     38 	 * @param viewportHeight the viewport height */
     39 	public PerspectiveCamera (float fieldOfViewY, float viewportWidth, float viewportHeight) {
     40 		this.fieldOfView = fieldOfViewY;
     41 		this.viewportWidth = viewportWidth;
     42 		this.viewportHeight = viewportHeight;
     43 		update();
     44 	}
     45 
     46 	final Vector3 tmp = new Vector3();
     47 
     48 	@Override
     49 	public void update () {
     50 		update(true);
     51 	}
     52 
     53 	@Override
     54 	public void update (boolean updateFrustum) {
     55 		float aspect = viewportWidth / viewportHeight;
     56 		projection.setToProjection(Math.abs(near), Math.abs(far), fieldOfView, aspect);
     57 		view.setToLookAt(position, tmp.set(position).add(direction), up);
     58 		combined.set(projection);
     59 		Matrix4.mul(combined.val, view.val);
     60 
     61 		if (updateFrustum) {
     62 			invProjectionView.set(combined);
     63 			Matrix4.inv(invProjectionView.val);
     64 			frustum.update(invProjectionView);
     65 		}
     66 	}
     67 }
     68