Advertisement
Guest User

Alnitak

a guest
May 12th, 2009
808
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.14 KB | None | 0 0
  1. import java.io.Serializable;
  2. import javax.vecmath.*;
  3.  
  4. public class Camera implements Serializable {
  5.  
  6.     protected Matrix4d  viewMatrix;
  7.  
  8.     protected Point3d   eye;
  9.     protected Point3d   center;
  10.  
  11.     protected Vector3d  up;
  12.     protected Vector3d  right;
  13.     protected Vector3d  view;
  14.  
  15.     protected double    fovy;       // half FoV in radians
  16.     protected double    tanf;
  17.     protected double    aspect;
  18.  
  19.     /* algorithm taken from gluLookAt */
  20.     public void lookAt(Point3d _eye, Point3d _center, Vector3d _up) {
  21.  
  22.         eye = new Point3d(_eye);
  23.         center = new Point3d(_center);
  24.         Vector3d u = new Vector3d(_up);
  25.  
  26.         Vector3d f = new Vector3d(center);
  27.         f.sub(eye);
  28.         f.normalize();
  29.  
  30.         Vector3d s = new Vector3d();
  31.         u.normalize();
  32.         s.cross(f, u);
  33.         s.normalize();
  34.         u.cross(s, f);
  35.  
  36.         Matrix4d m = new Matrix4d();
  37.         m.setRow(0,  s.x,  s.y,  s.z, 0);
  38.         m.setRow(1,  u.x,  u.y,  u.z, 0);
  39.         m.setRow(2, -f.x, -f.y, -f.z, 0);
  40.         m.setRow(3, 0, 0, 0, 1);
  41.  
  42.         Matrix4d t = new Matrix4d();
  43.         t.setIdentity();
  44.  
  45.         Vector3d e = new Vector3d(eye);
  46.         e.negate();
  47.         t.setTranslation(e);
  48.  
  49.         viewMatrix.mul(m);
  50.         viewMatrix.mul(t);
  51.  
  52.         view = new Vector3d(f);
  53.         right = new Vector3d(s);
  54.         up = new Vector3d(u);
  55.     }
  56.  
  57.     /* algorithm taken from gluPerspective */
  58.     public void perspective(double _fovy, double _aspect,
  59.                     double zNear, double zFar)
  60.     {
  61.         fovy = Math.toRadians(_fovy) / 2.0;
  62.         tanf = Math.tan(fovy);
  63.         aspect = _aspect;
  64.  
  65.         double f = 1.0 / tanf;
  66.         double zd = (zNear - zFar);
  67.  
  68.         Matrix4d m = new Matrix4d();
  69.         m.setRow(0, f / aspect, 0, 0, 0);
  70.         m.setRow(1, 0, f, 0, 0);
  71.         m.setRow(2, 0, 0, (zFar + zNear) / zd, 2 * zFar * zNear / zd);
  72.         m.setRow(3, 0, 0, -1, 0);
  73.  
  74.         viewMatrix.mul(m);
  75.     }
  76.  
  77.     public Ray castRay(double x, double y) {
  78.  
  79.         Vector3d dir = new Vector3d(view);
  80.         Vector3d t = new Vector3d();
  81.         t.scale(tanf * x * aspect, right);
  82.         dir.add(t);
  83.         t.scale(tanf * y, up);
  84.         dir.add(t);
  85.         dir.normalize();
  86.        
  87.         return new Ray(eye, dir);
  88.     }
  89.  
  90.     public Point4d project(Point4d in) {
  91.         Point4d out = new Point4d();
  92.         viewMatrix.transform(in, out);
  93.         return out;
  94.     }
  95.  
  96.     public Camera() {
  97.         viewMatrix = new Matrix4d();
  98.         viewMatrix.setIdentity();
  99.     }
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement