Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.Serializable;
- import javax.vecmath.*;
- public class Camera implements Serializable {
- protected Matrix4d viewMatrix;
- protected Point3d eye;
- protected Point3d center;
- protected Vector3d up;
- protected Vector3d right;
- protected Vector3d view;
- protected double fovy; // half FoV in radians
- protected double tanf;
- protected double aspect;
- /* algorithm taken from gluLookAt */
- public void lookAt(Point3d _eye, Point3d _center, Vector3d _up) {
- eye = new Point3d(_eye);
- center = new Point3d(_center);
- Vector3d u = new Vector3d(_up);
- Vector3d f = new Vector3d(center);
- f.sub(eye);
- f.normalize();
- Vector3d s = new Vector3d();
- u.normalize();
- s.cross(f, u);
- s.normalize();
- u.cross(s, f);
- Matrix4d m = new Matrix4d();
- m.setRow(0, s.x, s.y, s.z, 0);
- m.setRow(1, u.x, u.y, u.z, 0);
- m.setRow(2, -f.x, -f.y, -f.z, 0);
- m.setRow(3, 0, 0, 0, 1);
- Matrix4d t = new Matrix4d();
- t.setIdentity();
- Vector3d e = new Vector3d(eye);
- e.negate();
- t.setTranslation(e);
- viewMatrix.mul(m);
- viewMatrix.mul(t);
- view = new Vector3d(f);
- right = new Vector3d(s);
- up = new Vector3d(u);
- }
- /* algorithm taken from gluPerspective */
- public void perspective(double _fovy, double _aspect,
- double zNear, double zFar)
- {
- fovy = Math.toRadians(_fovy) / 2.0;
- tanf = Math.tan(fovy);
- aspect = _aspect;
- double f = 1.0 / tanf;
- double zd = (zNear - zFar);
- Matrix4d m = new Matrix4d();
- m.setRow(0, f / aspect, 0, 0, 0);
- m.setRow(1, 0, f, 0, 0);
- m.setRow(2, 0, 0, (zFar + zNear) / zd, 2 * zFar * zNear / zd);
- m.setRow(3, 0, 0, -1, 0);
- viewMatrix.mul(m);
- }
- public Ray castRay(double x, double y) {
- Vector3d dir = new Vector3d(view);
- Vector3d t = new Vector3d();
- t.scale(tanf * x * aspect, right);
- dir.add(t);
- t.scale(tanf * y, up);
- dir.add(t);
- dir.normalize();
- return new Ray(eye, dir);
- }
- public Point4d project(Point4d in) {
- Point4d out = new Point4d();
- viewMatrix.transform(in, out);
- return out;
- }
- public Camera() {
- viewMatrix = new Matrix4d();
- viewMatrix.setIdentity();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement