This repository was archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathvaCameraControllers.cpp
More file actions
320 lines (270 loc) · 12.8 KB
/
Copy pathvaCameraControllers.cpp
File metadata and controls
320 lines (270 loc) · 12.8 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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2016, Intel Corporation
// 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.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Author(s): Filip Strugar (filip.strugar@intel.com)
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "vaCameraControllers.h"
#include "vaCameraBase.h"
#include "Core/vaInput.h"
#include "IntegratedExternals/vaImguiIntegration.h"
using namespace VertexAsylum;
void vaCameraControllerBase::CameraAttached( const shared_ptr<vaCameraBase> & camera )
{
if( camera == nullptr )
{
assert( !m_attachedCamera.expired() );
m_attachedCamera.reset();
}
else
{
assert( m_attachedCamera.expired() );
m_attachedCamera = camera;
}
}
vaCameraControllerFreeFlight::vaCameraControllerFreeFlight( )
// orient the camera so that X is forward, Z is up, Y is right
: m_baseOrientation( vaMatrix4x4::RotationZ( VA_PIf * 0.5f ) * vaMatrix4x4::RotationY( VA_PIf * 0.5f ) )
{
// m_hasFocus = true; // temporary
m_accumMouseDeltaX = 0.0f;
m_accumMouseDeltaY = 0.0f;
m_accumMove = vaVector3( 0.0f, 0.0f, 0.0f );
m_rotationSpeed = 0.5f;
m_movementSpeed = 20.0f;
m_inputSmoothingLerpK = 200.0f;
m_yaw = 0.0f;
m_pitch = 0.0f; // look towards x
m_roll = 0.0f; // y is right
m_movementSpeedAccelerationModifier = 0.0f;
m_moveWhileNotCaptured = true;
}
vaCameraControllerFreeFlight::~vaCameraControllerFreeFlight( )
{
}
void vaCameraControllerFreeFlight::CameraAttached( const shared_ptr<vaCameraBase> & camera )
{
vaCameraControllerBase::CameraAttached( camera );
if( camera != nullptr )
{
// extract yaw/pitch from attached camera
vaMatrix4x4 debasedOrientation = m_baseOrientation.Inverse() * vaMatrix4x4::FromQuaternion( camera->GetOrientation() );
debasedOrientation.DecomposeRotationYawPitchRoll( m_yaw, m_pitch, m_roll );
m_roll = 0;
}
}
void vaCameraControllerFreeFlight::CameraTick( float deltaTime, vaCameraBase & camera, bool hasFocus )
{
if( ( vaInputMouseBase::GetCurrent( ) == NULL ) || ( vaInputKeyboardBase::GetCurrent( ) == NULL ) )
return;
vaVector3 objectPos = camera.GetPosition();
vaQuaternion objectOri = camera.GetOrientation();
vaInputMouseBase & mouse = *vaInputMouseBase::GetCurrent();
vaInputKeyboardBase & keyboard = *vaInputKeyboardBase::GetCurrent( );
{
float smoothingLerpK = vaMath::TimeIndependentLerpF( deltaTime, m_inputSmoothingLerpK );
float speedBoost = 1.0f;
if( hasFocus )
{
speedBoost *= (keyboard.IsKeyDown( KK_SHIFT ))?(12.0f):(1.0f);
speedBoost *= keyboard.IsKeyDown( KK_CONTROL )?(0.08f):(1.0f);
if( keyboard.IsKeyDown( KK_SHIFT ) && keyboard.IsKeyDown( KK_MENU ) )
speedBoost *= 10.0f;
}
//
///////////////////////////////////////////////////////////////////////////
// Update camera range/speed changes
if( hasFocus )
{
if( keyboard.IsKeyDown( KK_SUBTRACT ) ) m_movementSpeed *= 0.95f;
if( keyboard.IsKeyDown( KK_ADD ) ) m_movementSpeed *= 1.05f;
m_movementSpeed = vaMath::Clamp( m_movementSpeed, 1.0f, 5000.0f );
//if( vaIsKeyClicked( kcViewRangeDown ) ) m_viewRange *= 0.99f;
//if( vaIsKeyClicked( kcViewRangeUp ) ) m_viewRange *= 1.01f;
//m_viewRange = vaMath::Clamp( m_viewRange, 1000.0f, 1000000.0f );
//
}
///////////////////////////////////////////////////////////////////////////
// Update camera rotation
vaVector2 cdelta = vaVector2( 0.0f, 0.0f );
if( hasFocus )
cdelta = (vaVector2)mouse.GetCursorDelta( ) * m_rotationSpeed;
//
// smoothing
{
m_accumMouseDeltaX += cdelta.x;
m_accumMouseDeltaY += cdelta.y;
cdelta.x = smoothingLerpK * m_accumMouseDeltaX;
cdelta.y = smoothingLerpK * m_accumMouseDeltaY;
m_accumMouseDeltaX = (1 - smoothingLerpK) * m_accumMouseDeltaX;
m_accumMouseDeltaY = (1 - smoothingLerpK) * m_accumMouseDeltaY;
}
//
// Rotate
if( mouse.IsCaptured( ) )
{
if( mouse.IsKeyDown( MK_Middle ) )
m_roll -= cdelta.x * 0.005f;
else
m_yaw += cdelta.x * 0.005f;
m_pitch += cdelta.y * 0.003f;
m_yaw = vaMath::AngleWrap( m_yaw );
m_pitch = vaMath::Clamp( m_pitch, -(float)VA_PIf/2 + 1e-1f, +(float)VA_PIf/2 - 1e-1f );
m_roll = vaMath::AngleWrap( m_roll );
}
#if 1
{ // round yaw/pitch/roll slightly to avoid precision errors causing non-determinism when saving/loading cameras; 5 is precise enough for 10x zoom sniping (fov/10) but stepping could potential be seen with 100x
int precisionTrimDecimals = 5;
float k = vaMath::Pow( 10, (float)precisionTrimDecimals );
m_yaw = vaMath::Round(m_yaw * k)/k;
m_pitch = vaMath::Round(m_pitch * k)/k;
m_roll = vaMath::Round(m_roll * k)/k;
}
#endif
//
vaMatrix4x4 cameraWorld = vaMatrix4x4::FromYawPitchRoll( m_yaw, m_pitch, m_roll );
//
// Move
if( mouse.IsCaptured() || m_moveWhileNotCaptured )
{
///////////////////////////////////////////////////////////////////////////
// Update camera movement
bool hasInput = false;
if( hasFocus )
{
hasInput = keyboard.IsKeyDown( (vaKeyboardKeys)'W' ) || keyboard.IsKeyDown( (vaKeyboardKeys)'S' ) || keyboard.IsKeyDown( (vaKeyboardKeys)'A' ) ||
keyboard.IsKeyDown( (vaKeyboardKeys)'D' ) || keyboard.IsKeyDown( (vaKeyboardKeys)'Q' ) || keyboard.IsKeyDown( (vaKeyboardKeys)'E' );
}
m_movementSpeedAccelerationModifier = (hasInput)?(vaMath::Min(m_movementSpeedAccelerationModifier + deltaTime * 0.5f, 1.0f)):(0.0f);
float moveSpeed = m_movementSpeed * deltaTime * ( 0.3f + 0.7f * m_movementSpeedAccelerationModifier ) * speedBoost;
vaVector3 forward( cameraWorld.GetAxisX() );
vaVector3 right( cameraWorld.GetAxisY() );
vaVector3 up( cameraWorld.GetAxisZ() );
vaVector3 accumMove = m_accumMove;
if( hasFocus )
{
if( keyboard.IsKeyDown( (vaKeyboardKeys)'W' ) || keyboard.IsKeyDown( KK_UP ) ) accumMove += forward * moveSpeed;
if( keyboard.IsKeyDown( (vaKeyboardKeys)'S' ) || keyboard.IsKeyDown( KK_DOWN ) ) accumMove -= forward * moveSpeed;
if( keyboard.IsKeyDown( (vaKeyboardKeys)'D' ) || keyboard.IsKeyDown( KK_RIGHT ) ) accumMove += right * moveSpeed;
if( keyboard.IsKeyDown( (vaKeyboardKeys)'A' ) || keyboard.IsKeyDown( KK_LEFT ) ) accumMove -= right * moveSpeed;
if( keyboard.IsKeyDown( (vaKeyboardKeys)'Q' ) ) accumMove -= up * moveSpeed;
if( keyboard.IsKeyDown( (vaKeyboardKeys)'E' ) ) accumMove += up * moveSpeed;
}
objectPos += accumMove * smoothingLerpK;
m_accumMove = accumMove * (1-smoothingLerpK);
}
objectOri = vaQuaternion::FromRotationMatrix( m_baseOrientation * cameraWorld );
}
camera.SetPosition( objectPos );
camera.SetOrientation( objectOri );
}
vaCameraControllerFlythrough::vaCameraControllerFlythrough( )
{
m_currentTime = 0.0f;
m_totalTime = 0.0f;
//m_userParam0 = 0.0f;
//m_userParam1 = 0.0f;
m_playSpeed = 1.0f;
m_enableLoop = true;
m_fixedUp = false;
m_fixedUpVec = vaVector3( 0.0f, 0.0f, 1.0f );
}
vaCameraControllerFlythrough::~vaCameraControllerFlythrough( )
{
}
void vaCameraControllerFlythrough::CameraAttached( const shared_ptr<vaCameraBase> & camera )
{
vaCameraControllerBase::CameraAttached( camera );
}
bool vaCameraControllerFlythrough::FindKeys( float time, int & keyIndexFrom, int & keyIndexTo )
{
time = vaMath::Clamp( time, 0.0f, m_totalTime );
if( m_keys.size() == 0 )
return false;
keyIndexFrom = 0;
keyIndexTo = 0;
if( m_keys.size() == 1 )
return true;
// linear search - find binary in std:: algorithms when more perf needed :)
for( keyIndexTo = 1; keyIndexTo < m_keys.size(); keyIndexTo++ )
{
if( m_keys[keyIndexTo].Time >= time )
break;
}
keyIndexFrom = keyIndexTo-1;
return true;
}
void vaCameraControllerFlythrough::CameraTick( float deltaTime, vaCameraBase & camera, bool hasFocus )
{
hasFocus; //unreferenced
if( m_keys.size( ) == 0 )
return;
SetPlayTime( GetPlayTime() + deltaTime * GetPlaySpeed() );
int indexFrom, indexTo;
if( !FindKeys( GetPlayTime(), indexFrom, indexTo ) )
return;
Keyframe & keyFrom = m_keys[indexFrom];
Keyframe & keyTo = m_keys[indexTo];
float timeBetweenKeys = keyTo.Time - keyFrom.Time;
timeBetweenKeys = vaMath::Max( 0.00001f, timeBetweenKeys );
float lerpK = vaMath::Clamp( (m_currentTime-keyFrom.Time) / timeBetweenKeys, 0.0f, 1.0f );
//lerpK = vaMath::Smoothstep( lerpK );
vaVector3 pos = vaVector3::Lerp( keyFrom.Position, keyTo.Position, lerpK );
vaQuaternion rot = vaQuaternion::Slerp( keyFrom.Orientation, keyTo.Orientation, lerpK );
#if 1
int index0 = vaMath::Max( 0, indexFrom-1 );
int index1 = indexFrom;
int index2 = indexTo;
int index3 = vaMath::Min( (int)m_keys.size()-1, indexTo+1 );
Keyframe & key0 = m_keys[index0];
Keyframe & key1 = m_keys[index1];
Keyframe & key2 = m_keys[index2];
Keyframe & key3 = m_keys[index3];
pos = vaVector3::CatmullRom( key0.Position, key1.Position, key2.Position, key3.Position, lerpK );
//pos = vaVector3::Hermite( key1.Position, (key2.Position - key0.Position).Normalized(), key2.Position, (key3.Position - key1.Position).Normalized(), lerpK );
//rot = vaQuaternion::Squad( key0.Orientation, key1.Orientation, key2.Orientation, key3.Orientation, lerpK );
//rot = vaQuaternion::Slerp( key0.Orientation, key1.Orientation, lerpK );
rot = vaQuaternion::CatmullRom( key0.Orientation, key1.Orientation, key2.Orientation, key3.Orientation, lerpK );
#endif
if( m_fixedUp )
{
vaVector3 currentUp = rot.GetAxisY();
vaVector3 rotAxis = vaVector3::Cross( currentUp, m_fixedUpVec );
float rotAngle = vaVector3::AngleBetweenVectors( currentUp, m_fixedUpVec );
rot *= vaQuaternion::RotationAxis( rotAxis, rotAngle );
}
// float lf = vaMath::TimeIndependentLerpF( deltaTime, 5.0f / (currentKey.ShowTime+2.0f) );
//
// pos = vaMath::Lerp( camera.GetPosition(), pos, lf );
// rot = vaQuaternion::Slerp( camera.GetOrientation(), rot, lf );
camera.SetPosition( pos );
camera.SetOrientation( rot );
}
void vaCameraControllerFlythrough::AddKey( const Keyframe & newKey )
{
vector< Keyframe >::iterator it = std::lower_bound( m_keys.begin( ), m_keys.end( ), newKey, ( [ this ]( const Keyframe & a, const Keyframe & b ) { return a.Time < b.Time; } ) );
m_keys.insert( it, newKey ); // insert before iterator it
m_totalTime = m_keys.back().Time;
}
void vaCameraControllerFlythrough::UIPropertiesItemDraw( )
{
#ifdef VA_IMGUI_INTEGRATION_ENABLED
//ImGui::Text( "Time: %", m_currentTime );
ImGui::SliderFloat( "Playback position", &m_currentTime, 0.0f, m_totalTime );
m_currentTime = vaMath::Clamp( m_currentTime, 0.0f, m_totalTime );
ImGui::InputFloat( "Playback speed", &m_playSpeed, 0.2f );
m_playSpeed = vaMath::Clamp( m_playSpeed, -10.0f, 10.0f );
#endif
}