-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.java
More file actions
71 lines (64 loc) · 2.26 KB
/
Camera.java
File metadata and controls
71 lines (64 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.lang.Math;
/** Class to manage view angles and distances and focus blur
* @author Carol Chen
*/
public class Camera {
public Vector lowerLeft;
public Vector horizontal;
public Vector vertical;
public Vector origin;
public Vector u;
public Vector w;
public Vector v;
public double lensRadius;
public double time0;
public double time1;
/**
* @param lookFrom vector looking from
* @param lookAt vector looking at
* @param vup vertical distance up
* @param vfov angle from vertical onto scene
* @param aspect ascept ratio
* @param aperture aperture (contros amount of light)
* @param focusDist distance to focus
* @param t0 start time
* @param t1 end time
* @return [description]
*/
public Camera(Vector lookFrom, Vector lookAt, Vector vup, double vfov, double aspect, double aperture, double focusDist, double t0, double t1) {
lensRadius = aperture / 2;
time0 = t0;
time1 = t1;
double theta = vfov * Math.PI / 180;
double halfHeight = Math.tan(theta/2);
double halfWidth = halfHeight * aspect;
origin = lookFrom;
w = (lookFrom.subtract(lookAt)).unitVector();
u = (vup.cross(w)).unitVector();
v = w.cross(u);
lowerLeft = origin.subtract(u.multiply(halfWidth).multiply(focusDist)).subtract(v.multiply(halfHeight).multiply(focusDist)).subtract(w.multiply(focusDist));
horizontal = u.multiply(2 * halfWidth).multiply(focusDist);
vertical = v.multiply(2 * halfHeight).multiply(focusDist);
}
/**
* @param s point 1
* @param t point 2
* @return ray at that point
*/
public Ray getRay(double s, double t) {
Vector rd = randomDiskUnit().multiply(lensRadius);
Vector offset = u.multiply(rd.x()).add(v.multiply(rd.y()));
double time = time0 + Math.random() * (time1 - time0);
return new Ray(origin.add(offset), lowerLeft.add(horizontal.multiply(s)).add(vertical.multiply(t)).subtract(origin).subtract(offset), time);
}
/**
* @return Randomized ray vector from disk
*/
public static Vector randomDiskUnit() {
Vector p;
do {
p = new Vector(Math.random(), Math.random(), 0).multiply(2).subtract(new Vector(1, 1, 0));
} while(p.dot(p) >= 1.0);
return p;
}
}