Subversion Repositories gelsvn

Rev

Rev 299 | Rev 310 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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