-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.cpp
More file actions
252 lines (206 loc) · 5.62 KB
/
camera.cpp
File metadata and controls
252 lines (206 loc) · 5.62 KB
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
#include <windows.h>
#include <Fl/gl.h>
#include <gl/glu.h>
#include "camera.h"
#pragma warning(push)
#pragma warning(disable : 4244)
#ifndef M_PI
#define M_PI 3.141592653589793238462643383279502
#endif
const float kMouseRotationSensitivity = 1.0f/90.0f;
const float kMouseTranslationXSensitivity = 0.03f;
const float kMouseTranslationYSensitivity = 0.03f;
const float kMouseZoomSensitivity = 0.08f;
void MakeDiagonal(Mat4f &m, float k)
{
register int i,j;
for (i=0; i<4; i++)
for (j=0; j<4; j++)
m[i][j] = (i==j) ? k : 0.0f;
}
void MakeHScale(Mat4f &m, const Vec3f &s)
{
MakeDiagonal(m,1.0f);
m[0][0] = s[0]; m[1][1] = s[1]; m[2][2] = s[2];
}
void MakeHTrans(Mat4f &m, const Vec3f &s)
{
MakeDiagonal(m,1.0f);
m[0][3] = s[0]; m[1][3] = s[1]; m[2][3] = s[2];
}
void MakeHRotX(Mat4f &m, float theta)
{
MakeDiagonal(m,1.0f);
float cosTheta = cos(theta);
float sinTheta = sin(theta);
m[1][1] = cosTheta;
m[1][2] = -sinTheta;
m[2][1] = sinTheta;
m[2][2] = cosTheta;
}
void MakeHRotY(Mat4f &m, float theta)
{
MakeDiagonal(m,1.0f);
float cosTheta = cos(theta);
float sinTheta = sin(theta);
m[0][0] = cosTheta;
m[2][0] = -sinTheta;
m[0][2] = sinTheta;
m[2][2] = cosTheta;
}
void MakeHRotZ(Mat4f &m, float theta)
{
MakeDiagonal(m,1.0f);
float cosTheta = cos(theta);
float sinTheta = sin(theta);
m[0][0] = cosTheta;
m[0][1] = -sinTheta;
m[1][0] = sinTheta;
m[1][1] = cosTheta;
}
void Camera::calculateViewingTransformParameters()
{
Mat4f dollyXform;
Mat4f azimXform;
Mat4f elevXform;
Mat4f twistXform;
Mat4f originXform;
Vec3f upVector;
MakeHTrans(dollyXform, Vec3f(0,0,mDolly));
MakeHRotY(azimXform, mAzimuth);
MakeHRotX(elevXform, mElevation);
MakeDiagonal(twistXform, 1.0f);
MakeHTrans(originXform, mLookAt);
mPosition = Vec3f(0,0,0);
// grouped for (mat4 * vec3) ops instead of (mat4 * mat4) ops
mPosition = originXform * (azimXform * (elevXform * (dollyXform * mPosition)));
if ( fmod((double)mElevation, 2.0*M_PI) < 3*M_PI/2 && fmod((double)mElevation, 2.0*M_PI) > M_PI/2 )
mUpVector= Vec3f(0,-1,0);
else
mUpVector= Vec3f(0,1,0);
mDirtyTransform = false;
}
Camera::Camera()
{
mElevation = mAzimuth = mTwist = 0.0f;
mDolly = -20.0f;
mElevation = 0.2f;
mAzimuth = (float)M_PI;
mLookAt = Vec3f( 0, 0, 0 );
mCurrentMouseAction = kActionNone;
calculateViewingTransformParameters();
}
void Camera::clickMouse( MouseAction_t action, int x, int y )
{
mCurrentMouseAction = action;
mLastMousePosition[0] = x;
mLastMousePosition[1] = y;
}
void Camera::dragMouse( int x, int y )
{
Vec3f mouseDelta = Vec3f(x,y,0.0f) - mLastMousePosition;
mLastMousePosition = Vec3f(x,y,0.0f);
switch(mCurrentMouseAction)
{
case kActionTranslate:
{
calculateViewingTransformParameters();
double xTrack = -mouseDelta[0] * kMouseTranslationXSensitivity;
double yTrack = mouseDelta[1] * kMouseTranslationYSensitivity;
Vec3f transXAxis = mUpVector ^ (mPosition - mLookAt);
transXAxis /= sqrt((transXAxis*transXAxis));
Vec3f transYAxis = (mPosition - mLookAt) ^ transXAxis;
transYAxis /= sqrt((transYAxis*transYAxis));
setLookAt(getLookAt() + transXAxis*xTrack + transYAxis*yTrack);
break;
}
case kActionRotate:
{
float dAzimuth = -mouseDelta[0] * kMouseRotationSensitivity;
float dElevation = mouseDelta[1] * kMouseRotationSensitivity;
setAzimuth(getAzimuth() + dAzimuth);
setElevation(getElevation() + dElevation);
break;
}
case kActionZoom:
{
float dDolly = -mouseDelta[1] * kMouseZoomSensitivity;
setDolly(getDolly() + dDolly);
break;
}
case kActionTwist:
// Not implemented
default:
break;
}
}
void Camera::releaseMouse( int x, int y )
{
mCurrentMouseAction = kActionNone;
}
void Camera::applyViewingTransform() {
if( mDirtyTransform )
calculateViewingTransformParameters();
// Place the camera at mPosition, aim the camera at
// mLookAt, and twist the camera such that mUpVector is up
/*
gluLookAt( mPosition[0], mPosition[1], mPosition[2],
mLookAt[0], mLookAt[1], mLookAt[2],
mUpVector[0], mUpVector[1], mUpVector[2]);
*/
lookAt( Vec3f(mPosition[0], mPosition[1], mPosition[2]),
Vec3f(mLookAt[0], mLookAt[1], mLookAt[2]),
Vec3f(mUpVector[0], mUpVector[1], mUpVector[2]));
}
/**
* eye: the position of the carema
* at: the point it is looking at
* up: The up vector in world coordiantes
*/
void Camera::lookAt(Vec3f eye, Vec3f at, Vec3f up) {
//The carema position
float eyex = eye[0];
float eyey = eye[1];
float eyez = eye[2];
//The looking point
float atx = at[0];
float aty = at[1];
float atz = at[2];
//Up direction
float upx = up[0];
float upy = up[1];
float upz = up[2];
//The view direction, which should be the new z vector
//The eyece should be in
float vpnx = atx - eyex;
float vpny = aty - eyey;
float vpnz = atz - eyez;
//Normalize it to unit vector
float len = sqrt(vpnx * vpnx + vpny * vpny + vpnz * vpnz);
vpnx /= len;
vpny /= len;
vpnz /= len;
//Calculate right vector, it's basically the cross
//Product of the up axis(view direction),
//And the view direction, becasue the new up vector must
//be parallel to the original up direction, to agree the
//set up
float rvx = vpny * upz - vpnz * upy;
float rvy = vpnz * upx - vpnx * upz;
float rvz = vpnx * upy - vpny * upx;
//Calculate new up vector
float nux = rvy * vpnz - rvz * vpny;
float nuy = rvz * vpnx - rvx * vpnz;
float nuz = rvx * vpny - rvy * vpnx;
//Put it all in a pretty Matrix
float mat[16] = {
rvx, nux, -vpnx, 0,
rvy, nuy, -vpny, 0,
rvz, nuz, -vpnz, 0,
0, 0, 0, 1
};
//Apply the matrix and translate to eyepoint
glMultMatrixf(mat);
glTranslatef(-eyex, -eyey, -eyez);
}
#pragma warning(pop)