KiCad PCB EDA Suite
eda_3d_canvas.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
5  * Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
30 #include <GL/glew.h> // Must be included first
31 
32 #include "../common_ogl/openGL_includes.h"
33 #include "../common_ogl/ogl_utils.h"
34 #include "eda_3d_canvas.h"
35 #include "../3d_viewer_id.h"
36 #include "../3d_rendering/3d_render_raytracing/c3d_render_raytracing.h"
37 #include "../3d_viewer/eda_3d_viewer.h"
38 #include "../3d_rendering/test_cases.h"
39 #include <class_board.h>
40 #include "status_text_reporter.h"
41 #include <gl_context_mgr.h>
42 #include <profile.h> // To use GetRunningMicroSecs or an other profiling utility
43 #include <bitmaps.h>
44 #include <hotkeys_basic.h>
45 #include <menus_helpers.h>
46 
48 
49 
56 const wxChar * EDA_3D_CANVAS::m_logTrace = wxT( "KI_TRACE_EDA_3D_CANVAS" );
57 
59 
60 
61 BEGIN_EVENT_TABLE( EDA_3D_CANVAS, wxGLCanvas )
62  EVT_PAINT( EDA_3D_CANVAS::OnPaint )
63  EVT_CHAR( EDA_3D_CANVAS::OnKeyEvent )
64  EVT_CHAR_HOOK( EDA_3D_CANVAS::OnCharHook )
65 
66  // mouse events
67  EVT_LEFT_DOWN( EDA_3D_CANVAS::OnLeftDown )
68  EVT_LEFT_UP( EDA_3D_CANVAS::OnLeftUp )
69  EVT_MIDDLE_UP( EDA_3D_CANVAS::OnMiddleUp )
70  EVT_MIDDLE_DOWN( EDA_3D_CANVAS::OnMiddleDown)
71  EVT_RIGHT_DOWN( EDA_3D_CANVAS::OnRightClick )
72  EVT_MOUSEWHEEL( EDA_3D_CANVAS::OnMouseWheel )
73  EVT_MOTION( EDA_3D_CANVAS::OnMouseMove )
74 
75 #ifdef USE_OSX_MAGNIFY_EVENT
76  EVT_MAGNIFY( EDA_3D_CANVAS::OnMagnify )
77 #endif
78 
79  // other events
80  EVT_ERASE_BACKGROUND( EDA_3D_CANVAS::OnEraseBackground )
82  ID_POPUP_3D_VIEW_END, EDA_3D_CANVAS::OnPopUpMenu )
83 
84  EVT_CLOSE( EDA_3D_CANVAS::OnCloseWindow )
85 END_EVENT_TABLE()
86 
87 
88 EDA_3D_CANVAS::EDA_3D_CANVAS( wxWindow *aParent,
89  const int *aAttribList,
90  BOARD *aBoard,
91  CINFO3D_VISU &aSettings , S3D_CACHE *a3DCachePointer ) :
92 
93  wxGLCanvas( aParent,
94  wxID_ANY,
95  aAttribList,
96  wxDefaultPosition,
97  wxDefaultSize,
98  wxFULL_REPAINT_ON_RESIZE
99  ),
100  m_settings( aSettings )
101 {
102  // Run test cases in debug mode, once.
103  //DBG( Run_3d_viewer_test_cases() );
104 
105  wxLogTrace( m_logTrace, wxT( "EDA_3D_CANVAS::EDA_3D_CANVAS" ) );
106 
107  m_editing_timeout_timer.SetOwner( this );
108  Connect( m_editing_timeout_timer.GetId(),
109  wxEVT_TIMER,
110  wxTimerEventHandler( EDA_3D_CANVAS::OnTimerTimeout_Editing ),
111  NULL,
112  this );
113 
114  m_redraw_trigger_timer.SetOwner( this );
115  Connect( m_redraw_trigger_timer.GetId(),
116  wxEVT_TIMER,
117  wxTimerEventHandler( EDA_3D_CANVAS::OnTimerTimeout_Redraw ),
118  NULL,
119  this );
120 
121  m_mouse_was_moved = false;
122  m_mouse_is_moving = false;
123  m_camera_is_moving = false;
124  m_render_pivot = false;
125  m_camera_moving_speed = 1.0f;
126 
127  m_strtime_camera_movement = 0;
128 
129  m_is_opengl_initialized = false;
130 
131  m_render_raytracing_was_requested = false;
132 
133  m_parentStatusBar = NULL;
134  m_glRC = NULL;
135 
136  m_3d_render = NULL;
137 
138  m_3d_render_raytracing = new C3D_RENDER_RAYTRACING( aSettings );
139  m_3d_render_ogl_legacy = new C3D_RENDER_OGL_LEGACY( aSettings );
140 
141  wxASSERT( m_3d_render_raytracing != NULL );
142  wxASSERT( m_3d_render_ogl_legacy != NULL );
143 
144  RenderEngineChanged();
145 
146  wxASSERT( aBoard != NULL );
147  m_settings.SetBoard( aBoard );
148 
149  wxASSERT( a3DCachePointer != NULL );
150  m_settings.Set3DCacheManager( a3DCachePointer );
151 }
152 
153 
155 {
156  wxLogTrace( m_logTrace, wxT( "EDA_3D_CANVAS::~EDA_3D_CANVAS" ) );
157 
158  releaseOpenGL();
159 }
160 
161 
163 {
164  if( m_glRC )
165  {
167 
168  delete m_3d_render_raytracing;
169  m_3d_render_raytracing = NULL;
170 
171  delete m_3d_render_ogl_legacy;
172  m_3d_render_ogl_legacy = NULL;
173 
174  // This is just a copy of a pointer, can safelly be set to NULL
175  m_3d_render = NULL;
176 
179  m_glRC = NULL;
180  }
181 }
182 
183 
184 void EDA_3D_CANVAS::OnCloseWindow( wxCloseEvent &event )
185 {
186  releaseOpenGL();
187 
188  event.Skip();
189 }
190 
191 
193 {
194  wxLogTrace( m_logTrace, wxT( "EDA_3D_CANVAS::initializeOpenGL" ) );
195 
196  const GLenum err = glewInit();
197 
198  if( GLEW_OK != err )
199  {
200  const wxString msgError = (const char*) glewGetErrorString( err );
201 
202  wxLogMessage( msgError );
203 
204  return false;
205  }
206  else
207  {
208  wxLogTrace( m_logTrace,
209  wxString( wxT( "EDA_3D_CANVAS::initializeOpenGL Using GLEW " ) ) +
210  FROM_UTF8( (char*) glewGetString( GLEW_VERSION ) ) );
211  }
212 
214 
215  return true;
216 }
217 
218 
219 void EDA_3D_CANVAS::GetScreenshot( wxImage &aDstImage )
220 {
221  OGL_GetScreenshot( aDstImage );
222 }
223 
224 
225 void EDA_3D_CANVAS::ReloadRequest( BOARD *aBoard , S3D_CACHE *aCachePointer )
226 {
227  if( aCachePointer != NULL )
228  m_settings.Set3DCacheManager( aCachePointer );
229 
230  if( aBoard != NULL )
231  m_settings.SetBoard( aBoard );
232 
233  if( m_3d_render )
235 }
236 
237 
239 {
241 
242  if( m_3d_render )
244 
246  //m_mouse_was_moved = true;
247 
248  Request_refresh();
249 }
250 
251 
253 {
254  if( m_parentStatusBar )
255  {
256  wxString msg;
257 
258  msg.Printf( wxT( "dx %3.2f" ), m_settings.CameraGet().GetCameraPos().x );
259  m_parentStatusBar->SetStatusText( msg, 1 );
260 
261  msg.Printf( wxT( "dy %3.2f" ), m_settings.CameraGet().GetCameraPos().y );
262  m_parentStatusBar->SetStatusText( msg, 2 );
263 
264  //msg.Printf( _( "Zoom: %3.1f" ), 50 * m_settings.CameraGet().ZoomGet() );
265  //m_parentStatusBar->SetStatusText( msg, 3 );
266  }
267 }
268 
269 
270 void EDA_3D_CANVAS::OnPaint( wxPaintEvent &event )
271 {
272  // Please have a look at:
273  // https://lists.launchpad.net/kicad-developers/msg25149.html
274  // wxPaintDC( this );
275  // event.Skip( false );
276 
277  // SwapBuffer requires the window to be shown before calling
278  if( !IsShownOnScreen() )
279  {
280  wxLogTrace( m_logTrace, wxT( "EDA_3D_CANVAS::OnPaint !IsShown" ) );
281  return;
282  }
283 
284  // Because the board to draw is handled by the parent viewer frame,
285  // ensure this parent is still alive. When it is closed before the viewer
286  // frame, a paint event can be generated after the parent is closed,
287  // therefore with invalid board.
288  // This is dependant of the platform.
289  // Especially on OSX, but also on Windows, it frequently happens
290  if( !GetParent()->GetParent()->IsShown() )
291  return; // The parent board editor frame is no more alive
292 
293  wxString err_messages;
294 
295  // !TODO: implement error reporter
296  //WX_STRING_REPORTER errorReporter( &err_messages );
297  STATUS_TEXT_REPORTER activityReporter( m_parentStatusBar, 0 );
298 
299 
300  unsigned strtime = GetRunningMicroSecs();
301 
302  // "Makes the OpenGL state that is represented by the OpenGL rendering
303  // context context current, i.e. it will be used by all subsequent OpenGL calls.
304  // This function may only be called when the window is shown on screen"
305 
306  // Explicitly create a new rendering context instance for this canvas.
307  if( m_glRC == NULL )
309 
311 
312  // Set the OpenGL viewport according to the client size of this canvas.
313  // This is done here rather than in a wxSizeEvent handler because our
314  // OpenGL rendering context (and thus viewport setting) is used with
315  // multiple canvases: If we updated the viewport in the wxSizeEvent
316  // handler, changing the size of one canvas causes a viewport setting that
317  // is wrong when next another canvas is repainted.
318  wxSize clientSize = GetClientSize();
319 
320  const bool windows_size_changed = m_settings.CameraGet().SetCurWindowSize( clientSize );
321 
322 
323  // Initialize openGL if need
324  // /////////////////////////////////////////////////////////////////////////
326  {
327  if( !initializeOpenGL() )
328  {
330 
331  return;
332  }
333  }
334 
335 
336  // Check if a raytacing was requented and need to switch to raytracing mode
338  {
339  const bool was_camera_changed = m_settings.CameraGet().ParametersChanged();
340 
341  // It reverts back to OpenGL mode if it was requested a raytracing
342  // render of the current scene. AND the mouse / camera is moving
343  if( ( m_mouse_is_moving ||
345  was_camera_changed ||
346  windows_size_changed ) &&
348  {
351  }
352  }
353 
354 
355  float curtime_delta_s = 0.0f;
356 
357  if( m_camera_is_moving )
358  {
359  const unsigned curtime_delta = GetRunningMicroSecs() - m_strtime_camera_movement;
360  curtime_delta_s = (curtime_delta / 1e6) * m_camera_moving_speed;
361  m_settings.CameraGet().Interpolate( curtime_delta_s );
362 
363  if( curtime_delta_s > 1.0f )
364  {
365  m_render_pivot = false;
366  m_camera_is_moving = false;
367  m_mouse_was_moved = true;
368 
370  DisplayStatus();
371  }
372  else
373  {
374  Request_refresh();
375  }
376  }
377 
378 
379  // It will return true if the render request a new redraw
380  bool requested_redraw = false;
381 
382  if( m_3d_render )
383  {
384  m_3d_render->SetCurWindowSize( clientSize );
385 
386  requested_redraw = m_3d_render->Redraw( m_mouse_was_moved || m_camera_is_moving,
387  &activityReporter );
388  }
389 
390  if( m_render_pivot )
391  {
392  const float scale = glm::min( m_settings.CameraGet().ZoomGet(), 1.0f );
393  render_pivot( curtime_delta_s, scale * scale );
394  }
395 
396  // "Swaps the double-buffer of this window, making the back-buffer the
397  // front-buffer and vice versa, so that the output of the previous OpenGL
398  // commands is displayed on the window."
399  SwapBuffers();
400 
402 
403  if( !activityReporter.HasMessage() )
404  {
406  {
407  // Calculation time in miliseconds
408  const double calculation_time = (double)( GetRunningMicroSecs() - strtime) / 1e3;
409 
410  activityReporter.Report( wxString::Format( _( "Render time %.0f ms ( %.1f fps)" ),
411  calculation_time, 1000.0 / calculation_time ) );
412  }
413  }
414 
415  // This will reset the flag of camera parameters changed
417 
418  if( !err_messages.IsEmpty() )
419  wxLogMessage( err_messages );
420 
421  if( (!m_camera_is_moving) && requested_redraw )
422  {
423  m_mouse_was_moved = false;
424  Request_refresh( false );
425  }
426 }
427 
428 
429 void EDA_3D_CANVAS::OnEraseBackground( wxEraseEvent &event )
430 {
431  wxLogTrace( m_logTrace, wxT( "EDA_3D_CANVAS::OnEraseBackground" ) );
432  // Do nothing, to avoid flashing.
433 }
434 
435 
436 void EDA_3D_CANVAS::OnMouseWheel( wxMouseEvent &event )
437 {
438  bool mouseActivity = false;
439 
440  wxLogTrace( m_logTrace, wxT( "EDA_3D_CANVAS::OnMouseWheel" ) );
441 
442  if( m_camera_is_moving )
443  return;
444 
445  float delta_move = m_delta_move_step_factor * m_settings.CameraGet().ZoomGet();
446 
448  delta_move *= (0.01f * event.GetWheelRotation());
449  else
450  if( event.GetWheelRotation() < 0 )
451  delta_move = -delta_move;
452 
453  // mousewheel_panning enabled:
454  // wheel -> pan;
455  // wheel + shift -> horizontal scrolling;
456  // wheel + ctrl -> zooming;
457  // mousewheel_panning disabled:
458  // wheel + shift -> vertical scrolling;
459  // wheel + ctrl -> horizontal scrolling;
460  // wheel -> zooming.
461 
462  if( m_settings.GetFlag( FL_MOUSEWHEEL_PANNING ) && !event.ControlDown() )
463  {
464  if( event.GetWheelAxis() == wxMOUSE_WHEEL_HORIZONTAL || event.ShiftDown() )
465  m_settings.CameraGet().Pan( SFVEC3F( -delta_move, 0.0f, 0.0f ) );
466  else
467  m_settings.CameraGet().Pan( SFVEC3F( 0.0f, -delta_move, 0.0f ) );
468 
469  mouseActivity = true;
470  }
471  else if( event.ShiftDown() && !m_settings.GetFlag( FL_MOUSEWHEEL_PANNING ) )
472  {
473  m_settings.CameraGet().Pan( SFVEC3F( 0.0f, -delta_move, 0.0f ) );
474  mouseActivity = true;
475  }
476  else if( event.ControlDown() && !m_settings.GetFlag( FL_MOUSEWHEEL_PANNING ) )
477  {
478  m_settings.CameraGet().Pan( SFVEC3F( delta_move, 0.0f, 0.0f ) );
479  mouseActivity = true;
480  }
481  else
482  {
483  mouseActivity = m_settings.CameraGet().Zoom( event.GetWheelRotation() > 0 ? 1.1f : 1/1.1f );
484  }
485 
486  // If it results on a camera movement
487  if( mouseActivity )
488  {
489  DisplayStatus();
490  Request_refresh();
491 
492  m_mouse_is_moving = true;
493  m_mouse_was_moved = true;
494 
496  }
497 
498  // Update the cursor current mouse position on the camera
499  m_settings.CameraGet().SetCurMousePosition( event.GetPosition() );
500 }
501 
502 
503 #if wxCHECK_VERSION( 3, 1, 0 ) || defined( USE_OSX_MAGNIFY_EVENT )
504 void EDA_3D_CANVAS::OnMagnify( wxMouseEvent& event )
505 {
506  if( m_camera_is_moving )
507  return;
508 
509  //m_is_moving_mouse = true;
511 
512  float magnification = ( event.GetMagnification() + 1.0f );
513 
514  m_settings.CameraGet().Zoom( magnification );
515 
516  DisplayStatus();
517  Request_refresh();
518 
519  // Please someone test if this is need
520  //m_settings.CameraGet().SetCurMousePosition( event.GetPosition() );
521 }
522 #endif
523 
524 
525 void EDA_3D_CANVAS::OnMouseMove( wxMouseEvent &event )
526 {
527  //wxLogTrace( m_logTrace, wxT( "EDA_3D_CANVAS::OnMouseMove" ) );
528 
529  if( m_camera_is_moving )
530  return;
531 
532  m_settings.CameraGet().SetCurWindowSize( GetClientSize() );
533 
534  if( event.Dragging() )
535  {
536  if( event.LeftIsDown() ) // Drag
537  m_settings.CameraGet().Drag( event.GetPosition() );
538  else if( event.MiddleIsDown() ) // Pan
539  m_settings.CameraGet().Pan( event.GetPosition() );
540 
541  m_mouse_is_moving = true;
542  m_mouse_was_moved = true;
543 
544  // orientation has changed, redraw mesh
545  DisplayStatus();
546  Request_refresh();
547  }
548 
549  const wxPoint eventPosition = event.GetPosition();
550  m_settings.CameraGet().SetCurMousePosition( eventPosition );
551 }
552 
553 
554 void EDA_3D_CANVAS::OnLeftDown( wxMouseEvent &event )
555 {
557 }
558 
559 
560 void EDA_3D_CANVAS::OnLeftUp( wxMouseEvent &event )
561 {
562  if( m_camera_is_moving )
563  return;
564 
565  if( m_mouse_is_moving )
566  {
567  m_mouse_is_moving = false;
569  }
570 }
571 
572 
573 void EDA_3D_CANVAS::OnMiddleDown( wxMouseEvent &event )
574 {
576 }
577 
578 
579 void EDA_3D_CANVAS::OnMiddleUp( wxMouseEvent &event )
580 {
581  if( m_camera_is_moving )
582  return;
583 
584  if( m_mouse_is_moving )
585  {
586  m_mouse_is_moving = false;
588  }
589  else
590  {
592  }
593 }
594 
595 
596 void EDA_3D_CANVAS::OnRightClick( wxMouseEvent &event )
597 {
598  wxLogTrace( m_logTrace, wxT( "EDA_3D_CANVAS::OnRightClick" ) );
599 
600  if( m_camera_is_moving )
601  return;
602 
603  wxPoint pos;
604  wxMenu PopUpMenu;
605  wxString msg;
606  wxMenuItem* item;
607 
608  pos.x = event.GetX();
609  pos.y = event.GetY();
610 
611  msg = AddHotkeyName( _( "Zoom +" ), g_3DViewer_Hokeys_Descr,
612  ID_POPUP_ZOOMIN );
613  AddMenuItem( &PopUpMenu, ID_POPUP_ZOOMIN,
614  msg, KiBitmap( zoom_in_xpm ) );
615 
616 
617  msg = AddHotkeyName( _( "Zoom -" ), g_3DViewer_Hokeys_Descr,
619  AddMenuItem( &PopUpMenu, ID_POPUP_ZOOMOUT,
620  msg, KiBitmap( zoom_out_xpm ) );
621 
622  PopUpMenu.AppendSeparator();
623 
624  msg = AddHotkeyName( _( "Top View" ), g_3DViewer_Hokeys_Descr,
626  AddMenuItem( &PopUpMenu, ID_POPUP_VIEW_ZPOS,
627  msg, KiBitmap( axis3d_top_xpm ) );
628 
629  msg = AddHotkeyName( _( "Top View" ), g_3DViewer_Hokeys_Descr,
631  AddMenuItem( &PopUpMenu, ID_POPUP_VIEW_ZNEG,
632  msg, KiBitmap( axis3d_bottom_xpm ) );
633 
634  PopUpMenu.AppendSeparator();
635 
636  msg = AddHotkeyName( _( "Right View" ), g_3DViewer_Hokeys_Descr,
638  AddMenuItem( &PopUpMenu, ID_POPUP_VIEW_XPOS,
639  msg, KiBitmap( axis3d_right_xpm ) );
640 
641  msg = AddHotkeyName( _( "Left View" ), g_3DViewer_Hokeys_Descr,
643  AddMenuItem( &PopUpMenu, ID_POPUP_VIEW_XNEG,
644  msg, KiBitmap( axis3d_left_xpm ) );
645 
646  PopUpMenu.AppendSeparator();
647 
648  msg = AddHotkeyName( _( "Front View" ), g_3DViewer_Hokeys_Descr,
650  AddMenuItem( &PopUpMenu, ID_POPUP_VIEW_YPOS,
651  msg, KiBitmap( axis3d_front_xpm ) );
652 
653  msg = AddHotkeyName( _( "Back View" ), g_3DViewer_Hokeys_Descr,
655  AddMenuItem( &PopUpMenu, ID_POPUP_VIEW_YNEG,
656  msg, KiBitmap( axis3d_back_xpm ) );
657 
658  PopUpMenu.AppendSeparator();
659 
660  msg = AddHotkeyName( _( "Move left <-" ), g_3DViewer_Hokeys_Descr,
662  AddMenuItem( &PopUpMenu, ID_POPUP_MOVE3D_LEFT,
663  msg, KiBitmap( left_xpm ) );
664 
665  msg = AddHotkeyName( _( "Move right ->" ), g_3DViewer_Hokeys_Descr,
667  AddMenuItem( &PopUpMenu, ID_POPUP_MOVE3D_RIGHT,
668  msg, KiBitmap( right_xpm ) );
669 
670  msg = AddHotkeyName( _( "Move Up ^" ), g_3DViewer_Hokeys_Descr,
672  AddMenuItem( &PopUpMenu, ID_POPUP_MOVE3D_UP,
673  msg, KiBitmap( up_xpm ) );
674 
675  msg = AddHotkeyName( _( "Move Down" ), g_3DViewer_Hokeys_Descr,
677  AddMenuItem( &PopUpMenu, ID_POPUP_MOVE3D_DOWN,
678  msg, KiBitmap( down_xpm ) );
679 
680  PopupMenu( &PopUpMenu, pos );
681 }
682 
683 
684 void EDA_3D_CANVAS::OnPopUpMenu( wxCommandEvent &event )
685 {
686  int id = event.GetId();
687 
688  wxLogTrace( m_logTrace, wxT( "EDA_3D_CANVAS::OnPopUpMenu id:%d" ), id );
689 
690  int key = 0;
691 
692  switch( id )
693  {
694  case ID_POPUP_ZOOMIN:
695  key = WXK_F1;
696  break;
697 
698  case ID_POPUP_ZOOMOUT:
699  key = WXK_F2;
700  break;
701 
702  case ID_POPUP_VIEW_XPOS:
703  key = 'x';
704  break;
705 
706  case ID_POPUP_VIEW_XNEG:
707  key = 'X';
708  break;
709 
710  case ID_POPUP_VIEW_YPOS:
711  key = 'y';
712  break;
713 
714  case ID_POPUP_VIEW_YNEG:
715  key = 'Y';
716  break;
717 
718  case ID_POPUP_VIEW_ZPOS:
719  key = 'z';
720  break;
721 
722  case ID_POPUP_VIEW_ZNEG:
723  key = 'Z';
724  break;
725 
727  key = WXK_LEFT;
728  break;
729 
731  key = WXK_RIGHT;
732  break;
733 
734  case ID_POPUP_MOVE3D_UP:
735  key = WXK_UP;
736  break;
737 
739  key = WXK_DOWN;
740  break;
741 
742  default:
743  return;
744  }
745 
746  SetView3D( key );
747 }
748 
749 
750 void EDA_3D_CANVAS::OnCharHook( wxKeyEvent &event )
751 {
752  //wxLogTrace( m_logTrace, wxT( "EDA_3D_CANVAS::OnCharHook" ) );
753  event.Skip();
754 }
755 
756 
757 void EDA_3D_CANVAS::OnKeyEvent( wxKeyEvent& event )
758 {
759  //wxLogTrace( m_logTrace, wxT( "EDA_3D_CANVAS::OnKeyEvent" ) );
760 
761  if( m_camera_is_moving )
762  return;
763 
764  SetView3D( event.GetKeyCode() );
765  event.Skip();
766 }
767 
768 
769 void EDA_3D_CANVAS::OnTimerTimeout_Editing( wxTimerEvent &event )
770 {
771  (void)event;
772 
773  m_mouse_is_moving = false;
774  m_mouse_was_moved = false;
775 
776  Request_refresh();
777 }
778 
779 
781 {
783 }
784 
785 
787 {
788  m_editing_timeout_timer.Start( m_3d_render->GetWaitForEditingTimeOut() , wxTIMER_ONE_SHOT );
789 }
790 
791 
792 void EDA_3D_CANVAS::OnTimerTimeout_Redraw( wxTimerEvent &event )
793 {
794  (void)event;
795 
796  //Refresh();
797  //Update();
798 
799  wxPaintEvent redrawEvent;
800  wxPostEvent( this, redrawEvent );
801 }
802 
803 
804 void EDA_3D_CANVAS::Request_refresh( bool aRedrawImmediately )
805 {
806  if( aRedrawImmediately )
807  {
808  // On some systems, just calling Refresh does not work always
809  // (Issue experienced on Win7 MSYS2)
810  //Refresh();
811  //Update();
812 
813  // Using PostEvent will take priority to other events, like
814  // mouse movements, keys, etc.
815  wxPaintEvent redrawEvent;
816  wxPostEvent( this, redrawEvent );
817 
818  // This behaves the same
819  // wxQueueEvent( this,
820  // From wxWidget documentation: "The heap-allocated and
821  // non-NULL event to queue, the function takes ownership of it."
822  // new wxPaintEvent()
823  // );
824  }
825  else
826  {
827  // Schedule a timed redraw
828  m_redraw_trigger_timer.Start( 10 , wxTIMER_ONE_SHOT );
829  }
830 }
831 
832 
833 void EDA_3D_CANVAS::request_start_moving_camera( float aMovingSpeed, bool aRenderPivot )
834 {
835  wxASSERT( aMovingSpeed > FLT_EPSILON );
836 
837  m_render_pivot = aRenderPivot;
838  m_camera_moving_speed = aMovingSpeed;
839 
841 
842  DisplayStatus();
843  Request_refresh();
844 
845  m_camera_is_moving = true;
846 
848 }
849 
850 
852 {
853  SFVEC3F rayOrigin;
854  SFVEC3F rayDir;
855 
856  // Generate a ray origin and direction based on current mouser position and camera
857  m_settings.CameraGet().MakeRayAtCurrrentMousePosition( rayOrigin, rayDir );
858 
859  RAY mouseRay;
860  mouseRay.Init( rayOrigin, rayDir );
861 
862  float hit_t;
863 
864  // Test it with the board bounding box
865  if( m_settings.GetBBox3DU().Intersect( mouseRay, &hit_t ) )
866  {
869  m_settings.CameraGet().SetLookAtPos_T1( mouseRay.at( hit_t ) );
871 
873  }
874 }
875 
876 
877 void EDA_3D_CANVAS::SetView3D( int keycode )
878 {
879  if( m_camera_is_moving )
880  return;
881 
882  const float delta_move = m_delta_move_step_factor * m_settings.CameraGet().ZoomGet();
883  const float arrow_moving_time_speed = 8.0f;
884 
885  switch( keycode )
886  {
887  case WXK_SPACE:
889  return;
890 
891  case WXK_LEFT:
894  m_settings.CameraGet().Pan_T1( SFVEC3F( -delta_move, 0.0f, 0.0f ) );
895  request_start_moving_camera( arrow_moving_time_speed, false );
896  return;
897 
898  case WXK_RIGHT:
901  m_settings.CameraGet().Pan_T1( SFVEC3F( +delta_move, 0.0f, 0.0f ) );
902  request_start_moving_camera( arrow_moving_time_speed, false );
903  return;
904 
905  case WXK_UP:
908  m_settings.CameraGet().Pan_T1( SFVEC3F( 0.0f, +delta_move, 0.0f ) );
909  request_start_moving_camera( arrow_moving_time_speed, false );
910  return;
911 
912  case WXK_DOWN:
915  m_settings.CameraGet().Pan_T1( SFVEC3F( 0.0f, -delta_move, 0.0f ) );
916  request_start_moving_camera( arrow_moving_time_speed, false );
917  return;
918 
919  case WXK_HOME:
924  return;
925 
926  case WXK_END:
927  break;
928 
929  case WXK_TAB:
932  m_settings.CameraGet().RotateZ_T1( glm::radians( 45.0f ) );
934  break;
935 
936  case WXK_F1:
939  if( m_settings.CameraGet().Zoom_T1( 1.4f ) )
941  return;
942 
943  case WXK_F2:
946  if( m_settings.CameraGet().Zoom_T1( 1/1.4f ) )
948  return;
949 
950  case '+':
951  break;
952 
953  case '-':
954  break;
955 
956  case 't':
957  case 'T':
960  ReloadRequest();
961  break;
962 
963  case 's':
964  case 'S':
967  ReloadRequest();
968  break;
969 
970  case 'v':
971  case 'V':
974  ReloadRequest();
975  break;
976 
977  case 'r':
978  case 'R':
983  return;
984 
985  case 'x':
989  m_settings.CameraGet().RotateZ_T1( glm::radians( -90.0f ) );
990  m_settings.CameraGet().RotateX_T1( glm::radians( -90.0f ) );
992  return;
993 
994  case 'X':
998  m_settings.CameraGet().RotateZ_T1( glm::radians( 90.0f ) );
999  m_settings.CameraGet().RotateX_T1( glm::radians( -90.0f ) );
1001  return;
1002 
1003  case 'y':
1007  m_settings.CameraGet().RotateX_T1( glm::radians( -90.0f ) );
1009  return;
1010 
1011  case 'Y':
1015  m_settings.CameraGet().RotateX_T1( glm::radians( -90.0f ) );
1016  m_settings.CameraGet().RotateZ_T1( glm::radians( -180.0f ) );
1018  return;
1019 
1020  case 'z':
1025  glm::min( glm::max( m_settings.CameraGet().ZoomGet(), 0.5f ), 1.125f ) );
1026  return;
1027 
1028  case 'Z':
1032  m_settings.CameraGet().RotateX_T1( glm::radians( -180.0f ) );
1034  glm::min( glm::max( m_settings.CameraGet().ZoomGet(), 0.5f ), 1.125f ) );
1035  return;
1036 
1037  default:
1038  return;
1039  }
1040 
1041  m_mouse_was_moved = true;
1042 
1044 
1045  DisplayStatus();
1046  Request_refresh();
1047 }
1048 
1049 
1051 {
1052 
1053  switch( m_settings.RenderEngineGet() )
1054  {
1057  break;
1058 
1061  break;
1062 
1063  default:
1064  m_3d_render = NULL;
1065  break;
1066  }
1067 
1068  if( m_3d_render )
1070 
1071  m_mouse_was_moved = false;
1072 
1073  Request_refresh();
1074 }
C3D_RENDER_RAYTRACING * m_3d_render_raytracing
Raytracing render class.
void SetLookAtPos_T1(const SFVEC3F &aLookAtPos)
Definition: ccamera.h:125
void OGL_GetScreenshot(wxImage &aDstImage)
OGL_GetScreenshot - got the pixel data of current OpenGL image.
Definition: ogl_utils.cpp:35
float ZoomGet() const
Definition: ccamera.cpp:490
void OnMouseMove(wxMouseEvent &event)
Structure EDA_HOTKEY_CONFIG contains the information required to save hot key information to a config...
Definition: hotkeys_basic.h:87
void request_start_moving_camera(float aMovingSpeed=2.0f, bool aRenderPivot=true)
request_start_moving_camera - start a camera movement
void releaseOpenGL()
releaseOpenGL - free created targets and openGL context
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes...
Definition: macros.h:53
virtual void SetT0_and_T1_current_T()
SetT0_and_T1_current_T - This will set T0 and T1 with the current values.
Definition: ccamera.cpp:535
bool m_mouse_is_moving
true if mouse activity is on progress
CINFO3D_VISU & m_settings
Stores all pre-computed 3D information and visualization settings to render the board.
void ResetXYpos_T1()
Definition: ccamera.cpp:382
unsigned m_strtime_camera_movement
Stores the ticktime when the camera star its movement.
void stop_editingTimeOut_Timer()
stop_editingTimeOut_Timer - stop the editing time, so it will not timeout
void Init(const SFVEC3F &o, const SFVEC3F &d)
Definition: ray.cpp:38
wxMenuItem * AddMenuItem(wxMenu *aMenu, int aId, const wxString &aText, const wxBitmap &aImage, wxItemKind aType=wxITEM_NORMAL)
Function AddMenuItem is an inline helper function to create and insert a menu item with an icon into ...
Definition: bitmap.cpp:55
wxStatusBar * m_parentStatusBar
Parent statusbar to report progress.
Class BOARD to handle a board.
wxGLContext * m_glRC
current OpenGL context
SFVEC3F at(float t) const
Definition: ray.h:65
void OnCloseWindow(wxCloseEvent &event)
OnCloseWindow - called when the frame is closed.
Class EDA_3D_CANVAS Implement a canvas based on a wxGLCanvas.
Definition: eda_3d_canvas.h:53
wxTimer m_editing_timeout_timer
Time timeout will expires after some time sinalizing that the mouse / keyboard movements are over...
Definition: ray.h:43
void DisplayStatus()
DisplayStatus - Update the status bar with the position information.
C3D_RENDER_OGL_LEGACY * m_3d_render_ogl_legacy
OpenGL legacy render class.
void Request_refresh(bool aRedrawImmediately=true)
Request_refresh - Schedule a refresh update of the canvas.
void ReloadRequest(BOARD *aBoard=NULL, S3D_CACHE *aCachePointer=NULL)
virtual int GetWaitForEditingTimeOut()=0
GetWaitForEditingTimeOut - Give the interface the time (in ms) that it should wait for editing or mov...
void UnlockCtx(wxGLContext *aContext)
Function UnlockCtx allows other canvases to bind an OpenGL context.
void SetCurMousePosition(const wxPoint &aPosition)
It updates the current mouse position without make any new recalculations on camera.
Definition: ccamera.cpp:401
bool Zoom(float aFactor)
Definition: ccamera.cpp:454
void OnLeftDown(wxMouseEvent &event)
float m_camera_moving_speed
1.0f will be 1:1
static GL_CONTEXT_MANAGER & Get()
Function Get returns the GL_CONTEXT_MANAGER instance (singleton).
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Function KiBitmap constructs a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:36
void OnLeftUp(wxMouseEvent &event)
void restart_editingTimeOut_Timer()
restart_editingTimeOut_Timer - reset the editing timer
void RenderRaytracingRequest()
RenderRaytracingRequest - Request to render the current view in Raytracing mode.
void OnKeyEvent(wxKeyEvent &event)
void OnMiddleDown(wxMouseEvent &event)
bool m_mouse_was_moved
true if there was some type of activity, it will be used to render in preview mode ...
void OnEraseBackground(wxEraseEvent &event)
void OnMouseWheel(wxMouseEvent &event)
void RenderEngineChanged()
RenderEngineChanged - Notify that the render engine was changed.
Class STATUS_TEXT_REPORTER is a wrapper for reporting to a wxString in a wxFrame status text...
Class CINFO3D_VISU Helper class to handle information needed to display 3D board. ...
Definition: cinfo3d_visu.h:70
bool m_camera_is_moving
true if camera animation is ongoing
virtual void Interpolate(float t)
Interpolate - It will update the matrix to interpolate between T0 and T1 values.
Definition: ccamera.cpp:549
virtual void Drag(const wxPoint &aNewMousePosition)=0
Calculate a new mouse drag position.
EVT_MIDDLE_DOWN(mpWindow::OnMouseMiddleDown) EVT_MOUSEWHEEL(mpWindow
Definition: mathplot.cpp:1724
const SFVEC3F & GetCameraPos() const
Definition: ccamera.h:131
bool SetCurWindowSize(const wxSize &aSize)
SetCurWindowSize - update the windows size of the camera.
Definition: ccamera.cpp:428
void OnPopUpMenu(wxCommandEvent &event)
bool Intersect(const RAY &aRay, float *t) const
Function Intersect.
Definition: cbbox_ray.cpp:46
void OnTimerTimeout_Redraw(wxTimerEvent &event)
bool Zoom_T1(float aFactor)
Definition: ccamera.cpp:473
virtual void Pan_T1(const SFVEC3F &aDeltaOffsetInc)=0
void RotateX_T1(float aAngleInRadians)
Definition: ccamera.cpp:517
RENDER_ENGINE RenderEngineGet() const
RenderEngineGet.
Definition: cinfo3d_visu.h:234
virtual void Reset_T1()
Definition: ccamera.cpp:92
void LockCtx(wxGLContext *aContext, wxGLCanvas *aCanvas)
Function LockCtx sets a context as current and prevents other canvases from switching it...
bool m_render_pivot
activated the render of pivot while camera moving
bool m_is_opengl_initialized
Flag to store if opengl was initialized already.
C3D_RENDER_BASE * m_3d_render
The current render in used for this canvas.
void OnMiddleUp(wxMouseEvent &event)
The C3D_RENDER_OGL_LEGACY class render the board using openGL legacy mode.
bool GetFlag(DISPLAY3D_FLG aFlag) const
GetFlag - get a configuration status of a flag.
void GetScreenshot(wxImage &aDstImage)
Request a screenshot and output it to the aDstImage.
virtual bool Redraw(bool aIsMoving, REPORTER *aStatusTextReporter=NULL)=0
Redraw - Ask to redraw the view.
void OnCharHook(wxKeyEvent &event)
const int scale
wxString AddHotkeyName(const wxString &aText, EDA_HOTKEY **aList, int aCommandId, HOTKEY_ACTION_TYPE aShortCutType)
Function AddHotkeyName Add the key name from the Command id value ( m_Idcommand member value) ...
void SetView3D(int keycode)
SetView3D - Helper function to call view commands.
bool initializeOpenGL()
initializeOpenGL
void MakeRayAtCurrrentMousePosition(SFVEC3F &aOutOrigin, SFVEC3F &aOutDirection) const
MakeRayAtCurrrentMousePosition - Make a ray based on the latest mouse position.
Definition: ccamera.cpp:350
void move_pivot_based_on_cur_mouse_position()
move_pivot_based_on_cur_mouse_position - This function hits a ray to the board and start a moviment ...
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
#define max(a, b)
Definition: auxiliary.h:86
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:166
CCAMERA & CameraGet() const
CameraGet - get current camera in use.
Definition: cinfo3d_visu.h:210
unsigned GetRunningMicroSecs()
Function GetRunningMicroSecs An alternate way to calculate an elapset time (in microsecondes) to clas...
virtual void Pan(const wxPoint &aNewMousePosition)=0
glm::vec3 SFVEC3F
Definition: xv3d_types.h:47
void OnPaint(wxPaintEvent &event)
static const wxChar * m_logTrace
Trace mask used to enable or disable the trace output of this class.
void OnRightClick(wxMouseEvent &event)
void OnTimerTimeout_Editing(wxTimerEvent &event)
void DestroyCtx(wxGLContext *aContext)
Function DestroyCtx destroys a managed OpenGL context.
Some functions to handle hotkeys in KiCad.
wxGLContext * CreateCtx(wxGLCanvas *aCanvas, const wxGLContext *aOther=NULL)
Function CreateCtx creates a managed OpenGL context.
void SetFlag(DISPLAY3D_FLG aFlag, bool aState)
SetFlag - set the status of a flag.
virtual void SetCurWindowSize(const wxSize &aSize)=0
SetCurWindowSize - Before each render, the canvas will tell the render what is the size of its window...
struct EDA_HOTKEY_CONFIG g_3DViewer_Hokeys_Descr[]
EVT_MENU_RANGE(ID_POPUP_3D_VIEW_START, ID_POPUP_3D_VIEW_END, EDA_3D_CANVAS::OnPopUpMenu) EDA_3D_CANVAS
void ReloadRequest()
ReloadRequest - !TODO: this must be reviewed to add flags to improve specific render.
void RotateZ_T1(float aAngleInRadians)
Definition: ccamera.cpp:529
void SetBoard(BOARD *aBoard)
SetBoard - Set current board to be rendered.
Definition: cinfo3d_visu.h:122
wxTimer m_redraw_trigger_timer
This timer will be used to schedule a redraw event.
static const float m_delta_move_step_factor
Step factor to used with cursor on relation to the current zoom.
void render_pivot(float t, float aScale)
render_pivot - render the pivot cursor
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_UNDEFINED) override
Function Report is a pure virtual function to override in the derived object.
void SetInterpolateMode(CAMERA_INTERPOLATION aInterpolateMode)
Definition: ccamera.h:198
#define min(a, b)
Definition: auxiliary.h:85
bool ParametersChanged()
Function ParametersChanged.
Definition: ccamera.cpp:567
void Set3DCacheManager(S3D_CACHE *aCachePointer)
Set3DCacheManager - Update the Cache manager pointer.
Definition: cinfo3d_visu.h:82
const CBBOX & GetBBox3DU() const
GetBBox3DU - Get the bbox of the pcb board.
Definition: cinfo3d_visu.h:147
bool m_render_raytracing_was_requested
Flags that the user requested the current view to be render with raytracing.