| 1 | // SPDX-License-Identifier: LGPL-2.1-only |
| 2 | // Copyright (C) 2021 James Hogan <[email protected]> |
| 3 | |
| 4 | #include <osgXR/Manager> |
| 5 | #include <osgXR/Mirror> |
| 6 | |
| 7 | #include "XRState.h" |
| 8 | #include "XRRealizeOperation.h" |
| 9 | |
| 10 | using namespace osgXR; |
| 11 | |
| 12 | Manager::Manager() : |
| 13 | _settings(Settings::instance()), |
| 14 | _destroying(false), |
| 15 | _state(new XRState(_settings, const_cast<Manager *>(this))) |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | Manager::~Manager() |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | void Manager::setVisibilityMaskNodeMasks(osg::Node::NodeMask left, |
| 24 | osg::Node::NodeMask right) const |
| 25 | { |
| 26 | _state->setVisibilityMaskNodeMasks(left, right); |
| 27 | } |
| 28 | |
| 29 | void Manager::configure(osgViewer::View &view) const |
| 30 | { |
| 31 | osgViewer::ViewerBase *viewer = _viewer; |
| 32 | if (!viewer) |
| 33 | viewer = dynamic_cast<osgViewer::ViewerBase *>(&view); |
| 34 | if (!viewer) |
| 35 | return; |
| 36 | |
| 37 | _state->setViewer(viewer); |
| 38 | |
| 39 | // Its rather inconvenient that ViewConfig expects a const configure() |
| 40 | // Just cheat and cast away the constness here |
| 41 | osg::ref_ptr<XRRealizeOperation> realizeOp = new XRRealizeOperation(_state, &view); |
| 42 | viewer->setRealizeOperation(realizeOp); |
| 43 | if (viewer->isRealized()) |
| 44 | { |
| 45 | osgViewer::ViewerBase::Contexts contexts; |
| 46 | viewer->getContexts(contexts, true); |
| 47 | if (contexts.size() > 0) |
| 48 | (*realizeOp)(contexts[0]); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void Manager::update() |
| 53 | { |
| 54 | _state->update(); |
| 55 | } |
| 56 | |
| 57 | bool Manager::checkAndResetStateChanged() |
| 58 | { |
| 59 | return _state->checkAndResetStateChanged(); |
| 60 | } |
| 61 | |
| 62 | bool Manager::getPresent() const |
| 63 | { |
| 64 | return _state->getUpState() >= XRState::VRSTATE_SYSTEM; |
| 65 | } |
| 66 | |
| 67 | bool Manager::getEnabled() const |
| 68 | { |
| 69 | return _state->getUpState() == XRState::VRSTATE_ACTIONS; |
| 70 | } |
| 71 | |
| 72 | void Manager::setEnabled(bool enabled) |
| 73 | { |
| 74 | // Avoid needlessly discarding of the instance |
| 75 | // SteamVR 1.15 and 1.16 have issues with xrDestroySession() hanging |
| 76 | if (enabled) |
| 77 | { |
| 78 | _destroying = false; |
| 79 | _state->setProbing(true); |
| 80 | } |
| 81 | else if (_destroying) |
| 82 | { |
| 83 | _state->setProbing(false); |
| 84 | } |
| 85 | |
| 86 | _state->setDestState(enabled ? XRState::VRSTATE_ACTIONS |
| 87 | : _state->getProbingState()); |
| 88 | } |
| 89 | |
| 90 | void Manager::destroyAndWait() |
| 91 | { |
| 92 | _destroying = true; |
| 93 | setEnabled(false); |
| 94 | while (_state->isStateUpdateNeeded()) |
| 95 | _state->update(); |
| 96 | } |
| 97 | |
| 98 | bool Manager::isDestroying() const |
| 99 | { |
| 100 | return _destroying; |
| 101 | } |
| 102 | |
| 103 | bool Manager::isRunning() const |
| 104 | { |
| 105 | return _state->isRunning(); |
| 106 | } |
| 107 | |
| 108 | void Manager::syncSettings() |
| 109 | { |
| 110 | _state->syncSettings(); |
| 111 | } |
| 112 | |
| 113 | void Manager::syncActionSetup() |
| 114 | { |
| 115 | _state->syncActionSetup(); |
| 116 | } |
| 117 | |
| 118 | bool Manager::hasValidationLayer() const |
| 119 | { |
| 120 | return _state->hasValidationLayer(); |
| 121 | } |
| 122 | |
| 123 | bool Manager::hasDepthInfoExtension() const |
| 124 | { |
| 125 | return _state->hasDepthInfoExtension(); |
| 126 | } |
| 127 | |
| 128 | bool Manager::hasVisibilityMaskExtension() const |
| 129 | { |
| 130 | return _state->hasVisibilityMaskExtension(); |
| 131 | } |
| 132 | |
| 133 | const char *Manager::getRuntimeName() const |
| 134 | { |
| 135 | return _state->getRuntimeName(); |
| 136 | } |
| 137 | |
| 138 | const char *Manager::getSystemName() const |
| 139 | { |
| 140 | return _state->getSystemName(); |
| 141 | } |
| 142 | |
| 143 | const char *Manager::getStateString() const |
| 144 | { |
| 145 | return _state->getStateString(); |
| 146 | } |
| 147 | |
| 148 | void Manager::onRunning() |
| 149 | { |
| 150 | } |
| 151 | |
| 152 | void Manager::onStopped() |
| 153 | { |
| 154 | } |
| 155 | |
| 156 | void Manager::onFocus() |
| 157 | { |
| 158 | } |
| 159 | |
| 160 | void Manager::onUnfocus() |
| 161 | { |
| 162 | } |
| 163 | |
| 164 | void Manager::addMirror(Mirror *mirror) |
| 165 | { |
| 166 | if (!_state->valid()) |
| 167 | { |
| 168 | // handle this later, _state may not be created yet |
| 169 | _mirrorQueue.push_back(mirror); |
| 170 | } |
| 171 | else |
| 172 | { |
| 173 | // init the mirror right away |
| 174 | mirror->_init(); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void Manager::setupMirrorCamera(osg::Camera *camera) |
| 179 | { |
| 180 | addMirror(new Mirror(this, camera)); |
| 181 | } |
| 182 | |
| 183 | void Manager::_setupMirrors() |
| 184 | { |
| 185 | // init each mirror in the queue |
| 186 | while (!_mirrorQueue.empty()) |
| 187 | { |
| 188 | _mirrorQueue.front()->_init(); |
| 189 | _mirrorQueue.pop_front(); |
| 190 | } |
| 191 | } |