/
camera.cpp
227 lines (187 loc) · 6.48 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "camera.h"
PerspectiveCamera::PerspectiveCamera() : Camera()
{
resetLook();
_center = Point3(4.0,2.0,4.0);
_yRot = 225;
_upRot = 45;
_distance = _center.length();
updateLook();
_center = -lookDir();
//updateLook();
//moveType = MoveType::NOT_MOVING;
}
QMatrix4x4 PerspectiveCamera::getViewMatrix(int width, int height)
{
QMatrix4x4 m;
m.lookAt(eye(), lookat(), upDir());
return m;
}
QMatrix4x4 PerspectiveCamera::getProjMatrix(int width, int height)
{
// can probably remove this now
float pixdx = 0;
float pixdy = 0;
// taken from gluPerspective docs
float aspect = (float)width / (float)height;
float zNear = .1f;
float zFar = 100.0f;
float top = tan(fov()*3.14159/360.0) * zNear;
//float top = tan(fov*0.5) * zNear;
float bottom = -top;
float left = aspect * bottom;
float right = aspect * top;
//int viewport[4];
//glGetIntegerv(GL_VIEWPORT, viewport);
float xwsize = right - left;
float ywsize = top - bottom;
// MAINT: width/height should be pulled from viewport if it doesn't match
// size of render
float dx = -(pixdx * xwsize / (float)width);
float dy = -(pixdy * ywsize / (float)height);
QMatrix4x4 m;
m.frustum(left+dx, right+dx, bottom+dy, top+dy, zNear, zFar);
//m.perspective(PerspectiveCamera->fov(), aspect, zNear, zFar);
return m;
}
#define PI 3.14159265359
RotatePair PerspectiveCamera::aim(Vector3 dir)
{ // based on VTK aim
double xzlen, yzlen, yrot, xrot;
/*
* The initial rotation about the y axis is given by the projection of
* the direction vector onto the x,z plane: the x and z components
* of the direction.
*/
xzlen = sqrt(dir.x()*dir.x()+dir.z()*dir.z());
if (xzlen == 0)
yrot = (dir.y() < 0) ? 180 : 0;
else
yrot = 180*acos(dir.z()/xzlen)/PI;
/*
* The second rotation, about the x axis, is given by the projection on
* the y,z plane of the y-rotated direction vector: the original y
* component, and the rotated x,z vector from above.
*/
yzlen = sqrt(dir.y()*dir.y()+xzlen*xzlen);
xrot = 180*acos(xzlen/yzlen)/PI; /* yzlen should never be 0 */
RotatePair pair;
if (dir.y() > 0) {
pair.rot1 = QVector4D(xrot, 1.0, 0.0, 0.0);
//fprintf (filePtr, "Rotate %f %f %f %f\n", xrot, 1.0, 0.0, 0.0);
} else {
pair.rot1 = QVector4D(-xrot, 1.0, 0.0, 0.0);
//fprintf (filePtr, "Rotate %f %f %f %f\n", -xrot, 1.0, 0.0, 0.0);
}
/* The last rotation declared gets performed first */
if (dir.x() > 0) {
pair.rot2 = QVector4D(-yrot, 0.0, 1.0, 0.0);
//fprintf (filePtr, "Rotate %f %f %f %f\n", -yrot, 0.0, 1.0, 0.0);
} else {
pair.rot2 = QVector4D(yrot, 0.0, 1.0, 0.0);
//fprintf (filePtr, "Rotate %f %f %f %f\n", yrot, 0.0, 1.0, 0.0);
}
return pair;
}
void PerspectiveCamera::mousePressed(CameraScratch &scratch, QMouseEvent *event)
{
scratch.pickX = event->pos().x();
scratch.pickY = event->pos().y();
if (event->button() == Qt::LeftButton) {
scratch.moveType = MoveType::ROTATING;
}
else if (event->button() == Qt::MidButton) {
scratch.moveType = MoveType::PANNING;
scratch.origEye = eye();
scratch.origUp = upDir();
scratch.origLeft = leftDir();
} else if (event->button() == Qt::RightButton) {
scratch.moveType = MoveType::TRUCKING;
} else {
scratch.moveType = MoveType::NOT_MOVING;
}
}
void PerspectiveCamera::mouseReleased(CameraScratch &scratch, QMouseEvent *event)
{
scratch.moveType = MoveType::NOT_MOVING;
}
void PerspectiveCamera::mouseDragged(CameraScratch &scratch, QMouseEvent *event)
{
int xDiff = scratch.pickX - event->pos().x();
int yDiff = scratch.pickY - event->pos().y();
if (scratch.moveType == MoveType::ROTATING) {
Vector3 origLook = eye() + lookDir();
setYRot(yRot() + xDiff * 0.5f);
setUpRot(upRot() + yDiff * -0.5f);
// move eye to look at original focal point (Maya style)
Vector3 lookAway = lookDir() * -1;
setCenter(origLook + lookAway);
}
else if (scratch.moveType == MoveType::PANNING) {
float panScale = 0.05f;
Vector3 mUp = scratch.origUp * -1.0f * yDiff * panScale;
Vector3 mLeft = scratch.origLeft * -1.0f * xDiff * panScale;
setCenter(eye() + mUp + mLeft);
} else if (scratch.moveType == MoveType::TRUCKING) {
Point3 at = lookat();
Vector3 l = lookDir() * -0.01f * yDiff;
setCenter(l + center());
}
scratch.pickX = event->pos().x();
scratch.pickY = event->pos().y();
}
OrthographicCamera::OrthographicCamera() : Camera()
{
_center = Point3(0.5f, 0.5f, 1); // z is arbitrary
_fov = 0.6f;
}
// handles zoom and translation
QMatrix4x4 OrthographicCamera::getViewMatrix(int width, int height)
{
QMatrix4x4 m;
m.setToIdentity();
return m;
}
QMatrix4x4 OrthographicCamera::getProjMatrix(int width, int height)
{
float aspect = (float)width / (float)height;
float left = _center.x() + -_fov*aspect;
float right = _center.x() + _fov*aspect;
float bottom = _center.y() - _fov;
float top = _center.y() + _fov;
QMatrix4x4 m;
m.setToIdentity();
m.ortho(left, right, bottom, top, -1.f, 1.f);
return m;
}
void OrthographicCamera::mousePressed(CameraScratch &scratch, QMouseEvent *event)
{
scratch.pickX = event->pos().x();
scratch.pickY = event->pos().y();
if (event->button() == Qt::MidButton) {
scratch.moveType = MoveType::PANNING;
scratch.origEye = eye();
} else if (event->button() == Qt::RightButton) {
scratch.moveType = MoveType::TRUCKING;
}
}
void OrthographicCamera::mouseReleased(CameraScratch &scratch, QMouseEvent *event)
{
scratch.moveType = MoveType::NOT_MOVING;
}
void OrthographicCamera::mouseDragged(CameraScratch &scratch, QMouseEvent *event)
{
int xDiff = scratch.pickX - event->pos().x();
int yDiff = scratch.pickY - event->pos().y();
if (scratch.moveType == MoveType::PANNING) {
float pixelsToWorld = 2.0f * _fov / scratch.viewHeight;
Vector3 mUp = Vector3(1,0,0) * xDiff * pixelsToWorld;
Vector3 mLeft = Vector3(0,1,0) * -1 * yDiff * pixelsToWorld;
setCenter(eye() + mUp + mLeft);
} else if (scratch.moveType == MoveType::TRUCKING) {
float truckScale = 0.01f;
_fov = std::max(0.1f, _fov + truckScale * yDiff);
}
scratch.pickX = event->pos().x();
scratch.pickY = event->pos().y();
}