-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.cpp
308 lines (238 loc) · 7.11 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include "camera.h"
#include "extra.h"
#include <GL/glu.h>
#include <iostream>
using namespace std;
using namespace Eigen;
Camera::Camera()
{
mStartRot = Matrix4f::Identity();
mCurrentRot = Matrix4f::Identity();
}
void Camera::SetDimensions(int w, int h)
{
mDimensions[0] = w;
mDimensions[1] = h;
}
void Camera::SetPerspective(float fovy)
{
mPerspective[0] = fovy;
}
void Camera::SetViewport(int x, int y, int w, int h)
{
mViewport[0] = x;
mViewport[1] = y;
mViewport[2] = w;
mViewport[3] = h;
mPerspective[1] = float( w ) / h;
}
void Camera::SetCenter(const Vector3f& center)
{
mStartCenter = mCurrentCenter = center;
}
void Camera::SetRotation(const Matrix4f& rotation)
{
mStartRot = mCurrentRot = rotation;
}
void Camera::SetDistance(const float distance)
{
mStartDistance = mCurrentDistance = distance;
}
void Camera::MouseClick(Button button, int x, int y)
{
mStartClick[0] = x;
mStartClick[1] = y;
mButtonState = button;
switch (button)
{
case LEFT:
mCurrentRot = mStartRot;
break;
case MIDDLE:
mCurrentCenter = mStartCenter;
break;
case RIGHT:
mCurrentDistance = mStartDistance;
break;
default:
break;
}
}
void Camera::MouseDrag(int x, int y)
{
switch (mButtonState)
{
case LEFT:
ArcBallRotation(x,y);
break;
case MIDDLE:
PlaneTranslation(x,y);
break;
case RIGHT:
DistanceZoom(x,y);
break;
default:
break;
}
}
void Camera::MouseRelease(int x, int y)
{
mStartRot = mCurrentRot;
mStartCenter = mCurrentCenter;
mStartDistance = mCurrentDistance;
mButtonState = NONE;
}
Matrix4f generateRotationMatrix(const Vector3f& rDirection, float radians)
{
Vector3f normalizedDirection = rDirection.normalized();
float cosTheta = cos(radians);
float sinTheta = sin(radians);
float x = normalizedDirection.x();
float y = normalizedDirection.y();
float z = normalizedDirection.z();
Matrix <float, 4, 4, ColMajor> m;
m <<
x * x * (1.0f - cosTheta) + cosTheta, y * x * (1.0f - cosTheta) - z * sinTheta, z * x * (1.0f - cosTheta) + y * sinTheta, 0.0f,
x * y * (1.0f - cosTheta) + z * sinTheta, y * y * (1.0f - cosTheta) + cosTheta, z * y * (1.0f - cosTheta) - x * sinTheta, 0.0f,
x * z * (1.0f - cosTheta) - y * sinTheta, y * z * (1.0f - cosTheta) + x * sinTheta, z * z * (1.0f - cosTheta) + cosTheta, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f;
return m;
}
void Camera::ArcBallRotation(int x, int y)
{
float sx, sy, sz, ex, ey, ez;
float scale;
float sl, el;
float dotprod;
// find vectors from center of window
sx = mStartClick[0] - ( mDimensions[0] / 2.f );
sy = mStartClick[1] - ( mDimensions[1] / 2.f );
ex = x - ( mDimensions[0] / 2.f );
ey = y - ( mDimensions[1] / 2.f );
// invert y coordinates (raster versus device coordinates)
sy = -sy;
ey = -ey;
// scale by inverse of size of window and magical sqrt2 factor
if (mDimensions[0] > mDimensions[1]) {
scale = (float) mDimensions[1];
} else {
scale = (float) mDimensions[0];
}
scale = 1.f / scale;
sx *= scale;
sy *= scale;
ex *= scale;
ey *= scale;
// project points to unit circle
sl = hypot(sx, sy);
el = hypot(ex, ey);
if (sl > 1.f) {
sx /= sl;
sy /= sl;
sl = 1.0;
}
if (el > 1.f) {
ex /= el;
ey /= el;
el = 1.f;
}
// project up to unit sphere - find Z coordinate
sz = sqrt(1.0f - sl * sl);
ez = sqrt(1.0f - el * el);
// rotate (sx,sy,sz) into (ex,ey,ez)
// compute angle from dot-product of unit vectors (and double it).
// compute axis from cross product.
dotprod = sx * ex + sy * ey + sz * ez;
if( dotprod != 1 )
{
Vector3f axis = Vector3f( sy * ez - ey * sz, sz * ex - ez * sx, sx * ey - ex * sy );
axis.normalize();
float angle = 2.0f * acos( dotprod );
mCurrentRot = generateRotationMatrix( axis, angle );
mCurrentRot = mCurrentRot * mStartRot;
}
else
{
mCurrentRot = mStartRot;
}
}
void Camera::PlaneTranslation(int x, int y)
{
// map window x,y into viewport x,y
// start
int sx = mStartClick[0] - mViewport[0];
int sy = mStartClick[1] - mViewport[1];
// current
int cx = x - mViewport[0];
int cy = y - mViewport[1];
// compute "distance" of image plane (wrt projection matrix)
float d = float(mViewport[3])/2.0f / tan(mPerspective[0]*M_PI / 180.0f / 2.0f);
// compute up plane intersect of clickpoint (wrt fovy)
float su = -sy + mViewport[3]/2.0f;
float cu = -cy + mViewport[3]/2.0f;
// compute right plane intersect of clickpoint (ASSUMED FOVY is 1)
float sr = (sx - mViewport[2]/2.0f);
float cr = (cx - mViewport[2]/2.0f);
Vector2f move(cr-sr, cu-su);
// this maps move
move *= -mCurrentDistance/d;
mCurrentCenter = mStartCenter +
+ move[0] * Vector3f(mCurrentRot(0,0),mCurrentRot(0,1),mCurrentRot(0,2))
+ move[1] * Vector3f(mCurrentRot(1,0),mCurrentRot(1,1),mCurrentRot(1,2));
}
Ray Camera::generateRay(float x, float y) {
float x_space = (2 * x / mDimensions[0]) - 1;
float y_space = 1 - (2 * y / mDimensions[1]);
float proj_matrix[16];
Matrix<float,4,4,ColMajor> lookAtDisplacement;
lookAtDisplacement <<
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, -mCurrentDistance, 1;
Matrix <float,4,4,ColMajor> translationMatrix;
translationMatrix <<
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
mCurrentCenter.x(), mCurrentCenter.y(), mCurrentCenter.z(), 1;
glGetFloatv(GL_PROJECTION_MATRIX, proj_matrix);
Matrix4f cameraTransform = lookAtDisplacement * mCurrentRot * translationMatrix;
Matrix4f projectionMatrix = Matrix4f::Identity();
for (int i = 0; i < 16; i++) {
projectionMatrix(i) = proj_matrix[i];
}
Vector4f mouseClip = Vector4f(x_space, y_space, 0, 1);
Vector4f mouse = cameraTransform.inverse() * projectionMatrix.inverse() * mouseClip;
Vector3f directionFromMouse = Vector3f(mouse.x(), mouse.y(), mouse.z()).normalized();
Vector4f pTrans = mCurrentRot.inverse() * Vector4f(0, 0, mCurrentDistance, 1) + Vector4f(mCurrentCenter.x(), mCurrentCenter.y(), mCurrentCenter.z(), 1);
Ray r = Ray(Vector3f(pTrans.x(),pTrans.y(),pTrans.z()), directionFromMouse);
return r;
}
void Camera::ApplyViewport() const
{
glViewport(mViewport[0],mViewport[1],mViewport[2],mViewport[3]);
}
void Camera::ApplyPerspective() const
{
gluPerspective(mPerspective[0], mPerspective[1], 1.0, 1000.0);
}
void Camera::ApplyModelview() const
{
// back up distance
gluLookAt(0,0,mCurrentDistance,
0,0,0,
0.0, 1.0, 0.0);
// rotate object
glMultMatrix(mCurrentRot);
//translate object to center
glTranslatef(-mCurrentCenter.x(),-mCurrentCenter.y(),-mCurrentCenter.z());
}
void Camera::DistanceZoom(int x, int y)
{
int sy = mStartClick[1] - mViewport[1];
int cy = y - mViewport[1];
float delta = float(cy-sy)/mViewport[3];
// exponential zoom factor
mCurrentDistance = mStartDistance * exp(delta);
}