| 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 | |
| 9 | #include <osg/PolygonMode> |
| 10 | |
| 11 | using namespace osgXR; |
| 12 | |
| 13 | Mirror::Mirror(Manager *manager, osg::Camera *camera) : |
| 14 | _manager(manager), |
| 15 | _camera(camera), |
| 16 | _mirrorSettings(manager->_getSettings()->getMirrorSettings()) |
| 17 | { |
| 18 | } |
| 19 | |
| 20 | Mirror::~Mirror() |
| 21 | { |
| 22 | } |
| 23 | |
| 24 | void Mirror::_init() |
| 25 | { |
| 26 | _camera->setAllowEventFocus(false); |
| 27 | _camera->setViewMatrix(osg::Matrix::identity()); |
| 28 | _camera->setProjectionMatrix(osg::Matrix::ortho2D(0, 1, 0, 1)); |
| 29 | |
| 30 | // Find the mirror settings |
| 31 | MirrorSettings *mirrorSettings = &_mirrorSettings; |
| 32 | // but fall back to the manager's mirror settings |
| 33 | if (mirrorSettings->getMirrorMode() == MirrorSettings::MIRROR_AUTOMATIC) |
| 34 | mirrorSettings = &_manager->_getSettings()->getMirrorSettings(); |
| 35 | switch (mirrorSettings->getMirrorMode()) |
| 36 | { |
| 37 | case MirrorSettings::MIRROR_NONE: |
| 38 | // Draw nothing, but still clear the viewport |
| 39 | _camera->setClearMask(GL_COLOR_BUFFER_BIT); |
| 40 | break; |
| 41 | case MirrorSettings::MIRROR_AUTOMATIC: |
| 42 | // Fall-through: Default to MIRROR_SINGLE |
| 43 | case MirrorSettings::MIRROR_SINGLE: |
| 44 | { |
| 45 | int viewIndex = mirrorSettings->getMirrorViewIndex(); |
| 46 | if (viewIndex < 0) |
| 47 | viewIndex = 0; |
| 48 | setupQuad(viewIndex, 0.0f, 1.0f); |
| 49 | } |
| 50 | break; |
| 51 | case MirrorSettings::MIRROR_LEFT_RIGHT: |
| 52 | for (unsigned int viewIndex = 0; viewIndex < 2; ++viewIndex) |
| 53 | setupQuad(viewIndex, 0.5f * viewIndex, 0.5f); |
| 54 | break; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | namespace { |
| 59 | |
| 60 | class MirrorPreDrawCallback : public osg::Camera::DrawCallback |
| 61 | { |
| 62 | public: |
| 63 | |
| 64 | MirrorPreDrawCallback(osg::ref_ptr<XRState> xrState, |
| 65 | osg::ref_ptr<osg::StateSet> stateSet, |
| 66 | unsigned int viewIndex) : |
| 67 | _xrState(xrState), |
| 68 | _stateSet(stateSet), |
| 69 | _viewIndex(viewIndex) |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | void operator()(osg::RenderInfo& renderInfo) const override |
| 74 | { |
| 75 | const osg::FrameStamp *stamp = renderInfo.getState()->getFrameStamp(); |
| 76 | _stateSet->setTextureAttributeAndModes(0, |
| 77 | _xrState->getViewTexture(_viewIndex, stamp)); |
| 78 | } |
| 79 | |
| 80 | protected: |
| 81 | |
| 82 | osg::observer_ptr<XRState> _xrState; |
| 83 | osg::ref_ptr<osg::StateSet> _stateSet; |
| 84 | unsigned int _viewIndex; |
| 85 | }; |
| 86 | |
| 87 | class MirrorPostDrawCallback : public osg::Camera::DrawCallback |
| 88 | { |
| 89 | public: |
| 90 | |
| 91 | MirrorPostDrawCallback(osg::ref_ptr<osg::StateSet> stateSet) : |
| 92 | _stateSet(stateSet) |
| 93 | { |
| 94 | } |
| 95 | |
| 96 | void operator()(osg::RenderInfo& renderInfo) const override |
| 97 | { |
| 98 | _stateSet->removeTextureAttribute(0, osg::StateAttribute::Type::TEXTURE); |
| 99 | } |
| 100 | |
| 101 | protected: |
| 102 | |
| 103 | osg::ref_ptr<osg::StateSet> _stateSet; |
| 104 | }; |
| 105 | |
| 106 | } |
| 107 | |
| 108 | void Mirror::setupQuad(unsigned int viewIndex, |
| 109 | float x, float w) |
| 110 | { |
| 111 | XRState *xrState = _manager->_getXrState(); |
| 112 | |
| 113 | if (viewIndex >= xrState->getViewCount()) |
| 114 | return; |
| 115 | |
| 116 | // Build an always-visible quad to draw the view texture on |
| 117 | osg::ref_ptr<osg::Geode> quad = new osg::Geode; |
| 118 | quad->setCullingActive(false); |
| 119 | |
| 120 | XRState::TextureRect rect = xrState->getViewTextureRect(viewIndex); |
| 121 | quad->addDrawable(osg::createTexturedQuadGeometry( |
| 122 | osg::Vec3(x, 0.0f, 0.0f), |
| 123 | osg::Vec3(w, 0.0f, 0.0f), |
| 124 | osg::Vec3(0.0f, 1.0f, 0.0f), |
| 125 | rect.x, rect.y, |
| 126 | rect.x + rect.width, rect.y + rect.height)); |
| 127 | |
| 128 | osg::ref_ptr<osg::StateSet> state = quad->getOrCreateStateSet(); |
| 129 | int forceOff = osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED; |
| 130 | int forceOn = osg::StateAttribute::ON | osg::StateAttribute::PROTECTED; |
| 131 | state->setMode(GL_LIGHTING, forceOff); |
| 132 | state->setMode(GL_DEPTH_TEST, forceOff); |
| 133 | state->setMode(GL_FRAMEBUFFER_SRGB, forceOn); |
| 134 | |
| 135 | _camera->addChild(quad); |
| 136 | |
| 137 | // Set a callback so we can switch the texture to the active swapchain image |
| 138 | _camera->addPreDrawCallback(new MirrorPreDrawCallback(_manager->_getXrState(), |
| 139 | state, viewIndex)); |
| 140 | _camera->addPostDrawCallback(new MirrorPostDrawCallback(state)); |
| 141 | } |