Split view into worldview and menu, for ease of layer management. Basic pose management.

pull/615/head
Mads Buvik Sandvei 5 years ago
parent 2778775070
commit 2accdc4441

@ -239,6 +239,9 @@ if (WIN32)
# Get rid of useless crud from windows.h
add_definitions(-DNOMINMAX -DWIN32_LEAN_AND_MEAN)
# Get rid of the nonsense warning "enum type is unscoped"
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd26812")
endif()
if (NOT WIN32 AND BUILD_WIZARD) # windows users can just run the morrowind installer

@ -114,18 +114,26 @@ endif ()
if(BUILD_VR_OPENXR)
set(OPENMW_VR_FILES
engine_vr.cpp
mwvr/openxrinputmanager.hpp
mwvr/openxrinputmanager.cpp
mwvr/openxrlayer.hpp
mwvr/openxrlayer.cpp
mwvr/openxrmanager.hpp
mwvr/openxrmanager.cpp
mwvr/openxrviewer.hpp
mwvr/openxrviewer.cpp
mwvr/openxrmanagerimpl.hpp
mwvr/openxrmanagerimpl.cpp
mwvr/openxrview.hpp
mwvr/openxrview.cpp
mwvr/openxrinputmanager.hpp
mwvr/openxrinputmanager.cpp
mwvr/openxrmenu.hpp
mwvr/openxrmenu.cpp
mwvr/openxrswapchain.hpp
mwvr/openxrswapchain.cpp
mwvr/openxrtexture.hpp
mwvr/openxrtexture.cpp
mwvr/openxrview.hpp
mwvr/openxrview.cpp
mwvr/openxrviewer.hpp
mwvr/openxrviewer.cpp
mwvr/openxrworldview.hpp
mwvr/openxrworldview.cpp
)
openmw_add_executable(openmw_vr

@ -692,7 +692,6 @@ void OMW::Engine::go()
// Setup viewer
mViewer = new osgViewer::Viewer;
mViewer->setReleaseContextAtEndOfFrameHint(false);
mViewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
#if OSG_VERSION_GREATER_OR_EQUAL(3,5,5)
// Do not try to outsmart the OS thread scheduler (see bug #4785).
@ -727,6 +726,20 @@ void OMW::Engine::go()
osg::ref_ptr<Resource::StatsHandler> resourceshandler = new Resource::StatsHandler;
mViewer->addEventHandler(resourceshandler);
#ifdef USE_OPENXR
auto* root = mViewer->getSceneData();
mXRViewer->addChild(root);
mViewer->setSceneData(mXRViewer);
#ifndef _NDEBUG
mXR->addPoseUpdateCallback(new MWVR::PoseLogger(MWVR::TrackedLimb::HEAD, MWVR::TrackedSpace::STAGE));
mXR->addPoseUpdateCallback(new MWVR::PoseLogger(MWVR::TrackedLimb::HEAD, MWVR::TrackedSpace::VIEW));
mXR->addPoseUpdateCallback(new MWVR::PoseLogger(MWVR::TrackedLimb::LEFT_HAND, MWVR::TrackedSpace::STAGE));
mXR->addPoseUpdateCallback(new MWVR::PoseLogger(MWVR::TrackedLimb::LEFT_HAND, MWVR::TrackedSpace::VIEW));
mXR->addPoseUpdateCallback(new MWVR::PoseLogger(MWVR::TrackedLimb::RIGHT_HAND, MWVR::TrackedSpace::STAGE));
mXR->addPoseUpdateCallback(new MWVR::PoseLogger(MWVR::TrackedLimb::RIGHT_HAND, MWVR::TrackedSpace::VIEW));
#endif
#endif
// Start the game
if (!mSaveGameFile.empty())
{
@ -752,11 +765,6 @@ void OMW::Engine::go()
}
#ifdef USE_OPENXR
auto* root = mViewer->getSceneData();
mXRViewer->addChild(root);
mViewer->setSceneData(mXRViewer);
#endif
// Start the main rendering loop
osg::Timer frameTimer;
@ -776,12 +784,18 @@ void OMW::Engine::go()
}
else
{
mViewer->eventTraversal();
mViewer->updateTraversal();
mEnvironment.getWorld()->updateWindowManager();
#ifdef USE_OPENXR
mXRViewer->traversals();
#else
mViewer->updateTraversal();
mViewer->renderingTraversals();
#endif
bool guiActive = mEnvironment.getWindowManager()->isGuiMode();
if (!guiActive)

@ -11,10 +11,6 @@ void OMW::Engine::initVr()
throw std::logic_error("mViewer must be initialized before calling initVr()");
mXR = new MWVR::OpenXRManager();
osg::ref_ptr<MWVR::OpenXRManager::RealizeOperation> realizeOperation = new MWVR::OpenXRManager::RealizeOperation(mXR);
mViewer->setRealizeOperation(realizeOperation);
mXRViewer = new MWVR::OpenXRViewer(mXR, realizeOperation, mViewer, 1.f);
mXRViewer = new MWVR::OpenXRViewer(mXR, mViewer, 1.f);
// Viewers must be the top node of the scene.
//mViewer->setSceneData(mXRViewer);
}

