1 #ifndef DEPTHOFFIELD_HXX
2 #define DEPTHOFFIELD_HXX
3
4 #include "Camera.hxx"
5
6 class DepthOfField : public Camera
7 {
8 // input values
9 Vec3f pos,dir,up;
10 float angle;
11 float focallength;
12 float aperture;
13
14 // preprocessed values
15 float focus;
16 Vec3f xAxis,yAxis,zAxis;
17 float aspect;
18
19 public:
20 /**
21 * parameter as normal for PerspectiveCamera, new:
22 * (1) focallength:
23 * the distance from the camera position to the focal plane
24 * thus. the distance where the object is sharp
25 * (2) aperture:
26 * how blurred should the object look like?
27 * (larger value implies more blurring)
28 */
29 DepthOfField(Vec3f pos, Vec3f _dir, Vec3f up, float angle, float focallength, float aperture, int resX, int resY )
30 : Camera(resX,resY), pos(pos), up(up), angle(angle), focallength(focallength), aperture(aperture)
31 {
32 /**
33 * computation as in PerspectiveCamera
34 */
35 dir = _dir;
36 Normalize(_dir);
37
38 zAxis = dir;
39 xAxis = Cross(dir,up);
40 yAxis = Cross(xAxis,zAxis);
41
42 Normalize(xAxis);
43 Normalize(yAxis);
44 Normalize(zAxis);
45
46 aspect = resX / float(resY);
47
48 float angleInRad = angle * (float)M_PI / 180.f;
49 focus = 1.f / tan(angleInRad/2.f);
50 }
51
52 virtual ~DepthOfField()
53 {}
54
55 /**
56 * some set methods
57 */
58 void setAperture(float a)
59 {
60 aperture = a;
61 }
62
63 void setFocallength(float f)
64 {
65 focallength = f;
66 }
67
68 virtual bool InitRay(float x, float y, Ray &ray)
69 {
70 /**
71 * compute two random numbers distributed in a unit disk
72 */
73 float r = drand48();
74 float help = 1.0;
75 while (help == 1.0)
76 help = drand48();
77 float theta = 2.0 * M_PI * help;
78 float xRandom = sqrtf(r) * cosf(theta);
79 float yRandom = sqrtf(r) * sinf(theta);
80
81 /**
82 * position computation as in normal PerspectiveCamera
83 */
84 ray.org = pos;
85 ray.dir = ( 2.0f * (x/(float)resX - 0.5f) * aspect * xAxis)
86 + ( 2.0f * (y/(float)resY - 0.5f) * yAxis)
87 + (focus * zAxis);
88
89 /**
90 * compute then the intersection point of the ray with the plane of focus
91 * and the corresponding distance
92 */
93 float dist = focallength / Dot(ray.dir,zAxis);
94 Vec3f focuspoint = ray.org + dist * ray.dir;
95
96 /**
97 * move origin of the ray in x- and y-direction
98 * with a maximal movement of 0.5 to the left and to the right
99 */
100 ray.org += Vec3f( (xRandom-0.5)*aperture, 0, 0) + Vec3f( 0, (yRandom-0.5)*aperture, 0);
101
102 /**
103 * new direction vector must go through point on plane of focus
104 */
105 ray.dir = focuspoint - ray.org;
106 Normalize(ray.dir);
107
108 ray.t = Infinity;
109 ray.hit = NULL;
110
111 return true;
112 };
113 };
114
115 #endif
syntax highlighted by Code2HTML, v. 0.9.1