-
Notifications
You must be signed in to change notification settings - Fork 215
/
ControllerContainer.cpp
574 lines (499 loc) · 16.8 KB
/
ControllerContainer.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "ControllerContainer.h"
#include "Controller.h"
#include "Pointer.h"
#include "vrb/ConcreteClass.h"
#include "vrb/Color.h"
#include "vrb/CreationContext.h"
#include "vrb/Geometry.h"
#include "vrb/Group.h"
#include "vrb/Matrix.h"
#include "vrb/ModelLoaderAndroid.h"
#include "vrb/Program.h"
#include "vrb/ProgramFactory.h"
#include "vrb/RenderState.h"
#include "vrb/Toggle.h"
#include "vrb/Transform.h"
#include "vrb/Vector.h"
#include "vrb/VertexArray.h"
using namespace vrb;
namespace crow {
struct ControllerContainer::State {
std::vector<Controller> list;
CreationContextWeak context;
TogglePtr root;
GroupPtr pointerContainer;
std::vector<GroupPtr> models;
GeometryPtr beamModel;
bool visible = false;
vrb::Color pointerColor;
int gazeIndex = -1;
uint64_t immersiveFrameId;
uint64_t lastImmersiveFrameId;
void Initialize(vrb::CreationContextPtr& aContext) {
context = aContext;
root = Toggle::Create(aContext);
visible = true;
pointerColor = vrb::Color(1.0f, 1.0f, 1.0f, 1.0f);
immersiveFrameId = 0;
lastImmersiveFrameId = 0;
}
bool Contains(const int32_t aControllerIndex) {
return (aControllerIndex >= 0) && (aControllerIndex < list.size());
}
void SetUpModelsGroup(const int32_t aModelIndex) {
if (aModelIndex >= models.size()) {
models.resize((size_t)(aModelIndex + 1));
}
if (!models[aModelIndex]) {
CreationContextPtr create = context.lock();
models[aModelIndex] = std::move(Group::Create(create));
}
}
void updatePointerColor(Controller& aController) {
if (aController.beamParent && aController.beamParent->GetNodeCount() > 0) {
GeometryPtr geometry = std::dynamic_pointer_cast<vrb::Geometry>(aController.beamParent->GetNode(0));
if (geometry) {
geometry->GetRenderState()->SetMaterial(pointerColor, pointerColor, vrb::Color(0.0f, 0.0f, 0.0f), 0.0f);
}
}
if (aController.pointer) {
aController.pointer->SetPointerColor(pointerColor);
}
}
};
ControllerContainerPtr
ControllerContainer::Create(vrb::CreationContextPtr& aContext, const vrb::GroupPtr& aPointerContainer) {
auto result = std::make_shared<vrb::ConcreteClass<ControllerContainer, ControllerContainer::State> >(aContext);
result->m.pointerContainer = aPointerContainer;
return result;
}
TogglePtr
ControllerContainer::GetRoot() const {
return m.root;
}
void
ControllerContainer::LoadControllerModel(const int32_t aModelIndex, const ModelLoaderAndroidPtr& aLoader, const std::string& aFileName) {
m.SetUpModelsGroup(aModelIndex);
aLoader->LoadModel(aFileName, m.models[aModelIndex]);
}
void
ControllerContainer::InitializeBeam() {
if (m.beamModel) {
return;
}
CreationContextPtr create = m.context.lock();
VertexArrayPtr array = VertexArray::Create(create);
const float kLength = -1.0f;
const float kHeight = 0.002f;
array->AppendVertex(Vector(-kHeight, -kHeight, 0.0f)); // Bottom left
array->AppendVertex(Vector(kHeight, -kHeight, 0.0f)); // Bottom right
array->AppendVertex(Vector(kHeight, kHeight, 0.0f)); // Top right
array->AppendVertex(Vector(-kHeight, kHeight, 0.0f)); // Top left
array->AppendVertex(Vector(0.0f, 0.0f, kLength)); // Tip
array->AppendNormal(Vector(-1.0f, -1.0f, 0.0f).Normalize()); // Bottom left
array->AppendNormal(Vector(1.0f, -1.0f, 0.0f).Normalize()); // Bottom right
array->AppendNormal(Vector(1.0f, 1.0f, 0.0f).Normalize()); // Top right
array->AppendNormal(Vector(-1.0f, 1.0f, 0.0f).Normalize()); // Top left
array->AppendNormal(Vector(0.0f, 0.0f, -1.0f).Normalize()); // in to the screen
ProgramPtr program = create->GetProgramFactory()->CreateProgram(create, 0);
RenderStatePtr state = RenderState::Create(create);
state->SetProgram(program);
state->SetMaterial(Color(1.0f, 1.0f, 1.0f), Color(1.0f, 1.0f, 1.0f), Color(0.0f, 0.0f, 0.0f), 0.0f);
state->SetLightsEnabled(false);
GeometryPtr geometry = Geometry::Create(create);
geometry->SetVertexArray(array);
geometry->SetRenderState(state);
std::vector<int> index;
std::vector<int> uvIndex;
index.push_back(2);
index.push_back(1);
index.push_back(5);
geometry->AddFace(index, uvIndex, index);
index.clear();
index.push_back(3);
index.push_back(2);
index.push_back(5);
geometry->AddFace(index, uvIndex, index);
index.clear();
index.push_back(4);
index.push_back(3);
index.push_back(5);
geometry->AddFace(index, uvIndex, index);
index.clear();
index.push_back(1);
index.push_back(4);
index.push_back(5);
geometry->AddFace(index, uvIndex, index);
m.beamModel = std::move(geometry);
for (Controller& controller: m.list) {
if (controller.beamParent) {
controller.beamParent->AddNode(m.beamModel);
}
}
}
void
ControllerContainer::Reset() {
for (Controller& controller: m.list) {
controller.DetachRoot();
controller.Reset();
}
}
std::vector<Controller>&
ControllerContainer::GetControllers() {
return m.list;
}
const std::vector<Controller>&
ControllerContainer::GetControllers() const {
return m.list;
}
// crow::ControllerDelegate interface
uint32_t
ControllerContainer::GetControllerCount() {
return (uint32_t)m.list.size();
}
void
ControllerContainer::CreateController(const int32_t aControllerIndex, const int32_t aModelIndex, const std::string& aImmersiveName) {
CreateController(aControllerIndex, aModelIndex, aImmersiveName, vrb::Matrix::Identity());
}
void
ControllerContainer::CreateController(const int32_t aControllerIndex, const int32_t aModelIndex, const std::string& aImmersiveName, const vrb::Matrix& aBeamTransform) {
if ((size_t)aControllerIndex >= m.list.size()) {
m.list.resize((size_t)aControllerIndex + 1);
}
Controller& controller = m.list[aControllerIndex];
controller.DetachRoot();
controller.Reset();
controller.index = aControllerIndex;
controller.immersiveName = aImmersiveName;
controller.beamTransformMatrix = aBeamTransform;
controller.immersiveBeamTransform = aBeamTransform;
if (aModelIndex < 0) {
return;
}
m.SetUpModelsGroup(aModelIndex);
CreationContextPtr create = m.context.lock();
controller.transform = Transform::Create(create);
controller.pointer = Pointer::Create(create);
controller.pointer->SetVisible(true);
if (aControllerIndex != m.gazeIndex) {
if ((m.models.size() >= aModelIndex) && m.models[aModelIndex]) {
controller.transform->AddNode(m.models[aModelIndex]);
controller.beamToggle = vrb::Toggle::Create(create);
controller.beamToggle->ToggleAll(true);
if (aBeamTransform.IsIdentity()) {
controller.beamParent = controller.beamToggle;
} else {
vrb::TransformPtr beamTransform = Transform::Create(create);
beamTransform->SetTransform(aBeamTransform);
controller.beamParent = beamTransform;
controller.beamToggle->AddNode(beamTransform);
}
controller.transform->AddNode(controller.beamToggle);
if (m.beamModel && controller.beamParent) {
controller.beamParent->AddNode(m.beamModel);
}
} else {
VRB_ERROR("Failed to add controller model");
}
}
if (m.root) {
m.root->AddNode(controller.transform);
m.root->ToggleChild(*controller.transform, false);
}
if (m.pointerContainer) {
m.pointerContainer->AddNode(controller.pointer->GetRoot());
}
m.updatePointerColor(controller);
}
void
ControllerContainer::SetImmersiveBeamTransform(const int32_t aControllerIndex,
const vrb::Matrix& aImmersiveBeamTransform) {
if (!m.Contains(aControllerIndex)) {
return;
}
m.list[aControllerIndex].immersiveBeamTransform = aImmersiveBeamTransform;
}
void
ControllerContainer::SetFocused(const int32_t aControllerIndex) {
if (!m.Contains(aControllerIndex)) {
return;
}
for (Controller& controller: m.list) {
controller.focused = controller.index == aControllerIndex;
}
}
void
ControllerContainer::DestroyController(const int32_t aControllerIndex) {
if (m.Contains(aControllerIndex)) {
m.list[aControllerIndex].DetachRoot();
m.list[aControllerIndex].Reset();
}
}
void
ControllerContainer::SetCapabilityFlags(const int32_t aControllerIndex, const device::CapabilityFlags aFlags) {
if (!m.Contains(aControllerIndex)) {
return;
}
m.list[aControllerIndex].deviceCapabilities = aFlags;
}
void
ControllerContainer::SetEnabled(const int32_t aControllerIndex, const bool aEnabled) {
if (!m.Contains(aControllerIndex)) {
return;
}
m.list[aControllerIndex].enabled = aEnabled;
if (!aEnabled) {
m.list[aControllerIndex].focused = false;
SetVisible(aControllerIndex, false);
}
}
void
ControllerContainer::SetVisible(const int32_t aControllerIndex, const bool aVisible) {
if (!m.Contains(aControllerIndex)) {
return;
}
Controller& controller = m.list[aControllerIndex];
if (controller.transform && m.visible) {
m.root->ToggleChild(*controller.transform, aVisible);
}
if (controller.pointer && !aVisible) {
controller.pointer->SetVisible(false);
}
}
void
ControllerContainer::SetControllerType(const int32_t aControllerIndex, device::DeviceType aType) {
if (!m.Contains(aControllerIndex)) {
return;
}
Controller& controller = m.list[aControllerIndex];
controller.type = aType;
}
void
ControllerContainer::SetTargetRayMode(const int32_t aControllerIndex, device::TargetRayMode aMode) {
if (!m.Contains(aControllerIndex)) {
return;
}
Controller& controller = m.list[aControllerIndex];
controller.targetRayMode = aMode;
}
void
ControllerContainer::SetTransform(const int32_t aControllerIndex, const vrb::Matrix& aTransform) {
if (!m.Contains(aControllerIndex)) {
return;
}
Controller& controller = m.list[aControllerIndex];
controller.transformMatrix = aTransform;
if (controller.transform) {
controller.transform->SetTransform(aTransform);
}
}
void
ControllerContainer::SetButtonCount(const int32_t aControllerIndex, const uint32_t aNumButtons) {
if (!m.Contains(aControllerIndex)) {
return;
}
m.list[aControllerIndex].numButtons = aNumButtons;
}
void
ControllerContainer::SetButtonState(const int32_t aControllerIndex, const Button aWhichButton, const int32_t aImmersiveIndex, const bool aPressed, const bool aTouched, const float aImmersiveTrigger) {
assert(kControllerMaxButtonCount > aImmersiveIndex
&& "Button index must < kControllerMaxButtonCount.");
if (!m.Contains(aControllerIndex)) {
return;
}
const int32_t immersiveButtonMask = 1 << aImmersiveIndex;
if (aPressed) {
m.list[aControllerIndex].buttonState |= aWhichButton;
} else {
m.list[aControllerIndex].buttonState &= ~aWhichButton;
}
if (aImmersiveIndex >= 0) {
if (aPressed) {
m.list[aControllerIndex].immersivePressedState |= immersiveButtonMask;
} else {
m.list[aControllerIndex].immersivePressedState &= ~immersiveButtonMask;
}
if (aTouched) {
m.list[aControllerIndex].immersiveTouchedState |= immersiveButtonMask;
} else {
m.list[aControllerIndex].immersiveTouchedState &= ~immersiveButtonMask;
}
float trigger = aImmersiveTrigger;
if (trigger < 0.0f) {
trigger = aPressed ? 1.0f : 0.0f;
}
m.list[aControllerIndex].immersiveTriggerValues[aImmersiveIndex] = trigger;
}
}
void
ControllerContainer::SetAxes(const int32_t aControllerIndex, const float* aData, const uint32_t aLength) {
assert(kControllerMaxAxes >= aLength
&& "Axis length must <= kControllerMaxAxes.");
if (!m.Contains(aControllerIndex)) {
return;
}
m.list[aControllerIndex].numAxes = aLength;
for (int i = 0; i < aLength; ++i) {
m.list[aControllerIndex].immersiveAxes[i] = aData[i];
}
}
void
ControllerContainer::SetHapticCount(const int32_t aControllerIndex, const uint32_t aNumHaptics) {
if (!m.Contains(aControllerIndex)) {
return;
}
m.list[aControllerIndex].numHaptics = aNumHaptics;
}
uint32_t
ControllerContainer::GetHapticCount(const int32_t aControllerIndex) {
if (!m.Contains(aControllerIndex)) {
return 0;
}
return m.list[aControllerIndex].numHaptics;
}
void
ControllerContainer::SetHapticFeedback(const int32_t aControllerIndex, const uint64_t aInputFrameID,
const float aPulseDuration, const float aPulseIntensity) {
if (!m.Contains(aControllerIndex)) {
return;
}
m.list[aControllerIndex].inputFrameID = aInputFrameID;
m.list[aControllerIndex].pulseDuration = aPulseDuration;
m.list[aControllerIndex].pulseIntensity = aPulseIntensity;
}
void
ControllerContainer::GetHapticFeedback(const int32_t aControllerIndex, uint64_t & aInputFrameID,
float& aPulseDuration, float& aPulseIntensity) {
if (!m.Contains(aControllerIndex)) {
return;
}
aInputFrameID = m.list[aControllerIndex].inputFrameID;
aPulseDuration = m.list[aControllerIndex].pulseDuration;
aPulseIntensity = m.list[aControllerIndex].pulseIntensity;
}
void
ControllerContainer::SetSelectActionStart(const int32_t aControllerIndex) {
if (!m.Contains(aControllerIndex) || !m.immersiveFrameId) {
return;
}
if (m.list[aControllerIndex].selectActionStopFrameId >=
m.list[aControllerIndex].selectActionStartFrameId) {
m.list[aControllerIndex].selectActionStartFrameId = m.immersiveFrameId;
}
}
void
ControllerContainer::SetSelectActionStop(const int32_t aControllerIndex) {
if (!m.Contains(aControllerIndex) || !m.lastImmersiveFrameId) {
return;
}
if (m.list[aControllerIndex].selectActionStartFrameId >
m.list[aControllerIndex].selectActionStopFrameId) {
m.list[aControllerIndex].selectActionStopFrameId = m.lastImmersiveFrameId;
}
}
void
ControllerContainer::SetSqueezeActionStart(const int32_t aControllerIndex) {
if (!m.Contains(aControllerIndex) || !m.immersiveFrameId) {
return;
}
if (m.list[aControllerIndex].squeezeActionStopFrameId >=
m.list[aControllerIndex].squeezeActionStartFrameId) {
m.list[aControllerIndex].squeezeActionStartFrameId = m.immersiveFrameId;
}
}
void
ControllerContainer::SetSqueezeActionStop(const int32_t aControllerIndex) {
if (!m.Contains(aControllerIndex) || !m.lastImmersiveFrameId) {
return;
}
if (m.list[aControllerIndex].squeezeActionStartFrameId >
m.list[aControllerIndex].squeezeActionStopFrameId) {
m.list[aControllerIndex].squeezeActionStopFrameId = m.lastImmersiveFrameId;
}
}
void
ControllerContainer::SetLeftHanded(const int32_t aControllerIndex, const bool aLeftHanded) {
if (!m.Contains(aControllerIndex)) {
return;
}
m.list[aControllerIndex].leftHanded = aLeftHanded;
}
void
ControllerContainer::SetTouchPosition(const int32_t aControllerIndex, const float aTouchX, const float aTouchY) {
if (!m.Contains(aControllerIndex)) {
return;
}
Controller& controller = m.list[aControllerIndex];
controller.touched = true;
controller.touchX = aTouchX;
controller.touchY = aTouchY;
}
void
ControllerContainer::EndTouch(const int32_t aControllerIndex) {
if (!m.Contains(aControllerIndex)) {
return;
}
m.list[aControllerIndex].touched = false;
}
void
ControllerContainer::SetScrolledDelta(const int32_t aControllerIndex, const float aScrollDeltaX, const float aScrollDeltaY) {
if (!m.Contains(aControllerIndex)) {
return;
}
Controller& controller = m.list[aControllerIndex];
controller.scrollDeltaX = aScrollDeltaX;
controller.scrollDeltaY = aScrollDeltaY;
}
void ControllerContainer::SetPointerColor(const vrb::Color& aColor) const {
m.pointerColor = aColor;
for (Controller& controller: m.list) {
m.updatePointerColor(controller);
}
}
void
ControllerContainer::SetVisible(const bool aVisible) {
VRB_LOG("[ControllerContainer] SetVisible %d", aVisible)
if (m.visible == aVisible) {
return;
}
m.visible = aVisible;
if (aVisible) {
for (int i = 0; i < m.list.size(); ++i) {
if (m.list[i].enabled) {
m.root->ToggleChild(*m.list[i].transform, true);
}
}
} else {
m.root->ToggleAll(false);
}
}
void ControllerContainer::SetGazeModeIndex(const int32_t aControllerIndex) {
m.gazeIndex = aControllerIndex;
}
void
ControllerContainer::SetFrameId(const uint64_t aFrameId) {
if (m.immersiveFrameId) {
m.lastImmersiveFrameId = aFrameId ? aFrameId : m.immersiveFrameId;
} else {
m.lastImmersiveFrameId = 0;
for (Controller& controller: m.list) {
controller.selectActionStartFrameId = controller.selectActionStopFrameId = 0;
controller.squeezeActionStartFrameId = controller.squeezeActionStopFrameId = 0;
}
}
m.immersiveFrameId = aFrameId;
}
ControllerContainer::ControllerContainer(State& aState, vrb::CreationContextPtr& aContext) : m(aState) {
m.Initialize(aContext);
}
ControllerContainer::~ControllerContainer() {
if (m.root) {
m.root->RemoveFromParents();
m.root = nullptr;
}
}
} // namespace crow