1 // ----------------------------------------------------------------------------
2 // -                        Open3D: www.open3d.org                            -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #include "Visualizer.h"
28 
29 namespace three{
30 
WindowRefreshCallback(GLFWwindow * window)31 void Visualizer::WindowRefreshCallback(GLFWwindow *window)
32 {
33     if (is_redraw_required_) {
34         Render();
35         is_redraw_required_ = false;
36     }
37 }
38 
WindowResizeCallback(GLFWwindow * window,int w,int h)39 void Visualizer::WindowResizeCallback(GLFWwindow *window, int w, int h)
40 {
41     view_control_ptr_->ChangeWindowSize(w, h);
42     is_redraw_required_ = true;
43 }
44 
MouseMoveCallback(GLFWwindow * window,double x,double y)45 void Visualizer::MouseMoveCallback(GLFWwindow *window, double x, double y)
46 {
47 #ifdef __APPLE__
48     x /= pixel_to_screen_coordinate_;
49     y /= pixel_to_screen_coordinate_;
50 #endif
51     if (mouse_control_.is_mouse_left_button_down) {
52         if (mouse_control_.is_control_key_down) {
53             view_control_ptr_->Translate(
54                     x - mouse_control_.mouse_position_x,
55                     y - mouse_control_.mouse_position_y,
56                     mouse_control_.mouse_position_x,
57                     mouse_control_.mouse_position_y);
58         } else {
59             view_control_ptr_->Rotate(
60                     x - mouse_control_.mouse_position_x,
61                     y - mouse_control_.mouse_position_y,
62                     mouse_control_.mouse_position_x,
63                     mouse_control_.mouse_position_y);
64         }
65         is_redraw_required_ = true;
66     }
67     mouse_control_.mouse_position_x = x;
68     mouse_control_.mouse_position_y = y;
69 }
70 
MouseScrollCallback(GLFWwindow * window,double x,double y)71 void Visualizer::MouseScrollCallback(GLFWwindow* window, double x, double y)
72 {
73     view_control_ptr_->Scale(y);
74     is_redraw_required_ = true;
75 }
76 
MouseButtonCallback(GLFWwindow * window,int button,int action,int mods)77 void Visualizer::MouseButtonCallback(GLFWwindow* window,
78         int button, int action, int mods)
79 {
80     double x, y;
81     glfwGetCursorPos(window, &x, &y);
82 #ifdef __APPLE__
83     x /= pixel_to_screen_coordinate_;
84     y /= pixel_to_screen_coordinate_;
85 #endif
86     mouse_control_.mouse_position_x = x;
87     mouse_control_.mouse_position_y = y;
88     if (button == GLFW_MOUSE_BUTTON_LEFT) {
89         if (action == GLFW_PRESS) {
90             mouse_control_.is_mouse_left_button_down = true;
91             mouse_control_.is_control_key_down =
92                     (mods & GLFW_MOD_CONTROL) != 0;
93             mouse_control_.is_shift_key_down = (mods & GLFW_MOD_SHIFT) != 0;
94             mouse_control_.is_alt_key_down = (mods & GLFW_MOD_ALT) != 0;
95             mouse_control_.is_super_key_down = (mods & GLFW_MOD_SUPER) != 0;
96         } else {
97             mouse_control_.is_mouse_left_button_down = false;
98             mouse_control_.is_control_key_down = false;
99             mouse_control_.is_shift_key_down = false;
100             mouse_control_.is_alt_key_down = false;
101             mouse_control_.is_super_key_down = false;
102         }
103     }
104 }
105 
KeyPressCallback(GLFWwindow * window,int key,int scancode,int action,int mods)106 void Visualizer::KeyPressCallback(GLFWwindow *window,
107         int key, int scancode, int action, int mods)
108 {
109     if (action == GLFW_RELEASE) {
110         return;
111     }
112 
113     switch (key) {
114     case GLFW_KEY_LEFT_BRACKET:
115         view_control_ptr_->ChangeFieldOfView(-1.0);
116         PrintDebug("[Visualizer] Field of view set to %.2f.\n",
117                 view_control_ptr_->GetFieldOfView());
118         break;
119     case GLFW_KEY_RIGHT_BRACKET:
120         view_control_ptr_->ChangeFieldOfView(1.0);
121         PrintDebug("[Visualizer] Field of view set to %.2f.\n",
122                 view_control_ptr_->GetFieldOfView());
123         break;
124     case GLFW_KEY_R:
125         ResetViewPoint();
126         PrintDebug("[Visualizer] Reset view point.\n");
127         break;
128     case GLFW_KEY_C:
129         if (mods & GLFW_MOD_CONTROL || mods & GLFW_MOD_SUPER) {
130             CopyViewStatusToClipboard();
131         }
132         break;
133     case GLFW_KEY_V:
134         if (mods & GLFW_MOD_CONTROL || mods & GLFW_MOD_SUPER) {
135             CopyViewStatusFromClipboard();
136         }
137         break;
138     case GLFW_KEY_ESCAPE:
139     case GLFW_KEY_Q:
140         Close();
141         break;
142     case GLFW_KEY_H:
143         PrintVisualizerHelp();
144         break;
145     case GLFW_KEY_P:
146     case GLFW_KEY_PRINT_SCREEN:
147         CaptureScreenImage();
148         break;
149     case GLFW_KEY_D:
150         CaptureDepthImage();
151         break;
152     case GLFW_KEY_O:
153         CaptureRenderOption();
154         break;
155     case GLFW_KEY_L:
156         render_option_ptr_->ToggleLightOn();
157         PrintDebug("[Visualizer] Lighting %s.\n",
158                 render_option_ptr_->light_on_ ? "ON" : "OFF");
159         break;
160     case GLFW_KEY_EQUAL:
161         render_option_ptr_->ChangePointSize(1.0);
162         if (render_option_ptr_->point_show_normal_) {
163             UpdateGeometry();
164         }
165         PrintDebug("[Visualizer] Point size set to %.2f.\n",
166                 render_option_ptr_->point_size_);
167         break;
168     case GLFW_KEY_MINUS:
169         render_option_ptr_->ChangePointSize(-1.0);
170         if (render_option_ptr_->point_show_normal_) {
171             UpdateGeometry();
172         }
173         PrintDebug("[Visualizer] Point size set to %.2f.\n",
174                 render_option_ptr_->point_size_);
175         break;
176     case GLFW_KEY_N:
177         render_option_ptr_->TogglePointShowNormal();
178         if (render_option_ptr_->point_show_normal_) {
179             UpdateGeometry();
180         }
181         PrintDebug("[Visualizer] Point normal rendering %s.\n",
182                 render_option_ptr_->point_show_normal_ ? "ON" : "OFF");
183         break;
184     case GLFW_KEY_S:
185         render_option_ptr_->ToggleShadingOption();
186         UpdateGeometry();
187         PrintDebug("[Visualizer] Mesh shading mode is %s.\n",
188                 render_option_ptr_->mesh_shade_option_ ==
189                 RenderOption::MeshShadeOption::FlatShade ?
190                 "FLAT" : "SMOOTH");
191         break;
192     case GLFW_KEY_W:
193         render_option_ptr_->ToggleMeshShowWireframe();
194         PrintDebug("[Visualizer] Mesh wireframe rendering %s.\n",
195                 render_option_ptr_->mesh_show_wireframe_ ? "ON" : "OFF");
196         break;
197     case GLFW_KEY_B:
198         render_option_ptr_->ToggleMeshShowBackFace();
199         PrintDebug("[Visualizer] Mesh back face rendering %s.\n",
200                 render_option_ptr_->mesh_show_back_face_ ? "ON" : "OFF");
201         break;
202     case GLFW_KEY_I:
203         render_option_ptr_->ToggleInterpolationOption();
204         UpdateGeometry();
205         PrintDebug("[Visualizer] Image interpolation mode is %s.\n",
206                 render_option_ptr_->interpolation_option_ ==
207                 RenderOption::TextureInterpolationOption::Nearest ?
208                 "NEARST" : "LINEAR");
209         break;
210     case GLFW_KEY_T:
211         render_option_ptr_->ToggleImageStretchOption();
212         PrintDebug("[Visualizer] Image stretch mode is #%d.\n",
213                 int(render_option_ptr_->image_stretch_option_));
214         break;
215     case GLFW_KEY_0:
216         if (mods & GLFW_MOD_CONTROL) {
217             render_option_ptr_->mesh_color_option_ =
218                     RenderOption::MeshColorOption::Default;
219             UpdateGeometry();
220             PrintDebug("[Visualizer] Mesh color set to DEFAULT.\n");
221         } else if (mods & GLFW_MOD_SHIFT) {
222             SetGlobalColorMap(ColorMap::ColorMapOption::Gray);
223             UpdateGeometry();
224             PrintDebug("[Visualizer] Color map set to GRAY.\n");
225         } else {
226             render_option_ptr_->point_color_option_ =
227                     RenderOption::PointColorOption::Default;
228             UpdateGeometry();
229             PrintDebug("[Visualizer] Point color set to DEFAULT.\n");
230         }
231         break;
232     case GLFW_KEY_1:
233         if (mods & GLFW_MOD_CONTROL) {
234             render_option_ptr_->mesh_color_option_ =
235                     RenderOption::MeshColorOption::Color;
236             UpdateGeometry();
237             PrintDebug("[Visualizer] Mesh color set to COLOR.\n");
238         } else if (mods & GLFW_MOD_SHIFT) {
239             SetGlobalColorMap(ColorMap::ColorMapOption::Jet);
240             UpdateGeometry();
241             PrintDebug("[Visualizer] Color map set to JET.\n");
242         } else {
243             render_option_ptr_->point_color_option_ =
244                     RenderOption::PointColorOption::Color;
245             UpdateGeometry();
246             PrintDebug("[Visualizer] Point color set to COLOR.\n");
247         }
248         break;
249     case GLFW_KEY_2:
250         if (mods & GLFW_MOD_CONTROL) {
251             render_option_ptr_->mesh_color_option_ =
252                     RenderOption::MeshColorOption::XCoordinate;
253             UpdateGeometry();
254             PrintDebug("[Visualizer] Mesh color set to X.\n");
255         } else if (mods & GLFW_MOD_SHIFT) {
256             SetGlobalColorMap(ColorMap::ColorMapOption::Summer);
257             UpdateGeometry();
258             PrintDebug("[Visualizer] Color map set to SUMMER.\n");
259         } else {
260             render_option_ptr_->point_color_option_ =
261                     RenderOption::PointColorOption::XCoordinate;
262             UpdateGeometry();
263             PrintDebug("[Visualizer] Point color set to X.\n");
264         }
265         break;
266     case GLFW_KEY_3:
267         if (mods & GLFW_MOD_CONTROL) {
268             render_option_ptr_->mesh_color_option_ =
269                     RenderOption::MeshColorOption::YCoordinate;
270             UpdateGeometry();
271             PrintDebug("[Visualizer] Mesh color set to Y.\n");
272         } else if (mods & GLFW_MOD_SHIFT) {
273             SetGlobalColorMap(ColorMap::ColorMapOption::Winter);
274             UpdateGeometry();
275             PrintDebug("[Visualizer] Color map set to WINTER.\n");
276         } else {
277             render_option_ptr_->point_color_option_ =
278                     RenderOption::PointColorOption::YCoordinate;
279             UpdateGeometry();
280             PrintDebug("[Visualizer] Point color set to Y.\n");
281         }
282         break;
283     case GLFW_KEY_4:
284         if (mods & GLFW_MOD_CONTROL) {
285             render_option_ptr_->mesh_color_option_ =
286                     RenderOption::MeshColorOption::ZCoordinate;
287             UpdateGeometry();
288             PrintDebug("[Visualizer] Mesh color set to Z.\n");
289         } else if (mods & GLFW_MOD_SHIFT) {
290             SetGlobalColorMap(ColorMap::ColorMapOption::Hot);
291             UpdateGeometry();
292             PrintDebug("[Visualizer] Color map set to HOT.\n");
293         } else {
294             render_option_ptr_->point_color_option_ =
295                     RenderOption::PointColorOption::ZCoordinate;
296             UpdateGeometry();
297             PrintDebug("[Visualizer] Point color set to Z.\n");
298         }
299         break;
300     case GLFW_KEY_9:
301         if (mods & GLFW_MOD_CONTROL) {
302             render_option_ptr_->mesh_color_option_ =
303                     RenderOption::MeshColorOption::Normal;
304             UpdateGeometry();
305             PrintDebug("[Visualizer] Mesh color set to NORMAL.\n");
306         } else if (mods & GLFW_MOD_SHIFT) {
307         } else {
308             render_option_ptr_->point_color_option_ =
309                     RenderOption::PointColorOption::Normal;
310             UpdateGeometry();
311             PrintDebug("[Visualizer] Point color set to NORMAL.\n");
312         }
313         break;
314     default:
315         break;
316     }
317 
318     is_redraw_required_ = true;
319 }
320 
WindowCloseCallback(GLFWwindow * window)321 void Visualizer::WindowCloseCallback(GLFWwindow *window)
322 {
323     // happens when user click the close icon to close the window
324 }
325 
326 }    // namespace three
327