MeshLib C++ Docs
Loading...
Searching...
No Matches
MRViewer.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRViewerInstance.h"
4#include "MRMouse.h"
5#include <MRMesh/MRVector2.h>
8#include <cstdint>
9#include <filesystem>
10
11struct GLFWwindow;
12
14#define ENQUEUE_VIEWER_METHOD( NAME, METHOD ) MR::getViewerInstance().emplaceEvent( NAME, [] { \
15 MR::getViewerInstance() . METHOD (); \
16} )
17#define ENQUEUE_VIEWER_METHOD_ARGS( NAME, METHOD, ... ) MR::getViewerInstance().emplaceEvent( NAME, [__VA_ARGS__] { \
18 MR::getViewerInstance() . METHOD ( __VA_ARGS__ ); \
19} )
20#define ENQUEUE_VIEWER_METHOD_ARGS_SKIPABLE( NAME, METHOD, ... ) MR::getViewerInstance().emplaceEvent( NAME, [__VA_ARGS__] { \
21 MR::getViewerInstance() . METHOD ( __VA_ARGS__ ); \
22}, true )
23
24namespace MR
25{
26
27class FramebufferData;
28
29// This struct contains rules for viewer launch
31{
32 bool fullscreen{ false }; // if true starts fullscreen
33 int width{ 0 };
34 int height{ 0 };
36 {
37 Show, // Show window immediately
38 HideInit, // Show window after init
39 Hide, // Don't show window
40 TryHidden, // Launches in "Hide" mode if OpenGL is present and "NoWindow" if it is not
41 NoWindow // Don't initialize GL window (don't call GL functions)(force `isAnimating`)
42 } windowMode{ HideInit };
44 bool preferOpenGL3{ false };
45 bool render3dSceneInTexture{ true }; // If not set renders scene each frame
46 bool developerFeatures{ false }; // If set shows some developer features useful for debugging
47 bool multiViewport{ true }; // If set allows to move the imgui (tool) windows outside the main () window. (unavailable for Apple, Wayland and Emscripten)
48 std::string name{ "MRViewer" }; // Window name
49 bool resetConfig{ false }; // if true - resets config file on start of the application
50 bool startEventLoop{ true }; // If false - does not start event loop
51 bool close{ true }; // If !startEventLoop close immediately after start, otherwise close on window close, make sure you call `launchShut` manually if this flag is false
52 bool console{ false }; // If true - shows developers console
53 int argc{ 0 }; // Pass argc
54 char** argv{ nullptr }; // Pass argv
55
56 bool showMRVersionInTitle{ false }; // if true - print version info in window title
57 bool isAnimating{ false }; // if true - calls render without system events
58 int animationMaxFps{ 30 }; // max fps if animating
59 bool unloadPluginsAtEnd{ false }; // unload all extended libraries right before program exit
60
61 std::shared_ptr<SplashWindow> splashWindow; // if present will show this window while initializing plugins (after menu initialization)
62};
63
64// GLFW-based mesh viewer
65class MRVIEWER_CLASS Viewer
66{
67public:
68 using MouseButton = MR::MouseButton;
69 using MouseMode = MR::MouseMode;
70
72
73 // Accumulate launch params from cmd args
74 MRVIEWER_API static void parseLaunchParams( LaunchParams& params );
75
76 // Launch viewer with given params
77 MRVIEWER_API int launch( const LaunchParams& params );
78 // Starts event loop
79 MRVIEWER_API void launchEventLoop();
80 // Terminate window
81 MRVIEWER_API void launchShut();
82
83 bool isLaunched() const { return isLaunched_; }
84
85 // get full parameters with witch viewer was launched
86 const LaunchParams& getLaunchParams() const { return launchParams_; }
87
88 // provides non const access to viewer
89 static Viewer* instance() { return &getViewerInstance(); }
90 static Viewer& instanceRef() { return getViewerInstance(); }
91 // provide const access to viewer
92 static const Viewer* constInstance() { return &getViewerInstance(); }
93 static const Viewer& constInstanceRef() { return getViewerInstance(); }
94
95 template<typename PluginType>
96 PluginType* getPluginInstance()
97 {
98 for ( auto& plugin : plugins )
99 {
100 auto p = dynamic_cast< PluginType* >( plugin );
101 if ( p )
102 {
103 return p;
104 }
105 }
106 return nullptr;
107 }
108
109 // Mesh IO
110 // Check the supported file format
111 MRVIEWER_API bool isSupportedFormat( const std::filesystem::path& file_name );
112
113 // Load objects / scenes from files
114 // Note! load files with progress bar in next frame if it possible, otherwise load directly inside this function
115 MRVIEWER_API bool loadFiles( const std::vector< std::filesystem::path>& filesList, const FileLoadOptions & options );
116 MRVIEWER_API bool loadFiles( const std::vector< std::filesystem::path>& filesList );
117
118 // Save first selected objects to file
119 MRVIEWER_API bool saveToFile( const std::filesystem::path & mesh_file_name );
120
121 // Callbacks
122 MRVIEWER_API bool keyPressed( unsigned int unicode_key, int modifier );
123 MRVIEWER_API bool keyDown( int key, int modifier );
124 MRVIEWER_API bool keyUp( int key, int modifier );
125 MRVIEWER_API bool keyRepeat( int key, int modifier );
126 MRVIEWER_API bool mouseDown( MouseButton button, int modifier );
127 MRVIEWER_API bool mouseUp( MouseButton button, int modifier );
128 MRVIEWER_API bool mouseMove( int mouse_x, int mouse_y );
129 MRVIEWER_API bool mouseScroll( float delta_y );
130 MRVIEWER_API bool mouseClick( MouseButton button, int modifier );
131 MRVIEWER_API bool dragStart( MouseButton button, int modifier );
132 MRVIEWER_API bool dragEnd( MouseButton button, int modifier );
133 MRVIEWER_API bool drag( int mouse_x, int mouse_y );
134 MRVIEWER_API bool spaceMouseMove( const Vector3f& translate, const Vector3f& rotate );
135 MRVIEWER_API bool spaceMouseDown( int key );
136 MRVIEWER_API bool spaceMouseUp( int key );
137 MRVIEWER_API bool spaceMouseRepeat( int key );
138 MRVIEWER_API bool dragDrop( const std::vector<std::filesystem::path>& paths );
139 // Touch callbacks (now used in EMSCRIPTEN build only)
140 MRVIEWER_API bool touchStart( int id, int x, int y );
141 MRVIEWER_API bool touchMove( int id, int x, int y );
142 MRVIEWER_API bool touchEnd( int id, int x, int y );
143 // Touchpad gesture callbacks
144 MRVIEWER_API bool touchpadRotateGestureBegin();
145 MRVIEWER_API bool touchpadRotateGestureUpdate( float angle );
146 MRVIEWER_API bool touchpadRotateGestureEnd();
147 MRVIEWER_API bool touchpadSwipeGestureBegin();
148 MRVIEWER_API bool touchpadSwipeGestureUpdate( float dx, float dy, bool kinetic );
149 MRVIEWER_API bool touchpadSwipeGestureEnd();
150 MRVIEWER_API bool touchpadZoomGestureBegin();
151 MRVIEWER_API bool touchpadZoomGestureUpdate( float scale, bool kinetic );
152 MRVIEWER_API bool touchpadZoomGestureEnd();
153 // This function is called when window should close, if return value is true, window will stay open
154 MRVIEWER_API bool interruptWindowClose();
155
156 // Draw everything
157 MRVIEWER_API void draw( bool force = false );
158 // Draw 3d scene with UI
159 MRVIEWER_API void drawFull( bool dirtyScene );
160 // Draw 3d scene without UI
161 // framebuffer - bound framebuffer (needed if depth peeling is enabled)
162 MRVIEWER_API void drawScene( FramebufferData* framebuffer );
163 // Call this function to force redraw scene into scene texture
164 void setSceneDirty() { dirtyScene_ = true; }
165 // Setup viewports views
166 MRVIEWER_API void setupScene();
167 // Cleans framebuffers for all viewports (sets its background)
168 MRVIEWER_API void clearFramebuffers();
169 // OpenGL context resize
170 MRVIEWER_API void resize( int w, int h ); // explicitly set framebuffer size
171 MRVIEWER_API void postResize( int w, int h ); // external resize due to user interaction
172 MRVIEWER_API void postSetPosition( int xPos, int yPos ); // external set position due to user interaction
173 MRVIEWER_API void postSetMaximized( bool maximized ); // external set maximized due to user interaction
174 MRVIEWER_API void postSetIconified( bool iconified ); // external set iconified due to user interaction
175 MRVIEWER_API void postFocus( bool focused ); // external focus handler due to user interaction
176 MRVIEWER_API void postRescale( float x, float y ); // external rescale due to user interaction
177 MRVIEWER_API void postClose(); // called when close signal received
178
180 // Multi-mesh methods //
182
183 // reset objectRoot with newRoot, append all RenderObjects and basis objects
184 MRVIEWER_API void set_root( SceneRootObject& newRoot );
185
186 // removes all objects from scene
187 MRVIEWER_API void clearScene();
188
190 // Multi-viewport methods //
192
193 // Return the current viewport, or the viewport corresponding to a given unique identifier
194 //
195 // Inputs:
196 // viewportId unique identifier corresponding to the desired viewport (current viewport if 0)
197 MRVIEWER_API Viewport& viewport( ViewportId viewportId = {} );
198 MRVIEWER_API const Viewport& viewport( ViewportId viewportId = {} ) const;
199
200 // Append a new "slot" for a viewport (i.e., copy properties of the current viewport, only
201 // changing the viewport size/position)
202 //
203 // Inputs:
204 // viewport Vector specifying the viewport origin and size in screen coordinates.
205 // append_empty If true, existing meshes are hidden on the new viewport.
206 //
207 // Returns the unique id of the newly inserted viewport. There can be a maximum of 31
208 // viewports created in the same viewport. Erasing a viewport does not change the id of
209 // other existing viewports
210 MRVIEWER_API ViewportId append_viewport( const ViewportRectangle & viewportRect, bool append_empty = false );
211
212 // Calculates and returns viewports bounds in gl space:
213 // (0,0) - lower left angle
214 MRVIEWER_API Box2f getViewportsBounds() const;
215
216 // Erase a viewport
217 //
218 // Inputs:
219 // index index of the viewport to erase
220 MRVIEWER_API bool erase_viewport( const size_t index );
221 MRVIEWER_API bool erase_viewport( ViewportId viewport_id );
222
223 // Retrieve viewport index from its unique identifier
224 // Returns -1 if not found
225 MRVIEWER_API int viewport_index( ViewportId viewport_id ) const;
226
227 // Get unique id of the vieport containing the mouse
228 // if mouse is out of any viewport returns index of last selected viewport
229 MRVIEWER_API ViewportId getHoveredViewportId() const;
230
231 // Same, but returns an invalid ID if no viewport is hovered, instead of returning the last selected viewport.
233
234 // Change selected_core_index to the viewport containing the mouse
235 MRVIEWER_API void select_hovered_viewport();
236
237 // Calls fitData for single/each viewport in viewer
238 // fill = 0.6 parameter means that scene will 0.6 of screen,
239 // snapView - to snap camera angle to closest canonical quaternion
240 MRVIEWER_API void fitDataViewport( MR::ViewportMask vpList = MR::ViewportMask::all(), float fill = 0.6f, bool snapView = true );
241
242 // Calls fitBox for single/each viewport in viewer
243 // fill = 0.6 parameter means that scene will 0.6 of screen,
244 // snapView - to snap camera angle to closest canonical quaternion
245 MRVIEWER_API void fitBoxViewport( const Box3f& box, MR::ViewportMask vpList = MR::ViewportMask::all(), float fill = 0.6f, bool snapView = true );
246
247 // Calls fitData and change FOV to match the screen size then
248 // params - params fit data
249 MRVIEWER_API void preciseFitDataViewport( MR::ViewportMask vpList = MR::ViewportMask::all() );
250 MRVIEWER_API void preciseFitDataViewport( MR::ViewportMask vpList, const FitDataParams& param );
251
252 MRVIEWER_API size_t getTotalFrames() const;
253 MRVIEWER_API size_t getSwappedFrames() const;
254 MRVIEWER_API size_t getFPS() const;
255 MRVIEWER_API double getPrevFrameDrawTimeMillisec() const;
256
257 // Returns memory amount used by shared GL memory buffer
258 MRVIEWER_API size_t getStaticGLBufferSize() const;
259
260 // if true only last frame of force redraw after events will be swapped, otherwise each will be swapped
261 bool swapOnLastPostEventsRedraw{ true };
262 // minimum auto increment force redraw frames after events
263 int forceRedrawMinimumIncrementAfterEvents{ 4 };
264
265 // Increment number of forced frames to redraw in event loop
266 // if `swapOnLastOnly` only last forced frame will be present on screen and all previous will not
267 MRVIEWER_API void incrementForceRedrawFrames( int i = 1, bool swapOnLastOnly = false );
268
269 // Forces redraw no later than the specified number of frames
270 MRVIEWER_API void forceSwapOnFrame( int i = 0 );
271
272 // Returns true if current frame will be shown on display
273 MRVIEWER_API bool isCurrentFrameSwapping() const;
274
275 // types of counted events
276 enum class EventType
277 {
278 MouseDown,
279 MouseUp,
280 MouseMove,
281 MouseScroll,
282 KeyDown,
283 KeyUp,
284 KeyRepeat,
285 CharPressed,
286 Count
287 };
288 // Returns number of events of given type
289 MRVIEWER_API size_t getEventsCount( EventType type )const;
290
291 // types of gl primitives counters
293 {
294 // arrays and elements are different gl calls
295 PointArraySize,
296 LineArraySize,
297 TriangleArraySize,
298 PointElementsNum,
299 LineElementsNum,
300 TriangleElementsNum,
301 Count
302 };
303 // Returns number of events of given type
304 MRVIEWER_API size_t getLastFrameGLPrimitivesCount( GLPrimitivesType type ) const;
305 // Increment number of gl primitives drawed in this frame
306 MRVIEWER_API void incrementThisFrameGLPrimitivesCount( GLPrimitivesType type, size_t num );
307
308
309 // Returns mask of present viewports
310 ViewportMask getPresentViewports() const { return presentViewportsMask_; }
311
312 // Restes frames counter and events counter
313 MRVIEWER_API void resetAllCounters();
314
320 MRVIEWER_API Image captureSceneScreenShot( const Vector2i& resolution = Vector2i(), bool transparentBg = false );
321
328 MRVIEWER_API void captureUIScreenShot( std::function<void( const Image& )> callback,
329 const Vector2i& pos = Vector2i(), const Vector2i& size = Vector2i() );
330
331 // Returns true if can enable alpha sort
332 MRVIEWER_API bool isAlphaSortAvailable() const;
333 // Tries to enable alpha sort,
334 // returns true if value was changed, return false otherwise
335 MRVIEWER_API bool enableAlphaSort( bool on );
336 // Returns true if alpha sort is enabled, false otherwise
337 bool isAlphaSortEnabled() const { return alphaSortEnabled_; }
338
339 // Returns number of passes performed by depth peeler
340 MRVIEWER_API int getDepthPeelNumPasses() const;
341 // Sets desired number of passes to depth peeler
342 MRVIEWER_API void setDepthPeelNumPasses( int numPasses );
343 // Returns true if depth peeling is enabled, false otherwise
344 MRVIEWER_API bool isDepthPeelingEnabled() const;
345
346 // Returns if scene texture is now bound
347 MRVIEWER_API bool isSceneTextureBound() const;
348 // Binds or unbinds scene texture (should be called only with valid window)
349 // note that it does not clear framebuffer
350 MRVIEWER_API void bindSceneTexture( bool bind );
351 // Returns true if 3d scene is rendering in scene texture instead of main framebuffer
352 MRVIEWER_API bool isSceneTextureEnabled() const;
353
354 // Returns actual msaa level of:
355 // scene texture if it is present, or main framebuffer
356 MRVIEWER_API int getMSAA() const;
357 // Requests changing MSAA level
358 // if scene texture is using, request should be executed in the beginig of next frame
359 // otherwise restart of the app is required to apply change to main framebuffer
360 MRVIEWER_API void requestChangeMSAA( int newMSAA );
361 // Returns MSAA level that have been requested (might be different from actual MSAA using, because of GPU limitations or need to restart app)
362 MRVIEWER_API int getRequestedMSAA() const;
363
364 // Sets manager of viewer settings which loads user personal settings on beginning of app
365 // and saves it in app's ending
366 MRVIEWER_API void setViewportSettingsManager( std::unique_ptr<IViewerSettingsManager> mng );
367 MRVIEWER_API const std::unique_ptr<IViewerSettingsManager>& getViewerSettingsManager() const { return settingsMng_; }
368
370 // Finds point in all spaces from screen space pixel point
371 MRVIEWER_API PointInAllSpaces getPixelPointInfo( const Vector3f& screenPoint ) const;
372 // Finds point under mouse in all spaces and under mouse viewport id
374
375 // Converts screen space coordinate to viewport space coordinate
376 // (0,0) if viewport does not exist
377 // screen space: X [0,framebufferSize.x], Y [0,framebufferSize.y] - (0,0) is upper left of window
378 // viewport space: X [0,viewport_width], Y [0,viewport_height] - (0,0) is upper left of viewport
379 // Z [0,1] - 0 is Dnear, 1 is Dfar
380 MRVIEWER_API Vector3f screenToViewport( const Vector3f& screenPoint, ViewportId id ) const;
381 // Converts viewport space coordinate to screen space coordinate
382 // (0,0) if viewport does not exist
383 // screen space: X [0,framebufferSize.x], Y [0,framebufferSize.y] - (0,0) is upper left of window
384 // viewport space: X [0,viewport_width], Y [0,viewport_height] - (0,0) is upper left of viewport
385 // Z [0,1] - 0 is Dnear, 1 is Dfar
386 MRVIEWER_API Vector3f viewportToScreen( const Vector3f& viewportPoint, ViewportId id ) const;
387
388 // Returns viewports satisfying given mask
389 MRVIEWER_API std::vector<std::reference_wrapper<Viewport>> getViewports( ViewportMask mask = ViewportMask::any() );
390
391 // Enables or disables global history (clears it on disable)
392 MRVIEWER_API void enableGlobalHistory( bool on );
393 // Return true if global history is enabled, false otherwise
394 bool isGlobalHistoryEnabled() const { return bool( globalHistoryStore_ ); };
395 // Appends history action to current stack position (clearing redo)
396 // if global history is disabled do nothing
397 MRVIEWER_API void appendHistoryAction( const std::shared_ptr<HistoryAction>& action );
398 // Applies undo if global history is enabled
399 // return true if undo was applied
400 MRVIEWER_API bool globalHistoryUndo();
401 // Applies redo if global history is enabled
402 // return true if redo was applied
403 MRVIEWER_API bool globalHistoryRedo();
404
407 MRVIEWER_API HistoryStore* getGlobalHistoryStore() const { return globalHistoryStore_.get(); }
408
409 // Return spacemouse handler
410 const std::shared_ptr<SpaceMouse::Handler>& getSpaceMouseHandler() const { return spaceMouseHandler_; }
411
412 // This method is called after successful scene saving to update scene root, window title and undo
413 MRVIEWER_API void onSceneSaved( const std::filesystem::path& savePath, bool storeInRecent = true );
414
415 // Get/Set menu plugin (which is separated from other plugins to be inited first before splash window starts)
416 MRVIEWER_API const std::shared_ptr<ImGuiMenu>& getMenuPlugin() const;
417 MRVIEWER_API void setMenuPlugin( std::shared_ptr<ImGuiMenu> menu );
418
419 // get menu plugin casted in RibbonMenu
420 MRVIEWER_API std::shared_ptr<RibbonMenu> getRibbonMenu() const;
421
422 // Get the menu plugin casted in given type
423 template <typename T>
424 std::shared_ptr<T> getMenuPluginAs() const { return std::dynamic_pointer_cast<T>( getMenuPlugin() ); }
425
426 // sets stop event loop flag (this flag is glfwShouldWindowClose equivalent)
427 MRVIEWER_API void stopEventLoop();
428 // get stop event loop flag (this flag is glfwShouldWindowClose equivalent)
429 bool getStopEventLoopFlag() const { return stopEventLoop_; }
430
431 // return true if window should close
432 // calls interrupt signal and if no slot interrupts return true, otherwise return false
434
435 // returns true if viewer has valid GL context
436 // note that sometimes it is not enough, for example to free GL memory in destructor,
437 // glInitialized_ can be already reset and it requires `loadGL()` check too
438 bool isGLInitialized() const { return glInitialized_; }
439
440 // update the title of the main window and, if any scene was opened, show its filename
441 MRVIEWER_API void makeTitleFromSceneRootPath();
442
443 // returns true if the system framebuffer is scaled (valid for macOS and Wayland)
444 bool hasScaledFramebuffer() const { return hasScaledFramebuffer_; }
445
446public:
448 // Member variables //
450 GLFWwindow* window;
451
452 // A function to reset setting to initial state
453 // Overrides should call previous function
454 std::function<void( Viewer* viewer )> resetSettingsFunction;
455
456 // Stores all the viewing options
457 std::vector<Viewport> viewport_list;
459
460 // List of registered plugins
461 std::vector<ViewerPlugin*> plugins;
462
463 float pixelRatio{ 1.0f };
465 Vector2i windowSavePos; // pos to save
466 Vector2i windowSaveSize; // size to save
468 bool windowMaximized{ false };
469
470 // if true - calls render without system events
471 bool isAnimating{ false };
472 // max fps if animating
473 int animationMaxFps{ 30 };
474 // this parameter can force up/down mouse scroll
475 // useful for WebAssembler version because it has too powerful scroll
476 float scrollForce{ }; // init in resetSettingsFunction()
477 // opengl-based pick window radius in pixels
478 uint16_t glPickRadius{ }; // init in resetSettingsFunction()
479 // Experimental/developer features enabled
480 bool experimentalFeatures{ };
481 // command arguments, each parsed arg should be erased from here not to affect other parsers
482 std::vector<std::string> commandArgs;
483
484 std::shared_ptr<ObjectMesh> basisAxes;
485 std::unique_ptr<CornerControllerObject> basisViewController;
486 std::unique_ptr<ViewportGlobalBasis> globalBasis;
487 std::shared_ptr<ObjectMesh> rotationSphere;
488 // Stores clipping plane mesh
489 std::shared_ptr<ObjectMesh> clippingPlaneObject;
490
491 // class that updates viewer title
492 std::shared_ptr<ViewerTitle> windowTitle;
493
495 ViewerSignals & signals() { return *signals_; }
496
499 MRVIEWER_API void emplaceEvent( std::string name, ViewerEventCallback cb, bool skipable = false );
500 // pop all events from the queue while they have this name
501 MRVIEWER_API void popEventByName( const std::string& name );
502
503 MRVIEWER_API void postEmptyEvent();
504
505 [[nodiscard]] const TouchpadController &touchpadController() const { return *touchpadController_; }
506 [[nodiscard]] TouchpadController& touchpadController() { return *touchpadController_; }
507
508 [[nodiscard]] const SpaceMouse::Controller &spaceMouseController() const { return *spaceMouseController_; }
509 [[nodiscard]] SpaceMouse::Controller& spaceMouseController() { return *spaceMouseController_; }
510
511 [[nodiscard]] const MouseController &mouseController() const { return *mouseController_; }
512 [[nodiscard]] MouseController &mouseController() { return *mouseController_; }
513
514 // Store of recently opened files
515 [[nodiscard]] const RecentFilesStore &recentFilesStore() const { return *recentFilesStore_; }
516 [[nodiscard]] RecentFilesStore &recentFilesStore() { return *recentFilesStore_; }
517
519 [[nodiscard]] bool getSortDroppedFiles() const { return sortDroppedFiles_; }
520
522 void setSortDroppedFiles( bool value ) { sortDroppedFiles_ = value; }
523
526 MRVIEWER_API void initSpaceMouseHandler( std::function<void(const std::string&)> deviceSignal = {} );
527
529 MRVIEWER_API void drawUiRenderObjects();
530
533 MRVIEWER_API bool isMultiViewportAvailable();
534private:
535 Viewer();
536 ~Viewer();
537
538 // Init window
539 int launchInit_( const LaunchParams& params );
540
541 // Called from launchInit_ after window creating to configure it properly
542 bool setupWindow_( const LaunchParams& params );
543
544 // Return true if OpenGL loaded successfully
545 bool checkOpenGL_(const LaunchParams& params );
546
547 // Init base objects
548 void init_();
549
550 // Init all plugins on start
551 void initPlugins_();
552
553 // Shut all plugins at the end
554 void shutdownPlugins_();
555
556#ifdef __EMSCRIPTEN__
557 void mainLoopFunc_();
558 static void emsMainInfiniteLoop();
559#endif
560 // returns true if was swapped
561 bool draw_( bool force );
562
563 // the minimum number of frames to be rendered even if the scene is unchanged
564 int forceRedrawFrames_{ 0 };
565 // Should be `<= forceRedrawFrames_`. The next N frames will not be shown on screen.
566 int forceRedrawFramesWithoutSwap_{ 0 };
567
568 std::unique_ptr<ViewerEventQueue> eventQueue_;
569
570 // special plugin for menu (initialized before splash window starts)
571 std::shared_ptr<ImGuiMenu> menuPlugin_;
572
573 std::unique_ptr<TouchpadController> touchpadController_;
574 std::unique_ptr<SpaceMouse::Controller> spaceMouseController_;
575 std::unique_ptr<TouchesController> touchesController_;
576 std::unique_ptr<MouseController> mouseController_;
577 std::unique_ptr<IDragDropHandler> dragDropAdvancedHandler_;
578
579 std::unique_ptr<RecentFilesStore> recentFilesStore_;
580 std::unique_ptr<FrameCounter> frameCounter_;
581
582 mutable struct EventsCounter
583 {
584 std::array<size_t, size_t( EventType::Count )> counter{};
585 void reset();
586 } eventsCounter_;
587
588 mutable struct GLPrimitivesCounter
589 {
590 std::array<size_t, size_t( GLPrimitivesType::Count )> counter{};
591 void reset();
592 } glPrimitivesCounter_;
593
594
595 // creates glfw window with gl version major.minor, false if failed;
596 bool tryCreateWindow_( bool fullscreen, int& width, int& height, const std::string& name, int major, int minor );
597
598 bool needRedraw_() const;
599 void resetRedraw_();
600
601 void initGlobalBasisAxesObject_();
602 void initBasisAxesObject_();
603 void initBasisViewControllerObject_();
604 void initClippingPlaneObject_();
605 void initRotationCenterObject_();
606
607 // recalculate pixel ratio
608 void updatePixelRatio_();
609
610 // return MSAA that is required for framebuffer
611 // sceneTextureOn - true means that app is using scene texture for rendering (false means that scene is rendered directly in main framebuffer)
612 // forSceneTexture - true request MSAA required for scene texture (calling with !sceneTextureOn is invalid), false - request MSAA for main framebuffer
613 int getRequiredMSAA_( bool sceneTextureOn, bool forSceneTexture ) const;
614
615 bool stopEventLoop_{ false };
616
617 bool isLaunched_{ false };
618 // this flag is needed to know if all viewer setup was already done, and we can call draw
619 bool focusRedrawReady_{ false };
620
621 std::unique_ptr<SceneTextureGL> sceneTexture_;
622 std::unique_ptr<AlphaSortGL> alphaSorter_;
623 std::unique_ptr<DepthPeelingGL> depthPeeler_;
624
625 bool alphaSortEnabled_{ false };
626
627 bool glInitialized_{ false };
628
629 bool isInDraw_{ false };
630 bool dirtyScene_{ false };
631
632 bool hasScaledFramebuffer_{ false };
633
634 bool sortDroppedFiles_{ true };
635
636 LaunchParams launchParams_;
637
638 ViewportId getFirstAvailableViewportId_() const;
639 ViewportMask presentViewportsMask_;
640
641 std::unique_ptr<IViewerSettingsManager> settingsMng_;
642
643 std::unique_ptr<HistoryStore> globalHistoryStore_;
644
645 std::shared_ptr<SpaceMouse::Handler> spaceMouseHandler_;
646
647 struct Connections;
648 std::unique_ptr<Connections> connections_;
649 std::unique_ptr<ViewerSignals> signals_;
650
651 friend MRVIEWER_API Viewer& getViewerInstance();
652};
653
654// starts default viewer with given params and setup
655MRVIEWER_API int launchDefaultViewer( const Viewer::LaunchParams& params, const ViewerSetup& setup );
656
657} // end namespace
angle
Definition MRObjectDimensionsEnum.h:13
Definition MRRenderGLHelpers.h:214
This class stores history stack for undo/redo.
Definition MRHistoryStore.h:16
Definition MRImage.h:16
Definition MRMouseController.h:21
Definition MRRecentFilesStore.h:17
Definition MRSceneRoot.h:11
Definition MRSpaceMouseController.h:11
Definition MRTouchpadController.h:32
Definition MRSetupViewer.h:13
Definition MRViewer.h:66
MRVIEWER_API bool isDepthPeelingEnabled() const
bool hasScaledFramebuffer() const
Definition MRViewer.h:444
MRVIEWER_API bool saveToFile(const std::filesystem::path &mesh_file_name)
MRVIEWER_API bool keyUp(int key, int modifier)
bool windowShouldClose()
MRVIEWER_API void fitDataViewport(MR::ViewportMask vpList=MR::ViewportMask::all(), float fill=0.6f, bool snapView=true)
MRVIEWER_API bool isAlphaSortAvailable() const
MRVIEWER_API ViewportId getHoveredViewportId() const
MRVIEWER_API void setViewportSettingsManager(std::unique_ptr< IViewerSettingsManager > mng)
MRVIEWER_API void forceSwapOnFrame(int i=0)
MRVIEWER_API bool keyRepeat(int key, int modifier)
MRVIEWER_API std::vector< std::reference_wrapper< Viewport > > getViewports(ViewportMask mask=ViewportMask::any())
const MouseController & mouseController() const
Definition MRViewer.h:511
MRVIEWER_API void appendHistoryAction(const std::shared_ptr< HistoryAction > &action)
std::shared_ptr< ViewerTitle > windowTitle
Definition MRViewer.h:492
MRVIEWER_API PointInAllSpaces getMousePointInfo() const
MRVIEWER_API const std::shared_ptr< ImGuiMenu > & getMenuPlugin() const
MRVIEWER_API HistoryStore * getGlobalHistoryStore() const
Definition MRViewer.h:407
MRVIEWER_API void resize(int w, int h)
MRVIEWER_API bool mouseClick(MouseButton button, int modifier)
std::vector< ViewerPlugin * > plugins
Definition MRViewer.h:461
static MRVIEWER_API void parseLaunchParams(LaunchParams &params)
MRVIEWER_API Box2f getViewportsBounds() const
Vector2i windowSaveSize
Definition MRViewer.h:466
EventType
Definition MRViewer.h:277
const SpaceMouse::Controller & spaceMouseController() const
Definition MRViewer.h:508
ViewportMask getPresentViewports() const
Definition MRViewer.h:310
MRVIEWER_API bool touchpadSwipeGestureEnd()
MRVIEWER_API bool erase_viewport(ViewportId viewport_id)
MRVIEWER_API void fitBoxViewport(const Box3f &box, MR::ViewportMask vpList=MR::ViewportMask::all(), float fill=0.6f, bool snapView=true)
const TouchpadController & touchpadController() const
Definition MRViewer.h:505
MRVIEWER_API size_t getEventsCount(EventType type) const
std::shared_ptr< T > getMenuPluginAs() const
Definition MRViewer.h:424
bool isGLInitialized() const
Definition MRViewer.h:438
MRVIEWER_API void onSceneSaved(const std::filesystem::path &savePath, bool storeInRecent=true)
MRVIEWER_API bool touchStart(int id, int x, int y)
MRVIEWER_API void drawUiRenderObjects()
draw 2d (UI) part of objects in scene
MRVIEWER_API bool mouseMove(int mouse_x, int mouse_y)
std::shared_ptr< ObjectMesh > rotationSphere
Definition MRViewer.h:487
MRVIEWER_API void clearFramebuffers()
Vector2i windowOldPos
Definition MRViewer.h:467
std::vector< std::string > commandArgs
Definition MRViewer.h:482
MRVIEWER_API Vector3f viewportToScreen(const Vector3f &viewportPoint, ViewportId id) const
MRVIEWER_API Image captureSceneScreenShot(const Vector2i &resolution=Vector2i(), bool transparentBg=false)
MRVIEWER_API bool isCurrentFrameSwapping() const
MRVIEWER_API double getPrevFrameDrawTimeMillisec() const
MRVIEWER_API void enableGlobalHistory(bool on)
MRVIEWER_API int launch(const LaunchParams &params)
MRVIEWER_API size_t getSwappedFrames() const
MRVIEWER_API void popEventByName(const std::string &name)
const LaunchParams & getLaunchParams() const
Definition MRViewer.h:86
std::function< void(Viewer *viewer)> resetSettingsFunction
Definition MRViewer.h:454
MRVIEWER_API bool touchMove(int id, int x, int y)
MRVIEWER_API bool erase_viewport(const size_t index)
MRVIEWER_API bool touchpadSwipeGestureBegin()
MRVIEWER_API bool dragDrop(const std::vector< std::filesystem::path > &paths)
MRVIEWER_API bool spaceMouseDown(int key)
MRVIEWER_API bool touchpadSwipeGestureUpdate(float dx, float dy, bool kinetic)
friend MRVIEWER_API Viewer & getViewerInstance()
returns global instance of Viewer class
MRVIEWER_API PointInAllSpaces getPixelPointInfo(const Vector3f &screenPoint) const
bool isAlphaSortEnabled() const
Definition MRViewer.h:337
MRVIEWER_API void emplaceEvent(std::string name, ViewerEventCallback cb, bool skipable=false)
MRVIEWER_API int getMSAA() const
MRVIEWER_API bool spaceMouseUp(int key)
MRVIEWER_API void incrementForceRedrawFrames(int i=1, bool swapOnLastOnly=false)
size_t selected_viewport_index
Definition MRViewer.h:458
MRVIEWER_API void postSetMaximized(bool maximized)
MRVIEWER_API int getRequestedMSAA() const
static Viewer * instance()
Definition MRViewer.h:89
MRVIEWER_API bool mouseUp(MouseButton button, int modifier)
MRVIEWER_API bool enableAlphaSort(bool on)
PluginType * getPluginInstance()
Definition MRViewer.h:96
MRVIEWER_API bool mouseScroll(float delta_y)
bool isLaunched() const
Definition MRViewer.h:83
MRVIEWER_API void postEmptyEvent()
static const Viewer * constInstance()
Definition MRViewer.h:92
std::unique_ptr< ViewportGlobalBasis > globalBasis
Definition MRViewer.h:486
MR::MouseMode MouseMode
Definition MRViewer.h:69
MRVIEWER_API size_t getFPS() const
MRVIEWER_API Vector3f screenToViewport(const Vector3f &screenPoint, ViewportId id) const
MRVIEWER_API bool dragEnd(MouseButton button, int modifier)
std::shared_ptr< ObjectMesh > clippingPlaneObject
Definition MRViewer.h:489
MRVIEWER_API const Viewport & viewport(ViewportId viewportId={}) const
static const Viewer & constInstanceRef()
Definition MRViewer.h:93
MRVIEWER_API bool touchpadZoomGestureEnd()
void setSortDroppedFiles(bool value)
sets whether to sort the filenames received from Drag&Drop in lexicographical order before adding the...
Definition MRViewer.h:522
MRVIEWER_API bool touchpadRotateGestureBegin()
SpaceMouse::Controller & spaceMouseController()
Definition MRViewer.h:509
MRVIEWER_API bool mouseDown(MouseButton button, int modifier)
RecentFilesStore & recentFilesStore()
Definition MRViewer.h:516
MRVIEWER_API void select_hovered_viewport()
MRVIEWER_API ViewportId append_viewport(const ViewportRectangle &viewportRect, bool append_empty=false)
MRVIEWER_API bool touchpadRotateGestureUpdate(float angle)
MRVIEWER_API bool spaceMouseRepeat(int key)
MRVIEWER_API void incrementThisFrameGLPrimitivesCount(GLPrimitivesType type, size_t num)
MRVIEWER_API void postRescale(float x, float y)
static Viewer & instanceRef()
Definition MRViewer.h:90
std::shared_ptr< ObjectMesh > basisAxes
Definition MRViewer.h:484
MRVIEWER_API bool touchpadZoomGestureBegin()
MRVIEWER_API void bindSceneTexture(bool bind)
MRVIEWER_API bool touchpadZoomGestureUpdate(float scale, bool kinetic)
MRVIEWER_API void set_root(SceneRootObject &newRoot)
MRVIEWER_API void stopEventLoop()
MRVIEWER_API void launchEventLoop()
MRVIEWER_API void drawScene(FramebufferData *framebuffer)
MRVIEWER_API bool isMultiViewportAvailable()
MRVIEWER_API void captureUIScreenShot(std::function< void(const Image &)> callback, const Vector2i &pos=Vector2i(), const Vector2i &size=Vector2i())
MRVIEWER_API void launchShut()
MRVIEWER_API bool globalHistoryUndo()
MRVIEWER_API std::shared_ptr< RibbonMenu > getRibbonMenu() const
MRVIEWER_API const std::unique_ptr< IViewerSettingsManager > & getViewerSettingsManager() const
Definition MRViewer.h:367
MRVIEWER_API void preciseFitDataViewport(MR::ViewportMask vpList=MR::ViewportMask::all())
Vector2i framebufferSize
Definition MRViewer.h:464
ViewerSignals & signals()
return the structure with all viewer's signals
Definition MRViewer.h:495
MRVIEWER_API bool keyDown(int key, int modifier)
bool getSortDroppedFiles() const
returns whether to sort the filenames received from Drag&Drop in lexicographical order before adding ...
Definition MRViewer.h:519
MRVIEWER_API void setupScene()
MRVIEWER_API void setMenuPlugin(std::shared_ptr< ImGuiMenu > menu)
MRVIEWER_API Viewport & viewport(ViewportId viewportId={})
MRVIEWER_API size_t getStaticGLBufferSize() const
MR::MouseButton MouseButton
Definition MRViewer.h:68
GLPrimitivesType
Definition MRViewer.h:293
Vector2i windowSavePos
Definition MRViewer.h:465
MRVIEWER_API size_t getLastFrameGLPrimitivesCount(GLPrimitivesType type) const
MRVIEWER_API bool dragStart(MouseButton button, int modifier)
MRVIEWER_API ViewportId getHoveredViewportIdOrInvalid() const
MRVIEWER_API void drawFull(bool dirtyScene)
std::unique_ptr< CornerControllerObject > basisViewController
Definition MRViewer.h:485
MRVIEWER_API bool loadFiles(const std::vector< std::filesystem::path > &filesList)
MRVIEWER_API bool loadFiles(const std::vector< std::filesystem::path > &filesList, const FileLoadOptions &options)
MRVIEWER_API void requestChangeMSAA(int newMSAA)
GLFWwindow * window
Definition MRViewer.h:450
MRVIEWER_API bool drag(int mouse_x, int mouse_y)
MRVIEWER_API void postSetIconified(bool iconified)
MRVIEWER_API bool globalHistoryRedo()
MRVIEWER_API void makeTitleFromSceneRootPath()
MRVIEWER_API size_t getTotalFrames() const
MRVIEWER_API void postFocus(bool focused)
void setSceneDirty()
Definition MRViewer.h:164
MRVIEWER_API void resetAllCounters()
MRVIEWER_API bool isSupportedFormat(const std::filesystem::path &file_name)
MRVIEWER_API void draw(bool force=false)
MRVIEWER_API int getDepthPeelNumPasses() const
MRVIEWER_API int viewport_index(ViewportId viewport_id) const
MRVIEWER_API bool touchpadRotateGestureEnd()
MouseController & mouseController()
Definition MRViewer.h:512
MRVIEWER_API bool isSceneTextureBound() const
MRVIEWER_API void initSpaceMouseHandler(std::function< void(const std::string &)> deviceSignal={})
MRVIEWER_API bool touchEnd(int id, int x, int y)
const RecentFilesStore & recentFilesStore() const
Definition MRViewer.h:515
TouchpadController & touchpadController()
Definition MRViewer.h:506
MRVIEWER_API bool interruptWindowClose()
MRVIEWER_API bool spaceMouseMove(const Vector3f &translate, const Vector3f &rotate)
MRVIEWER_API void preciseFitDataViewport(MR::ViewportMask vpList, const FitDataParams &param)
MRVIEWER_API void setDepthPeelNumPasses(int numPasses)
bool isGlobalHistoryEnabled() const
Definition MRViewer.h:394
MRVIEWER_API void postResize(int w, int h)
MRVIEWER_API bool keyPressed(unsigned int unicode_key, int modifier)
MRVIEWER_API bool isSceneTextureEnabled() const
MRVIEWER_API void postSetPosition(int xPos, int yPos)
MRVIEWER_API void clearScene()
std::vector< Viewport > viewport_list
Definition MRViewer.h:457
const std::shared_ptr< SpaceMouse::Handler > & getSpaceMouseHandler() const
Definition MRViewer.h:410
MRVIEWER_API void postClose()
bool getStopEventLoopFlag() const
Definition MRViewer.h:429
Definition MRViewportId.h:42
Definition MRViewport.h:46
Definition MRCameraOrientationPlugin.h:8
MRVIEWER_API Viewer & getViewerInstance()
returns global instance of Viewer class
MRVIEWER_API int launchDefaultViewer(const Viewer::LaunchParams &params, const ViewerSetup &setup)
Definition MRFileLoadOptions.h:11
Definition MRFitData.h:29
Definition MRViewer.h:31
WindowMode
Definition MRViewer.h:36
@ HideInit
Definition MRViewer.h:38
@ NoWindow
Definition MRViewer.h:41
@ TryHidden
Definition MRViewer.h:40
@ Hide
Definition MRViewer.h:39
@ Show
Definition MRViewer.h:37
bool render3dSceneInTexture
Definition MRViewer.h:45
bool console
Definition MRViewer.h:52
bool developerFeatures
Definition MRViewer.h:46
char ** argv
Definition MRViewer.h:54
bool startEventLoop
Definition MRViewer.h:50
bool fullscreen
Definition MRViewer.h:32
int animationMaxFps
Definition MRViewer.h:58
int argc
Definition MRViewer.h:53
std::shared_ptr< SplashWindow > splashWindow
Definition MRViewer.h:61
bool unloadPluginsAtEnd
Definition MRViewer.h:59
bool close
Definition MRViewer.h:51
bool multiViewport
Definition MRViewer.h:47
bool showMRVersionInTitle
Definition MRViewer.h:56
bool enableTransparentBackground
Definition MRViewer.h:43
int height
Definition MRViewer.h:34
bool resetConfig
Definition MRViewer.h:49
bool preferOpenGL3
Definition MRViewer.h:44
int width
Definition MRViewer.h:33
enum MR::LaunchParams::WindowMode HideInit
std::string name
Definition MRViewer.h:48
bool isAnimating
Definition MRViewer.h:57
Definition MRPointInAllSpaces.h:13
Definition MRViewerSignals.h:10
Definition MRViewportId.h:16