309 |
jab |
1 |
#ifndef CAMERA_H
|
|
|
2 |
#define CAMERA_H
|
|
|
3 |
|
|
|
4 |
#include "GL/glut.h"
|
|
|
5 |
|
|
|
6 |
#include "CGLA/Vec2f.h"
|
|
|
7 |
#include "CGLA/Vec3f.h"
|
|
|
8 |
|
|
|
9 |
#include "Geometry/Ray.h"
|
|
|
10 |
|
|
|
11 |
const float FAR_PLANE = 1.0e8;
|
|
|
12 |
|
|
|
13 |
class Camera
|
|
|
14 |
{
|
|
|
15 |
public:
|
|
|
16 |
|
|
|
17 |
Camera(const CGLA::Vec3f& _eye, // Eye point (camera position)
|
|
|
18 |
const CGLA::Vec3f& _focus, // Focus point
|
|
|
19 |
const CGLA::Vec3f& _up, // Up vector
|
|
|
20 |
float fd) // Focal distance
|
|
|
21 |
: eye(_eye), focus(_focus), up(_up), focal_dist(fd)
|
|
|
22 |
{
|
|
|
23 |
#ifndef M_1_PI
|
|
|
24 |
const double M_1_PI = 0.318309886184;
|
|
|
25 |
#endif
|
|
|
26 |
|
|
|
27 |
line_of_sight = focus - eye;
|
|
|
28 |
|
|
|
29 |
// Calculate view plane normal and basis
|
|
|
30 |
vp_normal = normalize(line_of_sight);
|
|
|
31 |
assert(dot(vp_normal, normalize(up)) < 0.99999999);
|
|
|
32 |
vp_axes[0] = normalize(cross(vp_normal, up));
|
|
|
33 |
vp_axes[1] = normalize(cross(vp_axes[0], vp_normal));
|
|
|
34 |
|
|
|
35 |
// Account for focal distance
|
|
|
36 |
vp_normal *= focal_dist;
|
|
|
37 |
|
|
|
38 |
// Calculate field of view (using the pinhole camera model)
|
|
|
39 |
float tmp = atan(1.0/(2.0 * focal_dist));
|
|
|
40 |
fov = 360.0 * M_1_PI * tmp;
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
void set(const CGLA::Vec3f& _eye, // Eye point (camera position)
|
|
|
44 |
const CGLA::Vec3f& _focus, // Focus point
|
|
|
45 |
const CGLA::Vec3f& _up, // Up vector
|
|
|
46 |
float fd) // Focal distance
|
|
|
47 |
{
|
|
|
48 |
#ifndef M_1_PI
|
|
|
49 |
const double M_1_PI = 0.318309886184;
|
|
|
50 |
#endif
|
|
|
51 |
|
|
|
52 |
eye = _eye;
|
|
|
53 |
focus = _focus;
|
|
|
54 |
up = _up;
|
|
|
55 |
focal_dist = fd;
|
|
|
56 |
|
|
|
57 |
line_of_sight = focus - eye;
|
|
|
58 |
|
|
|
59 |
// Calculate view plane normal and basis
|
|
|
60 |
vp_normal = normalize(line_of_sight);
|
|
|
61 |
assert(dot(vp_normal, normalize(up)) < 0.99999999);
|
|
|
62 |
vp_axes[0] = normalize(cross(vp_normal, up));
|
|
|
63 |
vp_axes[1] = normalize(cross(vp_axes[0], vp_normal));
|
|
|
64 |
|
|
|
65 |
// Account for focal distance
|
|
|
66 |
vp_normal *= focal_dist;
|
|
|
67 |
|
|
|
68 |
// Calculate field of view (using the pinhole camera model)
|
|
|
69 |
float tmp = atan(1.0/(2.0 * focal_dist));
|
|
|
70 |
fov = 360.0 * M_1_PI * tmp;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
/// Get direction of viewing ray from image coords.
|
|
|
74 |
CGLA::Vec3f get_ray_dir(const CGLA::Vec2f& coords) const
|
|
|
75 |
{
|
|
|
76 |
// vp_normal is multiplied by focal_dist in the constructor
|
|
|
77 |
return vp_normal + vp_axes[0]*coords[0] + vp_axes[1]*coords[1];
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/// Return position of camera.
|
|
|
81 |
const CGLA::Vec3f& get_position() const { return eye; }
|
|
|
82 |
|
|
|
83 |
/// Return the ray corresponding to a set of image coords
|
|
|
84 |
Geometry::Ray get_ray(const CGLA::Vec2f& coords) const
|
|
|
85 |
{
|
|
|
86 |
return Geometry::Ray(eye, normalize(get_ray_dir(coords)));
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
float get_fov() const { return fov; }
|
|
|
90 |
|
|
|
91 |
float get_focal_dist() const { return focal_dist; }
|
|
|
92 |
|
|
|
93 |
// OpenGL
|
|
|
94 |
|
|
|
95 |
void glSetPerspective(float width, float height) const
|
|
|
96 |
{
|
|
|
97 |
GLdouble aspect = width/height;
|
|
|
98 |
|
|
|
99 |
glMatrixMode(GL_PROJECTION);
|
|
|
100 |
glLoadIdentity();
|
|
|
101 |
gluPerspective(fov, aspect, focal_dist, FAR_PLANE);
|
|
|
102 |
|
|
|
103 |
glMatrixMode(GL_MODELVIEW);
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
void glSetCamera() const
|
|
|
107 |
{
|
|
|
108 |
gluLookAt(eye[0], eye[1], eye[2],
|
|
|
109 |
focus[0], focus[1], focus[2],
|
|
|
110 |
up[0], up[1], up[2]);
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
private:
|
|
|
114 |
|
|
|
115 |
CGLA::Vec3f eye, focus, up;
|
|
|
116 |
float focal_dist;
|
|
|
117 |
float fov;
|
|
|
118 |
float phot_rad;
|
|
|
119 |
|
|
|
120 |
CGLA::Vec3f line_of_sight;
|
|
|
121 |
|
|
|
122 |
// Basis of camera coordinate system (vp - view-plane)
|
|
|
123 |
CGLA::Vec3f vp_normal;
|
|
|
124 |
CGLA::Vec3f vp_axes[2];
|
|
|
125 |
};
|
|
|
126 |
|
|
|
127 |
#endif
|