-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCamera.cpp
95 lines (78 loc) · 1.79 KB
/
Camera.cpp
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
#include "Camera.h"
Camera::Camera()
{
this->e = (Vector3());
this->d = (Vector3());
this->up = (Vector3());
this->C = Matrix4d();
C.identity();
C.setM4cm(Vector3(), Vector3(), Vector3(), e);
}
Camera::Camera(Vector3 e, Vector3 d, Vector3 up) {
this->e = e;
this->d = d;
this->up = up;
Vector3 z = e - d; // z = (e - d) / ||e - d||
z.normalize();
Vector3 x = x.cross(up, z); // z = (up X z) / ||up X z||
x.normalize();
Vector3 y = y.cross(z, x); // y = (z X x) / ||z X x||
y.normalize();
this->C = (Matrix4d());
this->C.setM4cm(x, y, z, e);
}
Camera::~Camera()
{
}
GLdouble* Camera::getGLMatrix() {
return (this->C.getPointer());
}
void Camera::update() {
Vector3 z = e - d; // z = (e - d) / ||e - d||
z.normalize();
Vector3 x = x.cross(up, z); // z = (up X z) / ||up X z||
x.normalize();
Vector3 y = y.cross(z, x); // y = (z X x) / ||z X x||
y.normalize();
//this->C = (*new Matrix4());
this->C.setM4cm(x, y, z, e);
}
void Camera::move(char c) {
Matrix4d m;
Vector3 dv, dv2;
Vector4d v4e = Vector4d(e.x, e.y, e.z, 1.0);
Vector4d upv = Vector4d(up.x, up.y, up.z, 1.0);
switch(c) {
case 'w':
dv = d - e;
dv2 = dv.cross(up, dv);
dv2.normalize();
m.makeRotate(1, dv2);
v4e = m * v4e;
upv = m * upv;
e = Vector3(v4e.x, v4e.y, v4e.z);
up = Vector3(upv.x, upv.y, upv.z);
break;
case 'a':
m.makeRotate(-1, up);
v4e = m * v4e;
e = Vector3(v4e.x, v4e.y, v4e.z);
break;
case 's':
dv = d - e;
dv2 = dv.cross(up, dv);
dv2.normalize();
m.makeRotate(-1, dv2);
v4e = m * v4e;
upv = m * upv;
e = Vector3(v4e.x, v4e.y, v4e.z);
up = Vector3(upv.x, upv.y, upv.z);
break;
case 'd':
m.makeRotate(1, up);
v4e = m * v4e;
e = Vector3(v4e.x, v4e.y, v4e.z);
break;
}
update();
}