-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathGLCamera.cpp
296 lines (247 loc) · 6.75 KB
/
GLCamera.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
/*This file is part of the FEBio Studio source code and is licensed under the MIT license
listed below.
See Copyright-FEBio-Studio.txt for details.
Copyright (c) 2021 University of Utah, The Trustees of Columbia University in
the City of New York, and others.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.*/
#include "stdafx.h"
#include "GLCamera.h"
#ifdef WIN32
#include <Windows.h>
#include <GL/gl.h>
#endif
#ifdef __APPLE__
#include <OpenGL/gl.h>
#endif
#ifdef LINUX
#include <GL/gl.h>
#endif
#include "glx.h"
//=============================================================================
GLCameraTransform::GLCameraTransform(const GLCameraTransform& key)
{
pos = key.pos;
trg = key.trg;
rot = key.rot;
SetName(key.GetName());
}
GLCameraTransform& GLCameraTransform::operator = (const GLCameraTransform& key)
{
pos = key.pos;
trg = key.trg;
rot = key.rot;
SetName(key.GetName());
return *this;
}
//=============================================================================
CGLCamera::CGLCamera()
{
Reset();
}
//-----------------------------------------------------------------------------
CGLCamera::~CGLCamera()
{
}
//-----------------------------------------------------------------------------
void CGLCamera::Reset()
{
SetCameraSpeed(0.8f);
SetCameraBias(0.8f);
m_rot.Target(quatd(0, vec3d(1,0,0)));
m_pos.Target(vec3d(0,0,0));
m_trg.Target(vec3d(0,0,0));
Update(true);
m_bdecal = false;
m_bortho = false;
m_isMoving = false;
}
//-----------------------------------------------------------------------------
void CGLCamera::SetOrthoProjection(bool b) { m_bortho = b; }
bool CGLCamera::IsOrtho() const { return m_bortho; }
void CGLCamera::MakeActive()
{
Interpolator::m_smooth = 0.5f + m_bias * 0.45f;
Interpolator::m_nsteps = 5 + (int)((1.0 - m_speed) * 60.0);
}
//-----------------------------------------------------------------------------
void CGLCamera::SetCameraSpeed(double f)
{
if (f > 1.0) f = 1.0;
if (f < 0.0) f = 0.0;
m_speed = f;
}
//-----------------------------------------------------------------------------
void CGLCamera::SetCameraBias(double f)
{
if (f > 1.f) f = 1.f;
if (f < 0.f) f = 0.f;
m_bias = f;
}
//-----------------------------------------------------------------------------
bool CGLCamera::IsAnimating()
{
bool banim = false;
banim |= m_pos.m_banim;
banim |= m_trg.m_banim;
banim |= m_rot.m_banim;
return banim;
}
//-----------------------------------------------------------------------------
void CGLCamera::Update(bool bhit)
{
if (bhit == false)
{
m_pos.Update();
m_trg.Update();
m_rot.Update();
}
else
{
m_pos.HitTarget();
m_trg.HitTarget();
m_rot.HitTarget();
}
}
//-----------------------------------------------------------------------------
// set line-draw or decal mode
void CGLCamera::LineDrawMode(bool b)
{
m_bdecal = b;
if (m_bdecal)
glPolygonOffset(0, 0);
else
glPolygonOffset(1, 1);
}
//-----------------------------------------------------------------------------
// This sets up the GL matrix transformation for rendering
void CGLCamera::PositionInScene()
{
// reset the modelview matrix mode
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// target in camera coordinates
vec3d r = Target();
// zoom-in a little when in decal mode
if (m_bdecal)
glPolygonOffset(0, 0);
else
glPolygonOffset(1, 1);
// position the target in camera coordinates
glx::translate(-r);
// orient the camera
glx::rotate(m_rot.Value());
// translate to world coordinates
glx::translate(-GetPosition());
}
//-----------------------------------------------------------------------------
void CGLCamera::Pan(const quatd& q)
{
vec3d p = GetPosition();
quatd Q = m_rot.Target();
Q.RotateVector(p);
p += Target();
q.RotateVector(p);
p -= Target();
Q = q * Q;
Q.Inverse().RotateVector(p);
SetTarget(p);
SetOrientation(Q);
}
void CGLCamera::PanView(const vec3d& r)
{
double f = 0.001f * (double)GetFinalTargetDistance();
Truck(r*f);
}
void CGLCamera::Dolly(double f)
{
vec3d dr(0, 0, -f);
m_rot.Target().Inverse().RotateVector(dr);
SetTarget(FinalPosition() + dr);
}
void CGLCamera::Truck(const vec3d& v)
{
vec3d dr(v);
m_rot.Target().Inverse().RotateVector(dr);
SetTarget(FinalPosition() + dr);
}
//-----------------------------------------------------------------------------
void CGLCamera::Orbit(quatd& q)
{
quatd o = q*m_rot.Target();
o.MakeUnit();
m_rot.Target(o);
}
//-----------------------------------------------------------------------------
void CGLCamera::Zoom(double f)
{
SetTargetDistance(GetFinalTargetDistance() * f);
}
//-----------------------------------------------------------------------------
void CGLCamera::SetTarget(const vec3d& r)
{
m_pos.Target(r);
}
// set the target in local coordinates
void CGLCamera::SetLocalTarget(const vec3d& r)
{
m_trg.Target(r);
}
void CGLCamera::SetViewDirection(const vec3d &r)
{
if (r.Length() != 0.f)
{
m_rot.Target(quatd(vec3d(0, 0, 1.f), r).Inverse());
}
}
void CGLCamera::SetTransform(GLCameraTransform& t)
{
m_pos.Target(t.pos);
m_trg.Target(t.trg);
m_rot.Target(t.rot);
}
void CGLCamera::GetTransform(GLCameraTransform& t)
{
t.pos = m_pos.Value();
t.trg = m_trg.Value();
t.rot = m_rot.Value();
}
//-----------------------------------------------------------------------------
vec3d CGLCamera::WorldToCam(vec3d r) const
{
r -= Target();
quatd q = m_rot.Value();
q.RotateVector(r);
r -= GetPosition();
return r;
}
//-----------------------------------------------------------------------------
vec3d CGLCamera::CamToWorld(vec3d r) const
{
r -= GetPosition();
m_rot.Value().RotateVector(r);
r -= Target();
return r;
}
//-----------------------------------------------------------------------------
// get the position in global coordinates
vec3d CGLCamera::GlobalPosition() const
{
vec3d r = Target();
m_rot.Value().Inverse().RotateVector(r);
r += GetPosition();
return r;
}