-
Notifications
You must be signed in to change notification settings - Fork 1
/
camera.h
112 lines (86 loc) · 2.66 KB
/
camera.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#ifndef _CAMERA_H_
#define _CAMERA_H_
#include <cassert>
#include <iostream>
#include <fstream>
#include "vectors.h"
#include "ray.h"
// ====================================================================
class Camera {
public:
// CONSTRUCTOR & DESTRUCTOR
Camera(const Vec3f &c, const Vec3f &poi, const Vec3f &u);
virtual ~Camera() {}
// RENDERING
virtual Ray generateRay(double x, double y) = 0;
// GL NAVIGATION
virtual void glInit(int w, int h) = 0;
void glPlaceCamera(void);
void dollyCamera(double dist);
virtual void zoomCamera(double dist) = 0;
void truckCamera(double dx, double dy);
void rotateCamera(double rx, double ry);
friend std::ostream& operator<<(std::ostream &ostr, const Camera &c);
protected:
Camera() { assert(0); } // don't use
// HELPER FUNCTIONS
Vec3f getHorizontal() const {
Vec3f answer;
Vec3f::Cross3(answer, getDirection(), up);
answer.Normalize();
return answer; }
Vec3f getScreenUp() const {
Vec3f answer;
Vec3f::Cross3(answer, getHorizontal(), getDirection());
return answer; }
Vec3f getDirection() const {
Vec3f answer = point_of_interest - camera_position;
answer.Normalize();
return answer; }
// REPRESENTATION
Vec3f point_of_interest;
Vec3f camera_position;
Vec3f up;
int width;
int height;
};
// ====================================================================
class OrthographicCamera : public Camera {
public:
// CONSTRUCTOR & DESTRUCTOR
OrthographicCamera(const Vec3f &c = Vec3f(0,0,1),
const Vec3f &poi = Vec3f(0,0,0),
const Vec3f &u = Vec3f(0,1,0),
double s=100);
// RENDERING
Ray generateRay(double x, double y);
// GL NAVIGATION
void glInit(int w, int h);
void zoomCamera(double factor);
friend std::ostream& operator<<(std::ostream &ostr, const OrthographicCamera &c);
friend std::istream& operator>>(std::istream &istr, OrthographicCamera &c);
private:
// REPRESENTATION
double size;
};
// ====================================================================
class PerspectiveCamera : public Camera {
public:
// CONSTRUCTOR & DESTRUCTOR
PerspectiveCamera(const Vec3f &c = Vec3f(0,0,1),
const Vec3f &poi = Vec3f(0,0,0),
const Vec3f &u = Vec3f(0,1,0),
double a = 45);
// RENDERING
Ray generateRay(double x, double y);
// GL NAVIGATION
void glInit(int w, int h);
void zoomCamera(double dist);
friend std::ostream& operator<<(std::ostream &ostr, const PerspectiveCamera &c);
friend std::istream& operator>>(std::istream &istr, PerspectiveCamera &c);
private:
// REPRESENTATION
double angle;
};
// ====================================================================
#endif