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 17package com.badlogic.gdx.graphics; 18 19import com.badlogic.gdx.Gdx; 20import com.badlogic.gdx.math.Matrix4; 21import com.badlogic.gdx.math.Vector2; 22import com.badlogic.gdx.math.Vector3; 23 24/** A camera with orthographic projection. 25 * 26 * @author mzechner */ 27public class OrthographicCamera extends Camera { 28 /** the zoom of the camera **/ 29 public float zoom = 1; 30 31 public OrthographicCamera () { 32 this.near = 0; 33 } 34 35 /** Constructs a new OrthographicCamera, using the given viewport width and height. For pixel perfect 2D rendering just supply 36 * the screen size, for other unit scales (e.g. meters for box2d) proceed accordingly. The camera will show the region 37 * [-viewportWidth/2, -(viewportHeight/2-1)] - [(viewportWidth/2-1), viewportHeight/2] 38 * @param viewportWidth the viewport width 39 * @param viewportHeight the viewport height */ 40 public OrthographicCamera (float viewportWidth, float viewportHeight) { 41 this.viewportWidth = viewportWidth; 42 this.viewportHeight = viewportHeight; 43 this.near = 0; 44 update(); 45 } 46 47 private final Vector3 tmp = new Vector3(); 48 49 @Override 50 public void update () { 51 update(true); 52 } 53 54 @Override 55 public void update (boolean updateFrustum) { 56 projection.setToOrtho(zoom * -viewportWidth / 2, zoom * (viewportWidth / 2), zoom * -(viewportHeight / 2), zoom 57 * viewportHeight / 2, near, far); 58 view.setToLookAt(position, tmp.set(position).add(direction), up); 59 combined.set(projection); 60 Matrix4.mul(combined.val, view.val); 61 62 if (updateFrustum) { 63 invProjectionView.set(combined); 64 Matrix4.inv(invProjectionView.val); 65 frustum.update(invProjectionView); 66 } 67 } 68 69 /** Sets this camera to an orthographic projection using a viewport fitting the screen resolution, centered at 70 * (Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2), with the y-axis pointing up or down. 71 * @param yDown whether y should be pointing down */ 72 public void setToOrtho (boolean yDown) { 73 setToOrtho(yDown, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 74 } 75 76 /** Sets this camera to an orthographic projection, centered at (viewportWidth/2, viewportHeight/2), with the y-axis pointing up 77 * or down. 78 * @param yDown whether y should be pointing down. 79 * @param viewportWidth 80 * @param viewportHeight */ 81 public void setToOrtho (boolean yDown, float viewportWidth, float viewportHeight) { 82 if (yDown) { 83 up.set(0, -1, 0); 84 direction.set(0, 0, 1); 85 } else { 86 up.set(0, 1, 0); 87 direction.set(0, 0, -1); 88 } 89 position.set(zoom * viewportWidth / 2.0f, zoom * viewportHeight / 2.0f, 0); 90 this.viewportWidth = viewportWidth; 91 this.viewportHeight = viewportHeight; 92 update(); 93 } 94 95 /** Rotates the camera by the given angle around the direction vector. The direction and up vector will not be orthogonalized. 96 * @param angle */ 97 public void rotate (float angle) { 98 rotate(direction, angle); 99 } 100 101 /** Moves the camera by the given amount on each axis. 102 * @param x the displacement on the x-axis 103 * @param y the displacement on the y-axis */ 104 public void translate (float x, float y) { 105 translate(x, y, 0); 106 } 107 108 /** Moves the camera by the given vector. 109 * @param vec the displacement vector */ 110 public void translate (Vector2 vec) { 111 translate(vec.x, vec.y, 0); 112 } 113} 114