1 #ifndef PERSPECTIVECAMERA_HXX
2 #define PERSPECTIVECAMERA_HXX
3
4 #include "Camera.hxx"
5
6 class PerspectiveCamera : public Camera
7 {
8 // input values
9 Vec3f pos,dir,up;
10 float angle;
11
12 // preprocessed values
13 float focus;
14 Vec3f xAxis,yAxis,zAxis;
15 float aspect;
16 public:
17 PerspectiveCamera(Vec3f pos,Vec3f dir,Vec3f up,
18 float angle,
19 int resX, int resY
20 )
21 : Camera(resX,resY),pos(pos),dir(dir),up(up),angle(angle)
22 {
23 focus = static_cast<float>(resY) / (tan(angle * M_PI / 360) * 2.0);
24 aspect = static_cast<float>(resX) / static_cast<float>(resY);
25
26 Normalize(up);
27 Normalize(dir);
28
29 // Axis precomputation. Z contains all further precomputations,
30 // including that rays are shot through the middle of the pixels
31 // ("+1")
32 xAxis = Cross(-up, dir);
33 yAxis = Cross(xAxis, dir);
34 zAxis = focus * dir - (xAxis * (resX - 1.0) + yAxis * (resY - 1.0)) / 2.0;
35 }
36
37 virtual ~PerspectiveCamera(){};
38
39 virtual bool InitRay(float x, float y, Ray &ray)
40 {
41 ray.org = pos;
42 ray.dir = xAxis * x + yAxis * y + zAxis;
43 ray.t = Infinity;
44 ray.hit = NULL;
45
46 // Normalize the ray direction
47 Normalize(ray.dir);
48
49 return true;
50 };
51
52 #ifdef STEREOVIEW
53 virtual bool InitRay(float x, float y, Ray &ray, float eyedistance)
54 {
55 ray.org = pos + eyedistance * xAxis;
56 ray.dir = xAxis * x + yAxis * y + zAxis;
57 ray.t = Infinity;
58 ray.hit = NULL;
59
60 // Normalize the ray direction
61 Normalize(ray.dir);
62
63 return true;
64 };
65 #endif
66
67 };
68 #endif
syntax highlighted by Code2HTML, v. 0.9.1