Skip to content

Commit

Permalink
[Render/RenderSystem] The camera's transform is used for XR views
Browse files Browse the repository at this point in the history
- If it exists, its position & orientation are added to the XR device's views before rendering

- It probably isn't useful in a real scenario, but can help a lot for debugging purposes
  • Loading branch information
Razakhel committed Nov 13, 2024
1 parent 0ee6928 commit 1256254
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
9 changes: 7 additions & 2 deletions examples/xrDemo.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "RaZ/RaZ.hpp"

#include "DemoUtils.hpp"

using namespace std::literals;

int main() {
Expand All @@ -20,8 +22,11 @@ int main() {
auto& xr = world.addSystem<Raz::XrSystem>("RaZ - XR demo");
render.enableXr(xr);

// The camera parameters aren't technically used for XR rendering, but its transform may be later
world.addEntityWithComponent<Raz::Transform>().addComponent<Raz::Camera>(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<Raz::Transform>();
camera.addComponent<Raz::Camera>(window.getWidth(), window.getHeight());

DemoUtils::setupCameraControls(camera, window);

world.addEntityWithComponent<Raz::Transform>(Raz::Vec3f(0.f, -1.f, -5.f), Raz::Quaternionf(90_deg, Raz::Axis::Y), Raz::Vec3f(0.01f))
.addComponent<Raz::MeshRenderer>(Raz::MeshFormat::load(RAZ_ROOT "assets/meshes/crytek_sponza.obj").second);
Expand Down
2 changes: 1 addition & 1 deletion include/RaZ/XR/XrSession.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct ViewFov {
Radiansf angleUp;
Radiansf angleDown;
};
using ViewRenderFunc = std::function<std::pair<const Texture2D&, const Texture2D&>(const Vec3f&, const Quaternionf&, const ViewFov&)>;
using ViewRenderFunc = std::function<std::pair<const Texture2D&, const Texture2D&>(Vec3f, Quaternionf, ViewFov)>;

class XrSession {
friend class XrSystem;
Expand Down
8 changes: 7 additions & 1 deletion src/RaZ/Render/RenderSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Transform>();
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();
Expand Down

0 comments on commit 1256254

Please sign in to comment.