@ -0,0 +1,854 @@
#include "openxrengine.hpp"
#include <iomanip>
#include <boost/filesystem/fstream.hpp>
#include <osgViewer/ViewerEventHandlers>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <SDL.h>
#include <components/debug/debuglog.hpp>
#include <components/misc/rng.hpp>
#include <components/vfs/manager.hpp>
#include <components/vfs/registerarchives.hpp>
#include <components/sdlutil/sdlgraphicswindow.hpp>
#include <components/sdlutil/imagetosurface.hpp>
#include <components/resource/resourcesystem.hpp>
#include <components/resource/scenemanager.hpp>
#include <components/resource/stats.hpp>
#include <components/compiler/extensions0.hpp>
#include <components/sceneutil/workqueue.hpp>
#include <components/files/configurationmanager.hpp>
#include <components/version/version.hpp>
#include <components/detournavigator/navigator.hpp>
#include "../mwinput/inputmanagerimp.hpp"
#include "../mwgui/windowmanagerimp.hpp"
#include "../mwscript/scriptmanagerimp.hpp"
#include "../mwscript/interpretercontext.hpp"
#include "../mwsound/soundmanagerimp.hpp"
#include "../mwworld/class.hpp"
#include "../mwworld/player.hpp"
#include "../mwworld/worldimp.hpp"
#include "../mwrender/vismask.hpp"
#include "../mwclass/classes.hpp"
#include "../mwdialogue/dialoguemanagerimp.hpp"
#include "../mwdialogue/journalimp.hpp"
#include "../mwdialogue/scripttest.hpp"
#include "../mwmechanics/mechanicsmanagerimp.hpp"
#include "../mwstate/statemanagerimp.hpp"
namespace
{
void checkSDLError(int ret)
{
if (ret != 0)
Log(Debug::Error) << "SDL error: " << SDL_GetError();
}
}
void MWVR::OpenXREngine::executeLocalScripts()
{
MWWorld::LocalScripts& localScripts = mEnvironment.getWorld()->getLocalScripts();
localScripts.startIteration();
std::pair<std::string, MWWorld::Ptr> script;
while (localScripts.getNext(script))
{
MWScript::InterpreterContext interpreterContext (
&script.second.getRefData().getLocals(), script.second);
mEnvironment.getScriptManager()->run (script.first, interpreterContext);
}
}
bool MWVR::OpenXREngine::frame(float frametime)
{
try
{
mStartTick = mViewer->getStartTick();
mEnvironment.setFrameDuration(frametime);
// update input
mEnvironment.getInputManager()->update(frametime, false);
// When the window is minimized, pause the game. Currently this *has* to be here to work around a MyGUI bug.
// If we are not currently rendering, then RenderItems will not be reused resulting in a memory leak upon changing widget textures (fixed in MyGUI 3.3.2),
// and destroyed widgets will not be deleted (not fixed yet, https://github.com/MyGUI/mygui/issues/21)
if (!mEnvironment.getInputManager()->isWindowVisible())
return false;
// sound
if (mUseSound)
mEnvironment.getSoundManager()->update(frametime);
// Main menu opened? Then scripts are also paused.
bool paused = mEnvironment.getWindowManager()->containsMode(MWGui::GM_MainMenu);
// update game state
mEnvironment.getStateManager()->update (frametime);
bool guiActive = mEnvironment.getWindowManager()->isGuiMode();
osg::Timer_t beforeScriptTick = osg::Timer::instance()->tick();
if (mEnvironment.getStateManager()->getState()!=
MWBase::StateManager::State_NoGame)
{
if (!paused)
{
if (mEnvironment.getWorld()->getScriptsEnabled())
{
// local scripts
executeLocalScripts();
// global scripts
mEnvironment.getScriptManager()->getGlobalScripts().run();
}
mEnvironment.getWorld()->markCellAsUnchanged();
}
if (!guiActive)
{
double hours = (frametime * mEnvironment.getWorld()->getTimeScaleFactor()) / 3600.0;
mEnvironment.getWorld()->advanceTime(hours, true);
mEnvironment.getWorld()->rechargeItems(frametime, true);
}
}
osg::Timer_t afterScriptTick = osg::Timer::instance()->tick();
// update actors
osg::Timer_t beforeMechanicsTick = osg::Timer::instance()->tick();
if (mEnvironment.getStateManager()->getState()!=
MWBase::StateManager::State_NoGame)
{
mEnvironment.getMechanicsManager()->update(frametime,
guiActive);
}
osg::Timer_t afterMechanicsTick = osg::Timer::instance()->tick();
if (mEnvironment.getStateManager()->getState()==
MWBase::StateManager::State_Running)
{
MWWorld::Ptr player = mEnvironment.getWorld()->getPlayerPtr();
if(!guiActive && player.getClass().getCreatureStats(player).isDead())
mEnvironment.getStateManager()->endGame();
}
// update physics
osg::Timer_t beforePhysicsTick = osg::Timer::instance()->tick();
if (mEnvironment.getStateManager()->getState()!=
MWBase::StateManager::State_NoGame)
{
mEnvironment.getWorld()->updatePhysics(frametime, guiActive);
}
osg::Timer_t afterPhysicsTick = osg::Timer::instance()->tick();
// update world
osg::Timer_t beforeWorldTick = osg::Timer::instance()->tick();
if (mEnvironment.getStateManager()->getState()!=
MWBase::StateManager::State_NoGame)
{
mEnvironment.getWorld()->update(frametime, guiActive);
}
osg::Timer_t afterWorldTick = osg::Timer::instance()->tick();
// update GUI
mEnvironment.getWindowManager()->onFrame(frametime);
unsigned int frameNumber = mViewer->getFrameStamp()->getFrameNumber();
osg::Stats* stats = mViewer->getViewerStats();
stats->setAttribute(frameNumber, "script_time_begin", osg::Timer::instance()->delta_s(mStartTick, beforeScriptTick));
stats->setAttribute(frameNumber, "script_time_taken", osg::Timer::instance()->delta_s(beforeScriptTick, afterScriptTick));
stats->setAttribute(frameNumber, "script_time_end", osg::Timer::instance()->delta_s(mStartTick, afterScriptTick));
stats->setAttribute(frameNumber, "mechanics_time_begin", osg::Timer::instance()->delta_s(mStartTick, beforeMechanicsTick));
stats->setAttribute(frameNumber, "mechanics_time_taken", osg::Timer::instance()->delta_s(beforeMechanicsTick, afterMechanicsTick));
stats->setAttribute(frameNumber, "mechanics_time_end", osg::Timer::instance()->delta_s(mStartTick, afterMechanicsTick));
stats->setAttribute(frameNumber, "physics_time_begin", osg::Timer::instance()->delta_s(mStartTick, beforePhysicsTick));
stats->setAttribute(frameNumber, "physics_time_taken", osg::Timer::instance()->delta_s(beforePhysicsTick, afterPhysicsTick));
stats->setAttribute(frameNumber, "physics_time_end", osg::Timer::instance()->delta_s(mStartTick, afterPhysicsTick));
stats->setAttribute(frameNumber, "world_time_begin", osg::Timer::instance()->delta_s(mStartTick, beforeWorldTick));
stats->setAttribute(frameNumber, "world_time_taken", osg::Timer::instance()->delta_s(beforeWorldTick, afterWorldTick));
stats->setAttribute(frameNumber, "world_time_end", osg::Timer::instance()->delta_s(mStartTick, afterWorldTick));
if (stats->collectStats("resource"))
{
mResourceSystem->reportStats(frameNumber, stats);
stats->setAttribute(frameNumber, "WorkQueue", mWorkQueue->getNumItems());
stats->setAttribute(frameNumber, "WorkThread", mWorkQueue->getNumActiveThreads());
mEnvironment.getWorld()->getNavigator()->reportStats(frameNumber, *stats);
}
}
catch (const std::exception& e)
{
Log(Debug::Error) << "Error in frame: " << e.what();
}
return true;
}
MWVR::OpenXREngine::OpenXREngine(Files::ConfigurationManager& configurationManager)
: mWindow(nullptr)
, mEncoding(ToUTF8::WINDOWS_1252)
, mEncoder(nullptr)
, mScreenCaptureOperation(nullptr)
, mSkipMenu (false)
, mUseSound (true)
, mCompileAll (false)
, mCompileAllDialogue (false)
, mWarningsMode (1)
, mScriptConsoleMode (false)
, mActivationDistanceOverride(-1)
, mGrab(true)
, mExportFonts(false)
, mRandomSeed(0)
, mScriptContext (0)
, mFSStrict (false)
, mScriptBlacklistUse (true)
, mNewGame (false)
, mCfgMgr(configurationManager)
{
MWClass::registerClasses();
Uint32 flags = SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE|SDL_INIT_GAMECONTROLLER|SDL_INIT_JOYSTICK;
if(SDL_WasInit(flags) == 0)
{
SDL_SetMainReady();
if(SDL_Init(flags) != 0)
{
throw std::runtime_error("Could not initialize SDL! " + std::string(SDL_GetError()));
}
}
mStartTick = osg::Timer::instance()->tick();
}
MWVR::OpenXREngine::~OpenXREngine()
{
mEnvironment.cleanup();
delete mScriptContext;
mScriptContext = nullptr;
mWorkQueue = nullptr;
mViewer = nullptr;
mResourceSystem.reset();
delete mEncoder;
mEncoder = nullptr;
mOpenXRManager = nullptr;
if (mWindow)
{
SDL_DestroyWindow(mWindow);
mWindow = nullptr;
}
SDL_Quit();
}
void MWVR::OpenXREngine::enableFSStrict(bool fsStrict)
{
mFSStrict = fsStrict;
}
// Set data dir
void MWVR::OpenXREngine::setDataDirs (const Files::PathContainer& dataDirs)
{
mDataDirs = dataDirs;
mDataDirs.insert(mDataDirs.begin(), (mResDir / "vfs"));
mFileCollections = Files::Collections (mDataDirs, !mFSStrict);
}
// Add BSA archive
void MWVR::OpenXREngine::addArchive (const std::string& archive) {
mArchives.push_back(archive);
}
// Set resource dir
void MWVR::OpenXREngine::setResourceDir (const boost::filesystem::path& parResDir)
{
mResDir = parResDir;
}
// Set start cell name
void MWVR::OpenXREngine::setCell (const std::string& cellName)
{
mCellName = cellName;
}
void MWVR::OpenXREngine::addContentFile(const std::string& file)
{
mContentFiles.push_back(file);
}
void MWVR::OpenXREngine::setSkipMenu (bool skipMenu, bool newGame)
{
mSkipMenu = skipMenu;
mNewGame = newGame;
}
std::string MWVR::OpenXREngine::loadSettings (Settings::Manager & settings)
{
// Create the settings manager and load default settings file
const std::string localdefault = (mCfgMgr.getLocalPath() / "settings-default.cfg").string();
const std::string globaldefault = (mCfgMgr.getGlobalPath() / "settings-default.cfg").string();
// prefer local
if (boost::filesystem::exists(localdefault))
settings.loadDefault(localdefault);
else if (boost::filesystem::exists(globaldefault))
settings.loadDefault(globaldefault);
else
throw std::runtime_error ("No default settings file found! Make sure the file \"settings-default.cfg\" was properly installed.");
// load user settings if they exist
const std::string settingspath = (mCfgMgr.getUserConfigPath() / "settings.cfg").string();
if (boost::filesystem::exists(settingspath))
settings.loadUser(settingspath);
return settingspath;
}
void MWVR::OpenXREngine::createWindow(Settings::Manager& settings)
{
int screen = settings.getInt("screen", "Video");
int width = settings.getInt("resolution x", "Video");
int height = settings.getInt("resolution y", "Video");
bool fullscreen = settings.getBool("fullscreen", "Video");
bool windowBorder = settings.getBool("window border", "Video");
bool vsync = settings.getBool("vsync", "Video");
int antialiasing = settings.getInt("antialiasing", "Video");
int pos_x = SDL_WINDOWPOS_CENTERED_DISPLAY(screen),
pos_y = SDL_WINDOWPOS_CENTERED_DISPLAY(screen);
if(fullscreen)
{
pos_x = SDL_WINDOWPOS_UNDEFINED_DISPLAY(screen);
pos_y = SDL_WINDOWPOS_UNDEFINED_DISPLAY(screen);
}
Uint32 flags = SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN|SDL_WINDOW_RESIZABLE;
if(fullscreen)
flags |= SDL_WINDOW_FULLSCREEN;
if (!windowBorder)
flags |= SDL_WINDOW_BORDERLESS;
SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS,
settings.getBool("minimize on focus loss", "Video") ? "1" : "0");
checkSDLError(SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8));
checkSDLError(SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8));
checkSDLError(SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8));
checkSDLError(SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0));
checkSDLError(SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24));
if (antialiasing > 0)
{
checkSDLError(SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1));
checkSDLError(SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, antialiasing));
}
while (!mWindow)
{
mWindow = SDL_CreateWindow("OpenMW", pos_x, pos_y, width, height, flags);
if (!mWindow)
{
// Try with a lower AA
if (antialiasing > 0)
{
Log(Debug::Warning) << "Warning: " << antialiasing << "x antialiasing not supported, trying " << antialiasing/2;
antialiasing /= 2;
Settings::Manager::setInt("antialiasing", "Video", antialiasing);
checkSDLError(SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, antialiasing));
continue;
}
else
{
std::stringstream error;
error << "Failed to create SDL window: " << SDL_GetError();
throw std::runtime_error(error.str());
}
}
}
setWindowIcon();
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
SDL_GetWindowPosition(mWindow, &traits->x, &traits->y);
SDL_GetWindowSize(mWindow, &traits->width, &traits->height);
traits->windowName = SDL_GetWindowTitle(mWindow);
traits->windowDecoration = !(SDL_GetWindowFlags(mWindow)&SDL_WINDOW_BORDERLESS);
traits->screenNum = SDL_GetWindowDisplayIndex(mWindow);
// We tried to get rid of the hardcoding but failed: https://github.com/OpenMW/openmw/pull/1771
// Here goes kcat's quote:
// It's ultimately a chicken and egg problem, and the reason why the code is like it was in the first place.
// It needs a context to get the current attributes, but it needs the attributes to set up the context.
// So it just specifies the same values that were given to SDL in the hopes that it's good enough to what the window eventually gets.
traits->red = 8;
traits->green = 8;
traits->blue = 8;
traits->alpha = 0; // set to 0 to stop ScreenCaptureHandler reading the alpha channel
traits->depth = 24;
traits->stencil = 8;
traits->vsync = vsync;
traits->doubleBuffer = true;
traits->inheritedWindowData = new SDLUtil::GraphicsWindowSDL2::WindowData(mWindow);
osg::ref_ptr<SDLUtil::GraphicsWindowSDL2> graphicsWindow = new SDLUtil::GraphicsWindowSDL2(traits);
if(!graphicsWindow->valid()) throw std::runtime_error("Failed to create GraphicsContext");
mOpenXRManager = std::make_unique<OpenXRManager>(graphicsWindow);
osg::ref_ptr<osg::Camera> camera = mViewer->getCamera();
camera->setGraphicsContext(graphicsWindow);
camera->setViewport(0, 0, traits->width, traits->height);
mViewer->realize();
mViewer->getEventQueue()->getCurrentEventState()->setWindowRectangle(0, 0, traits->width, traits->height);
}
void MWVR::OpenXREngine::setWindowIcon()
{
boost::filesystem::ifstream windowIconStream;
std::string windowIcon = (mResDir / "mygui" / "openmw.png").string();
windowIconStream.open(windowIcon, std::ios_base::in | std::ios_base::binary);
if (windowIconStream.fail())
Log(Debug::Error) << "Error: Failed to open " << windowIcon;
osgDB::ReaderWriter* reader = osgDB::Registry::instance()->getReaderWriterForExtension("png");
if (!reader)
{
Log(Debug::Error) << "Error: Failed to read window icon, no png readerwriter found";
return;
}
osgDB::ReaderWriter::ReadResult result = reader->readImage(windowIconStream);
if (!result.success())
Log(Debug::Error) << "Error: Failed to read " << windowIcon << ": " << result.message() << " code " << result.status();
else
{
osg::ref_ptr<osg::Image> image = result.getImage();
auto surface = SDLUtil::imageToSurface(image, true);
SDL_SetWindowIcon(mWindow, surface.get());
}
}
void MWVR::OpenXREngine::prepareEngine (Settings::Manager & settings)
{
mEnvironment.setStateManager (
new MWState::StateManager (mCfgMgr.getUserDataPath() / "saves", mContentFiles.at (0)));
createWindow(settings);
osg::ref_ptr<osg::Group> rootNode (new osg::Group);
mViewer->setSceneData(rootNode);
mVFS.reset(new VFS::Manager(mFSStrict));
VFS::registerArchives(mVFS.get(), mFileCollections, mArchives, true);
mResourceSystem.reset(new Resource::ResourceSystem(mVFS.get()));
mResourceSystem->getSceneManager()->setUnRefImageDataAfterApply(false); // keep to Off for now to allow better state sharing
mResourceSystem->getSceneManager()->setFilterSettings(
Settings::Manager::getString("texture mag filter", "General"),
Settings::Manager::getString("texture min filter", "General"),
Settings::Manager::getString("texture mipmap", "General"),
Settings::Manager::getInt("anisotropy", "General")
);
int numThreads = Settings::Manager::getInt("preload num threads", "Cells");
if (numThreads <= 0)
throw std::runtime_error("Invalid setting: 'preload num threads' must be >0");
mWorkQueue = new SceneUtil::WorkQueue(numThreads);
// Create input and UI first to set up a bootstrapping environment for
// showing a loading screen and keeping the window responsive while doing so
std::string keybinderUser = (mCfgMgr.getUserConfigPath() / "input_v3.xml").string();
bool keybinderUserExists = boost::filesystem::exists(keybinderUser);
if(!keybinderUserExists)
{
std::string input2 = (mCfgMgr.getUserConfigPath() / "input_v2.xml").string();
if(boost::filesystem::exists(input2)) {
boost::filesystem::copy_file(input2, keybinderUser);
keybinderUserExists = boost::filesystem::exists(keybinderUser);
Log(Debug::Info) << "Loading keybindings file: " << keybinderUser;
}
}
else
Log(Debug::Info) << "Loading keybindings file: " << keybinderUser;
// find correct path to the game controller bindings
// File format for controller mappings is different for SDL <= 2.0.4, 2.0.5, and >= 2.0.6
SDL_version linkedSdlVersion;
SDL_GetVersion(&linkedSdlVersion);
std::string controllerFileName;
if (linkedSdlVersion.major == 2 && linkedSdlVersion.minor == 0 && linkedSdlVersion.patch <= 4) {
controllerFileName = "gamecontrollerdb_204.txt";
} else if (linkedSdlVersion.major == 2 && linkedSdlVersion.minor == 0 && linkedSdlVersion.patch == 5) {
controllerFileName = "gamecontrollerdb_205.txt";
} else {
controllerFileName = "gamecontrollerdb.txt";
}
const std::string userdefault = mCfgMgr.getUserConfigPath().string() + "/" + controllerFileName;
const std::string localdefault = mCfgMgr.getLocalPath().string() + "/" + controllerFileName;
const std::string globaldefault = mCfgMgr.getGlobalPath().string() + "/" + controllerFileName;
std::string userGameControllerdb;
if (boost::filesystem::exists(userdefault)){
userGameControllerdb = userdefault;
}
else
userGameControllerdb = "";
std::string gameControllerdb;
if (boost::filesystem::exists(localdefault))
gameControllerdb = localdefault;
else if (boost::filesystem::exists(globaldefault))
gameControllerdb = globaldefault;
else
gameControllerdb = ""; //if it doesn't exist, pass in an empty string
MWInput::InputManager* input = new MWInput::InputManager (mWindow, mViewer, mScreenCaptureHandler, mScreenCaptureOperation, keybinderUser, keybinderUserExists, userGameControllerdb, gameControllerdb, mGrab);
mEnvironment.setInputManager (input);
std::string myguiResources = (mResDir / "mygui").string();
osg::ref_ptr<osg::Group> guiRoot = new osg::Group;
guiRoot->setName("GUI Root");
guiRoot->setNodeMask(MWRender::Mask_GUI);
rootNode->addChild(guiRoot);
MWGui::WindowManager* window = new MWGui::WindowManager(mViewer, guiRoot, mResourceSystem.get(), mWorkQueue.get(),
mCfgMgr.getLogPath().string() + std::string("/"), myguiResources,
mScriptConsoleMode, mTranslationDataStorage, mEncoding, mExportFonts,
Version::getOpenmwVersionDescription(mResDir.string()), mCfgMgr.getUserConfigPath().string());
mEnvironment.setWindowManager (window);
// Create sound system
mEnvironment.setSoundManager (new MWSound::SoundManager(mVFS.get(), mUseSound));
if (!mSkipMenu)
{
const std::string& logo = Fallback::Map::getString("Movies_Company_Logo");
if (!logo.empty())
window->playVideo(logo, true);
}
// Create the world
mEnvironment.setWorld( new MWWorld::World (mViewer, rootNode, mResourceSystem.get(), mWorkQueue.get(),
mFileCollections, mContentFiles, mEncoder, mActivationDistanceOverride, mCellName,
mStartupScript, mResDir.string(), mCfgMgr.getUserDataPath().string()));
mEnvironment.getWorld()->setupPlayer();
input->setPlayer(&mEnvironment.getWorld()->getPlayer());
window->setStore(mEnvironment.getWorld()->getStore());
window->initUI();
//Load translation data
mTranslationDataStorage.setEncoder(mEncoder);
for (size_t i = 0; i < mContentFiles.size(); i++)
mTranslationDataStorage.loadTranslationData(mFileCollections, mContentFiles[i]);
Compiler::registerExtensions (mExtensions);
// Create script system
mScriptContext = new MWScript::CompilerContext (MWScript::CompilerContext::Type_Full);
mScriptContext->setExtensions (&mExtensions);
mEnvironment.setScriptManager (new MWScript::ScriptManager (mEnvironment.getWorld()->getStore(), *mScriptContext, mWarningsMode,
mScriptBlacklistUse ? mScriptBlacklist : std::vector<std::string>()));
// Create game mechanics system
MWMechanics::MechanicsManager* mechanics = new MWMechanics::MechanicsManager;
mEnvironment.setMechanicsManager (mechanics);
// Create dialog system
mEnvironment.setJournal (new MWDialogue::Journal);
mEnvironment.setDialogueManager (new MWDialogue::DialogueManager (mExtensions, mTranslationDataStorage));
// scripts
if (mCompileAll)
{
std::pair<int, int> result = mEnvironment.getScriptManager()->compileAll();
if (result.first)
Log(Debug::Info)
<< "compiled " << result.second << " of " << result.first << " scripts ("
<< 100*static_cast<double> (result.second)/result.first
<< "%)";
}
if (mCompileAllDialogue)
{
std::pair<int, int> result = MWDialogue::ScriptTest::compileAll(&mExtensions, mWarningsMode);
if (result.first)
Log(Debug::Info)
<< "compiled " << result.second << " of " << result.first << " dialogue script/actor combinations a("
<< 100*static_cast<double> (result.second)/result.first
<< "%)";
}
}
class WriteScreenshotToFileOperation : public osgViewer::ScreenCaptureHandler::CaptureOperation
{
public:
WriteScreenshotToFileOperation(const std::string& screenshotPath, const std::string& screenshotFormat)
: mScreenshotPath(screenshotPath)
, mScreenshotFormat(screenshotFormat)
{
}
virtual void operator()(const osg::Image& image, const unsigned int context_id)
{
// Count screenshots.
int shotCount = 0;
// Find the first unused filename with a do-while
std::ostringstream stream;
do
{
// Reset the stream
stream.str("");
stream.clear();
stream << mScreenshotPath << "/screenshot" << std::setw(3) << std::setfill('0') << shotCount++ << "." << mScreenshotFormat;
} while (boost::filesystem::exists(stream.str()));
boost::filesystem::ofstream outStream;
outStream.open(boost::filesystem::path(stream.str()), std::ios::binary);
osgDB::ReaderWriter* readerwriter = osgDB::Registry::instance()->getReaderWriterForExtension(mScreenshotFormat);
if (!readerwriter)
{
Log(Debug::Error) << "Error: Can't write screenshot, no '" << mScreenshotFormat << "' readerwriter found";
return;
}
osgDB::ReaderWriter::WriteResult result = readerwriter->writeImage(image, outStream);
if (!result.success())
{
Log(Debug::Error) << "Error: Can't write screenshot: " << result.message() << " code " << result.status();
}
}
private:
std::string mScreenshotPath;
std::string mScreenshotFormat;
};
// Initialise and enter main loop.
void MWVR::OpenXREngine::go()
{
assert (!mContentFiles.empty());
Log(Debug::Info) << "OSG version: " << osgGetVersion();
SDL_version sdlVersion;
SDL_GetVersion(&sdlVersion);
Log(Debug::Info) << "SDL version: " << (int)sdlVersion.major << "." << (int)sdlVersion.minor << "." << (int)sdlVersion.patch;
Misc::Rng::init(mRandomSeed);
// Load settings
Settings::Manager settings;
std::string settingspath;
settingspath = loadSettings (settings);
// Create encoder
mEncoder = new ToUTF8::Utf8Encoder(mEncoding);
// Setup viewer
mViewer = new osgViewer::Viewer;
mViewer->setReleaseContextAtEndOfFrameHint(false);
#if OSG_VERSION_GREATER_OR_EQUAL(3,5,5)
// Do not try to outsmart the OS thread scheduler (see bug #4785).
mViewer->setUseConfigureAffinity(false);
#endif
mScreenCaptureOperation = new WriteScreenshotToFileOperation(mCfgMgr.getUserDataPath().string(),
Settings::Manager::getString("screenshot format", "General"));
mScreenCaptureHandler = new osgViewer::ScreenCaptureHandler(mScreenCaptureOperation);
mViewer->addEventHandler(mScreenCaptureHandler);
mEnvironment.setFrameRateLimit(Settings::Manager::getFloat("framerate limit", "Video"));
prepareEngine (settings);
// Setup profiler
osg::ref_ptr<Resource::Profiler> statshandler = new Resource::Profiler;
statshandler->addUserStatsLine("Script", osg::Vec4f(1.f, 1.f, 1.f, 1.f), osg::Vec4f(1.f, 1.f, 1.f, 1.f),
"script_time_taken", 1000.0, true, false, "script_time_begin", "script_time_end", 10000);
statshandler->addUserStatsLine("Mech", osg::Vec4f(1.f, 1.f, 1.f, 1.f), osg::Vec4f(1.f, 1.f, 1.f, 1.f),
"mechanics_time_taken", 1000.0, true, false, "mechanics_time_begin", "mechanics_time_end", 10000);
statshandler->addUserStatsLine("Phys", osg::Vec4f(1.f, 1.f, 1.f, 1.f), osg::Vec4f(1.f, 1.f, 1.f, 1.f),
"physics_time_taken", 1000.0, true, false, "physics_time_begin", "physics_time_end", 10000);
statshandler->addUserStatsLine("World", osg::Vec4f(1.f, 1.f, 1.f, 1.f), osg::Vec4f(1.f, 1.f, 1.f, 1.f),
"world_time_taken", 1000.0, true, false, "world_time_begin", "world_time_end", 10000);
mViewer->addEventHandler(statshandler);
osg::ref_ptr<Resource::StatsHandler> resourceshandler = new Resource::StatsHandler;
mViewer->addEventHandler(resourceshandler);
// Start the game
if (!mSaveGameFile.empty())
{
mEnvironment.getStateManager()->loadGame(mSaveGameFile);
}
else if (!mSkipMenu)
{
// start in main menu
mEnvironment.getWindowManager()->pushGuiMode (MWGui::GM_MainMenu);
mEnvironment.getSoundManager()->playTitleMusic();
const std::string& logo = Fallback::Map::getString("Movies_Morrowind_Logo");
if (!logo.empty())
mEnvironment.getWindowManager()->playVideo(logo, true);
}
else
{
mEnvironment.getStateManager()->newGame (!mNewGame);
}
if (!mStartupScript.empty() && mEnvironment.getStateManager()->getState() == MWState::StateManager::State_Running)
{
mEnvironment.getWindowManager()->executeInConsole(mStartupScript);
}
// Start the main rendering loop
osg::Timer frameTimer;
double simulationTime = 0.0;
while (!mViewer->done() && !mEnvironment.getStateManager()->hasQuitRequest())
{
double dt = frameTimer.time_s();
frameTimer.setStartTick();
dt = std::min(dt, 0.2);
mViewer->advance(simulationTime);
if (!frame(dt))
{
OpenThreads::Thread::microSleep(5000);
continue;
}
else
{
mViewer->eventTraversal();
mViewer->updateTraversal();
mEnvironment.getWorld()->updateWindowManager();
mViewer->renderingTraversals();
bool guiActive = mEnvironment.getWindowManager()->isGuiMode();
if (!guiActive)
simulationTime += dt;
}
mEnvironment.limitFrameRate(frameTimer.time_s());
}
// Save user settings
settings.saveUser(settingspath);
Log(Debug::Info) << "Quitting peacefully.";
}
void MWVR::OpenXREngine::setCompileAll (bool all)
{
mCompileAll = all;
}
void MWVR::OpenXREngine::setCompileAllDialogue (bool all)
{
mCompileAllDialogue = all;
}
void MWVR::OpenXREngine::setSoundUsage(bool soundUsage)
{
mUseSound = soundUsage;
}
void MWVR::OpenXREngine::setEncoding(const ToUTF8::FromType& encoding)
{
mEncoding = encoding;
}
void MWVR::OpenXREngine::setScriptConsoleMode (bool enabled)
{
mScriptConsoleMode = enabled;
}
void MWVR::OpenXREngine::setStartupScript (const std::string& path)
{
mStartupScript = path;
}
void MWVR::OpenXREngine::setActivationDistanceOverride (int distance)
{
mActivationDistanceOverride = distance;
}
void MWVR::OpenXREngine::setWarningsMode (int mode)
{
mWarningsMode = mode;
}
void MWVR::OpenXREngine::setScriptBlacklist (const std::vector<std::string>& list)
{
mScriptBlacklist = list;
}
void MWVR::OpenXREngine::setScriptBlacklistUse (bool use)
{
mScriptBlacklistUse = use;
}
void MWVR::OpenXREngine::enableFontExport(bool exportFonts)
{
mExportFonts = exportFonts;
}
void MWVR::OpenXREngine::setSaveGameFile(const std::string &savegame)
{
mSaveGameFile = savegame;
}
void MWVR::OpenXREngine::setRandomSeed(unsigned int seed)
{
mRandomSeed = seed;
}

