Advertisement
Guest User

Untitled

a guest
Jan 15th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.78 KB | None | 0 0
  1. package com.test24;
  2.  
  3. import java.nio.FloatBuffer;
  4.  
  5. import org.lwjgl.BufferUtils;
  6. import org.lwjgl.opengl.GL11;
  7. import org.lwjgl.util.vector.Vector3f;
  8.  
  9. public class Camera {
  10.     private float DEG2RAD = 3.141592653589793f / 180.0f;
  11.     private Vector3f pos;
  12.     private Vector3f left, up, forward;
  13.     private float[] rotationMat;
  14.     private FloatBuffer rotation;
  15.    
  16.     public Camera() {
  17.         pos = new Vector3f(0.0f, 0.0f, 0.0f);
  18.         left = new Vector3f(1.0f, 0.0f, 0.0f);
  19.         up = new Vector3f(0.0f, 1.0f, 0.0f);
  20.         forward = new Vector3f(0.0f, 0.0f, 1.0f);
  21.         rotationMat = new float[16];
  22.         rotation = BufferUtils.createFloatBuffer(16);
  23.     }
  24.    
  25.     public void moveForward(float amount) {
  26.         pos.x += forward.x * amount;
  27.         pos.y += forward.y * amount;
  28.         pos.z += forward.z * amount;
  29.     }
  30.    
  31.     public void rotateX(float delta) {
  32.         delta *= DEG2RAD;
  33.         Vector3f cross = new Vector3f();
  34.         Vector3f.cross(up, forward, cross);
  35.         calcRotationMat(delta, cross);
  36.         Vector3f newVect1 = new Vector3f(rotationMat[0] * forward.x + rotationMat[4] * forward.y + rotationMat[8] * forward.z,
  37.                 rotationMat[1] * forward.x + rotationMat[5] * forward.y + rotationMat[9] * forward.z,
  38.                 rotationMat[2] * forward.x + rotationMat[6] * forward.y + rotationMat[10] * forward.z);
  39.         forward = newVect1;
  40.        
  41.         Vector3f newVect2 = new Vector3f(rotationMat[0] * up.x + rotationMat[4] * up.y + rotationMat[8] * up.z,
  42.                 rotationMat[1] * up.x + rotationMat[5] * up.y + rotationMat[9] * up.z,
  43.                 rotationMat[2] * up.x + rotationMat[6] * up.y + rotationMat[10] * up.z);
  44.         up = newVect2;
  45.     }
  46.    
  47.     public void rotateY(float delta) {
  48.         delta *= DEG2RAD;
  49.         calcRotationMat(delta, up);
  50.         Vector3f newVect = new Vector3f(rotationMat[0] * forward.x + rotationMat[4] * forward.y + rotationMat[8] * forward.z,
  51.                 rotationMat[1] * forward.x + rotationMat[5] * forward.y + rotationMat[9] * forward.z,
  52.                 rotationMat[2] * forward.x + rotationMat[6] * forward.y + rotationMat[10] * forward.z);
  53.         forward = newVect;
  54.     }
  55.    
  56.     public void rotateZ(float delta) {
  57.         delta *= DEG2RAD;
  58.         calcRotationMat(delta, forward);
  59.         Vector3f newVect = new Vector3f(rotationMat[0] * up.x + rotationMat[4] * up.y + rotationMat[8] * up.z,
  60.                 rotationMat[1] * up.x + rotationMat[5] * up.y + rotationMat[9] * up.z,
  61.                 rotationMat[2] * up.x + rotationMat[6] * up.y + rotationMat[10] * up.z);
  62.         up = newVect;
  63.     }
  64.    
  65.     public void calcRotationMat(float angle, Vector3f v) {
  66.         float x = v.x;
  67.         float y = v.y;
  68.         float z = v.z;
  69.         float mag = (float) (1.0f / Math.sqrt(x * x + y * y + z * z));
  70.         x *= mag;
  71.         y *= mag;
  72.         z *= mag;
  73.         float c = (float) Math.cos(angle);
  74.         float s = (float) Math.sin(angle);
  75.        
  76.         float xx = x * x;
  77.         float yy = y * y;
  78.         float zz = z * z;
  79.         float xy = x * y;
  80.         float yz = y * z;
  81.         float zx = z * x;
  82.         float xs = x * s;
  83.         float ys = y * s;
  84.         float zs = z * s;
  85.         float one_c = 1.0f - c;
  86.        
  87.         rotationMat[0] = (one_c * xx) + c;
  88.         rotationMat[4] = (one_c * xy) - zs;
  89.         rotationMat[8] = (one_c * zx) + ys;
  90.         rotationMat[12] = 0.0f;
  91.        
  92.         rotationMat[1] = (one_c * xy) + zs;
  93.         rotationMat[5] = (one_c * yy) + c;
  94.         rotationMat[9] = (one_c * yz) - xs;
  95.         rotationMat[13] = 0.0f;
  96.        
  97.         rotationMat[2] = (one_c * zx) - ys;
  98.         rotationMat[6] = (one_c * yz) + xs;
  99.         rotationMat[10] = (one_c * zz) + c;
  100.         rotationMat[14] = 0.0f;
  101.        
  102.         rotationMat[3] = 0.0f;
  103.         rotationMat[7] = 0.0f;
  104.         rotationMat[11] = 0.0f;
  105.         rotationMat[15] = 1.0f;
  106.     }
  107.    
  108.     public void applyCameraTransform() {
  109.         Vector3f z = forward.negate(null);
  110.         left = Vector3f.cross(up, z, null);
  111.         rotation.clear();
  112.         float[] orient = new float[] {
  113.                 left.x, up.x, z.x, 0.0f,
  114.                 left.y, up.y, z.y, 0.0f,
  115.                 left.z, up.z, z.z, 0.0f,
  116.                 0.0f, 0.0f, 0.0f, 1.0f
  117.         };
  118.         rotation.put(orient).flip();
  119.         GL11.glMultMatrix(rotation);
  120.         GL11.glTranslatef(-pos.x, -pos.y, -pos.z);
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement