Package ponkOut.graphics

Source Code of ponkOut.graphics.Camera

/* Copyright 2010 Christian Matt
*
* This file is part of PonkOut.
*
* PonkOut is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PonkOut is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PonkOut.  If not, see <http://www.gnu.org/licenses/>.
*/

package ponkOut.graphics;

import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Matrix4f;
import org.lwjgl.util.vector.Vector3f;

public class Camera {
  private static Vector3f position;
  private static float rotX, rotY, rotZ;

  public static void init(Vector3f position) {
    setPosition(position);
    rotX = 0.0f;
    rotY = 0.0f;
    rotZ = 0.0f;
  }

  /**
   * apply rotation and translation matrices
   */
  public static void applyTransformation() {
    // rotX += InputManager.getInstance().getMouseMovementY();
    // rotY -= InputManager.getInstance().getMouseMovementX();

    GL11.glTranslatef(-position.x, -position.y, -position.z);
    GL11.glRotatef(-rotX, 1.0f, 0.0f, 0.0f);
    GL11.glRotatef(-rotY, 0.0f, 1.0f, 0.0f);
    GL11.glRotatef(-rotZ, 0.0f, 0.0f, 1.0f);
  }

  /**
   * apply rotations without applying translations
   */
  public static void rotateOnly() {
    rotateOnly(false);
  }

  /**
   * apply rotations without applying translations
   *
   * @param inverse
   *            inverse transformation is applied if true
   */
  public static void rotateOnly(boolean inverse) {
    if (inverse) {
      GL11.glRotatef(rotZ, 0.0f, 0.0f, 1.0f);
      GL11.glRotatef(rotY, 0.0f, 1.0f, 0.0f);
      GL11.glRotatef(rotX, 1.0f, 0.0f, 0.0f);
    } else {
      GL11.glRotatef(-rotX, 1.0f, 0.0f, 0.0f);
      GL11.glRotatef(-rotY, 0.0f, 1.0f, 0.0f);
      GL11.glRotatef(-rotZ, 0.0f, 0.0f, 1.0f);
    }
  }

  public static Matrix4f getTransformation() {
    Matrix4f matrix = new Matrix4f();

    matrix.translate(new Vector3f(-position.x, -position.y, -position.z));

    // convert angles from degrees to radians
    matrix.rotate((float) ((-rotX) * Math.PI / 180.0), new Vector3f(1.0f, 0.0f, 0.0f));
    matrix.rotate((float) ((-rotY) * Math.PI / 180.0), new Vector3f(0.0f, 1.0f, 0.0f));
    matrix.rotate((float) ((-rotZ) * Math.PI / 180.0), new Vector3f(0.0f, 0.0f, 1.0f));

    return matrix;
  }

  public static void setPosition(Vector3f position) {
    Camera.position = new Vector3f(position.x, position.y, position.z);
  }

  public static void setRotation(float x, float y, float z) {
    rotX = x;
    rotY = y;
    rotZ = z;
  }
}
TOP

Related Classes of ponkOut.graphics.Camera

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.