Subversion Repositories gelsvn

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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