-
Notifications
You must be signed in to change notification settings - Fork 44
/
MinimalScene.hh
472 lines (365 loc) · 16.6 KB
/
MinimalScene.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
* Copyright (C) 2017 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef IGNITION_GUI_PLUGINS_SCENE3D_HH_
#define IGNITION_GUI_PLUGINS_SCENE3D_HH_
#include <string>
#include <memory>
#include <ignition/common/KeyEvent.hh>
#include <ignition/common/MouseEvent.hh>
#include <ignition/math/Color.hh>
#include <ignition/math/Pose3.hh>
#include <ignition/math/Vector2.hh>
#include <ignition/utils/ImplPtr.hh>
#include <ignition/rendering/GraphicsAPI.hh>
#include "ignition/gui/Plugin.hh"
#include "MinimalSceneRhi.hh"
namespace ignition
{
namespace gui
{
namespace plugins
{
/// \brief Creates an ignition rendering scene and user camera.
/// It is possible to orbit the camera around the scene with
/// the mouse. Use other plugins to manage objects in the scene.
///
/// Only one plugin displaying an Gazebo Rendering scene can be used at a
/// time.
///
/// ## Configuration
///
/// * \<engine\> : Optional render engine name, defaults to 'ogre'. If another
/// engine is already loaded, that will be used, because only
/// one engine is supported at a time currently.
/// * \<scene\> : Optional scene name, defaults to 'scene'. The plugin will
/// create a scene with this name if there isn't one yet. If
/// there is already one, a new camera is added to it.
/// * \<ambient_light\> : Optional color for ambient light, defaults to
/// (0.3, 0.3, 0.3, 1.0)
/// * \<background_color\> : Optional background color, defaults to
/// (0.3, 0.3, 0.3, 1.0)
/// * \<camera_pose\> : Optional starting pose for the camera, defaults to
/// (0, 0, 5, 0, 0, 0)
/// * \<camera_clip\> : Optional near/far clipping distance for camera
/// * \<near\> : Camera's near clipping plane distance, defaults to 0.01
/// * \<far\> : Camera's far clipping plane distance, defaults to 1000.0
/// * \<sky\> : If present, sky is enabled.
/// * \<graphics_api\> : Optional graphics API name. Valid choices are:
/// 'opengl', 'metal'. Defaults to 'opengl'.
class MinimalScene : public Plugin
{
Q_OBJECT
/// \brief Loading error message
Q_PROPERTY(
QString loadingError
READ LoadingError
WRITE SetLoadingError
NOTIFY LoadingErrorChanged
)
/// \brief Constructor
public: MinimalScene();
/// \brief Callback when the mouse hovers to a new position.
/// \param[in] _mouseX x coordinate of the hovered mouse position.
/// \param[in] _mouseY y coordinate of the hovered mouse position.
public slots: void OnHovered(int _mouseX, int _mouseY);
/// \brief Callback when the mouse enters the render window to
/// focus the window for mouse/key events
public slots: void OnFocusWindow();
/// \brief Callback when receives a drop event.
/// \param[in] _drop Dropped string.
/// \param[in] _mouseX x coordinate of mouse position.
/// \param[in] _mouseY y coordinate of mouse position.
public slots: void OnDropped(const QString &_drop,
int _mouseX, int _mouseY);
// Documentation inherited
public: virtual void LoadConfig(const tinyxml2::XMLElement *_pluginElem)
override;
/// \brief Get the loading error string.
/// \return String explaining the loading error. If empty, there's no error.
public: Q_INVOKABLE QString LoadingError() const;
/// \brief Set the loading error message.
/// \param[in] _loadingError Error message.
public: Q_INVOKABLE void SetLoadingError(const QString &_loadingError);
/// \brief Notify that loading error has changed
signals: void LoadingErrorChanged();
/// \brief Loading error message
public: QString loadingError;
/// \internal
/// \brief Pointer to private data.
IGN_UTILS_UNIQUE_IMPL_PTR(dataPtr)
};
class RenderSync;
/// \brief Ign-rendering renderer.
/// All ign-rendering calls should be performed inside this class as it makes
/// sure that opengl calls in the underlying render engine do not interfere
/// with QtQuick's opengl render operations. The main Render function will
/// render to an offscreen texture and notify via signal and slots when it's
/// ready to be displayed.
class IgnRenderer
{
/// \brief Constructor
public: IgnRenderer();
/// \param[in] _renderSync RenderSync to safely
/// synchronize Qt and worker thread (this)
public: void Render(RenderSync *_renderSync);
/// \brief Initialize the render engine and scene.
/// On macOS this must be called on the main thread.
/// \return Error message if initialization failed. If empty, no errors
/// occurred.
public: std::string Initialize();
/// \brief Set the graphics API
/// \param[in] _graphicsAPI The type of graphics API
public: void SetGraphicsAPI(const rendering::GraphicsAPI &_graphicsAPI);
/// \brief Destroy camera associated with this renderer
public: void Destroy();
/// \brief New mouse event triggered
/// \param[in] _e New mouse event
public: void NewMouseEvent(const common::MouseEvent &_e);
/// \brief New hover event triggered.
/// \param[in] _hoverPos Mouse hover screen position
public: void NewHoverEvent(const math::Vector2i &_hoverPos);
/// \brief New hover event triggered.
/// \param[in] _dropText Text dropped on the scene
/// \param[in] _dropPos Mouse drop screen position
public: void NewDropEvent(const std::string &_dropText,
const math::Vector2i &_dropPos);
/// \brief Handle key press event for snapping
/// \param[in] _e The key event to process.
public: void HandleKeyPress(const common::KeyEvent &_e);
/// \brief Handle key release event for snapping
/// \param[in] _e The key event to process.
public: void HandleKeyRelease(const common::KeyEvent &_e);
/// \brief Handle mouse event for view control
private: void HandleMouseEvent();
/// \brief Broadcasts the currently hovered 3d scene location.
private: void BroadcastHoverPos();
/// \brief Broadcasts drag events.
private: void BroadcastDrag();
/// \brief Broadcasts a left click (release) within the scene
private: void BroadcastLeftClick();
/// \brief Broadcasts a right click (release) within the scene
private: void BroadcastRightClick();
/// \brief Broadcasts a mouse press within the scene
private: void BroadcastMousePress();
/// \brief Broadcasts a scroll event within the scene.
private: void BroadcastScroll();
/// \brief Broadcasts a key release event within the scene
private: void BroadcastKeyRelease();
/// \brief Broadcasts a drop event within the scene
private: void BroadcastDrop();
/// \brief Broadcasts a key press event within the scene
private: void BroadcastKeyPress();
/// Values is constantly constantly cycled/swapped/changed
/// from a worker thread
/// Don't read this directly
/// \param[out] _texturePtr Pointer to a texture Id
public: void TextureId(void* _texturePtr);
/// \brief Render engine to use
public: std::string engineName = "ogre";
/// \brief Unique scene name
public: std::string sceneName = "scene";
/// \brief Initial Camera pose
public: math::Pose3d cameraPose = math::Pose3d(0, 0, 2, 0, 0.4, 0);
/// \brief Default camera near clipping plane distance
public: double cameraNearClip = 0.01;
/// \brief Default camera far clipping plane distance
public: double cameraFarClip = 1000.0;
/// \brief Scene background color
public: math::Color backgroundColor = math::Color::Black;
/// \brief Ambient color
public: math::Color ambientLight = math::Color(0.3f, 0.3f, 0.3f, 1.0f);
/// \brief True if engine has been initialized;
public: bool initialized = false;
/// \brief Render texture size
public: QSize textureSize = QSize(1024, 1024);
/// \brief Flag to indicate texture size has changed.
public: bool textureDirty = true;
/// \brief True if sky is enabled;
public: bool skyEnable = false;
/// \internal
/// \brief Pointer to private data.
IGN_UTILS_UNIQUE_IMPL_PTR(dataPtr)
};
/// \brief Rendering thread
class RenderThread : public QThread
{
Q_OBJECT
/// \brief Constructor
public: RenderThread();
/// \brief Render when safe
/// \param[in] _renderSync RenderSync to safely
/// synchronize Qt and worker thread (this)
public slots: void RenderNext(RenderSync *_renderSync);
/// \brief Shutdown the thread and the render engine
public slots: void ShutDown();
/// \brief Slot called to update render texture size
public slots: void SizeChanged();
/// \brief Signal to indicate that a frame has been rendered and ready
/// to be displayed
/// \param[in] _texturePtr Pointer to a texture Id
/// \param[in] _size Size of the texture
signals: void TextureReady(void* _texturePtr, const QSize &_size);
/// \brief Set a callback to be called in case there are errors.
/// \param[in] _cb Error callback
public: void SetErrorCb(std::function<void(const QString &)> _cb);
/// \brief Function to be called if there are errors.
public: std::function<void(const QString &)> errorCb;
/// \brief Offscreen surface to render to
public: QOffscreenSurface *Surface() const;
/// \brief Set the offscreen surface to render to
/// \param[in] _surface Off-screen surface format
public: void SetSurface(QOffscreenSurface *_surface);
/// \brief OpenGL context to be passed to the render engine
public: QOpenGLContext *Context() const;
/// \brief Set the OpenGL context to be passed to the render engine
/// \param[in] _context OpenGL context
public: void SetContext(QOpenGLContext *_context);
/// \brief Set the graphics API
/// \param[in] _graphicsAPI The type of graphics API
public: void SetGraphicsAPI(const rendering::GraphicsAPI &_graphicsAPI);
/// \brief Carry out initialisation.
/// On macOS this must be run on the main thread
public: std::string Initialize();
/// \brief Ign-rendering renderer
public: IgnRenderer ignRenderer;
/// \brief Pointer to render interface to handle OpenGL/Metal compatibility
private: std::unique_ptr<RenderThreadRhi> rhi;
};
/// \brief A QQUickItem that manages the render window
class RenderWindowItem : public QQuickItem
{
Q_OBJECT
/// \brief Constructor
/// \param[in] _parent Parent item
public: explicit RenderWindowItem(QQuickItem *_parent = nullptr);
public: ~RenderWindowItem();
/// \brief Set background color of render window
/// \param[in] _color Color of render window background
public: void SetBackgroundColor(const math::Color &_color);
/// \brief Set ambient light of render window
/// \param[in] _ambient Color of ambient light
public: void SetAmbientLight(const math::Color &_ambient);
/// \brief Set engine name used to create the render window
/// \param[in] _name Name of render engine
public: void SetEngineName(const std::string &_name);
/// \brief Set name of scene created inside the render window
/// \param[in] _name Name of scene
public: void SetSceneName(const std::string &_name);
/// \brief Set the initial pose the render window camera
/// \param[in] _pose Initial camera pose
public: void SetCameraPose(const math::Pose3d &_pose);
/// \brief Set the render window camera's near clipping plane distance
/// \param[in] _near Near clipping plane distance
public: void SetCameraNearClip(double _near);
/// \brief Set the render window camera's far clipping plane distance
/// \param[in] _far Far clipping plane distance
public: void SetCameraFarClip(double _far);
/// \brief Called when the mouse hovers to a new position.
/// \param[in] _hoverPos 2D coordinates of the hovered mouse position on
/// the render window.
public: void OnHovered(const ignition::math::Vector2i &_hoverPos);
/// \brief Callback when receives a drop event.
/// \param[in] _drop Dropped string.
/// \param[in] _dropPos x coordinate of mouse position.
public: void OnDropped(const QString &_drop,
const ignition::math::Vector2i &_dropPos);
/// \brief Set if sky is enabled
/// \param[in] _sky True to enable the sky, false otherwise.
public: void SetSkyEnabled(const bool &_sky);
/// \brief Set the graphics API
/// \param[in] _graphicsAPI The type of graphics API
public: void SetGraphicsAPI(const rendering::GraphicsAPI& _graphicsAPI);
/// \brief Slot called when thread is ready to be started
public Q_SLOTS: void Ready();
/// \brief Handle key press event for snapping
/// \param[in] _e The key event to process.
public: void HandleKeyPress(const common::KeyEvent &_e);
/// \brief Handle key release event for snapping
/// \param[in] _e The key event to process.
public: void HandleKeyRelease(const common::KeyEvent &_e);
/// \brief Set a callback to be called in case there are errors.
/// \param[in] _cb Error callback
public: void SetErrorCb(std::function<void(const QString &)> _cb);
/// \brief Stop rendering and shutdown resources.
public: void StopRendering();
// Documentation inherited
protected: virtual void mousePressEvent(QMouseEvent *_e) override;
// Documentation inherited
protected: virtual void mouseReleaseEvent(QMouseEvent *_e) override;
// Documentation inherited
protected: virtual void mouseMoveEvent(QMouseEvent *_e) override;
// Documentation inherited
protected: virtual void keyPressEvent(QKeyEvent *_e) override;
// Documentation inherited
protected: virtual void keyReleaseEvent(QKeyEvent *_e) override;
// Documentation inherited
protected: virtual void wheelEvent(QWheelEvent *_e) override;
/// \brief Overrides the paint event to render the render engine
/// camera view
/// \param[in] _oldNode The node passed in previous updatePaintNode
/// function. It represents the visual representation of the item.
/// \param[in] _data The node transformation data.
/// \return Updated node.
private: QSGNode *updatePaintNode(QSGNode *_oldNode,
QQuickItem::UpdatePaintNodeData *_data) override;
/// \internal
/// \brief Pointer to private data.
IGN_UTILS_UNIQUE_IMPL_PTR(dataPtr)
};
/// \brief Texture node for displaying the render texture from ign-renderer
class TextureNode : public QObject, public QSGSimpleTextureNode
{
Q_OBJECT
/// \brief Constructor
/// \param[in] _window Window to display the texture
/// \param[in] _renderSync RenderSync to safely
/// synchronize Qt (this) and worker thread
/// \param[in] _graphicsAPI The type of graphics API
public: explicit TextureNode(QQuickWindow *_window,
RenderSync &_renderSync,
const rendering::GraphicsAPI &_graphicsAPI);
/// \brief Destructor
public: ~TextureNode() override;
/// \brief This function gets called on the FBO rendering thread and will
/// store the texture id and size and schedule an update on the window.
/// \param[in] _texturePtr Pointer to a texture Id
/// \param[in] _size Texture size
// public slots: void NewTexture(uint _id, const QSize &_size);
public slots: void NewTexture(void* _texturePtr, const QSize &_size);
/// \brief Before the scene graph starts to render, we update to the
/// pending texture
public slots: void PrepareNode();
/// \param[in] _renderSync RenderSync to send to the worker thread
signals: void TextureInUse(RenderSync *_renderSync);
/// \brief Signal emitted when a new texture is ready to trigger window
/// update
signals: void PendingNewTexture();
/// \brief Texture size
public: QSize size = QSize(0, 0);
/// \brief Mutex to protect the texture variables
public: QMutex mutex;
/// \brief See RenderSync
public: RenderSync &renderSync;
/// \brief Qt quick window
public: QQuickWindow *window = nullptr;
/// \brief Pointer to render interface to handle OpenGL/Metal compatibility
private: std::unique_ptr<TextureNodeRhi> rhi;
};
}
}
}
#endif