package com.test24; import java.nio.FloatBuffer; import org.lwjgl.BufferUtils; import org.lwjgl.opengl.GL11; import org.lwjgl.util.vector.Vector3f; public class Camera { private float DEG2RAD = 3.141592653589793f / 180.0f; private Vector3f pos; private Vector3f left, up, forward; private float[] rotationMat; private FloatBuffer rotation; public Camera() { pos = new Vector3f(0.0f, 0.0f, 0.0f); left = new Vector3f(1.0f, 0.0f, 0.0f); up = new Vector3f(0.0f, 1.0f, 0.0f); forward = new Vector3f(0.0f, 0.0f, 1.0f); rotationMat = new float[16]; rotation = BufferUtils.createFloatBuffer(16); } public void moveForward(float amount) { pos.x += forward.x * amount; pos.y += forward.y * amount; pos.z += forward.z * amount; } public void rotateX(float delta) { delta *= DEG2RAD; Vector3f cross = new Vector3f(); Vector3f.cross(up, forward, cross); calcRotationMat(delta, cross); Vector3f newVect1 = new Vector3f(rotationMat[0] * forward.x + rotationMat[4] * forward.y + rotationMat[8] * forward.z, rotationMat[1] * forward.x + rotationMat[5] * forward.y + rotationMat[9] * forward.z, rotationMat[2] * forward.x + rotationMat[6] * forward.y + rotationMat[10] * forward.z); forward = newVect1; Vector3f newVect2 = new Vector3f(rotationMat[0] * up.x + rotationMat[4] * up.y + rotationMat[8] * up.z, rotationMat[1] * up.x + rotationMat[5] * up.y + rotationMat[9] * up.z, rotationMat[2] * up.x + rotationMat[6] * up.y + rotationMat[10] * up.z); up = newVect2; } public void rotateY(float delta) { delta *= DEG2RAD; calcRotationMat(delta, up); Vector3f newVect = new Vector3f(rotationMat[0] * forward.x + rotationMat[4] * forward.y + rotationMat[8] * forward.z, rotationMat[1] * forward.x + rotationMat[5] * forward.y + rotationMat[9] * forward.z, rotationMat[2] * forward.x + rotationMat[6] * forward.y + rotationMat[10] * forward.z); forward = newVect; } public void rotateZ(float delta) { delta *= DEG2RAD; calcRotationMat(delta, forward); Vector3f newVect = new Vector3f(rotationMat[0] * up.x + rotationMat[4] * up.y + rotationMat[8] * up.z, rotationMat[1] * up.x + rotationMat[5] * up.y + rotationMat[9] * up.z, rotationMat[2] * up.x + rotationMat[6] * up.y + rotationMat[10] * up.z); up = newVect; } public void calcRotationMat(float angle, Vector3f v) { float x = v.x; float y = v.y; float z = v.z; float mag = (float) (1.0f / Math.sqrt(x * x + y * y + z * z)); x *= mag; y *= mag; z *= mag; float c = (float) Math.cos(angle); float s = (float) Math.sin(angle); float xx = x * x; float yy = y * y; float zz = z * z; float xy = x * y; float yz = y * z; float zx = z * x; float xs = x * s; float ys = y * s; float zs = z * s; float one_c = 1.0f - c; rotationMat[0] = (one_c * xx) + c; rotationMat[4] = (one_c * xy) - zs; rotationMat[8] = (one_c * zx) + ys; rotationMat[12] = 0.0f; rotationMat[1] = (one_c * xy) + zs; rotationMat[5] = (one_c * yy) + c; rotationMat[9] = (one_c * yz) - xs; rotationMat[13] = 0.0f; rotationMat[2] = (one_c * zx) - ys; rotationMat[6] = (one_c * yz) + xs; rotationMat[10] = (one_c * zz) + c; rotationMat[14] = 0.0f; rotationMat[3] = 0.0f; rotationMat[7] = 0.0f; rotationMat[11] = 0.0f; rotationMat[15] = 1.0f; } public void applyCameraTransform() { Vector3f z = forward.negate(null); left = Vector3f.cross(up, z, null); rotation.clear(); float[] orient = new float[] { left.x, up.x, z.x, 0.0f, left.y, up.y, z.y, 0.0f, left.z, up.z, z.z, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }; rotation.put(orient).flip(); GL11.glMultMatrix(rotation); GL11.glTranslatef(-pos.x, -pos.y, -pos.z); } }