2020-01-09 23:10:09 +00:00
|
|
|
#include "openxrview.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 <openxr/openxr_platform.h>
|
|
|
|
#include <openxr/openxr_platform_defines.h>
|
|
|
|
#include <openxr/openxr_reflection.h>
|
|
|
|
|
2020-01-23 23:14:23 +00:00
|
|
|
namespace MWVR {
|
2020-01-09 23:10:09 +00:00
|
|
|
|
2020-01-23 23:14:23 +00:00
|
|
|
OpenXRView::OpenXRView(
|
|
|
|
osg::ref_ptr<OpenXRManager> XR)
|
2020-01-09 23:10:09 +00:00
|
|
|
: mXR(XR)
|
2020-01-23 23:14:23 +00:00
|
|
|
, mSwapchain(nullptr)
|
|
|
|
, mSwapchainConfig{}
|
2020-01-09 23:10:09 +00:00
|
|
|
{
|
2020-01-23 23:14:23 +00:00
|
|
|
mSwapchainConfig.requestedFormats = {
|
2020-01-09 23:10:09 +00:00
|
|
|
GL_RGBA8,
|
|
|
|
GL_RGBA8_SNORM,
|
|
|
|
};
|
|
|
|
|
2020-01-23 23:14:23 +00:00
|
|
|
mXR->registerToBarrier();
|
2020-01-09 23:10:09 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 23:14:23 +00:00
|
|
|
OpenXRView::~OpenXRView()
|
2020-01-09 23:10:09 +00:00
|
|
|
{
|
2020-01-23 23:14:23 +00:00
|
|
|
mXR->unregisterFromBarrier();
|
2020-01-09 23:10:09 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 23:14:23 +00:00
|
|
|
osg::Camera* OpenXRView::createCamera(int eye, const osg::Vec4& clearColor, osg::GraphicsContext* gc)
|
2020-01-09 23:10:09 +00:00
|
|
|
{
|
|
|
|
osg::ref_ptr<osg::Camera> camera = new osg::Camera();
|
|
|
|
camera->setClearColor(clearColor);
|
|
|
|
camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
|
|
|
|
camera->setRenderOrder(osg::Camera::PRE_RENDER, eye);
|
|
|
|
camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
|
|
|
|
camera->setAllowEventFocus(false);
|
|
|
|
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
|
2020-01-23 23:14:23 +00:00
|
|
|
camera->setViewport(0, 0, mSwapchain->width(), mSwapchain->height());
|
2020-01-09 23:10:09 +00:00
|
|
|
camera->setGraphicsContext(gc);
|
|
|
|
|
|
|
|
camera->setInitialDrawCallback(new OpenXRView::InitialDrawCallback());
|
|
|
|
camera->setPreDrawCallback(new OpenXRView::PredrawCallback(camera.get(), this));
|
|
|
|
camera->setFinalDrawCallback(new OpenXRView::PostdrawCallback(camera.get(), this));
|
|
|
|
|
|
|
|
return camera.release();
|
|
|
|
}
|
|
|
|
|
2020-01-23 23:14:23 +00:00
|
|
|
void OpenXRView::setWidth(int width)
|
2020-01-09 23:10:09 +00:00
|
|
|
{
|
2020-01-23 23:14:23 +00:00
|
|
|
mSwapchainConfig.width = width;
|
2020-01-09 23:10:09 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 23:14:23 +00:00
|
|
|
void OpenXRView::setHeight(int height)
|
2020-01-09 23:10:09 +00:00
|
|
|
{
|
2020-01-23 23:14:23 +00:00
|
|
|
mSwapchainConfig.height = height;
|
2020-01-09 23:10:09 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 23:14:23 +00:00
|
|
|
void OpenXRView::setSamples(int samples)
|
2020-01-09 23:10:09 +00:00
|
|
|
{
|
2020-01-23 23:14:23 +00:00
|
|
|
mSwapchainConfig.samples = samples;
|
2020-01-09 23:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenXRView::prerenderCallback(osg::RenderInfo& renderInfo)
|
|
|
|
{
|
2020-01-23 23:14:23 +00:00
|
|
|
mXR->beginFrame();
|
|
|
|
if(mSwapchain)
|
|
|
|
mSwapchain->beginFrame(renderInfo.getState()->getGraphicsContext());
|
2020-01-09 23:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenXRView::postrenderCallback(osg::RenderInfo& renderInfo)
|
|
|
|
{
|
2020-01-23 23:14:23 +00:00
|
|
|
if (mSwapchain)
|
|
|
|
mSwapchain->endFrame(renderInfo.getState()->getGraphicsContext());
|
|
|
|
mXR->viewerBarrier();
|
2020-01-09 23:10:09 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 23:14:23 +00:00
|
|
|
bool OpenXRView::realize(osg::ref_ptr<osg::State> state)
|
2020-01-09 23:10:09 +00:00
|
|
|
{
|
2020-01-23 23:14:23 +00:00
|
|
|
try {
|
|
|
|
mSwapchain.reset(new OpenXRSwapchain(mXR, state, mSwapchainConfig));
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
}
|
2020-01-09 23:10:09 +00:00
|
|
|
|
2020-01-23 23:14:23 +00:00
|
|
|
return !!mSwapchain;
|
2020-01-09 23:10:09 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 23:14:23 +00:00
|
|
|
void OpenXRView::PredrawCallback::operator()(osg::RenderInfo& info) const
|
2020-01-09 23:10:09 +00:00
|
|
|
{
|
2020-01-23 23:14:23 +00:00
|
|
|
mView->prerenderCallback(info);
|
2020-01-09 23:10:09 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 23:14:23 +00:00
|
|
|
void OpenXRView::PostdrawCallback::operator()(osg::RenderInfo& info) const
|
2020-01-09 23:10:09 +00:00
|
|
|
{
|
2020-01-23 23:14:23 +00:00
|
|
|
mView->postrenderCallback(info);
|
2020-01-09 23:10:09 +00:00
|
|
|
}
|
|
|
|
}
|