Ocean
VRHandVisualizer.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_HAND_VISUALIZER_H
9 #define META_OCEAN_PLATFORM_META_QUEST_OPENXR_APPLICATION_VR_HAND_VISUALIZER_H
10 
12 
14 
16 
21 
22 namespace Ocean
23 {
24 
25 namespace Platform
26 {
27 
28 namespace Meta
29 {
30 
31 namespace Quest
32 {
33 
34 namespace OpenXR
35 {
36 
37 namespace Application
38 {
39 
40 /**
41  * This class implements helper functions allowing to visualize the hand mesh used by hand tracking (Nimble) in an Ocean-based VR application (e.g., VRNativeApplication).
42  * @see VRNativeApplication.
43  * @ingroup platformmetaquestopenxrapplication
44  */
45 class OCEAN_PLATFORM_META_QUEST_OPENXR_APPLICATION_EXPORT VRHandVisualizer : public Quest::Application::VRVisualizer
46 {
47  public:
48 
49  /**
50  * Definition of individual render modes.
51  */
52  enum RenderMode : uint32_t
53  {
54  /// An invalid render mode.
55  RM_INVALID = 0u,
56  /// The joints will be rendered as small coordinate systems.
57  RM_JOINTS = 1u << 0u,
58  /// The hand mesh will be rendered as a wireframe.
59  RM_WIREFRAME = 1u << 1u,
60  /// The hand mesh will be rendred.
61  RM_MESH = 1u << 2u,
62  /// The default rendering mode.
63  RM_DEFAULT = RM_MESH,
64  /// The render mode using all existing render modes.
65  RM_ALL = RM_JOINTS | RM_WIREFRAME | RM_MESH,
66  };
67 
68  /**
69  * This class implements a scoped state object allowing to reset all states of a visualizer.
70  * The state can be stored locally or states can be pushed onto a stack.
71  * @see pushState(), popState().
72  */
74  {
75  friend class VRHandVisualizer;
76 
77  protected:
78 
79  /// The default hand transparency.
80  static constexpr Scalar defaultTransparency_ = Scalar(0.8);
81 
82  public:
83 
84  /**
85  * Default constructor.
86  */
87  ScopedState() = default;
88 
89  /**
90  * Creates a new state object.
91  * @param vrHandVisualizer The visualizer to which this new object belongs
92  */
93  ScopedState(VRHandVisualizer& vrHandVisualizer);
94 
95  /**
96  * Move constructor.
97  * @param scopedState The state object to be moved
98  */
99  ScopedState(ScopedState&& scopedState);
100 
101  /**
102  * Destructs this object and releases the state.
103  */
105 
106  /**
107  * Explicitly releases this state.
108  * The properties of the owning visualizer will be reset to the situation when the state was created.
109  */
110  void release();
111 
112  /**
113  * Move operator.
114  * @param scopedState The state object to be moved
115  * @return Reference to this object
116  */
118 
119  protected:
120 
121  /**
122  * Disabled copy constructor.
123  */
124  ScopedState(const ScopedState&) = delete;
125 
126  /**
127  * Disabled assign operator.
128  * @return The reference to this object
129  */
130  ScopedState& operator=(const ScopedState&) = delete;
131 
132  protected:
133 
134  /// The visualizer to which this state object belongs.
135  VRHandVisualizer* vrHandVisualizer_ = nullptr;
136 
137  /// True, if the hands where shown when this object was created.
138  bool wasShown_ = false;
139 
140  /// The color to be used when rendering the hands.
141  RGBAColor handColor_ = RGBAColor(0.7f, 0.7f, 0.7f);
142 
143  /// The transparency when this object was created.
144  Scalar transparency_ = Scalar(0.6);
145 
146  /// The render mode when this object was created.
147  RenderMode renderMode_ = RM_DEFAULT;
148  };
149 
150  protected:
151 
152  /// The shader part for the platform.
153  static const char* partPlatform_;
154 
155  /// The shader part with the vertex shader.
156  static const char* partVertexShaderTexture_;
157 
158  /// The shader part with the fragment shader.
159  static const char* partFragmentShader_;
160 
161  /**
162  * Definition of a vector holding state objects.
163  */
164  typedef std::vector<ScopedState> ScopedStates;
165 
166  public:
167 
168  /**
169  * Default constructor, creates a new invalid visualizer.
170  */
171  VRHandVisualizer() = default;
172 
173  /**
174  * Creates a new hand visualizer and initializes the object with a given rendering engine and associated framebuffer
175  * Rendering engine and framebuffer are necessary so that the rendering objects (like Scene, Transform, Texture2D) can be created and attached to the existing rendering objects.
176  * @param engine The rendering engine to be used, must be valid
177  * @param framebuffer The framebuffer to be used, must be valid
178  */
179  inline VRHandVisualizer(const Rendering::EngineRef& engine, const Rendering::FramebufferRef framebuffer);
180 
181  /**
182  * Move constructor
183  * @param vrHandVisualizer Another instance of the hand visualizer that will be moved to this instance
184  */
185  inline VRHandVisualizer(VRHandVisualizer&& vrHandVisualizer);
186 
187  /**
188  * Disable the copy constructor
189  * @param vrHandVisualizer Another instance of the hand visualizer that would have been copied to this instance
190  */
191  VRHandVisualizer(const VRHandVisualizer& vrHandVisualizer) = delete;
192 
193  /**
194  * Destructor
195  */
196  ~VRHandVisualizer() override;
197 
198  /**
199  * Shows the hands.
200  * @see hide().
201  */
202  inline void show();
203 
204  /**
205  * Hides the hands.
206  * @see show().
207  */
208  inline void hide();
209 
210  /**
211  * Returns whether the hands are shown.
212  * @return True, if succeeded
213  */
214  inline bool isShown() const;
215 
216  /**
217  * Sets the hand's transparency.
218  * @param transparency The transparency to be used when rendering the hands, 0 is fully opaque, 1 is fully transparent, with range [0, 1]
219  * @return True, if succeeded
220  * @see transparency().
221  */
222  bool setTransparency(const Scalar transparency);
223 
224  /**
225  * Returns the hand's transparency.
226  * @return The transparency which is used when rendering the hands, with range [0, 1]
227  * @see setTransparency().
228  */
230 
231  /**
232  * Sets the hand's render mode.
233  * @param renderMode The render mode to be set
234  * @return True, if succeeded
235  * @see renderMode().
236  */
237  bool setRenderMode(const RenderMode renderMode);
238 
239  /**
240  * Returns the hand's render mode.
241  * @return The mode which is used to render the hands.
242  * @see setRenderMode().
243  */
245 
246  /**
247  * Visualizes the hands in relation to the given HandPoses' base space.
248  * Note: you need to call `handPoses.update()` before calling this function
249  * @param handPoses An instance of the hand poses object, must be valid
250  * @return True, if succeeded
251  */
252  bool visualizeHands(const HandPoses& handPoses);
253 
254  /**
255  * Pushes a new configuration state to the stack.
256  * Each push needs to be balanced with a pop.
257  * @see popState().
258  */
259  inline void pushState();
260 
261  /**
262  * Pops the most recent state from the stack and resets the visualizer's configuration accordingly.
263  */
264  inline void popState();
265 
266  /**
267  * Releases the hand visualizer and all associated resources explicitly.
268  */
269  void release();
270 
271  /**
272  * Disable the copy-assignment operator
273  * @param vrHandVisualizer Another instance of the hand visualizer that would have been copied to this instance
274  * @return A reference to this instance of the hand visualizer that would have been returned
275  */
276  inline VRHandVisualizer& operator=(VRHandVisualizer& vrHandVisualizer) = delete;
277 
278  /**
279  * Move-assignment operator
280  * @param vrHandVisualizer Another instance of the hand visualizer that will be moved to this instance
281  * @return A reference to this instance of the hand visualizer
282  */
283  inline VRHandVisualizer& operator=(VRHandVisualizer&& vrHandVisualizer);
284 
285  protected:
286 
287  /**
288  * Visualizes the joints of the hands.
289  * @param handPoses The hand poses object providing the necessary information to render the hands
290  * @return True, if succeeded
291  */
292  bool visualizeJoints(const HandPoses& handPoses);
293 
294  /**
295  * Visualizes the wireframe of the hands.
296  * @param handPoses The hand poses object providing the necessary information to render the hands
297  * @return True, if succeeded
298  */
299  bool visualizeWireframe(const HandPoses& handPoses);
300 
301  /**
302  * Visualizes the mesh of the hands.
303  * @param handPoses The hand poses object providing the necessary information to render the hands
304  * @return True, if succeeded
305  */
306  bool visualizeMesh(const HandPoses& handPoses);
307 
308  protected:
309 
310  /// The render mode to be used.
311  RenderMode renderMode_ = RM_DEFAULT;
312 
313  /// The scene object of the renderer;
315 
316  /// The rendering Transform node for rendering the joints.
318 
319  /// The rendering VertexSet object for rendering the joints.
321 
322  /// The rendering Group node for rendering the wireframe.
324 
325  /// The rendering Group node for rendering the mesh.
327 
328  /// The rendering Geometry nodes for rendering the left and right hand mesh.
330 
331  /// The rendering ShaderProgram objects for rendering the left and right hand mesh.
333 
334  /// The color to be used when rendering the hands.
335  RGBAColor handColor_ = RGBAColor(0.7f, 0.7f, 0.7f);
336 
337  /// The transparency which is used to render the hands, with range [0, 1]
338  Scalar transparency_ = ScopedState::defaultTransparency_;
339 
340  /// True, if the hands are visualized; False, if the hands are hidden.
341  std::atomic<bool> isShown_ = true;
342 
343  /// A stack of visualization states.
345 };
346 
348  VRVisualizer(engine, framebuffer)
349 {
350  // nothhing to do here
351 }
352 
355 {
356  *this = std::move(vrHandVisualizer);
357 }
358 
360 {
361  if (scene_)
362  {
363  framebuffer_->removeScene(scene_);
364  }
365 }
366 
368 {
369  isShown_ = true;
370 }
371 
373 {
374  isShown_ = false;
375 }
376 
377 inline bool VRHandVisualizer::isShown() const
378 {
379  return isShown_;
380 }
381 
383 {
384  const ScopedLock scopedLock(lock_);
385 
386  stateStack_.emplace_back(*this);
387 }
388 
390 {
391  const ScopedLock scopedLock(lock_);
392 
393  ocean_assert(!stateStack_.empty());
394 
395  stateStack_.pop_back();
396 }
397 
399 {
400  if (this != &vrHandVisualizer)
401  {
402  release();
403 
404  renderMode_ = vrHandVisualizer.renderMode_;
405  vrHandVisualizer.renderMode_ = RM_DEFAULT;
406 
407  scene_ = std::move(vrHandVisualizer.scene_);
408 
409  transformJoints_ = std::move(vrHandVisualizer.transformJoints_);
410  vertexSetJoints_ = std::move(vrHandVisualizer.vertexSetJoints_);
411 
412  groupWireFrame_ = std::move(vrHandVisualizer.groupWireFrame_);
413 
414  groupMesh_ = std::move(vrHandVisualizer.groupMesh_);
415 
416  for (size_t handIndex = 0; handIndex < HandPoses::numberHands_; ++handIndex)
417  {
418  geometriesMesh_[handIndex] = std::move(vrHandVisualizer.geometriesMesh_[handIndex]);
419  shaderProgramsMesh_[handIndex] = std::move(vrHandVisualizer.shaderProgramsMesh_[handIndex]);
420  }
421 
422  handColor_ = vrHandVisualizer.handColor_;
423  vrHandVisualizer.handColor_ = RGBAColor(0.7f, 0.7f, 0.7f);
424 
425  transparency_ = vrHandVisualizer.transparency_;
426  vrHandVisualizer.transparency_ = ScopedState::defaultTransparency_;
427 
428  isShown_ = bool(vrHandVisualizer.isShown_);
429  vrHandVisualizer.isShown_ = true;
430 
431  stateStack_ = std::move(vrHandVisualizer.stateStack_);
432 
433  VRVisualizer::operator=(std::move(vrHandVisualizer));
434  }
435 
436  return *this;
437 }
438 
439 }
440 
441 }
442 
443 }
444 
445 }
446 
447 }
448 
449 }
450 
451 #endif // META_OCEAN_PLATFORM_META_QUEST_OPENXR_APPLICATION_VR_HAND_VISUALIZER_H
This class implements the base class for all VR visualizers allowing to visualize e....
Definition: VRVisualizer.h:43
Lock lock_
The visualizer's lock.
Definition: VRVisualizer.h:170
Rendering::WindowFramebufferRef framebuffer_
The rendering framebuffer to be used for visualization.
Definition: VRVisualizer.h:167
This class implements a scoped state object allowing to reset all states of a visualizer.
Definition: VRHandVisualizer.h:74
ScopedState & operator=(ScopedState &&scopedState)
Move operator.
ScopedState(VRHandVisualizer &vrHandVisualizer)
Creates a new state object.
ScopedState & operator=(const ScopedState &)=delete
Disabled assign operator.
ScopedState(const ScopedState &)=delete
Disabled copy constructor.
static constexpr Scalar defaultTransparency_
The default hand transparency.
Definition: VRHandVisualizer.h:80
This class implements helper functions allowing to visualize the hand mesh used by hand tracking (Nim...
Definition: VRHandVisualizer.h:46
Rendering::VertexSetRef vertexSetJoints_
The rendering VertexSet object for rendering the joints.
Definition: VRHandVisualizer.h:320
std::vector< ScopedState > ScopedStates
Definition of a vector holding state objects.
Definition: VRHandVisualizer.h:164
VRHandVisualizer & operator=(VRHandVisualizer &vrHandVisualizer)=delete
Disable the copy-assignment operator.
Rendering::SceneRef scene_
The scene object of the renderer;.
Definition: VRHandVisualizer.h:314
void show()
Shows the hands.
Definition: VRHandVisualizer.h:367
void release()
Releases the hand visualizer and all associated resources explicitly.
VRHandVisualizer(const VRHandVisualizer &vrHandVisualizer)=delete
Disable the copy constructor.
bool setRenderMode(const RenderMode renderMode)
Sets the hand's render mode.
bool visualizeHands(const HandPoses &handPoses)
Visualizes the hands in relation to the given HandPoses' base space.
RenderMode renderMode() const
Returns the hand's render mode.
Scalar transparency_
The transparency which is used to render the hands, with range [0, 1].
Definition: VRHandVisualizer.h:338
static const char * partVertexShaderTexture_
The shader part with the vertex shader.
Definition: VRHandVisualizer.h:156
RenderMode renderMode_
The render mode to be used.
Definition: VRHandVisualizer.h:311
static const char * partFragmentShader_
The shader part with the fragment shader.
Definition: VRHandVisualizer.h:159
bool visualizeJoints(const HandPoses &handPoses)
Visualizes the joints of the hands.
RenderMode
Definition of individual render modes.
Definition: VRHandVisualizer.h:53
@ RM_DEFAULT
The default rendering mode.
Definition: VRHandVisualizer.h:63
Rendering::ShaderProgramRef shaderProgramsMesh_[HandPoses::numberHands_]
The rendering ShaderProgram objects for rendering the left and right hand mesh.
Definition: VRHandVisualizer.h:332
ScopedStates stateStack_
A stack of visualization states.
Definition: VRHandVisualizer.h:344
bool visualizeMesh(const HandPoses &handPoses)
Visualizes the mesh of the hands.
~VRHandVisualizer() override
Destructor.
Definition: VRHandVisualizer.h:359
bool visualizeWireframe(const HandPoses &handPoses)
Visualizes the wireframe of the hands.
VRHandVisualizer()=default
Default constructor, creates a new invalid visualizer.
bool isShown() const
Returns whether the hands are shown.
Definition: VRHandVisualizer.h:377
Rendering::TransformRef transformJoints_
The rendering Transform node for rendering the joints.
Definition: VRHandVisualizer.h:317
Rendering::GroupRef groupWireFrame_
The rendering Group node for rendering the wireframe.
Definition: VRHandVisualizer.h:323
static const char * partPlatform_
The shader part for the platform.
Definition: VRHandVisualizer.h:153
bool setTransparency(const Scalar transparency)
Sets the hand's transparency.
void hide()
Hides the hands.
Definition: VRHandVisualizer.h:372
Rendering::GroupRef groupMesh_
The rendering Group node for rendering the mesh.
Definition: VRHandVisualizer.h:326
void popState()
Pops the most recent state from the stack and resets the visualizer's configuration accordingly.
Definition: VRHandVisualizer.h:389
RGBAColor handColor_
The color to be used when rendering the hands.
Definition: VRHandVisualizer.h:335
Rendering::GeometryRef geometriesMesh_[HandPoses::numberHands_]
The rendering Geometry nodes for rendering the left and right hand mesh.
Definition: VRHandVisualizer.h:329
std::atomic< bool > isShown_
True, if the hands are visualized; False, if the hands are hidden.
Definition: VRHandVisualizer.h:341
Scalar transparency() const
Returns the hand's transparency.
void pushState()
Pushes a new configuration state to the stack.
Definition: VRHandVisualizer.h:382
This class implements a wrapper for OpenXR-based hand tracking.
Definition: HandPoses.h:41
static constexpr size_t numberHands_
The number of supported hands.
Definition: HandPoses.h:50
This class implements a color defined by red, green, blue and alpha parameters.
Definition: RGBAColor.h:41
This class implements a scoped lock object for recursive lock objects.
Definition: Lock.h:135
float Scalar
Definition of a scalar type.
Definition: Math.h:128
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15