Easy3D 2.5.3
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
34#include <easy3d/core/types.h>
35
36
37struct GLFWwindow;
38
39namespace easy3d {
40
41 class Camera;
42 class Model;
43 class Drawable;
44 class TrianglesDrawable;
45 class TextRenderer;
46 class KeyFrameInterpolator;
47
60 class Viewer
61 {
62 public:
76 explicit Viewer(
77 const std::string& title = "Easy3D Viewer",
78 int samples = 4,
79 int gl_major = 3, // must >= 3
80 int gl_minor = 2, // must >= 2
81 bool full_screen = false,
82 bool resizable = true,
83 int depth_bits = 24,
84 int stencil_bits = 8,
85 int width = 800,
86 int height = 600
87 );
88
92 virtual ~Viewer();
93
99 int run(bool see_all = true);
100
104 virtual void exit();
105
107
108
113 void set_title(const std::string &title);
114
119 const std::string& title() const { return title_; }
120
125 void resize(int w, int h);
126
128 int width() const { return width_; }
130 int height() const { return height_; }
132 void viewer_size(int& w, int& h) const { w = width_; h = height_; }
133
135 int framebuffer_width() const;
137 int framebuffer_height() const;
139 void framebuffer_size(int& w, int& h) const;
140
149 void set_position(int xpos, int ypos);
150
156 int samples() const { return samples_; }
157
162 float dpi_scaling() const { return dpi_scaling_; }
163
168 void set_background_color(const easy3d::vec4& c) { background_color_ = c; }
169
174 const easy3d::vec4& background_color() const { return background_color_; }
175
177 Camera* camera() { return camera_; }
179 const Camera* camera() const { return camera_; }
181
183
184
194 virtual bool open();
195
202 virtual bool save() const;
204
206
207
228 virtual Model* add_model(const std::string& file_name, bool create_default_drawables = true);
229
249 Model* add_model(Model* model, bool create_default_drawables = true);
250
257 bool delete_model(Model* model);
258
263 const std::vector<Model*>& models() const { return models_; }
264
273 Model* current_model() const;
275
277
278
289 bool add_drawable(Drawable* drawable);
290
296 bool delete_drawable(Drawable* drawable);
297
302 const std::vector<Drawable*>& drawables() const { return drawables_; }
303
307 void clear_scene();
309
311
312
318 void update() const;
319
326 void fit_screen(const easy3d::Model* model = nullptr);
327
334 bool snapshot() const;
335
351 bool snapshot(const std::string& file_name, float scaling = 1.0f, int samples = 0, int back_ground = 1, bool expand = true) const;
352
372 virtual vec3 point_under_pixel(int x, int y, bool &found) const;
373
377 const std::string& usage() const { return usage_string_; }
378 void set_usage(const std::string& usg) { usage_string_ = usg; }
380
382
383
387 enum Key { // Do NOT modify the values!!!
388 // the unknown key
389 KEY_UNKNOWN = -1,
390 // the number keys
391 KEY_0 = 48, KEY_1 = 49, KEY_2 = 50, KEY_3 = 51, KEY_4 = 52, KEY_5 = 53, KEY_6 = 54, KEY_7 = 55,
392 KEY_8 = 56, KEY_9 = 57,
393 // the character keys
394 KEY_A = 65, KEY_B = 66, KEY_C = 67, KEY_D = 68, KEY_E = 69, KEY_F = 70, KEY_G = 71, KEY_H = 72,
395 KEY_I = 73, KEY_J = 74, KEY_K = 75, KEY_L = 76, KEY_M = 77, KEY_N = 78, KEY_O = 79, KEY_P = 80,
396 KEY_Q = 81, KEY_R = 82, KEY_S = 83, KEY_T = 84, KEY_U = 85, KEY_V = 86, KEY_W = 87, KEY_X = 88,
397 KEY_Y = 89, KEY_Z = 90,
398 // the functional keys
399 KEY_RIGHT = 262, KEY_LEFT = 263, KEY_DOWN = 264, KEY_UP = 265,
400 KEY_F1 = 290, KEY_F2 = 291, KEY_F3 = 292, KEY_F4 = 293, KEY_F5 = 294,
401 KEY_F6 = 295, KEY_F7 = 296, KEY_F8 = 297, KEY_F9 = 298,
402 // some printable keys
403 KEY_SPACE = 32,
404 KEY_COMMA = 44/* , */, KEY_MINUS = 45/* - */, KEY_PERIOD = 46/* . */,
405 KEY_SLASH = 47/* / */, KEY_SEMICOLON = 59/* ; */, KEY_EQUAL = 61/* = */,
406 KEY_LEFT_BRACKET = 91/* [ */, KEY_BACKSLASH = 92/* \ */, KEY_RIGHT_BRACKET = 93/* ] */
407 };
408
412 enum Modifier { /* Do NOT modify the values!!! */
413 MODIF_NONE = 0x0000,
414 MODIF_SHIFT = 0x0001,
415#ifdef __APPLE__ // To have the same shortcut behavior on macOS and other platforms (i.e., Windows and Linux)
416 MODIF_CTRL = 0x0008,
417#else
418 MODIF_CTRL = 0x0002,
419#endif
420 MODIF_ALT = 0x0004
421 };
422
426 enum Button { // Do NOT modify the values!!!
427 BUTTON_LEFT = 0,
428 BUTTON_RIGHT = 1,
429 BUTTON_MIDDLE = 2
430 };
431
449 using Function = std::function<bool(Viewer* viewer, Model* model)>;
450
460 void bind(const Function& func, Model* model, Key key, Modifier modifier = MODIF_NONE) {
461 commands_[key][modifier] = std::make_pair(func, model);
462 }
464
466
467
468 std::function<bool(Viewer* viewer)> animation_func_;
469
472 void set_animation(bool b);
473
475 bool is_animating() const;
477
478 protected:
479
480 // rendering. Users can put their additional rendering function here by reimplementing it.
481 virtual void draw() const;
482
483 // OpenGL resources (e.g., shaders, textures, VAOs) must be created when
484 // there exists a valid rendering context. It is (usually) a bad idea to do
485 // this in a constructor because the OpenGL context may not have been
486 // created yet or the visible one is not *current*. This init() function is
487 // to ensure you have a valid rendering context. It will be called before
488 // the draw procedure. See also cleanup().
489 // NOTE: Don't forget to call Viewer::init() at the beginning of your
490 // inherited function.
491 virtual void init();
492
493 // To destroy OpenGL resources (e.g., shaders, textures, VAOs), there must exist a valid rendering context.
494 // It is (usually) a bad idea to clean up OpenGL in a destructor because the OpenGL context may not exist
495 // (e.g., destroyed already) or the visible one is not *current*. This cleanup() function is to ensure you
496 // have a valid rendering context. See also init().
497 // NOTE: Don't forget to call Viewer::cleanup() at the end of your inherited function.
498 void cleanup();
499
500 // This function will be called before the main draw procedure.
501 virtual void pre_draw();
502
503 // This function draws axes of the coordinate system, Easy3D logo, frame rate, etc. overlaid on the scene.
504 // It will be called after the main draw procedure.
505 virtual void post_draw();
506
507 // External resize due to user interaction.
508 // This function will be called after the window size being changed.
509 // w/h: the new width and height of the viewer (not framebuffer, not viewport)
510 virtual void post_resize(int w, int h) { (void)w, (void)h; }
511
512 // Mouse button press event handler
513 virtual bool mouse_press_event(int x, int y, int button, int modifiers);
514 // Mouse button release event handler
515 virtual bool mouse_release_event(int x, int y, int button, int modifiers);
516 // Mouse drag (i.e., a mouse button was pressed) event handler
517 virtual bool mouse_drag_event(int x, int y, int dx, int dy, int button, int modifiers);
518 // Mouse free move (i.e., no mouse button was pressed) event handler
519 virtual bool mouse_free_move_event(int x, int y, int dx, int dy, int modifiers);
520 // Mouse scroll event handler
521 virtual bool mouse_scroll_event(int x, int y, int dx, int dy);
522
523 // Text input event handler: codepoint is native endian UTF-32 format
524 // NOTE: This one reveals the actual character being sent (not just the physical key)
525 virtual bool char_input_event(unsigned int codepoint);
526 // Keyboard event handler.
527 // NOTE: This function does not reveal the actual character.
528 virtual bool key_press_event(int key, int modifiers);
529 virtual bool key_release_event(int key, int modifiers);
530
531 // Handle a file drop event
532 virtual bool drop_event(const std::vector<std::string> & filenames);
533
534 // Handle a focus change event
535 virtual bool focus_event(bool focused);
536
537 protected:
538 GLFWwindow *create_window(const std::string &title,
539 int samples,
540 int gl_major, // must >= 3
541 int gl_minor, // must >= 2
542 bool full_screen,
543 bool resizable,
544 int depth_bits,
545 int stencil_bits,
546 int width,
547 int height);
548
549 void setup_callbacks(GLFWwindow*);
550
551 /* Event handlers. Client code should not touch these */
552 virtual bool callback_event_cursor_pos(double x, double y);
553 virtual bool callback_event_mouse_button(int button, int action, int modifiers);
554 virtual bool callback_event_keyboard(int key, int action, int mods);
555 virtual bool callback_event_character(unsigned int codepoint);
556 virtual bool callback_event_drop(int count, const char **filenames);
557 virtual bool callback_event_scroll(double dx, double dy);
558 virtual void callback_event_resize(int w, int h);
559
560 void draw_corner_axes() const;
561
562 void draw_face_labels(Model* model, TextRenderer* texter, int font_id, const vec3& color) const;
563 void draw_vertex_labels(Model* model, TextRenderer* texter, int font_id, const vec3& color) const;
564
565 void copy_view();
566 void paste_view();
567
568 protected:
569 GLFWwindow* window_;
570 bool should_exit_;
571 float dpi_scaling_;
572 int width_;
573 int height_;
574
575 std::string title_;
576 Camera* camera_;
577
579 bool is_animating_;
580
581 std::string usage_string_;
582
583 int samples_; // the actual samples
584
585 bool full_screen_;
586 vec4 background_color_;
587
588 // enable/disable event processing
589 bool process_events_;
590 char gpu_time_[48]; // show the frame rate
591
592 TextRenderer* texter_;
593
594 // mouse
595 int pressed_button_; // for mouse drag
596 int modifiers_; // for mouse drag
597 bool drag_active_; // for mouse drag
598 int mouse_current_x_; // for mouse button and scroll events
599 int mouse_current_y_;
600 int mouse_pressed_x_; // last pressed position
601 int mouse_pressed_y_;
602 int pressed_key_;
603
604 bool show_pivot_point_;
605 bool show_frame_rate_;
606
607 //----------------- viewer data -------------------
608
609 // corner axes
610 TrianglesDrawable* drawable_axes_;
611
612 // camera path
613 bool show_camera_path_;
614
615 std::vector<Model*> models_;
616 int model_idx_;
617
618 // drawables independent of any model
619 std::vector<Drawable*> drawables_;
620
621 typedef std::pair<Function, Model*> FunctionModel;
622 std::map<Key, std::map<Modifier, FunctionModel> > commands_;
623 };
624
625}
626
627
628#endif // EASY3D_VIEWER_VIEWER_H
A perspective or orthographic camera.
Definition: camera.h:116
The base class for drawable objects. A drawable represent a set of points, line segments,...
Definition: drawable.h:56
A keyframe interpolator for animation generation.
Definition: key_frame_interpolator.h:104
The base class of renderable 3D models.
Definition: model.h:49
TextRenderer enables quick and easy string rendering in OpenGL applications.
Definition: text_renderer.h:53
The drawable for rendering a set of triangles, e.g., the surface of a triangular mesh.
Definition: drawable_triangles.h:46
The built-in Easy3D viewer.
Definition: viewer.h:61
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:460
Button
Mouse buttons.
Definition: viewer.h:426
virtual ~Viewer()
The destructor.
Definition: viewer.cpp:551
const easy3d::vec4 & background_color() const
Query the background color of the viewer.
Definition: viewer.h:174
bool is_animating() const
Is animation currently being performed.
Definition: viewer.cpp:1199
void framebuffer_size(int &w, int &h) const
Queries the size of the framebuffer.
Definition: viewer.cpp:626
void clear_scene()
Delete all visual contents of the viewer (all models and drawables).
Definition: viewer.cpp:556
virtual void exit()
Terminate the viewer.
Definition: viewer.cpp:1159
const std::string & usage() const
The usage information of the viewer. For the time being, it is the manual of this default viewer.
Definition: viewer.h:377
const std::string & title() const
Query the window title of the viewer.
Definition: viewer.h:119
void set_title(const std::string &title)
Set the window title of the viewer.
Definition: viewer.cpp:590
void viewer_size(int &w, int &h) const
Queries the size of the viewer/window.
Definition: viewer.h:132
const Camera * camera() const
Returns the camera used by the viewer. See Camera.
Definition: viewer.h:179
bool delete_drawable(Drawable *drawable)
Definition: viewer.cpp:1319
Model * current_model() const
Query the active model.
Definition: viewer.cpp:746
virtual bool save() const
Save the active model (if exists) to a file.
Definition: viewer.cpp:1395
void resize(int w, int h)
Set/Change the viewer size.
Definition: viewer.cpp:603
bool add_drawable(Drawable *drawable)
Add a drawable to the viewer to be visualized. After a drawable being added to the viewer,...
Definition: viewer.cpp:1302
Modifier
The key modifiers. Currently only Shift, Ctrl, and Alt are supported.
Definition: viewer.h:412
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:1064
int samples() const
Query the actual samples of the viewer.
Definition: viewer.h:156
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:74
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:1273
Key
The keys. Currently only a limited number of commonly used keys are supported.
Definition: viewer.h:387
const std::vector< Model * > & models() const
Query the models managed by this viewer.
Definition: viewer.h:263
const std::vector< Drawable * > & drawables() const
Query the drawables managed by this viewer.
Definition: viewer.h:302
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:1204
int framebuffer_width() const
Returns the width of the framebuffer, which is identical to: width() * dpi_scaling().
Definition: viewer.cpp:612
void fit_screen(const easy3d::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:1337
Camera * camera()
Returns the camera used by the viewer. See Camera.
Definition: viewer.h:177
int height() const
Returns the height of the viewer/window.
Definition: viewer.h:130
int width() const
Returns the width of the viewer/window.
Definition: viewer.h:128
std::function< bool(Viewer *viewer)> animation_func_
Function called at an equal interval for animation.
Definition: viewer.h:468
void set_animation(bool b)
Enable/Disable animation.
Definition: viewer.cpp:1193
virtual bool open()
Open a model (PointCloud/SurfaceMesh/Graph) from a file into the viewer. On success,...
Definition: viewer.cpp:1369
int run(bool see_all=true)
Run the viewer.
Definition: viewer.cpp:1090
void update() const
Update the display (i.e., repaint).
Definition: viewer.cpp:631
void set_background_color(const easy3d::vec4 &c)
Set the background color of the viewer.
Definition: viewer.h:168
bool snapshot() const
Take a snapshot of the screen and save it to a file.
Definition: viewer.cpp:1436
float dpi_scaling() const
Query the scaling factor for high DPI devices (e.g., MackBook pro).
Definition: viewer.h:162
void set_position(int xpos, int ypos)
Sets the position of the content area of the viewer.
Definition: viewer.cpp:598
std::function< bool(Viewer *viewer, Model *model)> Function
Definition: viewer.h:449
int framebuffer_height() const
Returns the height of the framebuffer, which is identical to: height() * dpi_scaling().
Definition: viewer.cpp:619
Definition: collider.cpp:182