diff --git a/examples/xrDemo.cpp b/examples/xrDemo.cpp index fe3d79ea..8ce89884 100644 --- a/examples/xrDemo.cpp +++ b/examples/xrDemo.cpp @@ -1,5 +1,7 @@ #include "RaZ/RaZ.hpp" +#include "DemoUtils.hpp" + using namespace std::literals; int main() { @@ -20,8 +22,11 @@ int main() { auto& xr = world.addSystem("RaZ - XR demo"); render.enableXr(xr); - // The camera parameters aren't technically used for XR rendering, but its transform may be later - world.addEntityWithComponent().addComponent(window.getWidth(), window.getHeight()); + // In an XR workflow, the camera is optional; its parameters aren't technically used, but its transform is + auto& camera = world.addEntityWithComponent(); + camera.addComponent(window.getWidth(), window.getHeight()); + + DemoUtils::setupCameraControls(camera, window); world.addEntityWithComponent(Raz::Vec3f(0.f, -1.f, -5.f), Raz::Quaternionf(90_deg, Raz::Axis::Y), Raz::Vec3f(0.01f)) .addComponent(Raz::MeshFormat::load(RAZ_ROOT "assets/meshes/crytek_sponza.obj").second); diff --git a/include/RaZ/XR/XrSession.hpp b/include/RaZ/XR/XrSession.hpp index 60323e87..cdebe431 100644 --- a/include/RaZ/XR/XrSession.hpp +++ b/include/RaZ/XR/XrSession.hpp @@ -32,7 +32,7 @@ struct ViewFov { Radiansf angleUp; Radiansf angleDown; }; -using ViewRenderFunc = std::function(const Vec3f&, const Quaternionf&, const ViewFov&)>; +using ViewRenderFunc = std::function(Vec3f, Quaternionf, ViewFov)>; class XrSession { friend class XrSystem; diff --git a/src/RaZ/Render/RenderSystem.cpp b/src/RaZ/Render/RenderSystem.cpp index b65f8998..c9391563 100644 --- a/src/RaZ/Render/RenderSystem.cpp +++ b/src/RaZ/Render/RenderSystem.cpp @@ -297,7 +297,13 @@ void RenderSystem::renderXrFrame() { ZoneScopedN("RenderSystem::renderXrFrame"); TracyGpuZone("RenderSystem::renderXrFrame") - m_xrSystem->renderFrame([this] (const Vec3f& position, const Quaternionf& rotation, const ViewFov& viewFov) { + m_xrSystem->renderFrame([this] (Vec3f position, Quaternionf rotation, ViewFov viewFov) { + if (m_cameraEntity) { + const auto& camTransform = m_cameraEntity->getComponent(); + position = camTransform.getRotation() * position + camTransform.getPosition(); + rotation = camTransform.getRotation() * rotation; + } + Mat4f invViewMat = rotation.computeMatrix(); invViewMat.getElement(3, 0) = position.x(); invViewMat.getElement(3, 1) = position.y();