@ -0,0 +1,217 @@
#ifndef ENGINE_H
#define ENGINE_H
#include <components/compiler/extensions.hpp>
#include <components/files/collections.hpp>
#include <components/translation/translation.hpp>
#include <components/settings/settings.hpp>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include "../mwbase/environment.hpp"
#include "../mwworld/ptr.hpp"
#include "openxrmanager.hpp"
namespace Resource
{
class ResourceSystem;
}
namespace SceneUtil
{
class WorkQueue;
}
namespace VFS
{
class Manager;
}
namespace Compiler
{
class Context;
}
namespace MWScript
{
class ScriptManager;
}
namespace MWSound
{
class SoundManager;
}
namespace MWWorld
{
class World;
}
namespace MWGui
{
class WindowManager;
}
namespace Files
{
struct ConfigurationManager;
}
namespace osgViewer
{
class ScreenCaptureHandler;
}
struct SDL_Window;
namespace MWVR
{
/// \brief Main VR engine class, that brings together all the components of OpenMW
class OpenXREngine
{
private:
SDL_Window* mWindow;
std::unique_ptr<OpenXRManager> mOpenXRManager;
std::unique_ptr<VFS::Manager> mVFS;
std::unique_ptr<Resource::ResourceSystem> mResourceSystem;
osg::ref_ptr<SceneUtil::WorkQueue> mWorkQueue;
MWBase::Environment mEnvironment;
ToUTF8::FromType mEncoding;
ToUTF8::Utf8Encoder* mEncoder;
Files::PathContainer mDataDirs;
std::vector<std::string> mArchives;
boost::filesystem::path mResDir;
osg::ref_ptr<osgViewer::Viewer> mViewer;
osg::ref_ptr<osgViewer::ScreenCaptureHandler> mScreenCaptureHandler;
osgViewer::ScreenCaptureHandler::CaptureOperation *mScreenCaptureOperation;
std::string mCellName;
std::vector<std::string> mContentFiles;
bool mSkipMenu;
bool mUseSound;
bool mCompileAll;
bool mCompileAllDialogue;
int mWarningsMode;
std::string mFocusName;
bool mScriptConsoleMode;
std::string mStartupScript;
int mActivationDistanceOverride;
std::string mSaveGameFile;
// Grab mouse?
bool mGrab;
bool mExportFonts;
unsigned int mRandomSeed;
Compiler::Extensions mExtensions;
Compiler::Context *mScriptContext;
Files::Collections mFileCollections;
bool mFSStrict;
Translation::Storage mTranslationDataStorage;
std::vector<std::string> mScriptBlacklist;
bool mScriptBlacklistUse;
bool mNewGame;
osg::Timer_t mStartTick;
private:
// not implemented
OpenXREngine(const OpenXREngine&) = delete;
OpenXREngine& operator= (const OpenXREngine&) = delete;
void executeLocalScripts();
bool frame (float dt);
/// Load settings from various files, returns the path to the user settings file
std::string loadSettings (Settings::Manager & settings);
/// Prepare engine for game play
void prepareEngine (Settings::Manager & settings);
void createWindow(Settings::Manager& settings);
void setWindowIcon();
public:
OpenXREngine(Files::ConfigurationManager& configurationManager);
virtual ~OpenXREngine();
/// Enable strict filesystem mode (do not fold case)
///
/// \attention The strict mode must be specified before any path-related settings
/// are given to the engine.
void enableFSStrict(bool fsStrict);
/// Set data dirs
void setDataDirs(const Files::PathContainer& dataDirs);
/// Add BSA archive
void addArchive(const std::string& archive);
/// Set resource dir
void setResourceDir(const boost::filesystem::path& parResDir);
/// Set start cell name
void setCell(const std::string& cellName);
/**
* @brief addContentFile - Adds content file (ie. esm/esp, or omwgame/omwaddon) to the content files container.
* @param file - filename (extension is required)
*/
void addContentFile(const std::string& file);
/// Disable or enable all sounds
void setSoundUsage(bool soundUsage);
/// Skip main menu and go directly into the game
///
/// \param newGame Start a new game instead off dumping the player into the game
/// (ignored if !skipMenu).
void setSkipMenu (bool skipMenu, bool newGame);
void setGrabMouse(bool grab) { mGrab = grab; }
/// Initialise and enter main loop.
void go();
/// Compile all scripts (excludign dialogue scripts) at startup?
void setCompileAll (bool all);
/// Compile all dialogue scripts at startup?
void setCompileAllDialogue (bool all);
/// Font encoding
void setEncoding(const ToUTF8::FromType& encoding);
/// Enable console-only script functionality
void setScriptConsoleMode (bool enabled);
/// Set path for a script that is run on startup in the console.
void setStartupScript (const std::string& path);
/// Override the game setting specified activation distance.
void setActivationDistanceOverride (int distance);
void setWarningsMode (int mode);
void setScriptBlacklist (const std::vector<std::string>& list);
void setScriptBlacklistUse (bool use);
void enableFontExport(bool exportFonts);
/// Set the save game file to load after initialising the engine.
void setSaveGameFile(const std::string& savegame);
void setRandomSeed(unsigned int seed);
private:
Files::ConfigurationManager& mCfgMgr;
};
}
#endif /* ENGINE_H */

