-
Notifications
You must be signed in to change notification settings - Fork 1
/
camera.cpp
218 lines (175 loc) · 4.87 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
#include <windows.h>
#include <Fl/gl.h>
#include <gl/glu.h>
#include <Eigen/Dense>
using namespace Eigen;
#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;
const float kTwistSensitivity = 0.02f;
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)));
float r = getTwist();// *M_PI / 180.f;
float c = cos(r), s = sin(r);
if ( fmod((double)mElevation, 2.0*M_PI) < 3*M_PI/2 && fmod((double)mElevation, 2.0*M_PI) > M_PI/2 )
mUpVector= Vec3f(s,-c,0);
else
mUpVector= Vec3f(s,c,0);
mUpVector.normalize();
mDirtyTransform = false;
}
Camera::Camera()
{
mElevation = mAzimuth = mTwist = 0.0f;
mDolly = -20.0f;
mElevation = 0.2f;
mAzimuth = (float)M_PI;
mTwist = 0.0f;
mLookAt = Vec3f( 0, 0, 0 );
mCurrentMouseAction = kActionNone;
calculateViewingTransformParameters();
}
void Camera::clickMouse( MouseAction_t action, int x, int y )
{
mLastMousePosition[0] = x;
mLastMousePosition[1] = y;
mCurrentMouseAction = action;
}
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:
{
setTwist(getTwist() + -mouseDelta[0] * kTwistSensitivity);
break;
}
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]);
// Reference: https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluLookAt.xml
Vector3d eye(mPosition[0], mPosition[1], mPosition[2]),
center(mLookAt[0], mLookAt[1], mLookAt[2]),
up(mUpVector[0], mUpVector[1], mUpVector[2]);
Vector3d F = center - eye;
Vector3d f = F.normalized(); //z
// Vector3d upn = up.normalized();
Vector3d s = f.cross(up).normalized(); //x
Vector3d u = s.cross(f).normalized(); //y
double M[4][4] = { {s(0),u(0),-f(0),0},
{s(1),u(1),-f(1),0},
{s(2),u(2),-f(2),0},
{0,0,0,1}};
glMultMatrixd(*M);
glTranslated(-eye(0), -eye(1), -eye(2));
}
#pragma warning(pop)