Advertisement
WeltEnSTurm

Untitled

Mar 26th, 2014
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 1.27 KB | None | 0 0
  1. module entity.camera;
  2.  
  3. import
  4.     ws.math.math,
  5.     ws.math.angle,
  6.     ws.math.quaternion,
  7.     ws.math.vector,
  8.     ws.math.matrix,
  9.     entity.entity,
  10.     engine;
  11.  
  12.  
  13. class Camera: Entity {
  14.  
  15.     this(Engine e){
  16.         super(e);
  17.         matrix = new Matrix!(4,4);
  18.         fov = 45;
  19.         near = 1;
  20.         far = 10000;
  21.         update();
  22.     }
  23.  
  24.     void setAspect(float aspect){
  25.         this.aspect = aspect;
  26.         update;
  27.     }
  28.    
  29.     void setFov(float fov){
  30.         this.fov = fov;
  31.         update;
  32.     }
  33.    
  34.     void setBounds(float near, float far){
  35.         this.near = near;
  36.         this.far = far;
  37.         update;
  38.     }
  39.  
  40.     void update(){
  41.         float xmin, xmax, ymin, ymax;
  42.         ymax = near * tan(fov*PI/360.0);
  43.         ymin = -ymax;
  44.         xmin = ymin * aspect;
  45.         xmax = -xmin;
  46.         matrix[0] = (2*near)/(xmax - xmin);
  47.         matrix[5] = (2*near)/(ymax - ymin);
  48.         matrix[8] = (xmax + xmin) / (xmax - xmin);
  49.         matrix[9] = (ymax + ymin) / (ymax - ymin);
  50.         matrix[10] = -((far + near)/(far - near));
  51.         matrix[11] = -1;
  52.         matrix[14] = -((2*far*near)/(far-near));
  53.         matrix[15] = 0;
  54.     }
  55.  
  56.     Matrix!(4,4) getProjection(){
  57.         auto m = matrix.dup();
  58.         Angle a = angle.euler();
  59.         a.r *= -1;
  60.         a.y *= -1;
  61.         m.rotate(Quaternion.euler(a));
  62.         m.translate(Vector!3(position.x, -position.y, position.z));
  63.         return m;
  64.     }
  65.  
  66.     protected {
  67.         float aspect, fov, near, far;
  68.         Matrix!(4,4) matrix;
  69.     }
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement