Ocean
VRNativeApplication.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_PLATFORM_META_QUEST_OPENXR_APPLICATION_VR_NATIVE_APPLICATION_H
9 #define META_OCEAN_PLATFORM_META_QUEST_OPENXR_APPLICATION_VR_NATIVE_APPLICATION_H
10 
13 
15 
18 
20 
24 
25 #include "ocean/rendering/Engine.h"
27 
29 
30 namespace Ocean
31 {
32 
33 namespace Platform
34 {
35 
36 namespace Meta
37 {
38 
39 namespace Quest
40 {
41 
42 namespace OpenXR
43 {
44 
45 namespace Application
46 {
47 
48 /**
49  * This class implements a basic OpenXR (VR) application using Ocean's scene graph rendering pipline (Rendering::GLESceneGraph).
50  * @ingroup platformmetaquestopenxrapplication
51  */
52 class OCEAN_PLATFORM_META_QUEST_OPENXR_APPLICATION_EXPORT VRNativeApplication : public NativeApplication
53 {
54  protected:
55 
56  /// The maximal number of supported eyes.
57  static constexpr size_t maximalNumberEyes_ = 2;
58 
59  /**
60  * Definition of a union allowing to define individual composition layers.
61  */
63  {
64  /// Composition layer for projection.
65  XrCompositionLayerProjection xrCompositionLayerProjection_;
66 
67  /// composition layer for a quad.
68  XrCompositionLayerQuad xrCompositionLayerQuad_;
69 
70  /// Composition layer for a cylinder.
71  XrCompositionLayerCylinderKHR xrCompositionLayerCylinderKHR_;
72 
73  /// Composition layer for a cube map.
74  XrCompositionLayerCubeKHR xrCompositionLayerCubeKHR_;
75 
76  /// Composition layer for a equirectangular projection.
77  XrCompositionLayerEquirectKHR xrCompositionLayerEquirectKHR_;
78 
79  /// A composition layer for passthrough.
80  XrCompositionLayerPassthroughFB xrCompositionLayerPassthroughFB_;
81  };
82 
83  /**
84  * Definition of a vector holding xrCompositorLayerUnion objects.
85  */
86  typedef std::vector<XrCompositorLayerUnion> XrCompositorLayerUnions;
87 
88  /**
89  * Definition of a vector holding XrCompositionLayerBaseHeader objects.
90  */
91  typedef std::vector<const XrCompositionLayerBaseHeader*> XrCompositionLayerBaseHeaders;
92 
93  /**
94  * Definition of a vector holding XrActionSet handles.
95  */
96  typedef std::vector<XrActionSet> XrActionSets;
97 
98  /**
99  * Definition of a vector holding XrActiveActionSet objects.
100  */
101  typedef std::vector<XrActiveActionSet> XrActiveActionSets;
102 
103  public:
104 
105  /**
106  * Destructs this object.
107  */
109 
110  protected:
111 
112 #ifdef OCEAN_PLATFORM_BUILD_ANDROID
113 
114  /**
115  * Creates a new application object.
116  * @param androidApp The android app object as provided in the main function of the native activity, must be valid
117  */
118  explicit VRNativeApplication(struct android_app* androidApp);
119 
120 #endif // OCEAN_PLATFORM_BUILD_ANDROID
121 
122  /**
123  * Disabled copy constructor.
124  */
126 
127  /**
128  * Creates the OpenXR session.
129  * @see NativeApplication::createOpenXRSession().
130  */
131  bool createOpenXRSession(const XrViewConfigurationViews& xrViewConfigurationViews) override;
132 
133  /**
134  * Releases the OpenXR session.
135  * @see NativeApplication::releaseOpenXRSession().
136  */
137  void releaseOpenXRSession() override;
138 
139  /**
140  * Main loop loop of the application.
141  * @see NativeApplication::applicationLoop().
142  */
143  void applicationLoop() override;
144 
145  /**
146  * Returns the names of the necessary OpenXR extensions the application needs.
147  * @see NativeApplication::necessaryOpenXRExtensionNames().
148  */
150 
151  /**
152  * Registers the system fonts.
153  */
155 
156  /**
157  * Renders a new frame.
158  * @param shouldRender True, if the frame should actually be rendered; False, to frame should actually be skipped
159  * @param xrPredictedDisplayTime The predicted display time as provided, with range (0, infinity)
160  * @param renderTimestamp The timestamp of the render call, must be valid
161  */
162  virtual void render(const bool shouldRender, const XrTime& xrPredictedDisplayTime, const Timestamp& renderTimestamp);
163 
164  /**
165  * Returns the application's base space.
166  * @return The base space which is currently used
167  */
168  virtual XrSpace baseSpace() const;
169 
170  /**
171  * Locates a space in relation to the applications' base space.
172  * @param xrSpace The space to locate, must be valid
173  * @param xrTime The time for which the pose will be determined, must be valid
174  * @param xrSpaceLocationFlags Optional resulting location flags, nullptr if not of interest
175  * @return The resulting transformation between the located space and the base space, will be baseSpace_T_space, invalid in case of an error
176  * @see baseSpace().
177  */
178  template <typename T = Scalar>
179  HomogenousMatrixT4<T> locateSpace(const XrSpace& xrSpace, const XrTime& xrTime, XrSpaceLocationFlags* xrSpaceLocationFlags = nullptr);
180 
181  /**
182  * Locates a space in relation to the applications' base space.
183  * @param xrSpace The space to locate, must be valid
184  * @param timestamp The time for which the pose will be determined, must be valid
185  * @param xrSpaceLocationFlags Optional resulting location flags, nullptr if not of interest
186  * @return The resulting transformation between the located space and the base space, will be baseSpace_T_space, invalid in case of an error
187  * @see baseSpace().
188  */
189  template <typename T = Scalar>
190  HomogenousMatrixT4<T> locateSpace(const XrSpace& xrSpace, const Timestamp& timestamp, XrSpaceLocationFlags* xrSpaceLocationFlags = nullptr);
191 
192  /**
193  * Returns the TrackedController object allowing access to controller events.
194  * @return The desired object
195  */
196  inline TrackedController& trackedController();
197 
198  /**
199  * Event function called whenever the session is ready, when the session state changed to XR_SESSION_STATE_READY.
200  * @see NativeApplication::onOpenXRSessionReady().
201  */
202  void onOpenXRSessionReady() override;
203 
204  /**
205  * Event function called whenever the session is stopping, when the session state changed to XR_SESSION_STATE_STOPPING.
206  * @see NativeApplication::onOpenXRSessionStopping().
207  */
208  void onOpenXRSessionStopping() override;
209 
210  /**
211  * Event function called to configure the action sets.
212  * @param actionSets The action sets to configure
213  */
215 
216  /**
217  * Event function called to release the action sets or/and resources associated with action sets.
218  */
219  virtual void onReleaseActionSets();
220 
221  /**
222  * Idle event function called within the main loop whenever all Android related events have been processed.
223  * @see NativeApplication::onIdle().
224  */
225  void onIdle() override;
226 
227  /**
228  * The event function which is called when all resources should finally be released.
229  */
230  virtual void onReleaseResources();
231 
232  /**
233  * Event function called after the framebuffer has been initialized.
234  * This event function can be used to configure the framebuffer or to add rendering content.
235  */
236  virtual void onFramebufferInitialized();
237 
238  /**
239  * Event function called before the framebuffer will be released.
240  * This event function can be used to release depending resources.
241  */
242  virtual void onFramebufferReleasing();
243 
244  /**
245  * Event function allows to add custom compositor layers at the very back.
246  * @param xrCompositorLayerUnions The compositor layers to which new layers can be added
247  */
248  virtual void onAddCompositorBackLayers(XrCompositorLayerUnions& xrCompositorLayerUnions);
249 
250  /**
251  * Event function allows to add custom compositor layers at the very front.
252  * @param xrCompositorLayerUnions The compositor layers to which new layers can be added
253  */
254  virtual void onAddCompositorFrontLayers(XrCompositorLayerUnions& xrCompositorLayerUnions);
255 
256  /**
257  * Events function called before the scene is rendered.
258  * @param xrPredictedDisplayTime The OpenXR timestamp which will be used for rendering, must be valid
259  * @param predictedDisplayTime The timestamp which will be used for rendering, must be valid
260  */
261  virtual void onPreRender(const XrTime& xrPredictedDisplayTime, const Timestamp& predictedDisplayTime);
262 
263  /**
264  * Event functions for pressed buttons (e.g., from a tracked controller).
265  * @param buttons The buttons currently pressed
266  * @param timestamp The timestamp of the event
267  */
268  virtual void onButtonPressed(const TrackedController::ButtonType buttons, const Timestamp& timestamp);
269 
270  /**
271  * Event functions for released buttons (e.g., from a tracked controller).
272  * @param buttons The buttons currently pressed
273  * @param timestamp The timestamp of the event
274  */
275  virtual void onButtonReleased(const TrackedController::ButtonType buttons, const Timestamp& timestamp);
276 
277  protected:
278 
279  /// True, to use the stencil buffer.
280  bool useStencilBuffer_ = false;
281 
282  /// The near distance used for clipping in the projection matrix.
283  float nearDistance_ = 0.1f;
284 
285  /// The far distance used for clipping in the projection matrix.
286  float farDistance_ = 100.0f;
287 
288  /// The application's EGL context
290 
291  /// The application's OpenXR session.
293 
294  /// The OpenXR view space.
296 
297  /// The OpenXR local space (the headset's world, which can change with the reset button).
299 
300  /// The OpenXR stage space (the headsets world).
302 
303  /// True, if the OpenXR session is currently running and ready for rendering.
304  bool xrSessionIsRunning_ = false;
305 
306  /// The OpenXR compositor layer unions (the rendering layers).
308 
309  /// The OpenXR composition layer base headers.
311 
312  /// The rendering engine to be used in the VR application.
314 
315  /// The rendering framebuffer to be used in the VR application.
317 
318  /// The stereo view to be used in the VR Application.
320 
321  /// The application's action sets.
323 
324  /// The active action sets.
326 
327  /// The visualizer for images (mainly a helper class creating textured SceneGraph objects to display image content).
329 
330  /// The visualizer for text (mainly a helper class creating textured SceneGraph objects to display text).
332 
333  private:
334 
335  /// The explicit pointer to the Quest-specialized framebuffer (identical to framebuffer_, but pointing to the derived class).
337 
338  /// The tracked controller object providing controller events and poses.
340 };
341 
342 template <typename T>
343 HomogenousMatrixT4<T> VRNativeApplication::locateSpace(const XrSpace& xrSpace, const XrTime& xrTime, XrSpaceLocationFlags* xrSpaceLocationFlags)
344 {
345  return Platform::OpenXR::Utilities::determinePose(xrSpace, baseSpace(), xrTime, xrSpaceLocationFlags);
346 }
347 
348 template <typename T>
349 HomogenousMatrixT4<T> VRNativeApplication::locateSpace(const XrSpace& xrSpace, const Timestamp& timestamp, XrSpaceLocationFlags* xrSpaceLocationFlags)
350 {
351  const XrTime xrTime(timestamp.nanoseconds());
352 
353  return locateSpace(xrSpace, xrTime, xrSpaceLocationFlags);
354 }
355 
357 {
358  return trackedController_;
359 }
360 
361 }
362 
363 }
364 
365 }
366 
367 }
368 
369 }
370 
371 }
372 
373 #endif // META_OCEAN_PLATFORM_META_QUEST_OPENXR_APPLICATION_VR_NATIVE_APPLICATION_H
This class implements a 4x4 homogeneous transformation matrix using floating point values with the pr...
Definition: HomogenousMatrix4.h:110
This class encapsulates an EGL context.
Definition: EGLContext.h:32
This class implements a helper function allowing to visualize images in an Ocean-based VR application...
Definition: VRImageVisualizer.h:59
This class implements a helper function allowing to visualize text in an Ocean-based VR application (...
Definition: VRTextVisualizer.h:60
This class implements a basic OpenXR application for Quest devices based on an Android NativeActivity...
Definition: NativeApplication.h:50
std::unordered_set< std::string > StringSet
Definition of an unordered set holding strings.
Definition: NativeApplication.h:69
std::vector< XrViewConfigurationView > XrViewConfigurationViews
Definition of a vector holding XrViewConfigurationView objects.
Definition: NativeApplication.h:74
This class implements a basic OpenXR (VR) application using Ocean's scene graph rendering pipline (Re...
Definition: VRNativeApplication.h:53
Quest::Application::VRTextVisualizer vrTextVisualizer_
The visualizer for text (mainly a helper class creating textured SceneGraph objects to display text).
Definition: VRNativeApplication.h:331
std::vector< XrCompositorLayerUnion > XrCompositorLayerUnions
Definition of a vector holding xrCompositorLayerUnion objects.
Definition: VRNativeApplication.h:86
virtual void onPreRender(const XrTime &xrPredictedDisplayTime, const Timestamp &predictedDisplayTime)
Events function called before the scene is rendered.
void onOpenXRSessionStopping() override
Event function called whenever the session is stopping, when the session state changed to XR_SESSION_...
Quest::Application::VRImageVisualizer vrImageVisualizer_
The visualizer for images (mainly a helper class creating textured SceneGraph objects to display imag...
Definition: VRNativeApplication.h:328
void applicationLoop() override
Main loop loop of the application.
virtual void onFramebufferReleasing()
Event function called before the framebuffer will be released.
void onIdle() override
Idle event function called within the main loop whenever all Android related events have been process...
TrackedController & trackedController()
Returns the TrackedController object allowing access to controller events.
Definition: VRNativeApplication.h:356
Rendering::EngineRef engine_
The rendering engine to be used in the VR application.
Definition: VRNativeApplication.h:313
XrActiveActionSets xrActiveActionSets_
The active action sets.
Definition: VRNativeApplication.h:325
virtual void onAddCompositorFrontLayers(XrCompositorLayerUnions &xrCompositorLayerUnions)
Event function allows to add custom compositor layers at the very front.
XrCompositorLayerUnions xrCompositorLayerUnions_
The OpenXR compositor layer unions (the rendering layers).
Definition: VRNativeApplication.h:307
Rendering::StereoViewRef stereoView_
The stereo view to be used in the VR Application.
Definition: VRNativeApplication.h:319
virtual void onAddCompositorBackLayers(XrCompositorLayerUnions &xrCompositorLayerUnions)
Event function allows to add custom compositor layers at the very back.
Platform::OpenXR::ScopedXrSpace xrSpaceView_
The OpenXR view space.
Definition: VRNativeApplication.h:295
Platform::GLES::EGLContext eglContext_
The application's EGL context.
Definition: VRNativeApplication.h:289
virtual void onConfigureActionSets(Platform::OpenXR::SharedActionSets &actionSets)
Event function called to configure the action sets.
virtual void onButtonPressed(const TrackedController::ButtonType buttons, const Timestamp &timestamp)
Event functions for pressed buttons (e.g., from a tracked controller).
Platform::OpenXR::Session xrSession_
The application's OpenXR session.
Definition: VRNativeApplication.h:292
virtual void onReleaseResources()
The event function which is called when all resources should finally be released.
void onOpenXRSessionReady() override
Event function called whenever the session is ready, when the session state changed to XR_SESSION_STA...
HomogenousMatrixT4< T > locateSpace(const XrSpace &xrSpace, const XrTime &xrTime, XrSpaceLocationFlags *xrSpaceLocationFlags=nullptr)
Locates a space in relation to the applications' base space.
Definition: VRNativeApplication.h:343
Platform::OpenXR::ScopedXrSpace xrSpaceStage_
The OpenXR stage space (the headsets world).
Definition: VRNativeApplication.h:301
Rendering::WindowFramebufferRef framebuffer_
The rendering framebuffer to be used in the VR application.
Definition: VRNativeApplication.h:316
Platform::OpenXR::SharedActionSets actionSets_
The application's action sets.
Definition: VRNativeApplication.h:322
std::vector< XrActiveActionSet > XrActiveActionSets
Definition of a vector holding XrActiveActionSet objects.
Definition: VRNativeApplication.h:101
virtual void onFramebufferInitialized()
Event function called after the framebuffer has been initialized.
virtual void onReleaseActionSets()
Event function called to release the action sets or/and resources associated with action sets.
Platform::OpenXR::ScopedXrSpace xrSpaceLocal_
The OpenXR local space (the headset's world, which can change with the reset button).
Definition: VRNativeApplication.h:298
virtual void render(const bool shouldRender, const XrTime &xrPredictedDisplayTime, const Timestamp &renderTimestamp)
Renders a new frame.
void releaseOpenXRSession() override
Releases the OpenXR session.
virtual XrSpace baseSpace() const
Returns the application's base space.
virtual void onButtonReleased(const TrackedController::ButtonType buttons, const Timestamp &timestamp)
Event functions for released buttons (e.g., from a tracked controller).
VRNativeApplication(const NativeApplication &)=delete
Disabled copy constructor.
XrCompositionLayerBaseHeaders xrCompositionLayerBaseHeaders_
The OpenXR composition layer base headers.
Definition: VRNativeApplication.h:310
std::vector< const XrCompositionLayerBaseHeader * > XrCompositionLayerBaseHeaders
Definition of a vector holding XrCompositionLayerBaseHeader objects.
Definition: VRNativeApplication.h:91
VRNativeApplication(struct android_app *androidApp)
Creates a new application object.
bool createOpenXRSession(const XrViewConfigurationViews &xrViewConfigurationViews) override
Creates the OpenXR session.
StringSet necessaryOpenXRExtensionNames() const override
Returns the names of the necessary OpenXR extensions the application needs.
std::vector< XrActionSet > XrActionSets
Definition of a vector holding XrActionSet handles.
Definition: VRNativeApplication.h:96
TrackedController trackedController_
The tracked controller object providing controller events and poses.
Definition: VRNativeApplication.h:339
This class implements a wrapper for tracker controllers.
Definition: TrackedController.h:40
ButtonType
Definition of individual button types, may be a combintation of several buttons.
Definition: TrackedController.h:47
This class wraps an OpenXR session.
Definition: Session.h:32
static HomogenousMatrixT4< T > determinePose(const XrSpace &xrSpace, const XrSpace &xrBaseSpace, const XrTime &xrTime, XrSpaceLocationFlags *xrSpaceLocationFlags=nullptr)
Determines the pose of an OpenXR space.
Definition: platform/openxr/Utilities.h:226
This class is the base class for all GLESceneGraph window framebuffers for Quest platforms.
Definition: quest/openxr/GLESWindowFramebuffer.h:47
This class implements a timestamp.
Definition: Timestamp.h:36
int64_t nanoseconds() const
Returns this timestamp in nanoseconds.
Definition: Timestamp.h:283
std::vector< SharedActionSet > SharedActionSets
Definition of a vector holding a SharedActionSet object.
Definition: ActionSet.h:47
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15
Definition of a union allowing to define individual composition layers.
Definition: VRNativeApplication.h:63
XrCompositionLayerQuad xrCompositionLayerQuad_
composition layer for a quad.
Definition: VRNativeApplication.h:68
XrCompositionLayerPassthroughFB xrCompositionLayerPassthroughFB_
A composition layer for passthrough.
Definition: VRNativeApplication.h:80
XrCompositionLayerEquirectKHR xrCompositionLayerEquirectKHR_
Composition layer for a equirectangular projection.
Definition: VRNativeApplication.h:77
XrCompositionLayerCubeKHR xrCompositionLayerCubeKHR_
Composition layer for a cube map.
Definition: VRNativeApplication.h:74
XrCompositionLayerProjection xrCompositionLayerProjection_
Composition layer for projection.
Definition: VRNativeApplication.h:65
XrCompositionLayerCylinderKHR xrCompositionLayerCylinderKHR_
Composition layer for a cylinder.
Definition: VRNativeApplication.h:71