@ -229,7 +229,7 @@ namespace MWVR
strcpy_s(createInfo.actionSetName, "gameplay");
strcpy_s(createInfo.localizedActionSetName, "Gameplay");
createInfo.priority = 0;
CHECK_XRCMD(xrCreateActionSet(mXR->mPrivate->mInstance, &createInfo, &actionSet));
CHECK_XRCMD(xrCreateActionSet(mXR->impl().mInstance, &createInfo, &actionSet));
return actionSet;
}
@ -273,7 +273,7 @@ namespace MWVR
getInfo.subactionPath = subactionPath;
XrActionStateFloat xrValue{ XR_TYPE_ACTION_STATE_FLOAT };
CHECK_XRCMD(xrGetActionStateFloat(mXR->mPrivate->mSession, &getInfo, &xrValue));
CHECK_XRCMD(xrGetActionStateFloat(mXR->impl().mSession, &getInfo, &xrValue));
if (xrValue.isActive)
value = xrValue.currentState;
@ -287,7 +287,7 @@ namespace MWVR
getInfo.subactionPath = subactionPath;
XrActionStateBoolean xrValue{ XR_TYPE_ACTION_STATE_BOOLEAN };
CHECK_XRCMD(xrGetActionStateBoolean(mXR->mPrivate->mSession, &getInfo, &xrValue));
CHECK_XRCMD(xrGetActionStateBoolean(mXR->impl().mSession, &getInfo, &xrValue));
if (xrValue.isActive)
value = xrValue.currentState;
@ -302,7 +302,7 @@ namespace MWVR
getInfo.subactionPath = subactionPath;
XrActionStatePose xrValue{ XR_TYPE_ACTION_STATE_POSE };
CHECK_XRCMD(xrGetActionStatePose(mXR->mPrivate->mSession, &getInfo, &xrValue));
CHECK_XRCMD(xrGetActionStatePose(mXR->impl().mSession, &getInfo, &xrValue));
return xrValue.isActive;
}
@ -317,7 +317,7 @@ namespace MWVR
XrHapticActionInfo hapticActionInfo{ XR_TYPE_HAPTIC_ACTION_INFO };
hapticActionInfo.action = mAction;
hapticActionInfo.subactionPath = subactionPath;
CHECK_XRCMD(xrApplyHapticFeedback(mXR->mPrivate->mSession, &hapticActionInfo, (XrHapticBaseHeader*)&vibration));
CHECK_XRCMD(xrApplyHapticFeedback(mXR->impl().mSession, &hapticActionInfo, (XrHapticBaseHeader*)&vibration));
return true;
}
@ -359,9 +359,9 @@ namespace MWVR
std::string right = std::string("/user/hand/right") + controllerAction;
std::string pad = std::string("/user/gamepad") + controllerAction;
CHECK_XRCMD(xrStringToPath(mXR->mPrivate->mInstance, left.c_str(), &actionPaths[LEFT_HAND]));
CHECK_XRCMD(xrStringToPath(mXR->mPrivate->mInstance, right.c_str(), &actionPaths[RIGHT_HAND]));
CHECK_XRCMD(xrStringToPath(mXR->mPrivate->mInstance, pad.c_str(), &actionPaths[GAMEPAD]));
CHECK_XRCMD(xrStringToPath(mXR->impl().mInstance, left.c_str(), &actionPaths[LEFT_HAND]));
CHECK_XRCMD(xrStringToPath(mXR->impl().mInstance, right.c_str(), &actionPaths[RIGHT_HAND]));
CHECK_XRCMD(xrStringToPath(mXR->impl().mInstance, pad.c_str(), &actionPaths[GAMEPAD]));
return actionPaths;
}
@ -391,10 +391,8 @@ namespace MWVR
, mYPath(generateControllerActionPaths("/input/y/click"))
, mAPath(generateControllerActionPaths("/input/a/click"))
, mBPath(generateControllerActionPaths("/input/b/click"))
, mTriggerValuePath(generateControllerActionPaths("/input/trigger/value"))
, mTriggerClickPath(generateControllerActionPaths("/input/trigger/click"))
, mActionsMenu(std::move(createAction(XR_ACTION_TYPE_BOOLEAN_INPUT, "actions_menu", "Actions Menu", { })))
, mSpellModifier(std::move(createAction(XR_ACTION_TYPE_BOOLEAN_INPUT, "spell_modifier", "Spell Modifier", { })))
, mTriggerValuePath(generateControllerActionPaths("/input/trigger/value"))
, mGameMenu(std::move(createAction(XR_ACTION_TYPE_BOOLEAN_INPUT, "game_menu", "GameMenu", { })))
, mInventory(std::move(createAction(XR_ACTION_TYPE_BOOLEAN_INPUT, "inventory", "Inventory", { })))
, mActivate(std::move(createAction(XR_ACTION_TYPE_BOOLEAN_INPUT, "activate", "Activate", { })))
@ -411,6 +409,8 @@ namespace MWVR
, mLookLeftRight(std::move(createAction(XR_ACTION_TYPE_FLOAT_INPUT, "look_left_right", "LookLeftRight", { })))
, mMoveForwardBackward(std::move(createAction(XR_ACTION_TYPE_FLOAT_INPUT, "move_forward_backward", "MoveForwardBackward", { })))
, mMoveLeftRight(std::move(createAction(XR_ACTION_TYPE_FLOAT_INPUT, "move_left_right", "MoveLeftRight", { })))
, mActionsMenu(std::move(createAction(XR_ACTION_TYPE_BOOLEAN_INPUT, "actions_menu", "Actions Menu", { })))
, mSpellModifier(std::move(createAction(XR_ACTION_TYPE_BOOLEAN_INPUT, "spell_modifier", "Spell Modifier", { })))
, mHandPoseAction(std::move(createAction(XR_ACTION_TYPE_POSE_INPUT, "hand_pose", "Hand Pose", { LEFT_HAND, RIGHT_HAND })))
, mHapticsAction(std::move(createAction(XR_ACTION_TYPE_VIBRATION_OUTPUT, "vibrate_hand", "Vibrate Hand", { LEFT_HAND, RIGHT_HAND })))
{
@ -479,7 +479,7 @@ namespace MWVR
{ // Set up default bindings for the oculus
XrPath oculusTouchInteractionProfilePath;
CHECK_XRCMD(
xrStringToPath(XR->mPrivate->mInstance, "/interaction_profiles/oculus/touch_controller", &oculusTouchInteractionProfilePath));
xrStringToPath(XR->impl().mInstance, "/interaction_profiles/oculus/touch_controller", &oculusTouchInteractionProfilePath));
std::vector<XrActionSuggestedBinding> bindings{ {
{mHandPoseAction, mPosePath[LEFT_HAND]},
{mHandPoseAction, mPosePath[RIGHT_HAND]},
@ -507,7 +507,7 @@ namespace MWVR
suggestedBindings.interactionProfile = oculusTouchInteractionProfilePath;
suggestedBindings.suggestedBindings = bindings.data();
suggestedBindings.countSuggestedBindings = (uint32_t)bindings.size();
CHECK_XRCMD(xrSuggestInteractionProfileBindings(XR->mPrivate->mInstance, &suggestedBindings));
CHECK_XRCMD(xrSuggestInteractionProfileBindings(XR->impl().mInstance, &suggestedBindings));
/*
mSpellModifier; // L-Squeeze
@ -534,16 +534,16 @@ namespace MWVR
createInfo.action = mHandPoseAction;
createInfo.poseInActionSpace.orientation.w = 1.f;
createInfo.subactionPath = mSubactionPath[LEFT_HAND];
CHECK_XRCMD(xrCreateActionSpace(XR->mPrivate->mSession, &createInfo, &mHandSpace[LEFT_HAND]));
CHECK_XRCMD(xrCreateActionSpace(XR->impl().mSession, &createInfo, &mHandSpace[LEFT_HAND]));
createInfo.subactionPath = mSubactionPath[RIGHT_HAND];
CHECK_XRCMD(xrCreateActionSpace(XR->mPrivate->mSession, &createInfo, &mHandSpace[RIGHT_HAND]));
CHECK_XRCMD(xrCreateActionSpace(XR->impl().mSession, &createInfo, &mHandSpace[RIGHT_HAND]));
}
{ // Set up the action set
XrSessionActionSetsAttachInfo attachInfo{ XR_TYPE_SESSION_ACTION_SETS_ATTACH_INFO };
attachInfo.countActionSets = 1;
attachInfo.actionSets = &mActionSet;
CHECK_XRCMD(xrAttachSessionActionSets(XR->mPrivate->mSession, &attachInfo));
CHECK_XRCMD(xrAttachSessionActionSets(XR->impl().mSession, &attachInfo));
}
};
@ -577,7 +577,7 @@ namespace MWVR
OpenXRInputManagerImpl::updateHandTracking()
{
for (auto hand : { LEFT_HAND, RIGHT_HAND }) {
CHECK_XRCMD(xrLocateSpace(mHandSpace[hand], mXR->mPrivate->mReferenceSpaceStage, mXR->mPrivate->mFrameState.predictedDisplayTime, &mHandSpaceLocation[hand]));
CHECK_XRCMD(xrLocateSpace(mHandSpace[hand], mXR->impl().mReferenceSpaceStage, mXR->impl().mFrameState.predictedDisplayTime, &mHandSpaceLocation[hand]));
}
}
@ -593,7 +593,7 @@ namespace MWVR
void
OpenXRInputManagerImpl::updateControls()
{
if (!mXR->mPrivate->mSessionRunning)
if (!mXR->impl().mSessionRunning)
return;
// TODO: Should be in OpenXRViewer
@ -668,6 +668,11 @@ namespace MWVR
float moveLeftRight = mMoveLeftRight.value();
float moveForwardBackward = mMoveForwardBackward.value();
// Until i implement the rest
(void)lookLeftRight;
(void)moveLeftRight;
(void)moveForwardBackward;
// Propagate movement to openmw
}
@ -714,7 +719,7 @@ namespace MWVR
XrPath OpenXRInputManagerImpl::generateXrPath(const std::string& path)
{
XrPath xrpath = 0;
CHECK_XRCMD(xrStringToPath(mXR->mPrivate->mInstance, path.c_str(), &xrpath));
CHECK_XRCMD(xrStringToPath(mXR->impl().mInstance, path.c_str(), &xrpath));
return xrpath;
}
@ -733,6 +738,6 @@ namespace MWVR
void
OpenXRInputManager::updateControls()
{
mPrivate->updateControls();
impl().updateControls();
}
}

@ -10,6 +10,35 @@
namespace MWVR
{
//struct OpenXRPoseImpl;
//// TODO: Make this an OSG node/group and attach posed elements to it ?
//struct OpenXRPose
//{
//public:
// enum
// {
// };
//protected:
// OpenXRPose(osg::ref_ptr<OpenXRManager> XR, TrackedLimb limb, TrackingMode mode);
// ~OpenXRPose();
//public:
// bool isActive();
// void update();
// TrackedLimb limb() const;
// TrackingMode trackingMode() const;
//
// OpenXRPoseImpl& impl() { return *mPrivate; }
//private:
// std::unique_ptr<OpenXRPoseImpl> mPrivate;
//};
struct OpenXRInputManagerImpl;
struct OpenXRInputManager
{
@ -18,6 +47,8 @@ namespace MWVR
void updateControls();
OpenXRInputManagerImpl& impl() { return *mPrivate; }
std::unique_ptr<OpenXRInputManagerImpl> mPrivate;
};
}

@ -0,0 +1,44 @@
#include "openxrlayer.hpp"
namespace MWVR {
void OpenXRLayerStack::setLayer(Layer layer, OpenXRLayer* layerObj)
{
mLayers[layer] = nullptr;
mLayerObjects[layer] = layerObj;
}
int OpenXRLayerStack::layerCount()
{
int count = 0;
for (auto* layer : mLayers)
if (layer)
count++;
return count;
}
const XrCompositionLayerBaseHeader** OpenXRLayerStack::layerHeaders()
{
// If i had more than two layers i'd make a general solution using a vector member instead.
for (unsigned i = 0; i < mLayerObjects.size(); i++)
if (mLayerObjects[i])
mLayers[i] = mLayerObjects[i]->layer();
else
mLayers[i] = nullptr;
if (mLayers[0])
return mLayers.data();
if (!mLayers[0])
if (mLayers[1])
return mLayers.data() + 1;
if (!mLayers[0])
if (!mLayers[1])
return nullptr;
}
}

@ -0,0 +1,45 @@
#ifndef OPENXR_LAYER_HPP
#define OPENXR_LAYER_HPP
#include <array>
#include "openxrmanager.hpp"
#include "openxrtexture.hpp"
struct XrCompositionLayerBaseHeader;
namespace MWVR
{
class OpenXRLayer
{
public:
OpenXRLayer(void) = default;
virtual ~OpenXRLayer(void) = default;
virtual const XrCompositionLayerBaseHeader* layer() = 0;
};
class OpenXRLayerStack
{
public:
enum Layer {
WORLD_VIEW_LAYER = 0,
MENU_VIEW_LAYER = 1,
LAYER_MAX = MENU_VIEW_LAYER //!< Used to size layer arrays. Not a valid input.
};
using LayerObjectStack = std::array< OpenXRLayer*, LAYER_MAX + 1>;
using LayerStack = std::array< const XrCompositionLayerBaseHeader*, LAYER_MAX + 1>;
OpenXRLayerStack() = default;
~OpenXRLayerStack() = default;
void setLayer(Layer layer, OpenXRLayer* layerObj);
int layerCount();
const XrCompositionLayerBaseHeader** layerHeaders();
private:
LayerObjectStack mLayerObjects;
LayerStack mLayers;
};
}
#endif

@ -44,50 +44,51 @@ namespace MWVR
OpenXRManager::frameIndex()
{
if (realized())
return mPrivate->frameIndex;
return impl().frameIndex;
return -1;
}
bool OpenXRManager::sessionRunning()
{
if (realized())
return mPrivate->mSessionRunning;
return impl().mSessionRunning;
return false;
}
void OpenXRManager::handleEvents()
{
if (realized())
return mPrivate->handleEvents();
return impl().handleEvents();
}
void OpenXRManager::waitFrame()
{
if (realized())
return mPrivate->waitFrame();
return impl().waitFrame();
}
void OpenXRManager::beginFrame()
{
if (realized())
return mPrivate->beginFrame();
return impl().beginFrame();
}
void OpenXRManager::endFrame()
{
if (realized())
return mPrivate->endFrame();
return impl().endFrame();
}
void OpenXRManager::updateControls()
{
if (realized())
return mPrivate->updateControls();
return impl().updateControls();
}
void OpenXRManager::updatePoses()
{
if (realized())
return mPrivate->updatePoses();
return impl().updatePoses();
}
void
@ -110,22 +111,18 @@ namespace MWVR
}
}
void OpenXRManager::setPoseUpdateCallback(PoseUpdateCallback::TrackedLimb limb, PoseUpdateCallback::TrackingMode mode, osg::ref_ptr<PoseUpdateCallback> cb)
{
if (realized())
return mPrivate->setPoseUpdateCallback(limb, mode, cb);
}
void OpenXRManager::setViewSubImage(int eye, const ::XrSwapchainSubImage& subImage)
void OpenXRManager::addPoseUpdateCallback(
osg::ref_ptr<PoseUpdateCallback> cb)
{
if (realized())
return mPrivate->setViewSubImage(eye, subImage);
return impl().addPoseUpdateCallback(cb);
}
int OpenXRManager::eyes()
{
if (realized())
return mPrivate->eyes();
return impl().eyes();
return 0;
}
@ -133,7 +130,6 @@ namespace MWVR
OpenXRManager::RealizeOperation::operator()(
osg::GraphicsContext* gc)
{
osg::ref_ptr<OpenXRManager> XR;
mXR->realize(gc);
}
@ -150,11 +146,57 @@ namespace MWVR
}
void
OpenXRManager::SwapBuffersCallback::swapBuffersImplementation(
osg::GraphicsContext* gc)
void OpenXRManager::viewerBarrier()
{
if (realized())
return impl().viewerBarrier();
}
void OpenXRManager::registerToBarrier()
{
if (realized())
return impl().registerToBarrier();
}
void OpenXRManager::unregisterFromBarrier()
{
gc->swapBuffersImplementation();
mXR->endFrame();
if (realized())
return impl().unregisterFromBarrier();
}
#ifndef _NDEBUG
void PoseLogger::operator()(MWVR::Pose pose)
{
const char* limb = nullptr;
const char* space = nullptr;
switch (mLimb)
{
case TrackedLimb::HEAD:
limb = "HEAD"; break;
case TrackedLimb::LEFT_HAND:
limb = "LEFT_HAND"; break;
case TrackedLimb::RIGHT_HAND:
limb = "RIGHT_HAND"; break;
}
switch (mSpace)
{
case TrackedSpace::STAGE:
space = "STAGE"; break;
case TrackedSpace::VIEW:
space = "VIEW"; break;
}
//TODO: Use a different output to avoid spamming the debug log when enabled
Log(Debug::Verbose) << space << "." << limb << ": " << pose;
}
#endif
}
std::ostream& operator <<(
std::ostream& os,
const MWVR::Pose& pose)
{
os << "position=" << pose.position << " orientation=" << pose.orientation << " velocity=" << pose.velocity;
return os;
}

@ -14,8 +14,35 @@
struct XrSwapchainSubImage;
namespace MWVR
{
//! Represents the pose of a limb in VR space.
struct Pose
{
//! Position in VR space
osg::Vec3 position;
//! Orientation in VR space.
osg::Quat orientation;
//! Speed of movement in VR space, expressed in meters per second
osg::Vec3 velocity;
};
//! Describes what limb to track.
enum class TrackedLimb
{
LEFT_HAND,
RIGHT_HAND,
HEAD
};
//! Describes what space to track the limb in
enum class TrackedSpace
{
STAGE, //!< Track limb in the VR stage space. Meaning a space with a floor level origin and fixed horizontal orientation.
VIEW //!< Track limb in the VR view space. Meaning a space with the head as origin and orientation.
};
// Use the pimpl pattern to avoid cluttering the namespace with openxr dependencies.
class OpenXRManagerImpl;
@ -26,21 +53,13 @@ namespace MWVR
class PoseUpdateCallback: public osg::Referenced
{
public:
enum TrackedLimb
{
LEFT_HAND,
RIGHT_HAND,
HEAD
};
//! Describes how position is tracked. Orientation is always absolute.
enum TrackingMode
{
STAGE_ABSOLUTE, //!< Position in VR stage. Meaning relative to some floor level origin
STAGE_RELATIVE, //!< Same as STAGE_ABSOLUTE but receive only changes in position
};
virtual void operator()(osg::Vec3 position, osg::Quat orientation) = 0;
PoseUpdateCallback(TrackedLimb limb, TrackedSpace space)
: mLimb(limb), mSpace(space){}
virtual void operator()(MWVR::Pose pose) = 0;
TrackedLimb mLimb;
TrackedSpace mSpace;
};
public:
@ -49,7 +68,7 @@ namespace MWVR
public:
RealizeOperation(osg::ref_ptr<OpenXRManager> XR) : osg::GraphicsOperation("OpenXRRealizeOperation", false), mXR(XR) {};
void operator()(osg::GraphicsContext* gc) override;
bool realized();
virtual bool realized();
private:
osg::ref_ptr<OpenXRManager> mXR;
@ -65,16 +84,6 @@ namespace MWVR
osg::ref_ptr<OpenXRManager> mXR;
};
class SwapBuffersCallback : public osg::GraphicsContext::SwapCallback
{
public:
SwapBuffersCallback(osg::ref_ptr<OpenXRManager> XR) : mXR(XR) {};
void swapBuffersImplementation(osg::GraphicsContext* gc) override;
private:
osg::ref_ptr<OpenXRManager> mXR;
};
public:
OpenXRManager();
@ -94,23 +103,36 @@ namespace MWVR
void realize(osg::GraphicsContext* gc);
void setPoseUpdateCallback(PoseUpdateCallback::TrackedLimb limb, PoseUpdateCallback::TrackingMode mode, osg::ref_ptr<PoseUpdateCallback> cb);
void setViewSubImage(int eye, const ::XrSwapchainSubImage& subImage);
void addPoseUpdateCallback(osg::ref_ptr<PoseUpdateCallback> cb);
int eyes();
private:
friend class OpenXRViewImpl;
friend class OpenXRInputManagerImpl;
friend class OpenXRAction;
friend class OpenXRViewer;
//! A barrier used internally to ensure all views have released their frames before endFrame can complete.
void viewerBarrier();
//! Increments the target viewer counter of the barrier
void registerToBarrier();
//! Decrements the target viewer counter of the barrier
void unregisterFromBarrier();
OpenXRManagerImpl& impl() { return *mPrivate; }
private:
std::shared_ptr<OpenXRManagerImpl> mPrivate;
std::mutex mMutex;
using lock_guard = std::lock_guard<std::mutex>;
};
#ifndef _NDEBUG
class PoseLogger : public OpenXRManager::PoseUpdateCallback
{
public:
PoseLogger(TrackedLimb limb, TrackedSpace space)
: OpenXRManager::PoseUpdateCallback(limb, space) {};
void operator()(MWVR::Pose pose) override;
};
#endif
}
std::ostream& operator <<(std::ostream& os, const MWVR::Pose& pose);
#endif

@ -119,9 +119,9 @@ namespace MWVR
{ // Set up layers
mLayer.space = mReferenceSpaceStage;
mLayer.viewCount = (uint32_t)mProjectionLayerViews.size();
mLayer.views = mProjectionLayerViews.data();
//mLayer.space = mReferenceSpaceStage;
//mLayer.viewCount = (uint32_t)mProjectionLayerViews.size();
//mLayer.views = mProjectionLayerViews.data();
}
{ // Read and log graphics properties for the swapchain
@ -280,8 +280,61 @@ namespace MWVR
if (!mSessionRunning)
return;
XrFrameBeginInfo frameBeginInfo{ XR_TYPE_FRAME_BEGIN_INFO };
CHECK_XRCMD(xrBeginFrame(mSession, &frameBeginInfo));
std::unique_lock<std::mutex> lock(mFrameStatusMutex);
// We need to wait for the frame to become idle or ready
// (There is no guarantee osg won't get us here before endFrame() returns)
while (mFrameStatus == OPENXR_FRAME_STATUS_ENDING)
mFrameStatusSignal.wait(lock);
if (mFrameStatus == OPENXR_FRAME_STATUS_IDLE)
{
handleEvents();
waitFrame();
XrFrameBeginInfo frameBeginInfo{ XR_TYPE_FRAME_BEGIN_INFO };
CHECK_XRCMD(xrBeginFrame(mSession, &frameBeginInfo));
updateControls();
updatePoses();
mFrameStatus = OPENXR_FRAME_STATUS_READY;
}
assert(mFrameStatus == OPENXR_FRAME_STATUS_READY);
}
void OpenXRManagerImpl::viewerBarrier()
{
std::unique_lock<std::mutex> lock(mBarrierMutex);
mBarrier++;
Log(Debug::Verbose) << "mBarrier=" << mBarrier << ", tid=" << std::this_thread::get_id();
if (mBarrier == mNBarrier)
{
std::unique_lock<std::mutex> lock(mFrameStatusMutex);
mFrameStatus = OPENXR_FRAME_STATUS_ENDING;
mFrameStatusSignal.notify_all();
//mBarrierSignal.notify_all();
mBarrier = 0;
}
//else
//{
// mBarrierSignal.wait(lock, [this]() { return mBarrier == mNBarrier; });
//}
}
void OpenXRManagerImpl::registerToBarrier()
{
std::unique_lock<std::mutex> lock(mBarrierMutex);
mNBarrier++;
}
void OpenXRManagerImpl::unregisterFromBarrier()
{
std::unique_lock<std::mutex> lock(mBarrierMutex);
mNBarrier--;
assert(mNBarrier >= 0);
}
void
@ -290,12 +343,20 @@ namespace MWVR
if (!mSessionRunning)
return;
std::unique_lock<std::mutex> lock(mFrameStatusMutex);
while(mFrameStatus != OPENXR_FRAME_STATUS_ENDING)
mFrameStatusSignal.wait(lock);
XrFrameEndInfo frameEndInfo{ XR_TYPE_FRAME_END_INFO };
frameEndInfo.displayTime = mFrameState.predictedDisplayTime;
frameEndInfo.environmentBlendMode = mEnvironmentBlendMode;
frameEndInfo.layerCount = (uint32_t)1;
frameEndInfo.layers = &mLayer_p;
//frameEndInfo.layerCount = (uint32_t)1;
//frameEndInfo.layers = &mLayer_p;
frameEndInfo.layerCount = mLayerStack.layerCount();
frameEndInfo.layers = mLayerStack.layerHeaders();
CHECK_XRCMD(xrEndFrame(mSession, &frameEndInfo));
mFrameStatus = OPENXR_FRAME_STATUS_IDLE;
mFrameStatusSignal.notify_all();
}
std::array<XrView, 2> OpenXRManagerImpl::getStageViews()
@ -352,12 +413,42 @@ namespace MWVR
return views;
}
XrSpaceLocation OpenXRManagerImpl::getHeadLocation()
MWVR::Pose OpenXRManagerImpl::getLimbPose(TrackedLimb limb, TrackedSpace space)
{
// The pose of the "View" reference space is the pose of the HMD.
XrSpaceLocation location{ XR_TYPE_SPACE_LOCATION };
CHECK_XRCMD(xrLocateSpace(mReferenceSpaceView, mReferenceSpaceStage, mFrameState.predictedDisplayTime, &location));
return location;
XrSpaceVelocity velocity{ XR_TYPE_SPACE_VELOCITY };
location.next = &velocity;
XrSpace limbSpace = XR_NULL_HANDLE;
XrSpace referenceSpace = XR_NULL_HANDLE;
switch (limb)
{
case TrackedLimb::HEAD:
limbSpace = mReferenceSpaceView;
break;
case TrackedLimb::LEFT_HAND:
limbSpace = mReferenceSpaceView;
break;
case TrackedLimb::RIGHT_HAND:
limbSpace = mReferenceSpaceView;
break;
}
switch (space)
{
case TrackedSpace::STAGE:
referenceSpace = mReferenceSpaceStage;
break;
case TrackedSpace::VIEW:
referenceSpace = mReferenceSpaceView;
break;
}
CHECK_XRCMD(xrLocateSpace(limbSpace, referenceSpace, mFrameState.predictedDisplayTime, &location));
if (!(velocity.velocityFlags & XR_SPACE_VELOCITY_LINEAR_VALID_BIT))
Log(Debug::Warning) << "Unable to acquire linear velocity";
return MWVR::Pose{
osg::fromXR(location.pose.position),
osg::fromXR(location.pose.orientation),
osg::fromXR(velocity.linearVelocity)
};
}
int OpenXRManagerImpl::eyes()
@ -367,6 +458,8 @@ namespace MWVR
void OpenXRManagerImpl::handleEvents()
{
std::unique_lock<std::mutex> lock(mEventMutex);
// React to events
while (auto* event = nextEvent())
{
@ -426,88 +519,37 @@ namespace MWVR
}
}
void
OpenXRManagerImpl::setViewSubImage(
int eye,
const ::XrSwapchainSubImage& subImage)
{
if (eye >= mProjectionLayerViews.size())
throw std::out_of_range("OpenXRManagerImpl::setViewSubImage: Eye index out of range");
mProjectionLayerViews[eye].subImage = subImage;
}
static void
poseCallbacks(
const XrPosef& newPose,
const XrPosef& oldPose,
OpenXRManager::PoseUpdateCallback* absoluteCB,
OpenXRManager::PoseUpdateCallback* relativeCB
)
{
osg::Quat quat = osg::Quat(
newPose.orientation.x,
newPose.orientation.y,
newPose.orientation.z,
newPose.orientation.w
);
osg::Vec3 oldPos = osg::Vec3(
oldPose.position.x,
oldPose.position.y,
oldPose.position.z
);
osg::Vec3 newPos = osg::Vec3(
oldPose.position.x,
oldPose.position.y,
oldPose.position.z
);
if (absoluteCB)
(*absoluteCB)(newPos, quat);
if (relativeCB)
(*relativeCB)(newPos - oldPos, quat);
}
void OpenXRManagerImpl::updatePoses()
{
auto newHeadTrackedPose = getHeadLocation();
poseCallbacks(newHeadTrackedPose.pose, mHeadTrackedPose, mHeadAbsoluteCB, mHeadRelativeCB);
mHeadTrackedPose = newHeadTrackedPose.pose;
auto stageViews = getStageViews();
mProjectionLayerViews[0].pose = stageViews[0].pose;
mProjectionLayerViews[1].pose = stageViews[1].pose;
mProjectionLayerViews[0].fov = stageViews[0].fov;
mProjectionLayerViews[1].fov = stageViews[1].fov;
//auto oldHeadTrackedPose = mHeadTrackedPose;
//auto oldLefthandPose = mLeftHandTrackedPose;
//auto oldRightHandPose = mRightHandTrackedPose;
//mHeadTrackedPose = getLimbPose(TrackedLimb::HEAD);
//mLeftHandTrackedPose = getLimbPose(TrackedLimb::LEFT_HAND);
//mRightHandTrackedPose = getLimbPose(TrackedLimb::RIGHT_HAND);
//for (auto& cb : mPoseUpdateCallbacks)
//{
// switch (cb->mLimb)
// {
// case TrackedLimb::HEAD:
// (*cb)(mHeadTrackedPose); break;
// case TrackedLimb::LEFT_HAND:
// (*cb)(mLeftHandTrackedPose); break;
// case TrackedLimb::RIGHT_HAND:
// (*cb)(mRightHandTrackedPose); break;
// }
//}
for (auto& cb : mPoseUpdateCallbacks)
(*cb)(getLimbPose(cb->mLimb, cb->mSpace));
}
void OpenXRManagerImpl::setPoseUpdateCallback(
OpenXRManager::PoseUpdateCallback::TrackedLimb limb,
OpenXRManager::PoseUpdateCallback::TrackingMode mode,
osg::ref_ptr<OpenXRManager::PoseUpdateCallback> cb)
void OpenXRManagerImpl::addPoseUpdateCallback(
osg::ref_ptr<PoseUpdateCallback> cb)
{
// This can clearly be made more generic if that ever becomes relevant
switch (limb) {
case OpenXRManager::PoseUpdateCallback::HEAD:
if (mode == OpenXRManager::PoseUpdateCallback::STAGE_ABSOLUTE)
mHeadAbsoluteCB = cb;
else
mHeadRelativeCB = cb;
break;
case OpenXRManager::PoseUpdateCallback::LEFT_HAND:
if (mode == OpenXRManager::PoseUpdateCallback::STAGE_ABSOLUTE)
mLeftHandAbsoluteCB = cb;
else
mLeftHandRelativeCB = cb;
break;
case OpenXRManager::PoseUpdateCallback::RIGHT_HAND:
if (mode == OpenXRManager::PoseUpdateCallback::STAGE_ABSOLUTE)
mRightHandAbsoluteCB = cb;
else
mRightHandRelativeCB = cb;
break;
}
mPoseUpdateCallbacks.push_back(cb);
}
@ -532,4 +574,38 @@ namespace MWVR
CHECK_XRRESULT(result, "xrPollEvent");
return nullptr;
}
MWVR::Pose fromXR(XrPosef pose)
{
return MWVR::Pose{ osg::fromXR(pose.position), osg::fromXR(pose.orientation) };
}
XrPosef toXR(MWVR::Pose pose)
{
return XrPosef{ osg::toXR(pose.orientation), osg::toXR(pose.position) };
}
}
namespace osg
{
Vec3 fromXR(XrVector3f v)
{
return Vec3{ v.x, v.y, v.z };
}
Quat fromXR(XrQuaternionf quat)
{
return Quat{ quat.x, quat.y, quat.z, quat.w };
}
XrVector3f toXR(Vec3 v)
{
return XrVector3f{ v.x(), v.y(), v.z() };
}
XrQuaternionf toXR(Quat quat)
{
return XrQuaternionf{ quat.x(), quat.y(), quat.z(), quat.w() };
}
}

@ -2,7 +2,9 @@
#define OPENXR_MANAGER_IMPL_HPP
#include "openxrmanager.hpp"
#include "openxrlayer.hpp"
#include "../mwinput/inputmanagerimp.hpp"
#include "../mwrender/vismask.hpp"
#include <components/debug/debuglog.hpp>
#include <components/sdlutil/sdlgraphicswindow.hpp>
@ -19,6 +21,14 @@
#include <array>
#include <map>
#include <iostream>
#include <thread>
namespace osg {
Vec3 fromXR(XrVector3f);
Quat fromXR(XrQuaternionf quat);
XrVector3f toXR(Vec3 v);
XrQuaternionf toXR(Quat quat);
}
namespace MWVR
{
@ -30,9 +40,12 @@ namespace MWVR
#define CHECK_XRRESULT(res, cmdStr) CheckXrResult(res, cmdStr, FILE_AND_LINE);
XrResult CheckXrResult(XrResult res, const char* originator = nullptr, const char* sourceLocation = nullptr);
MWVR::Pose fromXR(XrPosef pose);
XrPosef toXR(MWVR::Pose pose);
struct OpenXRManagerImpl
{
using PoseUpdateCallback = OpenXRManager::PoseUpdateCallback;
OpenXRManagerImpl(void);
~OpenXRManagerImpl(void);
@ -47,14 +60,13 @@ namespace MWVR
void endFrame();
std::array<XrView, 2> getStageViews();
std::array<XrView, 2> getHmdViews();
XrSpaceLocation getHeadLocation();
MWVR::Pose getLimbPose(TrackedLimb limb, TrackedSpace space);
int eyes();
void handleEvents();
void updateControls();
void updatePoses();
void setPoseUpdateCallback(OpenXRManager::PoseUpdateCallback::TrackedLimb limb, OpenXRManager::PoseUpdateCallback::TrackingMode mode, osg::ref_ptr<OpenXRManager::PoseUpdateCallback> cb);
void addPoseUpdateCallback(osg::ref_ptr<PoseUpdateCallback> cb);
void HandleSessionStateChanged(const XrEventDataSessionStateChanged& stateChangedEvent);
void setViewSubImage(int eye, const ::XrSwapchainSubImage& subImage);
bool initialized = false;
long long frameIndex = 0;
@ -71,23 +83,38 @@ namespace MWVR
XrSpace mReferenceSpaceView = XR_NULL_HANDLE;
XrSpace mReferenceSpaceStage = XR_NULL_HANDLE;
XrEventDataBuffer mEventDataBuffer{ XR_TYPE_EVENT_DATA_BUFFER };
XrCompositionLayerProjection mLayer{ XR_TYPE_COMPOSITION_LAYER_PROJECTION };
XrCompositionLayerBaseHeader const* mLayer_p = reinterpret_cast<XrCompositionLayerBaseHeader*>(&mLayer);
std::array<XrCompositionLayerProjectionView, 2> mProjectionLayerViews{ { {XR_TYPE_COMPOSITION_LAYER_PROJECTION_VIEW}, {XR_TYPE_COMPOSITION_LAYER_PROJECTION_VIEW} } };
//XrCompositionLayerProjection mLayer{ XR_TYPE_COMPOSITION_LAYER_PROJECTION };
//XrCompositionLayerBaseHeader const* mLayer_p = reinterpret_cast<XrCompositionLayerBaseHeader*>(&mLayer);
//std::array<XrCompositionLayerProjectionView, 2> mProjectionLayerViews{ { {XR_TYPE_COMPOSITION_LAYER_PROJECTION_VIEW}, {XR_TYPE_COMPOSITION_LAYER_PROJECTION_VIEW} } };
OpenXRLayerStack mLayerStack{};
XrFrameState mFrameState{ XR_TYPE_FRAME_STATE };
XrSessionState mSessionState = XR_SESSION_STATE_UNKNOWN;
bool mSessionRunning = false;
XrPosef mHeadTrackedPose{};
XrPosef mLeftHandTrackedPose{};
XrPosef mRightHandTrackedPose{};
osg::ref_ptr<OpenXRManager::PoseUpdateCallback> mHeadAbsoluteCB = nullptr;
osg::ref_ptr<OpenXRManager::PoseUpdateCallback> mHeadRelativeCB = nullptr;
osg::ref_ptr<OpenXRManager::PoseUpdateCallback> mLeftHandAbsoluteCB = nullptr;
osg::ref_ptr<OpenXRManager::PoseUpdateCallback> mLeftHandRelativeCB = nullptr;
osg::ref_ptr<OpenXRManager::PoseUpdateCallback> mRightHandAbsoluteCB = nullptr;
osg::ref_ptr<OpenXRManager::PoseUpdateCallback> mRightHandRelativeCB = nullptr;
//osg::Pose mHeadTrackedPose{};
//osg::Pose mLeftHandTrackedPose{};
//osg::Pose mRightHandTrackedPose{};
std::vector< osg::ref_ptr<PoseUpdateCallback> > mPoseUpdateCallbacks{};
std::mutex mEventMutex{};
enum {
OPENXR_FRAME_STATUS_IDLE, //!< Frame is ready for initialization
OPENXR_FRAME_STATUS_READY, //!< Frame has been initialized and swapchains may be acquired
OPENXR_FRAME_STATUS_ENDING //!< All swapchains have been releazed, frame ready for presentation
} mFrameStatus{ OPENXR_FRAME_STATUS_IDLE };
std::condition_variable mFrameStatusSignal{};
std::mutex mFrameStatusMutex;
int mBarrier{ 0 };
int mNBarrier{ 0 };
std::condition_variable mBarrierSignal{};
std::mutex mBarrierMutex;
void viewerBarrier();
void registerToBarrier();
void unregisterFromBarrier();
};
}

@ -0,0 +1,64 @@
#include "openxrmenu.hpp"
#include "openxrmanagerimpl.hpp"
#include <openxr/openxr.h>
namespace MWVR
{
OpenXRMenu::OpenXRMenu(osg::ref_ptr<OpenXRManager> XR, osg::ref_ptr<osg::State> state, const std::string& title, int width, int height, osg::Vec2 extent_meters)
: OpenXRView(XR)
, mTitle(title)
{
setWidth(width);
setHeight(height);
setSamples(1);
if (!realize(state))
throw std::runtime_error(std::string("Failed to create swapchain for menu \"") + title + "\"");
mLayer.reset(new XrCompositionLayerQuad);
mLayer->type = XR_TYPE_COMPOSITION_LAYER_QUAD;
mLayer->next = nullptr;
mLayer->layerFlags = 0;
mLayer->space = XR->impl().mReferenceSpaceStage;
mLayer->eyeVisibility = XR_EYE_VISIBILITY_BOTH;
mLayer->subImage = swapchain().subImage();
mLayer->size.width = extent_meters.x();
mLayer->size.height = extent_meters.y();
// Orientation needs a norm of 1 to be accepted by OpenXR, so we default it to 0,0,0,1
mLayer->pose.orientation.w = 1.f;
updatePosition();
}
OpenXRMenu::~OpenXRMenu()
{
}
const XrCompositionLayerBaseHeader*
OpenXRMenu::layer()
{
updatePosition();
return reinterpret_cast<XrCompositionLayerBaseHeader*>(mLayer.get());
}
void OpenXRMenu::updatePosition()
{
if (!mPositionNeedsUpdate)
return;
if (!mXR->sessionRunning())
return;
// Menus are position one meter in front of the player, facing the player.
// Go via osg since OpenXR doesn't distribute a linear maths library
auto pose = mXR->impl().getLimbPose(TrackedLimb::HEAD, TrackedSpace::STAGE);
pose.position += pose.orientation * osg::Vec3(0, 0, -1);
mLayer->pose.position = osg::toXR(pose.position);
mLayer->pose.orientation = osg::toXR(-pose.orientation);
mPositionNeedsUpdate = false;
}
void OpenXRMenu::postrenderCallback(osg::RenderInfo& info)
{
OpenXRView::postrenderCallback(info);
}
}

@ -0,0 +1,28 @@
#ifndef OPENXR_MENU_HPP
#define OPENXR_MENU_HPP
#include "openxrview.hpp"
#include "openxrlayer.hpp"
struct XrCompositionLayerQuad;
namespace MWVR
{
class OpenXRMenu : public OpenXRView, public OpenXRLayer
{
public:
OpenXRMenu(osg::ref_ptr<OpenXRManager> XR, osg::ref_ptr<osg::State> state, const std::string& title, int width, int height, osg::Vec2 extent_meters);
~OpenXRMenu();
const XrCompositionLayerBaseHeader* layer() override;
const std::string& title() const { return mTitle; }
void updatePosition();
void postrenderCallback(osg::RenderInfo& renderInfo) override;
protected:
bool mPositionNeedsUpdate{ true };
std::unique_ptr<XrCompositionLayerQuad> mLayer = nullptr;
std::string mTitle;
};
}
#endif

@ -0,0 +1,213 @@
#include "openxrswapchain.hpp"
#include "openxrmanager.hpp"
#include "openxrmanagerimpl.hpp"
#include <components/debug/debuglog.hpp>
#include <Windows.h>
#include <openxr/openxr.h>
#include <openxr/openxr_platform.h>
#include <openxr/openxr_platform_defines.h>
#include <openxr/openxr_reflection.h>
namespace MWVR {
class OpenXRSwapchainImpl
{
public:
enum SubView
{
LEFT_VIEW = 0,
RIGHT_VIEW = 1,
SUBVIEW_MAX = RIGHT_VIEW, //!< Used to size subview arrays. Not a valid input.
};
OpenXRSwapchainImpl(osg::ref_ptr<OpenXRManager> XR, osg::ref_ptr<osg::State> state, OpenXRSwapchain::Config config);
~OpenXRSwapchainImpl();
osg::ref_ptr<OpenXRTextureBuffer> prepareNextSwapchainImage();
void releaseSwapchainImage();
void beginFrame(osg::GraphicsContext* gc);
void endFrame(osg::GraphicsContext* gc);
osg::ref_ptr<OpenXRManager> mXR;
XrSwapchain mSwapchain = XR_NULL_HANDLE;
std::vector<XrSwapchainImageOpenGLKHR> mSwapchainImageBuffers{};
std::vector<osg::ref_ptr<OpenXRTextureBuffer> > mTextureBuffers{};
XrSwapchainSubImage mSubImage{};
int32_t mWidth = -1;
int32_t mHeight = -1;
int32_t mSamples = -1;
int64_t mSwapchainColorFormat = -1;
OpenXRTextureBuffer* mCurrentBuffer = nullptr;
};
OpenXRSwapchainImpl::OpenXRSwapchainImpl(
osg::ref_ptr<OpenXRManager> XR, osg::ref_ptr<osg::State> state, OpenXRSwapchain::Config config)
: mXR(XR)
, mWidth(config.width)
, mHeight(config.height)
, mSamples(config.samples)
{
if (mWidth <= 0)
throw std::invalid_argument("Width must be a positive integer");
if (mHeight <= 0)
throw std::invalid_argument("Height must be a positive integer");
if (mSamples <= 0)
throw std::invalid_argument("Samples must be a positive integer");
auto& pXR = mXR->impl();
// Select a swapchain format.
uint32_t swapchainFormatCount;
CHECK_XRCMD(xrEnumerateSwapchainFormats(pXR.mSession, 0, &swapchainFormatCount, nullptr));
std::vector<int64_t> swapchainFormats(swapchainFormatCount);
CHECK_XRCMD(xrEnumerateSwapchainFormats(pXR.mSession, (uint32_t)swapchainFormats.size(), &swapchainFormatCount, swapchainFormats.data()));
// List of supported color swapchain formats.
constexpr int64_t SupportedColorSwapchainFormats[] = {
GL_RGBA8,
GL_RGBA8_SNORM,
};
auto swapchainFormatIt =
std::find_first_of(swapchainFormats.begin(), swapchainFormats.end(), std::begin(SupportedColorSwapchainFormats),
std::end(SupportedColorSwapchainFormats));
if (swapchainFormatIt == swapchainFormats.end()) {
Log(Debug::Error) << "No swapchain format supported at runtime";
}
mSwapchainColorFormat = *swapchainFormatIt;
Log(Debug::Verbose) << "Creating swapchain with dimensions Width=" << mWidth << " Heigh=" << mHeight << " SampleCount=" << mSamples;
// Create the swapchain.
XrSwapchainCreateInfo swapchainCreateInfo{ XR_TYPE_SWAPCHAIN_CREATE_INFO };
swapchainCreateInfo.arraySize = 1;
swapchainCreateInfo.format = mSwapchainColorFormat;
swapchainCreateInfo.width = mWidth;
swapchainCreateInfo.height = mHeight;
swapchainCreateInfo.mipCount = 1;
swapchainCreateInfo.faceCount = 1;
swapchainCreateInfo.sampleCount = mSamples;
swapchainCreateInfo.usageFlags = XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT;
CHECK_XRCMD(xrCreateSwapchain(pXR.mSession, &swapchainCreateInfo, &mSwapchain));
uint32_t imageCount = 0;
CHECK_XRCMD(xrEnumerateSwapchainImages(mSwapchain, 0, &imageCount, nullptr));
mSwapchainImageBuffers.resize(imageCount, { XR_TYPE_SWAPCHAIN_IMAGE_OPENGL_KHR });
CHECK_XRCMD(xrEnumerateSwapchainImages(mSwapchain, imageCount, &imageCount, reinterpret_cast<XrSwapchainImageBaseHeader*>(mSwapchainImageBuffers.data())));
for (const auto& swapchainImage : mSwapchainImageBuffers)
mTextureBuffers.push_back(new OpenXRTextureBuffer(state, swapchainImage.image, mWidth, mHeight, 0));
mSubImage.swapchain = mSwapchain;
mSubImage.imageRect.offset = { 0, 0 };
mSubImage.imageRect.extent = { mWidth, mHeight };
}
OpenXRSwapchainImpl::~OpenXRSwapchainImpl()
{
if (mSwapchain)
CHECK_XRCMD(xrDestroySwapchain(mSwapchain));
}
osg::ref_ptr<OpenXRTextureBuffer>
OpenXRSwapchainImpl::prepareNextSwapchainImage()
{
if (!mXR->sessionRunning())
return nullptr;
XrSwapchainImageAcquireInfo acquireInfo{ XR_TYPE_SWAPCHAIN_IMAGE_ACQUIRE_INFO };
uint32_t swapchainImageIndex = 0;
CHECK_XRCMD(xrAcquireSwapchainImage(mSwapchain, &acquireInfo, &swapchainImageIndex));
XrSwapchainImageWaitInfo waitInfo{ XR_TYPE_SWAPCHAIN_IMAGE_WAIT_INFO };
waitInfo.timeout = XR_INFINITE_DURATION;
CHECK_XRCMD(xrWaitSwapchainImage(mSwapchain, &waitInfo));
return mTextureBuffers[swapchainImageIndex];
}
void
OpenXRSwapchainImpl::releaseSwapchainImage()
{
if (!mXR->sessionRunning())
return;
XrSwapchainImageReleaseInfo releaseInfo{ XR_TYPE_SWAPCHAIN_IMAGE_RELEASE_INFO };
CHECK_XRCMD(xrReleaseSwapchainImage(mSwapchain, &releaseInfo));
//mCurrentBuffer = nullptr;
}
void OpenXRSwapchainImpl::beginFrame(osg::GraphicsContext* gc)
{
mCurrentBuffer = prepareNextSwapchainImage();
if (mCurrentBuffer)
mCurrentBuffer->beginFrame(gc);
}
void OpenXRSwapchainImpl::endFrame(osg::GraphicsContext* gc)
{
if (mCurrentBuffer)
mCurrentBuffer->endFrame(gc);
releaseSwapchainImage();
}
OpenXRSwapchain::OpenXRSwapchain(
osg::ref_ptr<OpenXRManager> XR, osg::ref_ptr<osg::State> state, OpenXRSwapchain::Config config)
: mPrivate(new OpenXRSwapchainImpl(XR, state, config))
{
}
OpenXRSwapchain::~OpenXRSwapchain()
{
}
osg::ref_ptr<OpenXRTextureBuffer> OpenXRSwapchain::prepareNextSwapchainImage()
{
return impl().prepareNextSwapchainImage();
}
void OpenXRSwapchain::releaseSwapchainImage()
{
impl().releaseSwapchainImage();
}
void OpenXRSwapchain::beginFrame(osg::GraphicsContext* gc)
{
impl().beginFrame(gc);
}
void OpenXRSwapchain::endFrame(osg::GraphicsContext* gc)
{
impl().endFrame(gc);
}
const XrSwapchainSubImage&
OpenXRSwapchain::subImage(void) const
{
return impl().mSubImage;
}
int OpenXRSwapchain::width()
{
return impl().mWidth;
}
int OpenXRSwapchain::height()
{
return impl().mHeight;
}
int OpenXRSwapchain::samples()
{
return impl().mSamples;
}
OpenXRTextureBuffer* OpenXRSwapchain::current()
{
return impl().mCurrentBuffer;
}
}

@ -0,0 +1,58 @@
#ifndef OPENXR_SWAPCHAIN_HPP
#define OPENXR_SWAPCHAIN_HPP
#include "openxrmanager.hpp"
#include "openxrtexture.hpp"
struct XrSwapchainSubImage;
namespace MWVR
{
class OpenXRSwapchainImpl;
class OpenXRSwapchain
{
public:
struct Config
{
std::vector<int64_t> requestedFormats{};
int width = -1;
int height = -1;
int samples = -1;
};
public:
OpenXRSwapchain(osg::ref_ptr<OpenXRManager> XR, osg::ref_ptr<osg::State> state, Config config);
~OpenXRSwapchain();
public:
//! Get the next color buffer.
//! \return The GL texture ID of the now current swapchain image
osg::ref_ptr<OpenXRTextureBuffer> prepareNextSwapchainImage();
//! Release current color buffer. Do not forget to call this after rendering to the color buffer.
void releaseSwapchainImage();
//! Prepare for render (set FBO)
void beginFrame(osg::GraphicsContext* gc);
//! Finalize render
void endFrame(osg::GraphicsContext* gc);
//! Get the view surface
const XrSwapchainSubImage& subImage(void) const;
//! Width of the view surface
int width();
//! Height of the view surface
int height();
//! Samples of the view surface
int samples();
//! Get the current texture
OpenXRTextureBuffer* current();
//! Get the private implementation
OpenXRSwapchainImpl& impl() { return *mPrivate; }
//! Get the private implementation
const OpenXRSwapchainImpl& impl() const { return *mPrivate; }
private:
std::unique_ptr<OpenXRSwapchainImpl> mPrivate;
};
}
#endif

@ -14,14 +14,12 @@
namespace MWVR
{
OpenXRTextureBuffer::OpenXRTextureBuffer(
osg::ref_ptr<OpenXRManager> XR,
osg::ref_ptr<osg::State> state,
uint32_t XRColorBuffer,
std::size_t width,
std::size_t height,
uint32_t msaaSamples)
: mXR(XR)
, mState(state)
: mState(state)
, mWidth(width)
, mHeight(height)
, mXRColorBuffer(XRColorBuffer)
@ -136,9 +134,9 @@ namespace MWVR
mFBO = mMSAAFBO = mDepthBuffer = mMSAAColorBuffer = mMSAADepthBuffer = 0;
}
void OpenXRTextureBuffer::beginFrame(osg::RenderInfo& renderInfo)
void OpenXRTextureBuffer::beginFrame(osg::GraphicsContext* gc)
{
auto state = renderInfo.getState();
auto state = gc->getState();
auto* gl = osg::GLExtensions::Get(state->getContextID(), false);
@ -152,9 +150,9 @@ namespace MWVR
}
}
void OpenXRTextureBuffer::endFrame(osg::RenderInfo& renderInfo)
void OpenXRTextureBuffer::endFrame(osg::GraphicsContext* gc)
{
auto* state = renderInfo.getState();
auto* state = gc->getState();
auto* gl = osg::GLExtensions::Get(state->getContextID(), false);
if (mMSAASamples == 0)
{
@ -165,8 +163,17 @@ namespace MWVR
gl->glBindFramebuffer(GL_READ_FRAMEBUFFER_EXT, mMSAAFBO);
gl->glBindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT, mFBO);
gl->glBlitFramebuffer(0, 0, mWidth, mHeight, 0, 0, mWidth, mHeight, GL_COLOR_BUFFER_BIT, GL_NEAREST);
gl->glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
gl->glBindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT, 0);
gl->glBindFramebuffer(GL_READ_FRAMEBUFFER_EXT, 0);
}
}
void OpenXRTextureBuffer::blit(osg::GraphicsContext* gc, int x, int y, int w, int h)
{
auto* state = gc->getState();
auto* gl = osg::GLExtensions::Get(state->getContextID(), false);
gl->glBindFramebuffer(GL_READ_FRAMEBUFFER_EXT, mFBO);
gl->glBlitFramebuffer(0, 0, mWidth, mHeight, x, y, w, h, GL_COLOR_BUFFER_BIT, GL_NEAREST);
gl->glBindFramebuffer(GL_READ_FRAMEBUFFER_EXT, 0);
}
}

@ -13,7 +13,7 @@ namespace MWVR
class OpenXRTextureBuffer : public osg::Referenced
{
public:
OpenXRTextureBuffer(osg::ref_ptr<OpenXRManager> XR, osg::ref_ptr<osg::State> state, uint32_t XRColorBuffer, std::size_t width, std::size_t height, uint32_t msaaSamples);
OpenXRTextureBuffer(osg::ref_ptr<osg::State> state, uint32_t XRColorBuffer, std::size_t width, std::size_t height, uint32_t msaaSamples);
~OpenXRTextureBuffer();
void destroy(osg::State* state);
@ -22,14 +22,16 @@ namespace MWVR
auto height() const { return mHeight; }
auto msaaSamples() const { return mMSAASamples; }
void beginFrame(osg::RenderInfo& renderInfo);
void endFrame(osg::RenderInfo& renderInfo);
void beginFrame(osg::GraphicsContext* gc);
void endFrame(osg::GraphicsContext* gc);
void writeToJpg(osg::State& state, std::string filename);
private:
osg::observer_ptr<OpenXRManager> mXR;
uint32_t fbo(void) const { return mFBO; }
void blit(osg::GraphicsContext* gc, int x, int y, int w, int h);
private:
// Set aside a weak pointer to the constructor state to use when freeing FBOs, if no state is given to destroy()
osg::observer_ptr<osg::State> mState;

@ -13,277 +13,28 @@
#include <openxr/openxr_platform_defines.h>
#include <openxr/openxr_reflection.h>
namespace MWVR {
#include <osg/Camera>
#include <osgViewer/Renderer>
#include <vector>
#include <array>
#include <iostream>
namespace osg {
Quat fromXR(XrQuaternionf quat)
{
return Quat{ quat.x, quat.y, quat.z, quat.w };
}
}
namespace MWVR
{
class OpenXRViewImpl
{
public:
enum SubView
{
LEFT_VIEW = 0,
RIGHT_VIEW = 1,
SUBVIEW_MAX = RIGHT_VIEW, //!< Used to size subview arrays. Not a valid input.
};
OpenXRViewImpl(osg::ref_ptr<OpenXRManager> XR, osg::ref_ptr<osg::State> state, float metersPerUnit, unsigned int viewIndex);
~OpenXRViewImpl();
osg::ref_ptr<OpenXRTextureBuffer> prepareNextSwapchainImage();
void releaseSwapchainImage();
void prerenderCallback(osg::RenderInfo& renderInfo);
void postrenderCallback(osg::RenderInfo& renderInfo);
osg::Camera* createCamera(int eye, const osg::Vec4& clearColor, osg::GraphicsContext* gc);
osg::Matrix projectionMatrix();
osg::Matrix viewMatrix();
osg::ref_ptr<OpenXRManager> mXR;
XrSwapchain mSwapchain = XR_NULL_HANDLE;
std::vector<XrSwapchainImageOpenGLKHR> mSwapchainImageBuffers{};
std::vector<osg::ref_ptr<OpenXRTextureBuffer> > mTextureBuffers{};
int32_t mWidth = -1;
int32_t mHeight = -1;
int64_t mSwapchainColorFormat = -1;
XrViewConfigurationView mConfig{ XR_TYPE_VIEW_CONFIGURATION_VIEW };
XrSwapchainSubImage mSubImage{};
OpenXRTextureBuffer* mCurrentBuffer = nullptr;
float mMetersPerUnit = 1.f;
int mViewIndex = -1;
};
OpenXRViewImpl::OpenXRViewImpl(
osg::ref_ptr<OpenXRManager> XR,
osg::ref_ptr<osg::State> state,
float metersPerUnit,
unsigned int viewIndex)
OpenXRView::OpenXRView(
osg::ref_ptr<OpenXRManager> XR)
: mXR(XR)
, mConfig()
, mMetersPerUnit(metersPerUnit)
, mViewIndex(viewIndex)
, mSwapchain(nullptr)
, mSwapchainConfig{}
{
auto& pXR = *(mXR->mPrivate);
if (viewIndex >= pXR.mConfigViews.size())
throw std::logic_error("viewIndex out of range");
mConfig = pXR.mConfigViews[viewIndex];
// Select a swapchain format.
uint32_t swapchainFormatCount;
CHECK_XRCMD(xrEnumerateSwapchainFormats(pXR.mSession, 0, &swapchainFormatCount, nullptr));
std::vector<int64_t> swapchainFormats(swapchainFormatCount);
CHECK_XRCMD(xrEnumerateSwapchainFormats(pXR.mSession, (uint32_t)swapchainFormats.size(), &swapchainFormatCount, swapchainFormats.data()));
// List of supported color swapchain formats.
constexpr int64_t SupportedColorSwapchainFormats[] = {
mSwapchainConfig.requestedFormats = {
GL_RGBA8,
GL_RGBA8_SNORM,
};
auto swapchainFormatIt =
std::find_first_of(swapchainFormats.begin(), swapchainFormats.end(), std::begin(SupportedColorSwapchainFormats),
std::end(SupportedColorSwapchainFormats));
if (swapchainFormatIt == swapchainFormats.end()) {
Log(Debug::Error) << "No swapchain format supported at runtime";
}
mSwapchainColorFormat = *swapchainFormatIt;
Log(Debug::Verbose) << "Creating swapchain with dimensions Width=" << mConfig.recommendedImageRectWidth << " Heigh=" << mConfig.recommendedImageRectHeight << " SampleCount=" << mConfig.recommendedSwapchainSampleCount;
// Create the swapchain.
XrSwapchainCreateInfo swapchainCreateInfo{ XR_TYPE_SWAPCHAIN_CREATE_INFO };
swapchainCreateInfo.arraySize = 1;
swapchainCreateInfo.format = mSwapchainColorFormat;
swapchainCreateInfo.width = mConfig.recommendedImageRectWidth;
swapchainCreateInfo.height = mConfig.recommendedImageRectHeight;
swapchainCreateInfo.mipCount = 1;
swapchainCreateInfo.faceCount = 1;
swapchainCreateInfo.sampleCount = mConfig.recommendedSwapchainSampleCount;
swapchainCreateInfo.usageFlags = XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT;
mWidth = mConfig.recommendedImageRectWidth;
mHeight = mConfig.recommendedImageRectHeight;
CHECK_XRCMD(xrCreateSwapchain(pXR.mSession, &swapchainCreateInfo, &mSwapchain));
uint32_t imageCount = 0;
CHECK_XRCMD(xrEnumerateSwapchainImages(mSwapchain, 0, &imageCount, nullptr));
mSwapchainImageBuffers.resize(imageCount, { XR_TYPE_SWAPCHAIN_IMAGE_OPENGL_KHR });
CHECK_XRCMD(xrEnumerateSwapchainImages(mSwapchain, imageCount, &imageCount, reinterpret_cast<XrSwapchainImageBaseHeader*>(mSwapchainImageBuffers.data())));
for (const auto& swapchainImage : mSwapchainImageBuffers)
mTextureBuffers.push_back(new OpenXRTextureBuffer(mXR, state, swapchainImage.image, mWidth, mHeight, 0));
mSubImage.swapchain = mSwapchain;
mSubImage.imageRect.offset = { 0, 0 };
mSubImage.imageRect.extent = { mWidth, mHeight };
mXR->setViewSubImage(viewIndex, mSubImage);
}
OpenXRViewImpl::~OpenXRViewImpl()
{
if (mSwapchain)
CHECK_XRCMD(xrDestroySwapchain(mSwapchain));
}
osg::ref_ptr<OpenXRTextureBuffer>
OpenXRViewImpl::prepareNextSwapchainImage()
{
if (!mXR->sessionRunning())
return nullptr;
XrSwapchainImageAcquireInfo acquireInfo{ XR_TYPE_SWAPCHAIN_IMAGE_ACQUIRE_INFO };
uint32_t swapchainImageIndex = 0;
CHECK_XRCMD(xrAcquireSwapchainImage(mSwapchain, &acquireInfo, &swapchainImageIndex));
XrSwapchainImageWaitInfo waitInfo{ XR_TYPE_SWAPCHAIN_IMAGE_WAIT_INFO };
waitInfo.timeout = XR_INFINITE_DURATION;
CHECK_XRCMD(xrWaitSwapchainImage(mSwapchain, &waitInfo));
return mTextureBuffers[swapchainImageIndex];
}
void
OpenXRViewImpl::releaseSwapchainImage()
{
if (!mXR->sessionRunning())
return;
XrSwapchainImageReleaseInfo releaseInfo{ XR_TYPE_SWAPCHAIN_IMAGE_RELEASE_INFO };
CHECK_XRCMD(xrReleaseSwapchainImage(mSwapchain, &releaseInfo));
}
void OpenXRViewImpl::prerenderCallback(osg::RenderInfo& renderInfo)
{
mCurrentBuffer = prepareNextSwapchainImage();
if(mCurrentBuffer)
mCurrentBuffer->beginFrame(renderInfo);
}
void OpenXRViewImpl::postrenderCallback(osg::RenderInfo& renderInfo)
{
if (mCurrentBuffer)
mCurrentBuffer->endFrame(renderInfo);
releaseSwapchainImage();
}
// Some headers like to define these.
#ifdef near
#undef near
#endif
#ifdef far
#undef far
#endif
static osg::Matrix
perspectiveFovMatrix(float near, float far, XrFovf fov)
{
const float tanLeft = tanf(fov.angleLeft);
const float tanRight = tanf(fov.angleRight);
const float tanDown = tanf(fov.angleDown);
const float tanUp = tanf(fov.angleUp);
const float tanWidth = tanRight - tanLeft;
const float tanHeight = tanUp - tanDown;
// Set to nearZ for a [-1,1] Z clip space (OpenGL / OpenGL ES).
// Set to zero for a [0,1] Z clip space (Vulkan / D3D / Metal).
const float offset = near;
float matrix[16] = {};
if (far <= near) {
// place the far plane at infinity
matrix[0] = 2 / tanWidth;
matrix[4] = 0;
matrix[8] = (tanRight + tanLeft) / tanWidth;
matrix[12] = 0;
matrix[1] = 0;
matrix[5] = 2 / tanHeight;
matrix[9] = (tanUp + tanDown) / tanHeight;
matrix[13] = 0;
matrix[2] = 0;
matrix[6] = 0;
matrix[10] = -1;
matrix[14] = -(near + offset);
matrix[3] = 0;
matrix[7] = 0;
matrix[11] = -1;
matrix[15] = 0;
}
else {
// normal projection
matrix[0] = 2 / tanWidth;
matrix[4] = 0;
matrix[8] = (tanRight + tanLeft) / tanWidth;
matrix[12] = 0;
matrix[1] = 0;
matrix[5] = 2 / tanHeight;
matrix[9] = (tanUp + tanDown) / tanHeight;
matrix[13] = 0;
matrix[2] = 0;
matrix[6] = 0;
matrix[10] = -(far + offset) / (far - near);
matrix[14] = -(far * (near + offset)) / (far - near);
matrix[3] = 0;
matrix[7] = 0;
matrix[11] = -1;
matrix[15] = 0;
}
return osg::Matrix(matrix);
}
osg::Matrix OpenXRViewImpl::projectionMatrix()
{
auto hmdViews = mXR->mPrivate->getHmdViews();
float near = Settings::Manager::getFloat("near clip", "Camera");
float far = Settings::Manager::getFloat("viewing distance", "Camera");
//return perspectiveFovMatrix()
return perspectiveFovMatrix(near, far, hmdViews[mViewIndex].fov);
mXR->registerToBarrier();
}
osg::Matrix OpenXRViewImpl::viewMatrix()
OpenXRView::~OpenXRView()
{
osg::Matrix viewMatrix;
auto hmdViews = mXR->mPrivate->getHmdViews();
auto pose = hmdViews[mViewIndex].pose;
osg::Vec3 position = osg::Vec3(pose.position.x, pose.position.y, pose.position.z);
// invert orientation (conjugate of Quaternion) and position to apply to the view matrix as offset
// TODO: Why invert/conjugate?
viewMatrix.setTrans(position);
viewMatrix.postMultRotate(osg::fromXR(pose.orientation));
// Scale to world units
viewMatrix.postMultScale(osg::Vec3d(mMetersPerUnit, mMetersPerUnit, mMetersPerUnit));
return viewMatrix;
mXR->unregisterFromBarrier();
}
osg::Camera* OpenXRViewImpl::createCamera(int eye, const osg::Vec4& clearColor, osg::GraphicsContext* gc)
osg::Camera* OpenXRView::createCamera(int eye, const osg::Vec4& clearColor, osg::GraphicsContext* gc)
{
osg::ref_ptr<osg::Camera> camera = new osg::Camera();
camera->setClearColor(clearColor);
@ -293,7 +44,7 @@ namespace MWVR
camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
camera->setAllowEventFocus(false);
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
camera->setViewport(0, 0, mWidth, mHeight);
camera->setViewport(0, 0, mSwapchain->width(), mSwapchain->height());
camera->setGraphicsContext(gc);
camera->setInitialDrawCallback(new OpenXRView::InitialDrawCallback());
@ -303,73 +54,54 @@ namespace MWVR
return camera.release();
}
OpenXRView::OpenXRView(
osg::ref_ptr<OpenXRManager> XR, osg::ref_ptr<osg::State> state, float metersPerUnit, unsigned int viewIndex)
: mPrivate(new OpenXRViewImpl(XR, state, metersPerUnit, viewIndex))
void OpenXRView::setWidth(int width)
{
mSwapchainConfig.width = width;
}
OpenXRView::~OpenXRView()
void OpenXRView::setHeight(int height)
{
mSwapchainConfig.height = height;
}
//! Get the next color buffer
osg::ref_ptr<OpenXRTextureBuffer> OpenXRView::prepareNextSwapchainImage()
void OpenXRView::setSamples(int samples)
{
return mPrivate->prepareNextSwapchainImage();
}
//! Release current color buffer. Do not forget to call this after rendering to the color buffer.
void OpenXRView::releaseSwapchainImage()
{
mPrivate->releaseSwapchainImage();
}
void OpenXRView::PredrawCallback::operator()(osg::RenderInfo& info) const
{
mView->prerenderCallback(info);
}
void OpenXRView::PostdrawCallback::operator()(osg::RenderInfo& info) const
{
mView->postrenderCallback(info);
mSwapchainConfig.samples = samples;
}
void OpenXRView::prerenderCallback(osg::RenderInfo& renderInfo)
{
mPrivate->prerenderCallback(renderInfo);
mXR->beginFrame();
if(mSwapchain)
mSwapchain->beginFrame(renderInfo.getState()->getGraphicsContext());
}
void OpenXRView::postrenderCallback(osg::RenderInfo& renderInfo)
{
mPrivate->postrenderCallback(renderInfo);
if (mSwapchain)
mSwapchain->endFrame(renderInfo.getState()->getGraphicsContext());
mXR->viewerBarrier();
}
osg::Camera* OpenXRView::createCamera(int eye, const osg::Vec4& clearColor, osg::GraphicsContext* gc)
bool OpenXRView::realize(osg::ref_ptr<osg::State> state)
{
return mPrivate->createCamera(eye, clearColor, gc);
}
try {
mSwapchain.reset(new OpenXRSwapchain(mXR, state, mSwapchainConfig));
}
catch (...)
{
}
osg::Matrix OpenXRView::projectionMatrix()
{
return mPrivate->projectionMatrix();
return !!mSwapchain;
}
osg::Matrix OpenXRView::viewMatrix()
void OpenXRView::PredrawCallback::operator()(osg::RenderInfo& info) const
{
return mPrivate->viewMatrix();
mView->prerenderCallback(info);
}
void OpenXRView::InitialDrawCallback::operator()(osg::RenderInfo& renderInfo) const
void OpenXRView::PostdrawCallback::operator()(osg::RenderInfo& info) const
{
osg::GraphicsOperation* graphicsOperation = renderInfo.getCurrentCamera()->getRenderer();
osgViewer::Renderer* renderer = dynamic_cast<osgViewer::Renderer*>(graphicsOperation);
if (renderer != nullptr)
{
// Disable normal OSG FBO camera setup because it will undo the MSAA FBO configuration.
renderer->setCameraRequiresSetUp(false);
}
mView->postrenderCallback(info);
}
}

@ -2,21 +2,19 @@
#define OPENXR_VIEW_HPP
#include "openxrmanager.hpp"
#include "openxrtexture.hpp"
#include "openxrswapchain.hpp"
struct XrSwapchainSubImage;
namespace MWVR
{
class OpenXRViewImpl;
class OpenXRView : public osg::Referenced
{
public:
class PredrawCallback : public osg::Camera::DrawCallback
{
public:
PredrawCallback(osg::Camera* camera, OpenXRViewImpl* view)
PredrawCallback(osg::Camera* camera, OpenXRView* view)
: mCamera(camera)
, mView(view)
{}
@ -26,12 +24,12 @@ namespace MWVR
private:
osg::observer_ptr<osg::Camera> mCamera;
OpenXRViewImpl* mView;
OpenXRView* mView;
};
class PostdrawCallback : public osg::Camera::DrawCallback
{
public:
PostdrawCallback(osg::Camera* camera, OpenXRViewImpl* view)
PostdrawCallback(osg::Camera* camera, OpenXRView* view)
: mCamera(camera)
, mView(view)
{}
@ -41,7 +39,7 @@ namespace MWVR
private:
osg::observer_ptr<osg::Camera> mCamera;
OpenXRViewImpl* mView;
OpenXRView* mView;
};
class InitialDrawCallback : public osg::Camera::DrawCallback
@ -50,27 +48,29 @@ namespace MWVR
virtual void operator()(osg::RenderInfo& renderInfo) const;
};
public:
OpenXRView(osg::ref_ptr<OpenXRManager> XR, osg::ref_ptr<osg::State> state, float metersPerUnit, unsigned int viewIndex);
~OpenXRView();
protected:
OpenXRView(osg::ref_ptr<OpenXRManager> XR);
virtual ~OpenXRView();
void setWidth(int width);
void setHeight(int height);
void setSamples(int samples);
//! Get the next color buffer.
//! \return The GL texture ID of the now current swapchain image
osg::ref_ptr<OpenXRTextureBuffer> prepareNextSwapchainImage();
//! Release current color buffer. Do not forget to call this after rendering to the color buffer.
void releaseSwapchainImage();
//! Prepare for render
void prerenderCallback(osg::RenderInfo& renderInfo);
public:
//! Prepare for render (set FBO)
virtual void prerenderCallback(osg::RenderInfo& renderInfo);
//! Finalize render
void postrenderCallback(osg::RenderInfo& renderInfo);
virtual void postrenderCallback(osg::RenderInfo& renderInfo);
//! Create camera for this view
osg::Camera* createCamera(int eye, const osg::Vec4& clearColor, osg::GraphicsContext* gc);
//! Projection offset for this view
osg::Matrix projectionMatrix();
//! View offset for this view
osg::Matrix viewMatrix();
std::unique_ptr<OpenXRViewImpl> mPrivate;
osg::Camera* createCamera(int order, const osg::Vec4& clearColor, osg::GraphicsContext* gc);
//! Get the view surface
OpenXRSwapchain& swapchain(void) { return *mSwapchain; }
//! Create the view surface
bool realize(osg::ref_ptr<osg::State> state);
protected:
osg::ref_ptr<OpenXRManager> mXR;
std::unique_ptr<OpenXRSwapchain> mSwapchain;
OpenXRSwapchain::Config mSwapchainConfig;
};
}

@ -1,22 +1,37 @@
#include "openxrviewer.hpp"
#include "openxrmanagerimpl.hpp"
#include "Windows.h"
#include "../mwrender/vismask.hpp"
namespace MWVR
{
OpenXRViewer::OpenXRViewer(
osg::ref_ptr<OpenXRManager> XR,
osg::ref_ptr<OpenXRManager::RealizeOperation> realizeOperation,
osg::ref_ptr<osgViewer::Viewer> viewer,
float metersPerUnit)
: mXR(XR)
, mRealizeOperation(realizeOperation)
, mRealizeOperation(new RealizeOperation(XR, this))
, mViewer(viewer)
, mMetersPerUnit(metersPerUnit)
, mConfigured(false)
, mCompositionLayerProjectionViews(2, {XR_TYPE_COMPOSITION_LAYER_PROJECTION_VIEW})
{
mViewer->setRealizeOperation(mRealizeOperation);
//auto* mainContext = mViewer->getCamera()->getGraphicsContext();
//assert(mainContext);
//auto* mainTraits = mainContext->getTraits();
//osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits(*mainTraits);
//traits->sharedContext = mainContext;
//mViewerGW = new SDLUtil::GraphicsWindowSDL2(traits);
//if (!mViewerGW->valid()) throw std::runtime_error("Failed to create GraphicsContext");
//mLeftGW = new SDLUtil::GraphicsWindowSDL2(traits);
//if (!mLeftGW->valid()) throw std::runtime_error("Failed to create GraphicsContext");
//mRightGW = new SDLUtil::GraphicsWindowSDL2(traits);
//if (!mRightGW->valid()) throw std::runtime_error("Failed to create GraphicsContext");
}
OpenXRViewer::~OpenXRViewer(void)
@ -27,61 +42,77 @@ namespace MWVR
OpenXRViewer::traverse(
osg::NodeVisitor& visitor)
{
osg::ref_ptr<OpenXRManager::RealizeOperation> realizeOperation = nullptr;
Log(Debug::Verbose) << "Traversal";
if (mRealizeOperation->realized())
osg::Group::traverse(visitor);
}
if (mRealizeOperation.lock(realizeOperation))
if (mRealizeOperation->realized())
if (configure())
osg::Group::traverse(visitor);
const XrCompositionLayerBaseHeader*
OpenXRViewer::layer()
{
auto stageViews = mXR->impl().getStageViews();
mCompositionLayerProjectionViews[0].pose = stageViews[0].pose;
mCompositionLayerProjectionViews[1].pose = stageViews[1].pose;
mCompositionLayerProjectionViews[0].fov = stageViews[0].fov;
mCompositionLayerProjectionViews[1].fov = stageViews[1].fov;
return reinterpret_cast<XrCompositionLayerBaseHeader*>(mLayer.get());
}
bool
OpenXRViewer::configure()
void OpenXRViewer::traversals()
{
if (mConfigured)
return true;
Log(Debug::Verbose) << "Pre-Update";
mViewer->updateTraversal();
Log(Debug::Verbose) << "Post-Update";
Log(Debug::Verbose) << "Pre-Rendering";
mViewer->renderingTraversals();
Log(Debug::Verbose) << "Post-Rendering";
}
auto context = mViewer->getCamera()->getGraphicsContext();
void OpenXRViewer::realize(osg::GraphicsContext* context)
{
std::unique_lock<std::mutex> lock(mMutex);
if (mConfigured)
return;
if (!context->isCurrent())
if (!context->makeCurrent())
{
Log(Debug::Warning) << "OpenXRViewer::configure() failed to make graphics context current.";
return false;
throw std::logic_error("OpenXRViewer::configure() failed to make graphics context current.");
}
context->setSwapCallback(new OpenXRManager::SwapBuffersCallback(mXR));
auto DC = wglGetCurrentDC();
auto GLRC = wglGetCurrentContext();
mMainCamera = mViewer->getCamera();
mMainCamera->setName("Main");
mMainCamera->setInitialDrawCallback(new OpenXRView::InitialDrawCallback());
if (DC != mXR->mPrivate->mGraphicsBinding.hDC)
{
Log(Debug::Warning) << "Graphics DC does not match DC used to construct XR context";
}
// Use the main camera to render any GUI to the OpenXR GUI quad's swapchain.
// (When swapping the window buffer we'll blit the mirror texture to it instead.)
mMainCamera->setCullMask(MWRender::Mask_GUI);
mMainCamera->getGraphicsContext()->setSwapCallback(new OpenXRViewer::SwapBuffersCallback(this));
if (GLRC != mXR->mPrivate->mGraphicsBinding.hGLRC)
{
Log(Debug::Warning) << "Graphics GLRC does not match GLRC used to construct XR context";
}
osg::Vec4 clearColor = mMainCamera->getClearColor();
mViews[OpenXRWorldView::LEFT_VIEW] = new OpenXRWorldView(mXR, context->getState(), mMetersPerUnit, OpenXRWorldView::LEFT_VIEW);
mViews[OpenXRWorldView::RIGHT_VIEW] = new OpenXRWorldView(mXR, context->getState(), mMetersPerUnit, OpenXRWorldView::RIGHT_VIEW);
osg::ref_ptr<osg::Camera> camera = mViewer->getCamera();
camera->setName("Main");
osg::Vec4 clearColor = camera->getClearColor();
mLeftCamera = mViews[OpenXRWorldView::LEFT_VIEW]->createCamera(OpenXRWorldView::LEFT_VIEW, clearColor, context);
mRightCamera = mViews[OpenXRWorldView::RIGHT_VIEW]->createCamera(OpenXRWorldView::RIGHT_VIEW, clearColor, context);
// Stereo cameras should only draw the scene (AR layers should later add minimap, health, etc.)
mViews[LEFT_VIEW] = new OpenXRView(mXR, context->getState(), mMetersPerUnit, 0);
mViews[RIGHT_VIEW] = new OpenXRView(mXR, context->getState(), mMetersPerUnit, 1);
//mLeftCamera->setGraphicsContext(mLeftGW);
//mRightCamera->setGraphicsContext(mRightGW);
osg::Camera* leftCamera = mViews[LEFT_VIEW]->createCamera(LEFT_VIEW, clearColor, context);
osg::Camera* rightCamera = mViews[RIGHT_VIEW]->createCamera(RIGHT_VIEW, clearColor, context);
mLeftCamera->setCullMask(~MWRender::Mask_GUI);
mRightCamera->setCullMask(~MWRender::Mask_GUI);
leftCamera->setName("LeftEye");
rightCamera->setName("RightEye");
mLeftCamera->setName("LeftEye");
mRightCamera->setName("RightEye");
mViewer->addSlave(leftCamera, mViews[LEFT_VIEW]->projectionMatrix(), mViews[LEFT_VIEW]->viewMatrix(), true);
mViewer->addSlave(rightCamera, mViews[RIGHT_VIEW]->projectionMatrix(), mViews[RIGHT_VIEW]->viewMatrix(), true);
mViewer->addSlave(mLeftCamera, mViews[OpenXRWorldView::LEFT_VIEW]->projectionMatrix(), mViews[OpenXRWorldView::LEFT_VIEW]->viewMatrix(), true);
mViewer->addSlave(mRightCamera, mViews[OpenXRWorldView::RIGHT_VIEW]->projectionMatrix(), mViews[OpenXRWorldView::RIGHT_VIEW]->viewMatrix(), true);
mViewer->getSlave(LEFT_VIEW)._updateSlaveCallback = new UpdateSlaveCallback(mXR, mViews[LEFT_VIEW]);
mViewer->getSlave(RIGHT_VIEW)._updateSlaveCallback = new UpdateSlaveCallback(mXR, mViews[RIGHT_VIEW]);
mViewer->getSlave(OpenXRWorldView::LEFT_VIEW)._updateSlaveCallback = new UpdateSlaveCallback(mXR, mViews[OpenXRWorldView::LEFT_VIEW], context);
mViewer->getSlave(OpenXRWorldView::RIGHT_VIEW)._updateSlaveCallback = new UpdateSlaveCallback(mXR, mViews[OpenXRWorldView::RIGHT_VIEW], context);
mViewer->setLightingMode(osg::View::SKY_LIGHT);
mViewer->setReleaseContextAtEndOfFrameHint(false);
@ -90,8 +121,71 @@ namespace MWVR
//camera->setGraphicsContext(nullptr);
mXRInput.reset(new OpenXRInputManager(mXR));
mLayer.reset(new XrCompositionLayerProjection);
mLayer->type = XR_TYPE_COMPOSITION_LAYER_PROJECTION;
mLayer->space = mXR->impl().mReferenceSpaceStage;
mLayer->viewCount = 2;
mLayer->views = mCompositionLayerProjectionViews.data();
mCompositionLayerProjectionViews[OpenXRWorldView::LEFT_VIEW].subImage = mViews[OpenXRWorldView::LEFT_VIEW]->swapchain().subImage();
mCompositionLayerProjectionViews[OpenXRWorldView::RIGHT_VIEW].subImage = mViews[OpenXRWorldView::RIGHT_VIEW]->swapchain().subImage();
mXR->impl().mLayerStack.setLayer(OpenXRLayerStack::WORLD_VIEW_LAYER, this);
OpenXRSwapchain::Config config;
config.requestedFormats = {
GL_RGBA8,
GL_RGBA8_SNORM,
};
config.width = mMainCamera->getViewport()->width();
config.height = mMainCamera->getViewport()->height();
config.samples = 1;
// Mirror texture doesn't have to be an OpenXR swapchain.
// It's just convenient.
mMirrorTextureSwapchain.reset(new OpenXRSwapchain(mXR, context->getState(), config));
mXRMenu.reset(new OpenXRMenu(mXR, context->getState(), "MainMenu", config.width, config.height, osg::Vec2(1.f, 1.f)));
mMainCamera->setPreDrawCallback(new OpenXRView::PredrawCallback(mMainCamera, mXRMenu.get()));
mMainCamera->setFinalDrawCallback(new OpenXRView::PostdrawCallback(mMainCamera, mXRMenu.get()));
mXR->impl().mLayerStack.setLayer(OpenXRLayerStack::MENU_VIEW_LAYER, mXRMenu.get());
mConfigured = true;
return true;
}
void OpenXRViewer::blitEyesToMirrorTexture(osg::GraphicsContext* gc) const
{
mMirrorTextureSwapchain->beginFrame(gc);
mViews[OpenXRWorldView::LEFT_VIEW]->swapchain().current()->blit(gc, 0, 0, mMirrorTextureSwapchain->width() / 2, mMirrorTextureSwapchain->height());
mViews[OpenXRWorldView::RIGHT_VIEW]->swapchain().current()->blit(gc, mMirrorTextureSwapchain->width() / 2, 0, mMirrorTextureSwapchain->width(), mMirrorTextureSwapchain->height());
//mXRMenu->swapchain().current()->blit(gc, 0, 0, mMirrorTextureSwapchain->width() / 2, mMirrorTextureSwapchain->height());
}
void
OpenXRViewer::SwapBuffersCallback::swapBuffersImplementation(
osg::GraphicsContext* gc)
{
mViewer->swapBuffers(gc);
}
void OpenXRViewer::swapBuffers(osg::GraphicsContext* gc)
{
std::unique_lock<std::mutex> lock(mMutex);
if (!mConfigured)
return;
auto* state = gc->getState();
auto* gl = osg::GLExtensions::Get(state->getContextID(), false);
blitEyesToMirrorTexture(gc);
mXR->endFrame();
gl->glBindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT, 0);
mMirrorTextureSwapchain->current()->blit(gc, 0, 0, mMirrorTextureSwapchain->width(), mMirrorTextureSwapchain->height());
gc->swapBuffersImplementation();
mMirrorTextureSwapchain->releaseSwapchainImage();
}
void
@ -103,24 +197,32 @@ namespace MWVR
if (!mXR->sessionRunning())
return;
auto* camera = slave._camera.get();
auto name = camera->getName();
Log(Debug::Debug) << "Updating camera " << name;
if (camera->getName() == "LeftEye")
{
mXR->handleEvents();
mXR->waitFrame();
mXR->beginFrame();
mXR->updateControls();
mXR->updatePoses();
}
auto viewMatrix = view.getCamera()->getViewMatrix() * mView->viewMatrix();
auto projMatrix = mView->projectionMatrix();
camera->setViewMatrix(viewMatrix);
camera->setProjectionMatrix(projMatrix);
}
void
OpenXRViewer::RealizeOperation::operator()(
osg::GraphicsContext* gc)
{
OpenXRManager::RealizeOperation::operator()(gc);
mViewer->realize(gc);
}
bool
OpenXRViewer::RealizeOperation::realized()
{
return mViewer->realized();
}
}

@ -8,42 +8,56 @@
#include <osgViewer/Viewer>
#include "openxrmanager.hpp"
#include "openxrview.hpp"
#include "openxrlayer.hpp"
#include "openxrworldview.hpp"
#include "openxrmenu.hpp"
#include "openxrinputmanager.hpp"
struct XrCompositionLayerProjection;
struct XrCompositionLayerProjectionView;
namespace MWVR
{
class OpenXRViewer : public osg::Group
class OpenXRViewer : public osg::Group, public OpenXRLayer
{
public:
class RealizeOperation : public OpenXRManager::RealizeOperation
{
public:
RealizeOperation(osg::ref_ptr<OpenXRManager> XR, osg::ref_ptr<OpenXRViewer> viewer) : OpenXRManager::RealizeOperation(XR), mViewer(viewer) {};
void operator()(osg::GraphicsContext* gc) override;
bool realized() override;
private:
osg::ref_ptr<OpenXRViewer> mViewer;
};
class UpdateSlaveCallback : public osg::View::Slave::UpdateSlaveCallback
{
public:
UpdateSlaveCallback(osg::ref_ptr<OpenXRManager> XR, osg::ref_ptr<OpenXRView> view)
: mXR(XR), mView(view)
UpdateSlaveCallback(osg::ref_ptr<OpenXRManager> XR, osg::ref_ptr<OpenXRWorldView> view, osg::GraphicsContext* gc)
: mXR(XR), mView(view), mGC(gc)
{}
void updateSlave(osg::View& view, osg::View::Slave& slave) override;
private:
osg::ref_ptr<OpenXRManager> mXR;
osg::ref_ptr<OpenXRView> mView;
osg::ref_ptr<OpenXRWorldView> mView;
osg::ref_ptr<osg::GraphicsContext> mGC;
};
public:
enum Views
class SwapBuffersCallback : public osg::GraphicsContext::SwapCallback
{
LEFT_VIEW = 0,
RIGHT_VIEW = 1
public:
SwapBuffersCallback(OpenXRViewer* viewer) : mViewer(viewer) {};
void swapBuffersImplementation(osg::GraphicsContext* gc) override;
private:
OpenXRViewer* mViewer;
};
public:
//! Create an OpenXR manager based on the graphics context from the given window.
//! The OpenXRManager will make its own context with shared resources.
OpenXRViewer(
osg::ref_ptr<OpenXRManager> XR,
osg::ref_ptr<OpenXRManager::RealizeOperation> realizeOperation,
osg::ref_ptr<osgViewer::Viewer> viewer,
float metersPerUnit = 1.f);
@ -51,14 +65,38 @@ namespace MWVR
virtual void traverse(osg::NodeVisitor& visitor) override;
const XrCompositionLayerBaseHeader* layer() override;
void traversals();
void blitEyesToMirrorTexture(osg::GraphicsContext* gc) const;
void swapBuffers(osg::GraphicsContext* gc) ;
void realize(osg::GraphicsContext* gc);
bool realized() { return mConfigured; }
protected:
virtual bool configure();
std::unique_ptr<XrCompositionLayerProjection> mLayer = nullptr;
std::vector<XrCompositionLayerProjectionView> mCompositionLayerProjectionViews;
osg::observer_ptr<OpenXRManager> mXR = nullptr;
osg::observer_ptr<OpenXRManager::RealizeOperation> mRealizeOperation = nullptr;
osg::ref_ptr<OpenXRManager::RealizeOperation> mRealizeOperation = nullptr;
osg::observer_ptr<osgViewer::Viewer> mViewer = nullptr;
std::unique_ptr<MWVR::OpenXRInputManager> mXRInput = nullptr;
std::array<osg::ref_ptr<OpenXRView>, 2> mViews{};
std::unique_ptr<MWVR::OpenXRMenu> mXRMenu = nullptr;
std::array<osg::ref_ptr<OpenXRWorldView>, 2> mViews{};
//osg::ref_ptr<SDLUtil::GraphicsWindowSDL2> mViewerGW;
//osg::ref_ptr<SDLUtil::GraphicsWindowSDL2> mLeftGW;
//osg::ref_ptr<SDLUtil::GraphicsWindowSDL2> mRightGW;
osg::Camera* mMainCamera = nullptr;
osg::Camera* mLeftCamera = nullptr;
osg::Camera* mRightCamera = nullptr;
std::unique_ptr<OpenXRSwapchain> mMirrorTextureSwapchain = nullptr;
std::mutex mMutex;
float mMetersPerUnit = 1.f;
bool mConfigured = false;
};

@ -0,0 +1,138 @@
#include "openxrworldview.hpp"
#include "openxrmanager.hpp"
#include "openxrmanagerimpl.hpp"
#include "../mwinput/inputmanagerimp.hpp"
#include <components/debug/debuglog.hpp>
#include <components/sdlutil/sdlgraphicswindow.hpp>
#include <Windows.h>
#include <openxr/openxr.h>
#include <osg/Camera>
#include <osgViewer/Renderer>
#include <vector>
#include <array>
#include <iostream>
namespace MWVR
{
// Some headers like to define these.
#ifdef near
#undef near
#endif
#ifdef far
#undef far
#endif
static osg::Matrix
perspectiveFovMatrix(float near, float far, XrFovf fov)
{
const float tanLeft = tanf(fov.angleLeft);
const float tanRight = tanf(fov.angleRight);
const float tanDown = tanf(fov.angleDown);
const float tanUp = tanf(fov.angleUp);
const float tanWidth = tanRight - tanLeft;
const float tanHeight = tanUp - tanDown;
const float offset = near;
float matrix[16] = {};
matrix[0] = 2 / tanWidth;
matrix[4] = 0;
matrix[8] = (tanRight + tanLeft) / tanWidth;
matrix[12] = 0;
matrix[1] = 0;
matrix[5] = 2 / tanHeight;
matrix[9] = (tanUp + tanDown) / tanHeight;
matrix[13] = 0;
if (far <= near) {
matrix[2] = 0;
matrix[6] = 0;
matrix[10] = -1;
matrix[14] = -(near + offset);
}
else {
matrix[2] = 0;
matrix[6] = 0;
matrix[10] = -(far + offset) / (far - near);
matrix[14] = -(far * (near + offset)) / (far - near);
}
matrix[3] = 0;
matrix[7] = 0;
matrix[11] = -1;
matrix[15] = 0;
return osg::Matrix(matrix);
}
osg::Matrix OpenXRWorldView::projectionMatrix()
{
auto hmdViews = mXR->impl().getHmdViews();
float near = Settings::Manager::getFloat("near clip", "Camera");
float far = Settings::Manager::getFloat("viewing distance", "Camera");
//return perspectiveFovMatrix()
return perspectiveFovMatrix(near, far, hmdViews[mView].fov);
}
osg::Matrix OpenXRWorldView::viewMatrix()
{
osg::Matrix viewMatrix;
auto hmdViews = mXR->impl().getHmdViews();
auto pose = hmdViews[mView].pose;
osg::Vec3 position = osg::Vec3(pose.position.x, pose.position.y, pose.position.z);
auto stageViews = mXR->impl().getStageViews();
auto stagePose = stageViews[mView].pose;
// invert orientation (conjugate of Quaternion) and position to apply to the view matrix as offset
viewMatrix.setTrans(position);
viewMatrix.postMultRotate(osg::fromXR(stagePose.orientation).conj());
// Scale to world units
viewMatrix.postMultScale(osg::Vec3d(mMetersPerUnit, mMetersPerUnit, mMetersPerUnit));
return viewMatrix;
}
OpenXRWorldView::OpenXRWorldView(
osg::ref_ptr<OpenXRManager> XR, osg::ref_ptr<osg::State> state, float metersPerUnit, SubView view)
: OpenXRView(XR)
, mMetersPerUnit(metersPerUnit)
, mView(view)
{
auto config = mXR->impl().mConfigViews[view];
setWidth(config.recommendedImageRectWidth);
setHeight(config.recommendedImageRectHeight);
setSamples(config.recommendedSwapchainSampleCount);
realize(state);
// XR->setViewSubImage(view, mSwapchain->subImage());
}
OpenXRWorldView::~OpenXRWorldView()
{
}
void OpenXRWorldView::InitialDrawCallback::operator()(osg::RenderInfo& renderInfo) const
{
osg::GraphicsOperation* graphicsOperation = renderInfo.getCurrentCamera()->getRenderer();
osgViewer::Renderer* renderer = dynamic_cast<osgViewer::Renderer*>(graphicsOperation);
if (renderer != nullptr)
{
// Disable normal OSG FBO camera setup because it will undo the MSAA FBO configuration.
renderer->setCameraRequiresSetUp(false);
}
}
}

@ -0,0 +1,34 @@
#ifndef OPENXRWORLDVIEW_HPP
#define OPENXRWORLDVIEW_HPP
#include "openxrview.hpp"
namespace MWVR
{
class OpenXRWorldView : public OpenXRView
{
public:
enum SubView
{
LEFT_VIEW = 0,
RIGHT_VIEW = 1,
SUBVIEW_MAX = RIGHT_VIEW, //!< Used to size subview arrays. Not a valid input.
};
public:
OpenXRWorldView(osg::ref_ptr<OpenXRManager> XR, osg::ref_ptr<osg::State> state, float metersPerUnit, SubView view);
~OpenXRWorldView();
//! Projection offset for this view
osg::Matrix projectionMatrix();
//! View offset for this view
osg::Matrix viewMatrix();
//! Which view this is
SubView view() { return mView; }
float mMetersPerUnit = 1.f;
SubView mView;
};
}
#endif
Loading…
Cancel
Save