Java tutorial
/* * Copyright (C) 2012 Robert 'Bobby' Zenz * * This program 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 2 * of the License, or any later version. * * This program 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 this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package org.bonsaimind.badgersvoyage.tools.planetstudio; import java.nio.FloatBuffer; import org.lwjgl.BufferUtils; import org.lwjgl.opengl.GL20; import org.lwjgl.util.vector.Matrix4f; import org.lwjgl.util.vector.Vector3f; /** * * @author Robert 'Bobby' Zenz */ public class Camera { private float aspectRatio; private FloatBuffer buffer; private Vector3f camera; private float farPlane; private float fieldOfView; private float frustumLength; private float nearPlane; private Matrix4f projectionMatrix; private int projectionMatrixLocation; private int programId; private float xScale; private float yScale; private Matrix4f viewMatrix; private int viewMatrixLocation; public Camera(float aspectRatio, Vector3f camera, float farPlane, float fieldOfView, float nearPlane, int programId) { this.aspectRatio = aspectRatio; this.camera = camera; this.farPlane = farPlane; this.fieldOfView = fieldOfView; this.nearPlane = nearPlane; this.programId = programId; this.projectionMatrix = new Matrix4f(); this.projectionMatrixLocation = GL20.glGetUniformLocation(this.programId, "projectionMatrix"); ErrorChecker.exitOnOpenGlError("ProjectionMatrixLocation."); this.viewMatrix = new Matrix4f(); this.viewMatrixLocation = GL20.glGetUniformLocation(this.programId, "viewMatrix"); ErrorChecker.exitOnOpenGlError("ViewMatrixLocation."); this.yScale = 1 / (float) Math.tan(Math.toRadians(this.fieldOfView / 2f)); this.xScale = this.yScale / aspectRatio; this.frustumLength = this.farPlane - this.nearPlane; this.projectionMatrix.m00 = this.xScale; this.projectionMatrix.m11 = this.yScale; this.projectionMatrix.m22 = -((this.farPlane - this.nearPlane) / this.frustumLength); this.projectionMatrix.m23 = -1; this.projectionMatrix.m32 = -((2 * nearPlane * farPlane) / frustumLength); this.buffer = BufferUtils.createFloatBuffer(16); } public void moveCamera(float changeX, float changeY, float changeZ) { // currentRotation.x += changeX; // currentRotation.y += changeY; // currentRotation.z += changeZ; // // currentLocation = new Vector3f( // (float) (currentRotation.z * -Math.sin(Math.toRadians(currentRotation.y)) * Math.cos(Math.toRadians(currentRotation.x))), // (float) (currentRotation.z * Math.cos(Math.toRadians(currentRotation.y)) * Math.sin(Math.toRadians(currentRotation.x))), // (float) (currentRotation.z * Math.cos(Math.toRadians(currentRotation.y)))); } public void endTranslate() { viewMatrix.store(buffer); buffer.flip(); GL20.glUniformMatrix4(viewMatrixLocation, false, buffer); GL20.glUseProgram(0); } public void startTranslate() { viewMatrix = new Matrix4f(); Matrix4f.translate(camera, viewMatrix, viewMatrix); GL20.glUseProgram(programId); projectionMatrix.store(buffer); buffer.flip(); GL20.glUniformMatrix4(projectionMatrixLocation, false, buffer); } }