-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.cpp
executable file
·266 lines (211 loc) · 5.45 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
#include "camera.h"
#include <iostream>
#ifdef __APPLE__
# include <OpenGL/glu.h>
#else
# include <GL/glu.h>
#endif
#ifndef M_PI
#define M_PI 3.14159265358979f
#endif
using namespace std;
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;
}
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( sy * ez - ey * sz, sz * ex - ez * sx, sx * ey - ex * sy );
axis.normalize();
float angle = 2.0f * acos( dotprod );
mCurrentRot = Matrix4f::rotation( 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));
}
void Camera::ApplyViewport() const
{
glViewport(mViewport[0],mViewport[1],mViewport[2],mViewport[3]);
}
Matrix4f Camera::projectionMatrix() const
{
return Matrix4f::perspectiveProjection
(
mPerspective[ 0 ] * M_PI / 180.f, mPerspective[ 1 ],
0.1f, 1000.f, false
);
}
Matrix4f Camera::viewMatrix() const
{
// back up distance
Matrix4f lookAt = Matrix4f::lookAt
(
Vector3f( 0, 0, mCurrentDistance ),
Vector3f::ZERO,
Vector3f::UP
);
return lookAt * mCurrentRot * Matrix4f::translation( -mCurrentCenter );
/*
gluLookAt(0,0,mCurrentDistance,
0,0,0,
0.0, 1.0, 0.0);
// rotate object
glMultMatrixf(mCurrentRot);
//translate object to center
glTranslatef(-mCurrentCenter[0],-mCurrentCenter[1],-mCurrentCenter[2]);
*/
}
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);
}