Advertisement
Guest User

Camera.java

a guest
Oct 31st, 2013
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.34 KB | None | 0 0
  1. package de.skysoldier.graphics3D;
  2.  
  3. import de.skysoldier.graphics3D.Values3D;
  4.  
  5. public class Camera {
  6.        
  7.     private Matrix projection;
  8.     private Values3D type;
  9.     private double l, r, t, b, n, f;
  10.     //only for debug...
  11.     public boolean active = true;
  12.    
  13.     protected Camera(){
  14.         this(Values3D.CAMERA_PERSPECTIVE);
  15.     }
  16.    
  17.     protected Camera(Values3D type){
  18.         this(type, -100, 100, 100, -100, 50, 500);
  19.     }
  20.    
  21.     protected Camera(Values3D type, double l, double r, double t, double b, double n, double f){
  22.         projection = new Matrix(4, 4, type);
  23.         this.type = type;
  24.         this.l = l;
  25.         this.r = r;
  26.         this.t = t;
  27.         this.b = b;
  28.         this.n = n;
  29.         this.f = f;
  30.         if(type == Values3D.CAMERA_PERSPECTIVE){
  31.             projection.setCellData(0, 0, (2 * n) / (r - l));
  32.             projection.setCellData(0, 1, 0);
  33.             projection.setCellData(0, 2, (r + l) / (r - l));
  34.             projection.setCellData(0, 3, 0);
  35.             projection.setCellData(1, 0, 0);
  36.             projection.setCellData(1, 1, (2 * n) / (t - b));
  37.             projection.setCellData(1, 2, (t + b) / (t - b));
  38.             projection.setCellData(1, 3, 0);
  39.             projection.setCellData(2, 0, 0);
  40.             projection.setCellData(2, 1, 0);
  41.             projection.setCellData(2, 2, -(f + n) / (f - n));
  42.             projection.setCellData(2, 3, -(2 * f * n) / (f - n));
  43.             projection.setCellData(3, 0, 0);
  44.             projection.setCellData(3, 1, 0);
  45.             projection.setCellData(3, 2, -1);
  46.             projection.setCellData(3, 3, 0);
  47.         }
  48.         else{
  49.             throw new UnsupportedOperationException("Camera Type not supported.");
  50.         }
  51.     }
  52.    
  53.     public Matrix getProjectionMatrix(){
  54.         return projection;
  55.     }
  56.    
  57.     public void updateLeft(double l){
  58.         this.l = l;
  59.         if(type == Values3D.CAMERA_PERSPECTIVE){
  60.             projection.setCellData(0, 0, (2 * n) / (r - l));
  61.             projection.setCellData(0, 2, (r + l) / (r - l));
  62.         }
  63.     }
  64.    
  65.     public void updateRight(double r){
  66.         this.r = r;
  67.         if(type == Values3D.CAMERA_PERSPECTIVE){
  68.             projection.setCellData(0, 0, (2 * n) / (r - l));
  69.             projection.setCellData(0, 2, (r + l) / (r - l));
  70.         }
  71.     }
  72.    
  73.     public void updateTop(double t){
  74.         this.t = t;
  75.         if(type == Values3D.CAMERA_PERSPECTIVE){
  76.             projection.setCellData(1, 1, (2 * n) / (t - b));
  77.             projection.setCellData(1, 2, (t + b) / (t - b));
  78.         }
  79.     }
  80.    
  81.     public void updateBottom(double b){
  82.         this.b = b;
  83.         if(type == Values3D.CAMERA_PERSPECTIVE){
  84.             projection.setCellData(1, 1, (2 * n) / (t - b));
  85.             projection.setCellData(1, 2, (t + b) / (t - b));
  86.         }
  87.     }
  88.    
  89.     public void updateNear(double n){
  90.         this.n = n;
  91.         if(type == Values3D.CAMERA_PERSPECTIVE){
  92.             projection.setCellData(0, 0, (2 * n) / (r - l));
  93.             projection.setCellData(1, 1, (2 * n) / (t - b));
  94.             projection.setCellData(2, 2, -(f + n) / (f - n));
  95.             projection.setCellData(2, 3, -(2 * f * n) / (f - n));
  96.         }
  97.     }
  98.    
  99.     public void updateFar(double f){
  100.         this.f = f;
  101.         if(type == Values3D.CAMERA_PERSPECTIVE){
  102.             projection.setCellData(2, 2, -(f + n) / (f - n));
  103.             projection.setCellData(2, 3, -(2 * f * n) / (f - n));
  104.         }
  105.     }
  106.    
  107.     public void transform(Matrix... transformations){
  108.         for(int i = 0; i < transformations.length; i++){
  109.             projection = transformations[i].multiply(projection);
  110.         }
  111.     }
  112.    
  113.     public double getLeft(){
  114.         return l;
  115.     }
  116.    
  117.     public double getRight(){
  118.         return r;
  119.     }
  120.    
  121.     public double getTop(){
  122.         return t;
  123.     }
  124.    
  125.     public double getBottom(){
  126.         return b;
  127.     }
  128.    
  129.     public double getNear(){
  130.         return n;
  131.     }
  132.    
  133.     public double getFar(){
  134.         return f;
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement