VR-Engage  2.2
Loading...
Searching...
No Matches
vrvOverlay.h
Go to the documentation of this file.
1/******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4******************************************************************************/
5
6//! \file vrvOverlay.h
7//! \brief Provides a 2D overlay rendering system for VRV-based applications
8//!
9//! The DtVrvOverlay class provides an interface for creating and managing 2D
10//! overlay graphics (text, lines, quads, textures) that are rendered on top of
11//! a 3D scene using the VRV rendering engine. This is commonly used for HUDs,
12//! UI elements, and debugging visualizations.
13
14#pragma once
15
17
18#include <vreUtil/delegate.h>
19
20#include <vrvGraphics/DtFont.h>
21
22#include <osg/Geode>
23#include <osg/Texture2D>
24#include <osg/ref_ptr>
25
26namespace makVrv
27{
28//! Forward declaration of display engine class
29class DtDe;
30
31//! Forward declaration of overlay renderer interface
32class DtOverlayRendererInterface;
33
34//! Forward declaration of overlay renderer implementation
35class DtOverlayRenderer;
36
37//! Forward declaration of OSG channel class
38class DtOsgChannel;
39
40//! Forward declaration of font class
41class DtFont;
42
43//! Forward declaration of text proxy class
44class DtTextProxy;
45
46//! Forward declaration of line segment class
47class DtLineSegment;
48
49//! Forward declaration of quad proxy class
50class DtQuadProxy;
51
52//! Forward declaration of texture wrapper class
53class DtOsgTextureWrapper;
54} // namespace makVrv
55
56
57namespace makVre
58{
59//! \brief Simple structure to hold a reference to a font
60//!
61//! This structure holds a pointer to a font interface to prevent it
62//! from being deleted while still in use by the overlay system.
64{
65 //! \brief Pointer to the font interface
66 makVrv::DtFontInterface* myFont;
67};
68
69//! \brief Delegate type for overlay resize events
71
72//! \brief Class for creating and managing 2D overlays on a 3D scene
73//!
74//! DtVrvOverlay provides functionality for creating and managing 2D overlay
75//! elements (text, lines, quads, and textures) that are rendered on top of
76//! a 3D scene within a specific window and channel. It handles viewport
77//! transformations, resource management, and cleanup.
79{
80public:
81 //! \brief Constructor that creates an overlay for a specific window and channel
82 //! \param windowName The name of the window to create the overlay on
83 //! \param channelName The name of the channel within the window
84 //!
85 //! Creates a new overlay on the specified window and channel. Throws an
86 //! exception if the window or channel cannot be found, or if the overlay
87 //! cannot be created.
88 DtVrvOverlay(const std::string& windowName, const std::string& channelName);
89
90 //! \brief Constructor that creates an overlay for a specific channel
91 //! \param channel Pointer to the OSG channel to create the overlay on
92 //!
93 //! Creates a new overlay on the specified channel. Throws an exception
94 //! if the channel is invalid or if the overlay cannot be created.
95 DtVrvOverlay(makVrv::DtOsgChannel* channel);
96
97 //! \brief Virtual destructor that cleans up resources
98 //!
99 //! Destroys the overlay and cleans up associated resources.
100 virtual ~DtVrvOverlay();
101
102 //! \brief Gets the overlay renderer object
103 //! \return Pointer to the overlay renderer, or nullptr if not initialized
104 //!
105 //! Returns the underlying overlay renderer that can be used for direct
106 //! rendering operations.
107 virtual makVrv::DtOverlayRenderer* overlay();
108
109 //! \brief Gets the geode node used for the overlay
110 //! \return Pointer to the OSG geode node
111 //!
112 //! Returns the OSG geode node that contains the overlay drawable.
113 virtual osg::Geode* geode();
114
115 //! \brief Gets the channel associated with this overlay
116 //! \return Pointer to the OSG channel
117 //!
118 //! Returns the OSG channel that this overlay is attached to.
119 virtual makVrv::DtOsgChannel* channel();
120
121 //! \brief Creates a font for use with overlay text
122 //! \param filename The font file path (will be resolved through path configuration)
123 //! \param size The font size in points
124 //! \return A font holder containing the created font
125 //!
126 //! Creates and caches a font that can be used for rendering text in the overlay.
127 //! Throws an exception if the font cannot be loaded.
128 virtual DtFontHolder* createFont(const std::string& filename, float size);
129
130 //! \brief Creates a text element for the overlay
131 //! \return A new text proxy object
132 //!
133 //! Creates a new text element that can be configured and added to the overlay.
134 virtual makVrv::DtTextProxy* createText();
135
136 //! \brief Creates a line element for the overlay
137 //! \return A new line segment object
138 //!
139 //! Creates a new line segment that can be configured and added to the overlay.
140 virtual makVrv::DtLineSegment* createLine();
141
142 //! \brief Creates a quad element for the overlay
143 //! \return A new quad proxy object
144 //!
145 //! Creates a new quad that can be textured and added to the overlay.
146 //! By default, creates textured quads.
147 virtual makVrv::DtQuadProxy* createQuad();
148
149 //! \brief Creates a texture from an image file
150 //! \param filename The image file path (will be resolved through path configuration)
151 //! \return A texture wrapper containing the loaded texture, or nullptr if loading failed
152 //!
153 //! Loads an image from the specified file and creates a texture that can be
154 //! used with quads in the overlay. The texture is cached to prevent it from
155 //! being garbage collected.
156 virtual makVrv::DtOsgTextureWrapper* createTexture(const std::string& filename);
157
158 //! \brief Converts a normalized X coordinate to pixel coordinates
159 //! \param normalizedX The normalized X coordinate (0.0 to 1.0, left to right)
160 //! \return The pixel X coordinate relative to the channel viewport
161 virtual float pixelX(float normalizedX);
162
163 //! \brief Converts a normalized Y coordinate to pixel coordinates
164 //! \param normalizedY The normalized Y coordinate (0.0 to 1.0, top to bottom)
165 //! \return The pixel Y coordinate relative to the channel viewport
166 virtual float pixelY(float normalizedY);
167
168 //! \brief Converts a normalized height to pixel height
169 //! \param normalizedH The normalized height (0.0 to 1.0)
170 //! \return The height in pixels
171 virtual float pixelH(float normalizedH);
172
173 //! \brief Converts a normalized width to pixel width
174 //! \param normalizedW The normalized width (0.0 to 1.0)
175 //! \return The width in pixels
176 virtual float pixelW(float normalizedW);
177
178 //! \brief Adds a handler for overlay resize events
179 //! \param del The delegate to call when the overlay is resized
180 //!
181 //! Registers a callback function that will be called whenever the overlay's
182 //! viewport changes size. Multiple callbacks can be registered, and each will
183 //! be called in the order they were added.
185
186 //! \brief Removes a previously added resize handler
187 //! \param del The delegate to remove
188 //!
189 //! Unregisters a callback function that was previously added with
190 //! addOverlayResizedHandler. If the delegate is not found, a warning
191 //! is logged.
193
194protected:
195 //! \brief Finds a channel by name and sets it as the current channel
196 //! \param windowName The name of the window containing the channel
197 //! \param channelName The name of the channel to find
198 //! \return True if the channel was found, false otherwise
199 //!
200 //! Searches for a channel with the specified name in the specified window
201 //! and sets it as the current channel for this overlay.
202 virtual bool findChannel(const std::string& windowName, const std::string& channelName);
203
204 //! \brief Sets the channel for this overlay
205 //! \param channel Pointer to the channel to use
206 //! \return True if the channel was set successfully, false otherwise
207 //!
208 //! Sets the specified channel as the current channel for this overlay
209 //! and registers for viewport change notifications.
210 virtual bool setChannel(makVrv::DtOsgChannel* channel);
211
212 //! \brief Creates the overlay rendering system
213 //! \return True if the overlay was created successfully, false otherwise
214 //!
215 //! Creates the overlay renderer and adds it to the channel's scene graph.
216 virtual bool createOverlay();
217
218 //! \brief Destroys the overlay and cleans up resources
219 //!
220 //! Removes the overlay from the channel's scene graph and releases
221 //! allocated resources.
222 virtual void destroyOverlay();
223
224 //! \brief Called when the viewport of the channel changes
225 //!
226 //! Updates the cached viewport dimensions and notifies registered
227 //! resize handlers of the change.
228 virtual void onViewportChanged();
229
230 //! \brief Called when a channel is about to be destroyed
231 //! \param displayName The name of the display containing the channel
232 //! \param windowName The name of the window containing the channel
233 //! \param channelName The name of the channel being destroyed
234 //!
235 //! Checks if the channel being destroyed is the one used by this overlay,
236 //! and if so, cleans up the overlay resources.
237 virtual void onChannelDestroyed(
238 const std::string& displayName, const std::string& windowName, const std::string& channelName);
239
240 //! \brief Pointer to the display engine
241 makVrv::DtDe* myDe;
242
243 //! \brief Pointer to the overlay renderer
244 makVrv::DtOverlayRenderer* myOverlayRenderer;
245
246 //! \brief Pointer to the channel this overlay is attached to
247 makVrv::DtOsgChannel* myChannel;
248
249 //! \brief The OSG geode node containing the overlay drawable
250 osg::ref_ptr<osg::Geode> myGeode;
251
252 //! \brief The X position of the channel viewport
253 int myX;
254
255 //! \brief The Y position of the channel viewport
256 int myY;
257
258 //! \brief The width of the channel viewport
259 int myW;
260
261 //! \brief The height of the channel viewport
262 int myH;
263
264 //! \brief Collection of cached fonts to prevent garbage collection
265 std::vector<DtFontHolder*> myCachedFonts;
266
267 //! \brief Collection of cached textures to prevent garbage collection
268 std::vector<osg::ref_ptr<osg::Texture2D>> myCachedTextures;
269
270 //! \brief Type definition for a list of resize handlers
271 using OverlayResizedList = std::vector<DtOverlayResizedDelegate>;
272
273 //! \brief List of callbacks to notify when the overlay is resized
275};
276} // namespace makVre
Modern delegate class that can bind and invoke callables with any number of parameters.
Definition delegate.h:31
virtual makVrv::DtOverlayRenderer * overlay()
Gets the overlay renderer object.
virtual osg::Geode * geode()
Gets the geode node used for the overlay.
virtual float pixelH(float normalizedH)
Converts a normalized height to pixel height.
virtual void removeOverlayResizedHandler(const DtOverlayResizedDelegate &del)
Removes a previously added resize handler.
OverlayResizedList myOverlayResizedCallbacks
List of callbacks to notify when the overlay is resized.
Definition vrvOverlay.h:274
std::vector< osg::ref_ptr< osg::Texture2D > > myCachedTextures
Collection of cached textures to prevent garbage collection.
Definition vrvOverlay.h:268
virtual void destroyOverlay()
Destroys the overlay and cleans up resources.
virtual makVrv::DtTextProxy * createText()
Creates a text element for the overlay.
int myY
The Y position of the channel viewport.
Definition vrvOverlay.h:256
osg::ref_ptr< osg::Geode > myGeode
The OSG geode node containing the overlay drawable.
Definition vrvOverlay.h:250
int myW
The width of the channel viewport.
Definition vrvOverlay.h:259
virtual void onChannelDestroyed(const std::string &displayName, const std::string &windowName, const std::string &channelName)
Called when a channel is about to be destroyed.
virtual float pixelW(float normalizedW)
Converts a normalized width to pixel width.
std::vector< DtOverlayResizedDelegate > OverlayResizedList
Type definition for a list of resize handlers.
Definition vrvOverlay.h:271
virtual ~DtVrvOverlay()
Virtual destructor that cleans up resources.
virtual DtFontHolder * createFont(const std::string &filename, float size)
Creates a font for use with overlay text.
virtual makVrv::DtQuadProxy * createQuad()
Creates a quad element for the overlay.
makVrv::DtOsgChannel * myChannel
Pointer to the channel this overlay is attached to.
Definition vrvOverlay.h:247
virtual float pixelX(float normalizedX)
Converts a normalized X coordinate to pixel coordinates.
virtual bool setChannel(makVrv::DtOsgChannel *channel)
Sets the channel for this overlay.
int myH
The height of the channel viewport.
Definition vrvOverlay.h:262
DtVrvOverlay(const std::string &windowName, const std::string &channelName)
Constructor that creates an overlay for a specific window and channel.
virtual makVrv::DtLineSegment * createLine()
Creates a line element for the overlay.
int myX
The X position of the channel viewport.
Definition vrvOverlay.h:253
virtual bool createOverlay()
Creates the overlay rendering system.
makVrv::DtDe * myDe
Pointer to the display engine.
Definition vrvOverlay.h:241
std::vector< DtFontHolder * > myCachedFonts
Collection of cached fonts to prevent garbage collection.
Definition vrvOverlay.h:265
DtVrvOverlay(makVrv::DtOsgChannel *channel)
Constructor that creates an overlay for a specific channel.
virtual bool findChannel(const std::string &windowName, const std::string &channelName)
Finds a channel by name and sets it as the current channel.
virtual float pixelY(float normalizedY)
Converts a normalized Y coordinate to pixel coordinates.
virtual makVrv::DtOsgTextureWrapper * createTexture(const std::string &filename)
Creates a texture from an image file.
virtual void addOverlayResizedHandler(const DtOverlayResizedDelegate &del)
Adds a handler for overlay resize events.
makVrv::DtOverlayRenderer * myOverlayRenderer
Pointer to the overlay renderer.
Definition vrvOverlay.h:244
virtual void onViewportChanged()
Called when the viewport of the channel changes.
virtual makVrv::DtOsgChannel * channel()
Gets the channel associated with this overlay.
Defines export macros for the VR-Engage Player Station library.
#define PLAYERSTATION_DLL
Definition export.h:24
Include export definitions for this library.
Definition glsVreMessageUtil.h:49
DtDelegate< void > DtOverlayResizedDelegate
Delegate type for overlay resize events.
Definition vrvOverlay.h:70
Definition appLauncherComponent.h:28
Simple structure to hold a reference to a font.
Definition vrvOverlay.h:64
makVrv::DtFontInterface * myFont
Pointer to the font interface.
Definition vrvOverlay.h:66