Easy3D 2.6.1
Loading...
Searching...
No Matches
viewer.h
1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#ifndef EASY3D_VIEWER_VIEWER_H
28#define EASY3D_VIEWER_VIEWER_H
29
30
31#include <string>
32#include <vector>
33#include <memory>
34
35#include <easy3d/core/types.h>
36
37
38struct GLFWwindow;
39
40namespace easy3d {
41
42 class Camera;
43 class Model;
44 class Drawable;
45 class LinesDrawable2D;
47 class TextRenderer;
49
62 class Viewer
63 {
64 public:
80 explicit Viewer(
81 const std::string& title = "Easy3D Viewer",
82 int samples = 4,
83 int gl_major = 3,
84 int gl_minor = 2,
85 bool full_screen = false,
86 bool resizable = true,
87 int depth_bits = 24,
88 int stencil_bits = 8,
89 int width = 800,
90 int height = 600
91 );
92
96 virtual ~Viewer();
97
103 int run(bool see_all = true);
104
108 virtual void exit();
109
111
112
117 void set_title(const std::string &title);
118
123 const std::string& title() const { return title_; }
124
129 void resize(int w, int h);
130
132 int width() const { return width_; }
134 int height() const { return height_; }
136 void viewer_size(int& w, int& h) const { w = width_; h = height_; }
137
139 int framebuffer_width() const;
141 int framebuffer_height() const;
143 void framebuffer_size(int& w, int& h) const;
144
153 void set_position(int xpos, int ypos);
154
160 int samples() const { return samples_; }
161
166 float dpi_scaling() const { return dpi_scaling_; }
167
172 void set_background_color(const vec4& color) { background_color_ = color; }
173
178 const vec4& background_color() const { return background_color_; }
179
181 Camera* camera() { return camera_.get(); }
183 const Camera* camera() const { return camera_.get(); }
185
187
188
198 virtual bool open();
199
206 virtual bool save() const;
208
210
211
232 virtual Model* add_model(const std::string& file_name, bool create_default_drawables = true);
233
253 Model* add_model(std::shared_ptr<Model> model, bool create_default_drawables = true);
273 Model* add_model(Model* model, bool create_default_drawables = true);
274
281 virtual bool delete_model(Model* model);
282
287 const std::vector< std::shared_ptr<Model> >& models() const { return models_; }
288
297 Model* current_model() const;
299
301
302
313 Drawable* add_drawable(std::shared_ptr<Drawable> drawable);
324 Drawable* add_drawable(Drawable* drawable);
325
331 bool delete_drawable(Drawable* drawable);
332
337 const std::vector< std::shared_ptr<Drawable> >& drawables() const { return drawables_; }
338
342 void clear_scene();
344
346
347
353 void update() const;
354
361 void fit_screen(const Model* model = nullptr);
362
369 virtual bool snapshot() const;
370
386 bool snapshot(const std::string& file_name, float scaling = 1.0f, int samples = 4, int back_ground = 1, bool expand = true) const;
387
407 virtual vec3 point_under_pixel(int x, int y, bool &found) const;
408
416 void set_usage(const std::string& manual = "default", const std::string& screen_hint = "");
418
420
421
425 enum Key { // Do NOT modify the values!!!
426 // the unknown key
427 KEY_UNKNOWN = -1,
428 // the number keys
429 KEY_0 = 48, KEY_1 = 49, KEY_2 = 50, KEY_3 = 51, KEY_4 = 52, KEY_5 = 53, KEY_6 = 54, KEY_7 = 55,
430 KEY_8 = 56, KEY_9 = 57,
431 // the character keys
432 KEY_A = 65, KEY_B = 66, KEY_C = 67, KEY_D = 68, KEY_E = 69, KEY_F = 70, KEY_G = 71, KEY_H = 72,
433 KEY_I = 73, KEY_J = 74, KEY_K = 75, KEY_L = 76, KEY_M = 77, KEY_N = 78, KEY_O = 79, KEY_P = 80,
434 KEY_Q = 81, KEY_R = 82, KEY_S = 83, KEY_T = 84, KEY_U = 85, KEY_V = 86, KEY_W = 87, KEY_X = 88,
435 KEY_Y = 89, KEY_Z = 90,
436 // the functional keys
437 KEY_RIGHT = 262, KEY_LEFT = 263, KEY_DOWN = 264, KEY_UP = 265,
438 KEY_F1 = 290, KEY_F2 = 291, KEY_F3 = 292, KEY_F4 = 293, KEY_F5 = 294,
439 KEY_F6 = 295, KEY_F7 = 296, KEY_F8 = 297, KEY_F9 = 298,
440 // some printable keys
441 KEY_SPACE = 32,
442 KEY_COMMA = 44/* , */, KEY_MINUS = 45/* - */, KEY_PERIOD = 46/* . */,
443 KEY_SLASH = 47/* / */, KEY_SEMICOLON = 59/* ; */, KEY_EQUAL = 61/* = */,
444 KEY_LEFT_BRACKET = 91/* [ */, KEY_BACKSLASH = 92/* \ */, KEY_RIGHT_BRACKET = 93/* ] */
445 };
446
450 enum Modifier { /* Do NOT modify the values!!! */
451 MODIF_NONE = 0x0000,
452 MODIF_SHIFT = 0x0001,
453#ifdef __APPLE__ // To have the same shortcut behavior on macOS and other platforms (i.e., Windows and Linux)
454 MODIF_CTRL = 0x0008,
455#else
456 MODIF_CTRL = 0x0002,
457#endif
458 MODIF_ALT = 0x0004
459 };
460
464 enum Button { // Do NOT modify the values!!!
465 BUTTON_LEFT = 0,
466 BUTTON_RIGHT = 1,
467 BUTTON_MIDDLE = 2
468 };
469
487 using Function = std::function<bool(Viewer* viewer, Model* model)>;
488
498 void bind(const Function& func, Model* model, Key key, Modifier modifier = MODIF_NONE) {
499 commands_[key][modifier] = std::make_pair(func, model);
500 }
501
502
504
505
506 std::function<bool(Viewer* viewer)> animation_func_;
507
510 void set_animation(bool b);
511
513 bool is_animating() const;
515
516 protected:
517
518 // rendering. Users can put their additional rendering function here by reimplementing it.
519 virtual void draw() const;
520
521 // OpenGL resources (e.g., shaders, textures, VAOs) must be created when
522 // there exists a valid rendering context. It is (usually) a bad idea to do
523 // this in a constructor because the OpenGL context may not have been
524 // created yet or the visible one is not *current*. This init() function is
525 // to ensure you have a valid rendering context. It will be called before
526 // the draw procedure. See also cleanup().
527 // NOTE: Don't forget to call Viewer::init() at the beginning of your
528 // inherited function.
529 virtual void init();
530
531 // To destroy OpenGL resources (e.g., shaders, textures, VAOs), there must exist a valid rendering context.
532 // It is (usually) a bad idea to clean up OpenGL in a destructor because the OpenGL context may not exist
533 // (e.g., destroyed already) or the visible one is not *current*. This cleanup() function is to ensure you
534 // have a valid rendering context. See also init().
535 // NOTE: Don't forget to call Viewer::cleanup() at the end of your inherited function.
536 void cleanup();
537
538 // This function will be called before the main draw procedure.
539 virtual void pre_draw();
540
541 // This function draws axes of the coordinate system, Easy3D logo, frame rate, etc. overlaid on the scene.
542 // It will be called after the main draw procedure.
543 virtual void post_draw();
544
545 // External resize due to user interaction.
546 // This function will be called after the window size being changed.
547 // w/h: the new width and height of the viewer (not framebuffer, not viewport)
548 virtual void post_resize(int w, int h) { (void)w, (void)h; }
549
550 // Mouse button press event handler
551 virtual bool mouse_press_event(int x, int y, int button, int modifiers);
552 // Mouse button release event handler
553 virtual bool mouse_release_event(int x, int y, int button, int modifiers);
554 // Mouse drag (i.e., a mouse button was pressed) event handler
555 virtual bool mouse_drag_event(int x, int y, int dx, int dy, int button, int modifiers);
556 // Mouse free move (i.e., no mouse button was pressed) event handler
557 virtual bool mouse_free_move_event(int x, int y, int dx, int dy, int modifiers);
558 // Mouse scroll event handler
559 virtual bool mouse_scroll_event(int x, int y, int dx, int dy);
560
561 // Text input event handler: codepoint is native endian UTF-32 format
562 // NOTE: This one reveals the actual character being sent (not just the physical key)
563 virtual bool char_input_event(unsigned int codepoint);
564 // Keyboard event handler.
565 // NOTE: This function does not reveal the actual character.
566 virtual bool key_press_event(int key, int modifiers);
567 virtual bool key_release_event(int key, int modifiers);
568
569 // Handle a file drop event
570 virtual bool drop_event(const std::vector<std::string> & filenames);
571
572 // Handle a focus change event
573 virtual bool focus_event(bool focused);
574
575 protected:
576 GLFWwindow *create_window(const std::string &title,
577 int samples,
578 int gl_major, // must >= 3
579 int gl_minor, // must >= 2
580 bool full_screen,
581 bool resizable,
582 int depth_bits,
583 int stencil_bits,
584 int width,
585 int height);
586
587 void setup_callbacks(GLFWwindow*);
588
589 /* Event handlers. Client code should not touch these */
590 virtual bool callback_event_cursor_pos(double x, double y);
591 virtual bool callback_event_mouse_button(int button, int action, int modifiers);
592 virtual bool callback_event_keyboard(int key, int action, int mods);
593 virtual bool callback_event_character(unsigned int codepoint);
594 virtual bool callback_event_drop(int count, const char **filenames);
595 virtual bool callback_event_scroll(double dx, double dy);
596 virtual void callback_event_resize(int w, int h);
597
598 void draw_corner_axes() const;
599
600 void draw_face_labels(Model* model, TextRenderer* texter, int font_id, const vec3& color) const;
601 void draw_vertex_labels(Model* model, TextRenderer* texter, int font_id, const vec3& color) const;
602
603 void copy_view();
604 void paste_view();
605
606 protected:
607 GLFWwindow* window_;
608 bool should_exit_;
609 float dpi_scaling_;
610 int width_;
611 int height_;
612
613 std::string title_;
614 std::unique_ptr<Camera> camera_;
615
616 std::unique_ptr<KeyFrameInterpolator> kfi_;
617 bool is_animating_;
618
619 std::string manual_;
620 std::string hint_; // shown in the top-left corner of the screen
621
622 int samples_; // the actual samples
623
624 bool full_screen_;
625 vec4 background_color_;
626
627 // enable/disable event processing
628 bool process_events_;
629 char framerate_[48]; // show the frame rate
630
631 std::unique_ptr<TextRenderer> texter_;
632 bool show_easy3d_logo_;
633
634 // mouse
635 int pressed_button_; // for mouse drag
636 int modifiers_; // for mouse drag
637 bool drag_active_; // for mouse drag
638 int mouse_current_x_; // for mouse button and scroll events
639 int mouse_current_y_;
640 int mouse_pressed_x_; // last pressed position
641 int mouse_pressed_y_;
642 int pressed_key_;
643
644 bool show_pivot_point_;
645 bool show_frame_rate_;
646
647 //----------------- viewer data -------------------
648
649 // corner axes
650 std::unique_ptr<TrianglesDrawable> drawable_axes_;
651
652 // pivot cross
653 std::unique_ptr<LinesDrawable2D> drawable_pivot_;
654
655 // camera path
656 bool show_camera_path_;
657
658 std::vector< std::shared_ptr<Model> > models_;
659 int model_idx_;
660
661 // drawables independent of any model
662 std::vector< std::shared_ptr<Drawable> > drawables_;
663
664 typedef std::pair<Function, Model*> FunctionModel;
665 std::map<Key, std::map<Modifier, FunctionModel> > commands_;
666 };
667
668}
669
670
671#endif // EASY3D_VIEWER_VIEWER_H
A perspective or orthographic camera.
Definition camera.h:113
The base class for drawable objects. A drawable represent a set of points, line segments,...
Definition drawable.h:58
A keyframe interpolator for animation generation.
Definition key_frame_interpolator.h:104
The drawable for rendering a set of line segments in the screen space.
Definition drawable_lines_2D.h:43
The base class of renderable 3D models.
Definition model.h:50
TextRenderer enables quick and easy string rendering in OpenGL applications.
Definition text_renderer.h:52
The drawable for rendering a set of triangles, e.g., the surface of a triangular mesh.
Definition drawable_triangles.h:46
void bind(const Function &func, Model *model, Key key, Modifier modifier=MODIF_NONE)
Bind a function that will be triggered by the shortcut 'modifier + key'.
Definition viewer.h:498
Button
Mouse buttons.
Definition viewer.h:464
virtual ~Viewer()
The destructor.
Definition viewer.cpp:553
bool is_animating() const
Is animation currently being performed.
Definition viewer.cpp:1237
void framebuffer_size(int &w, int &h) const
Queries the size of the framebuffer.
Definition viewer.cpp:625
void clear_scene()
Delete all visual contents of the viewer (all models and drawables).
Definition viewer.cpp:562
virtual void exit()
Terminate the viewer.
Definition viewer.cpp:1196
const std::string & title() const
Query the window title of the viewer.
Definition viewer.h:123
void set_title(const std::string &title)
Set the window title of the viewer.
Definition viewer.cpp:589
void viewer_size(int &w, int &h) const
Queries the size of the viewer/window.
Definition viewer.h:136
const Camera * camera() const
Returns the camera used by the viewer. See Camera.
Definition viewer.h:183
bool delete_drawable(Drawable *drawable)
Definition viewer.cpp:1368
Model * current_model() const
Query the active model.
Definition viewer.cpp:767
const vec4 & background_color() const
Query the background color of the viewer.
Definition viewer.h:178
virtual bool save() const
Save the active model (if exists) to a file.
Definition viewer.cpp:1446
const std::vector< std::shared_ptr< Drawable > > & drawables() const
Query the drawables managed by this viewer.
Definition viewer.h:337
const std::vector< std::shared_ptr< Model > > & models() const
Query the models managed by this viewer.
Definition viewer.h:287
void resize(int w, int h)
Set/Change the viewer size.
Definition viewer.cpp:602
Modifier
The key modifiers. Currently only Shift, Ctrl, and Alt are supported.
Definition viewer.h:450
virtual vec3 point_under_pixel(int x, int y, bool &found) const
Query the XYZ coordinates of the surface point under the cursor.
Definition viewer.cpp:1093
void set_background_color(const vec4 &color)
Set the background color of the viewer.
Definition viewer.h:172
int samples() const
Query the actual samples of the viewer.
Definition viewer.h:160
Viewer(const std::string &title="Easy3D Viewer", int samples=4, int gl_major=3, int gl_minor=2, bool full_screen=false, bool resizable=true, int depth_bits=24, int stencil_bits=8, int width=800, int height=600)
Constructor.
Definition viewer.cpp:75
virtual bool delete_model(Model *model)
Delete a model. The memory of the model will be released and its existing drawables also be deleted.
Definition viewer.cpp:1318
void fit_screen(const Model *model=nullptr)
Moves the camera so that the entire scene or the active model is centered on the screen at a proper s...
Definition viewer.cpp:1387
Key
The keys. Currently only a limited number of commonly used keys are supported.
Definition viewer.h:425
void set_usage(const std::string &manual="default", const std::string &screen_hint="")
The usage information of the viewer.
Definition viewer.cpp:1119
virtual Model * add_model(const std::string &file_name, bool create_default_drawables=true)
Add a model from a file to the viewer to be visualized. On success, the viewer will be in charge of t...
Definition viewer.cpp:1242
int framebuffer_width() const
Returns the width of the framebuffer, which is identical to: width() * dpi_scaling().
Definition viewer.cpp:611
Camera * camera()
Returns the camera used by the viewer. See Camera.
Definition viewer.h:181
int height() const
Returns the height of the viewer/window.
Definition viewer.h:134
int width() const
Returns the width of the viewer/window.
Definition viewer.h:132
std::function< bool(Viewer *viewer)> animation_func_
Function called at an equal interval for animation.
Definition viewer.h:506
void set_animation(bool b)
Enable/Disable animation.
Definition viewer.cpp:1231
virtual bool open()
Open a model (PointCloud/SurfaceMesh/Graph) from a file into the viewer. On success,...
Definition viewer.cpp:1420
int run(bool see_all=true)
Run the viewer.
Definition viewer.cpp:1127
void update() const
Update the display (i.e., repaint).
Definition viewer.cpp:630
Drawable * add_drawable(std::shared_ptr< Drawable > drawable)
Add a drawable to the viewer to be visualized. On success, the viewer will be in charge of the memory...
Definition viewer.cpp:1346
virtual bool snapshot() const
Take a snapshot of the screen and save it to a file.
Definition viewer.cpp:1487
float dpi_scaling() const
Query the scaling factor for high DPI devices (e.g., MackBook pro).
Definition viewer.h:166
void set_position(int xpos, int ypos)
Sets the position of the content area of the viewer.
Definition viewer.cpp:597
std::function< bool(Viewer *viewer, Model *model)> Function
Definition viewer.h:487
int framebuffer_height() const
Returns the height of the framebuffer, which is identical to: height() * dpi_scaling().
Definition viewer.cpp:618
Definition collider.cpp:182
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition types.h:44
Vec< 4, float > vec4
A 4D point/vector of float type.
Definition types.h:46