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-2019 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 #include <gl_utils.h>
32 #include <wx/tokenzr.h>
33 
34 #include "../common_ogl/ogl_utils.h"
35 #include "eda_3d_canvas.h"
36 #include <eda_3d_viewer.h>
39 #include <3d_viewer_id.h>
40 #include <class_board.h>
41 #include <reporter.h>
42 #include <gl_context_mgr.h>
43 #include <profile.h> // To use GetRunningMicroSecs or another profiling utility
44 #include <bitmaps.h>
45 #include <menus_helpers.h>
46 #include <pgm_base.h>
48 #include <tool/tool_dispatcher.h>
49 
51 
52 
60 const wxChar * EDA_3D_CANVAS::m_logTrace = wxT( "KI_TRACE_EDA_3D_CANVAS" );
61 
63 
64 
65 BEGIN_EVENT_TABLE( EDA_3D_CANVAS, wxGLCanvas )
66  EVT_PAINT( EDA_3D_CANVAS::OnPaint )
67 
68  // mouse events
69  EVT_LEFT_DOWN( EDA_3D_CANVAS::OnLeftDown )
70  EVT_LEFT_UP( EDA_3D_CANVAS::OnLeftUp )
71  EVT_MIDDLE_UP( EDA_3D_CANVAS::OnMiddleUp )
73  EVT_MOUSEWHEEL( EDA_3D_CANVAS::OnMouseWheel )
74  EVT_MOTION( EDA_3D_CANVAS::OnMouseMove )
75 
76 #if wxCHECK_VERSION( 3, 1, 0 ) || defined( USE_OSX_MAGNIFY_EVENT )
77  EVT_MAGNIFY( EDA_3D_CANVAS::OnMagnify )
78 #endif
79 
80  // other events
81  EVT_ERASE_BACKGROUND( EDA_3D_CANVAS::OnEraseBackground )
82 
83  EVT_CLOSE( EDA_3D_CANVAS::OnCloseWindow )
84  EVT_SIZE( EDA_3D_CANVAS::OnResize )
85 END_EVENT_TABLE()
86 
87 
88 EDA_3D_CANVAS::EDA_3D_CANVAS( wxWindow* aParent, const int* aAttribList, BOARD* aBoard,
89  BOARD_ADAPTER& aBoardAdapter, CCAMERA& aCamera,
90  S3D_CACHE* a3DCachePointer )
91  : HIDPI_GL_CANVAS( aParent, wxID_ANY, aAttribList, wxDefaultPosition, wxDefaultSize,
92  wxFULL_REPAINT_ON_RESIZE ),
93  m_eventDispatcher( nullptr ),
94  m_parentStatusBar( nullptr ),
95  m_parentInfoBar( nullptr ),
96  m_glRC( nullptr ),
97  m_is_opengl_initialized( false ),
98  m_mouse_is_moving( false ),
99  m_mouse_was_moved( false ),
100  m_camera_is_moving( false ),
101  m_render_pivot( false ),
102  m_camera_moving_speed( 1.0f ),
103  m_strtime_camera_movement( 0 ),
104  m_animation_enabled( true ),
105  m_moving_speed_multiplier( 3 ),
106  m_boardAdapter( aBoardAdapter ),
107  m_camera( aCamera ),
108  m_3d_render( nullptr ),
109  m_opengl_supports_raytracing( false ),
110  m_render_raytracing_was_requested( false )
111 {
112  wxLogTrace( m_logTrace, "EDA_3D_CANVAS::EDA_3D_CANVAS" );
113 
114  m_editing_timeout_timer.SetOwner( this );
115  Connect( m_editing_timeout_timer.GetId(),
116  wxEVT_TIMER,
117  wxTimerEventHandler( EDA_3D_CANVAS::OnTimerTimeout_Editing ),
118  NULL,
119  this );
120 
121  m_redraw_trigger_timer.SetOwner( this );
122  Connect( m_redraw_trigger_timer.GetId(),
123  wxEVT_TIMER,
124  wxTimerEventHandler( EDA_3D_CANVAS::OnTimerTimeout_Redraw ),
125  NULL,
126  this );
127 
128  m_3d_render_raytracing = new C3D_RENDER_RAYTRACING( m_boardAdapter, m_camera );
129  m_3d_render_ogl_legacy = new C3D_RENDER_OGL_LEGACY( m_boardAdapter, m_camera );
130 
131  wxASSERT( m_3d_render_raytracing != NULL );
132  wxASSERT( m_3d_render_ogl_legacy != NULL );
133 
134  auto busy_indicator_factory = []() { return std::make_unique<WX_BUSY_INDICATOR>(); };
135 
136  m_3d_render_raytracing->SetBusyIndicatorFactory( busy_indicator_factory );
137  m_3d_render_ogl_legacy->SetBusyIndicatorFactory( busy_indicator_factory );
138 
139  RenderEngineChanged();
140 
141  wxASSERT( aBoard != NULL );
142  m_boardAdapter.SetBoard( aBoard );
143 
144  m_boardAdapter.SetColorSettings( Pgm().GetSettingsManager().GetColorSettings() );
145 
146  wxASSERT( a3DCachePointer != NULL );
147  m_boardAdapter.Set3DCacheManager( a3DCachePointer );
148 
149  const wxEventType events[] =
150  {
151  // Binding both EVT_CHAR and EVT_CHAR_HOOK ensures that all key events,
152  // especially special key like arrow keys, are handled by the GAL event dispatcher,
153  // and not sent to GUI without filtering, because they have a default action (scroll)
154  // that must not be called.
155  wxEVT_LEFT_UP, wxEVT_LEFT_DOWN, wxEVT_LEFT_DCLICK,
156  wxEVT_RIGHT_UP, wxEVT_RIGHT_DOWN, wxEVT_RIGHT_DCLICK,
157  wxEVT_MIDDLE_UP, wxEVT_MIDDLE_DOWN, wxEVT_MIDDLE_DCLICK,
158  wxEVT_MOTION, wxEVT_MOUSEWHEEL, wxEVT_CHAR, wxEVT_CHAR_HOOK,
159 #if wxCHECK_VERSION( 3, 1, 0 ) || defined( USE_OSX_MAGNIFY_EVENT )
160  wxEVT_MAGNIFY,
161 #endif
162  wxEVT_MENU_OPEN, wxEVT_MENU_CLOSE, wxEVT_MENU_HIGHLIGHT
163  };
164 
165  for( wxEventType eventType : events )
166  Connect( eventType, wxEventHandler( EDA_3D_CANVAS::OnEvent ), NULL, m_eventDispatcher );
167 }
168 
169 
171 {
172  wxLogTrace( m_logTrace, "EDA_3D_CANVAS::~EDA_3D_CANVAS" );
173 
174  releaseOpenGL();
175 }
176 
177 
179 {
180  if( m_glRC )
181  {
183 
184  delete m_3d_render_raytracing;
186 
187  delete m_3d_render_ogl_legacy;
189 
190  // This is just a copy of a pointer, can safely be set to NULL
191  m_3d_render = NULL;
192 
195  m_glRC = NULL;
196  }
197 }
198 
199 
200 void EDA_3D_CANVAS::OnCloseWindow( wxCloseEvent &event )
201 {
202  releaseOpenGL();
203 
204  event.Skip();
205 }
206 
207 
208 void EDA_3D_CANVAS::OnResize( wxSizeEvent &event )
209 {
210  this->Request_refresh();
211 }
212 
213 
215 {
216  wxLogTrace( m_logTrace, "EDA_3D_CANVAS::initializeOpenGL" );
217 
218  const GLenum err = glewInit();
219 
220  if( GLEW_OK != err )
221  {
222  const wxString msgError = (const char*) glewGetErrorString( err );
223 
224  wxLogMessage( msgError );
225 
226  return false;
227  }
228  else
229  {
230  wxLogTrace( m_logTrace, "EDA_3D_CANVAS::initializeOpenGL Using GLEW version %s",
231  FROM_UTF8( (char*) glewGetString( GLEW_VERSION ) ) );
232  }
233 
234  wxString version = FROM_UTF8( (char *) glGetString( GL_VERSION ) );
235 
236  wxLogTrace( m_logTrace, "EDA_3D_CANVAS::%s OpenGL version string %s.",
237  __WXFUNCTION__, version );
238 
239  // Extract OpenGL version from string. This method is used because prior to OpenGL 2,
240  // getting the OpenGL major and minor version as integers didn't exist.
241  wxString tmp;
242 
243  wxStringTokenizer tokenizer( version );
244 
246 
247  if( tokenizer.HasMoreTokens() )
248  {
249  long major = 0;
250  long minor = 0;
251 
252  tmp = tokenizer.GetNextToken();
253 
254  tokenizer.SetString( tmp, wxString( "." ) );
255 
256  if( tokenizer.HasMoreTokens() )
257  tokenizer.GetNextToken().ToLong( &major );
258 
259  if( tokenizer.HasMoreTokens() )
260  tokenizer.GetNextToken().ToLong( &minor );
261 
262  if( major < 2 || ( (major == 2 ) && (minor < 1) ) )
263  {
264  wxLogTrace( m_logTrace, "EDA_3D_CANVAS::%s OpenGL ray tracing not supported.",
265  __WXFUNCTION__ );
266 
267  if( GetParent() )
268  {
269  wxCommandEvent evt( wxEVT_MENU, ID_DISABLE_RAY_TRACING );
270  GetParent()->ProcessWindowEvent( evt );
271  }
272 
274  }
275  }
276 
279 
280  return true;
281 }
282 
283 
284 void EDA_3D_CANVAS::GetScreenshot( wxImage &aDstImage )
285 {
286  OGL_GetScreenshot( aDstImage );
287 }
288 
289 
290 void EDA_3D_CANVAS::ReloadRequest( BOARD *aBoard , S3D_CACHE *aCachePointer )
291 {
292  if( aCachePointer != NULL )
293  m_boardAdapter.Set3DCacheManager( aCachePointer );
294 
295  if( aBoard != NULL )
296  m_boardAdapter.SetBoard( aBoard );
297 
298  m_boardAdapter.SetColorSettings( Pgm().GetSettingsManager().GetColorSettings() );
299 
300  if( m_3d_render )
302 }
303 
304 
306 {
308 
309  if( m_3d_render )
311 
313  //m_mouse_was_moved = true;
314 
315  Request_refresh();
316 }
317 
318 
320 {
321  if( m_parentStatusBar )
322  {
323  wxString msg;
324 
325  msg.Printf( "dx %3.2f", m_camera.GetCameraPos().x );
326  m_parentStatusBar->SetStatusText( msg, static_cast<int>( EDA_3D_VIEWER_STATUSBAR::X_POS ) );
327 
328  msg.Printf( "dy %3.2f", m_camera.GetCameraPos().y );
329  m_parentStatusBar->SetStatusText( msg, static_cast<int>( EDA_3D_VIEWER_STATUSBAR::Y_POS ) );
330  }
331 }
332 
333 
334 void EDA_3D_CANVAS::OnPaint( wxPaintEvent &event )
335 {
336  // Please have a look at:
337  // https://lists.launchpad.net/kicad-developers/msg25149.html
338  // wxPaintDC( this );
339  // event.Skip( false );
340 
341  // SwapBuffer requires the window to be shown before calling
342  if( !IsShownOnScreen() )
343  {
344  wxLogTrace( m_logTrace, "EDA_3D_CANVAS::OnPaint !IsShown" );
345  return;
346  }
347 
348  // Because the board to draw is handled by the parent viewer frame,
349  // ensure this parent is still alive. When it is closed before the viewer
350  // frame, a paint event can be generated after the parent is closed,
351  // therefore with invalid board.
352  // This is dependent of the platform.
353  // Especially on OSX, but also on Windows, it frequently happens
354  if( !GetParent()->GetParent()->IsShown() )
355  return; // The parent board editor frame is no more alive
356 
357  wxString err_messages;
358 
359  // !TODO: implement error reporter
360  //WX_STRING_REPORTER errorReporter( &err_messages );
361  INFOBAR_REPORTER warningReporter( m_parentInfoBar );
362  STATUSBAR_REPORTER activityReporter(
364 
365  unsigned strtime = GetRunningMicroSecs();
366 
367  // "Makes the OpenGL state that is represented by the OpenGL rendering
368  // context context current, i.e. it will be used by all subsequent OpenGL calls.
369  // This function may only be called when the window is shown on screen"
370 
371  // Explicitly create a new rendering context instance for this canvas.
372  if( m_glRC == NULL )
374 
376 
377  // Set the OpenGL viewport according to the client size of this canvas.
378  // This is done here rather than in a wxSizeEvent handler because our
379  // OpenGL rendering context (and thus viewport setting) is used with
380  // multiple canvases: If we updated the viewport in the wxSizeEvent
381  // handler, changing the size of one canvas causes a viewport setting that
382  // is wrong when next another canvas is repainted.
383  wxSize clientSize = GetNativePixelSize();
384 
385  const bool windows_size_changed = m_camera.SetCurWindowSize( clientSize );
386 
387  // Initialize openGL if need
388  // /////////////////////////////////////////////////////////////////////////
390  {
391  if( !initializeOpenGL() )
392  {
394 
395  return;
396  }
397  }
398 
399  // Don't attend to ray trace if OpenGL doesn't support it.
401  {
405  }
406 
407  // Check if a raytacing was requested and need to switch to raytracing mode
409  {
410  const bool was_camera_changed = m_camera.ParametersChanged();
411 
412  // It reverts back to OpenGL mode if it was requested a raytracing
413  // render of the current scene. AND the mouse / camera is moving
414  if( ( m_mouse_is_moving ||
416  was_camera_changed ||
417  windows_size_changed ) &&
419  {
422  }
423  }
424 
425  float curtime_delta_s = 0.0f;
426 
427  if( m_camera_is_moving )
428  {
429  const unsigned curtime_delta = GetRunningMicroSecs() - m_strtime_camera_movement;
430  curtime_delta_s = (curtime_delta / 1e6) * m_camera_moving_speed;
431  m_camera.Interpolate( curtime_delta_s );
432 
433  if( curtime_delta_s > 1.0f )
434  {
435  m_render_pivot = false;
436  m_camera_is_moving = false;
437  m_mouse_was_moved = true;
438 
440  DisplayStatus();
441  }
442  else
443  {
444  Request_refresh();
445  }
446  }
447 
448  // It will return true if the render request a new redraw
449  bool requested_redraw = false;
450 
451  if( m_3d_render )
452  {
453  m_3d_render->SetCurWindowSize( clientSize );
454 
455  requested_redraw = m_3d_render->Redraw( m_mouse_was_moved || m_camera_is_moving,
456  &activityReporter, &warningReporter );
457  }
458 
459  if( m_render_pivot )
460  {
461  const float scale = glm::min( m_camera.ZoomGet(), 1.0f );
462  render_pivot( curtime_delta_s, scale * scale );
463  }
464 
465  // "Swaps the double-buffer of this window, making the back-buffer the
466  // front-buffer and vice versa, so that the output of the previous OpenGL
467  // commands is displayed on the window."
468  SwapBuffers();
469 
471 
472  if( !activityReporter.HasMessage() )
473  {
475  {
476  // Calculation time in milliseconds
477  const double calculation_time = (double)( GetRunningMicroSecs() - strtime) / 1e3;
478 
479  activityReporter.Report( wxString::Format( _( "Render time %.0f ms ( %.1f fps)" ),
480  calculation_time, 1000.0 / calculation_time ) );
481  }
482  }
483 
484  // This will reset the flag of camera parameters changed
486 
487  warningReporter.Finalize();
488 
489  if( !err_messages.IsEmpty() )
490  wxLogMessage( err_messages );
491 
492  if( (!m_camera_is_moving) && requested_redraw )
493  {
494  m_mouse_was_moved = false;
495  Request_refresh( false );
496  }
497 }
498 
499 
501 {
502  m_eventDispatcher = aEventDispatcher;
503 
504  if( m_eventDispatcher )
505  {
506  m_parent->Connect( wxEVT_TOOL,
507  wxCommandEventHandler( TOOL_DISPATCHER::DispatchWxCommand ),
509  }
510  else
511  {
512  // While loop is used to be sure that all event handlers are removed.
513  while( m_parent->Disconnect( wxEVT_TOOL,
514  wxCommandEventHandler( TOOL_DISPATCHER::DispatchWxCommand ),
515  NULL, m_eventDispatcher ) );
516  }
517 }
518 
519 
520 void EDA_3D_CANVAS::OnEvent( wxEvent& aEvent )
521 {
522  if( !m_eventDispatcher )
523  aEvent.Skip();
524  else
526 
527  Refresh();
528 }
529 
530 
531 void EDA_3D_CANVAS::OnEraseBackground( wxEraseEvent &event )
532 {
533  wxLogTrace( m_logTrace, "EDA_3D_CANVAS::OnEraseBackground" );
534  // Do nothing, to avoid flashing.
535 }
536 
537 
538 void EDA_3D_CANVAS::OnMouseWheel( wxMouseEvent &event )
539 {
540  bool mouseActivity = false;
541 
542  wxLogTrace( m_logTrace, "EDA_3D_CANVAS::OnMouseWheel" );
543 
544  if( m_camera_is_moving )
545  return;
546 
547  float delta_move = m_delta_move_step_factor * m_camera.ZoomGet();
548 
550  delta_move *= (0.01f * event.GetWheelRotation());
551  else
552  if( event.GetWheelRotation() < 0 )
553  delta_move = -delta_move;
554 
555  // mousewheel_panning enabled:
556  // wheel -> pan;
557  // wheel + shift -> horizontal scrolling;
558  // wheel + ctrl -> zooming;
559  // mousewheel_panning disabled:
560  // wheel + shift -> vertical scrolling;
561  // wheel + ctrl -> horizontal scrolling;
562  // wheel -> zooming.
563 
564  if( m_boardAdapter.GetFlag( FL_MOUSEWHEEL_PANNING ) && !event.ControlDown() )
565  {
566  if( event.GetWheelAxis() == wxMOUSE_WHEEL_HORIZONTAL || event.ShiftDown() )
567  m_camera.Pan( SFVEC3F( -delta_move, 0.0f, 0.0f ) );
568  else
569  m_camera.Pan( SFVEC3F( 0.0f, -delta_move, 0.0f ) );
570 
571  mouseActivity = true;
572  }
573  else if( event.ShiftDown() && !m_boardAdapter.GetFlag( FL_MOUSEWHEEL_PANNING ) )
574  {
575  m_camera.Pan( SFVEC3F( 0.0f, -delta_move, 0.0f ) );
576  mouseActivity = true;
577  }
578  else if( event.ControlDown() && !m_boardAdapter.GetFlag( FL_MOUSEWHEEL_PANNING ) )
579  {
580  m_camera.Pan( SFVEC3F( delta_move, 0.0f, 0.0f ) );
581  mouseActivity = true;
582  }
583  else
584  {
585  mouseActivity = m_camera.Zoom( event.GetWheelRotation() > 0 ? 1.1f : 1/1.1f );
586  }
587 
588  // If it results on a camera movement
589  if( mouseActivity )
590  {
591  DisplayStatus();
592  Request_refresh();
593 
594  m_mouse_is_moving = true;
595  m_mouse_was_moved = true;
596 
598  }
599 
600  // Update the cursor current mouse position on the camera
601  m_camera.SetCurMousePosition( event.GetPosition() );
602 }
603 
604 
605 #if wxCHECK_VERSION( 3, 1, 0 ) || defined( USE_OSX_MAGNIFY_EVENT )
606 void EDA_3D_CANVAS::OnMagnify( wxMouseEvent& event )
607 {
608  SetFocus();
609 
610  if( m_camera_is_moving )
611  return;
612 
613  //m_is_moving_mouse = true;
615 
616  float magnification = ( event.GetMagnification() + 1.0f );
617 
618  m_camera.Zoom( magnification );
619 
620  DisplayStatus();
621  Request_refresh();
622 }
623 #endif
624 
625 
626 void EDA_3D_CANVAS::OnMouseMove( wxMouseEvent &event )
627 {
628  //wxLogTrace( m_logTrace, wxT( "EDA_3D_CANVAS::OnMouseMove" ) );
629 
630  if( m_camera_is_moving )
631  return;
632 
634 
635  if( event.Dragging() )
636  {
637  if( event.LeftIsDown() ) // Drag
638  m_camera.Drag( event.GetPosition() );
639  else if( event.MiddleIsDown() ) // Pan
640  m_camera.Pan( event.GetPosition() );
641 
642  m_mouse_is_moving = true;
643  m_mouse_was_moved = true;
644 
645  // orientation has changed, redraw mesh
646  DisplayStatus();
647  Request_refresh();
648  }
649 
650  const wxPoint eventPosition = event.GetPosition();
651  m_camera.SetCurMousePosition( eventPosition );
652 }
653 
654 
655 void EDA_3D_CANVAS::OnLeftDown( wxMouseEvent &event )
656 {
657  SetFocus();
659 }
660 
661 
662 void EDA_3D_CANVAS::OnLeftUp( wxMouseEvent &event )
663 {
664  if( m_camera_is_moving )
665  return;
666 
667  if( m_mouse_is_moving )
668  {
669  m_mouse_is_moving = false;
671  }
672 }
673 
674 
675 void EDA_3D_CANVAS::OnMiddleDown( wxMouseEvent &event )
676 {
677  SetFocus();
679 }
680 
681 
682 void EDA_3D_CANVAS::OnMiddleUp( wxMouseEvent &event )
683 {
684  if( m_camera_is_moving )
685  return;
686 
687  if( m_mouse_is_moving )
688  {
689  m_mouse_is_moving = false;
691  }
692  else
693  {
695  }
696 }
697 
698 
699 void EDA_3D_CANVAS::OnTimerTimeout_Editing( wxTimerEvent &event )
700 {
701  (void)event;
702 
703  m_mouse_is_moving = false;
704  m_mouse_was_moved = false;
705 
706  Request_refresh();
707 }
708 
709 
711 {
713 }
714 
715 
717 {
718  m_editing_timeout_timer.Start( m_3d_render->GetWaitForEditingTimeOut() , wxTIMER_ONE_SHOT );
719 }
720 
721 
722 void EDA_3D_CANVAS::OnTimerTimeout_Redraw( wxTimerEvent &event )
723 {
724  wxPaintEvent redrawEvent;
725  wxPostEvent( this, redrawEvent );
726 }
727 
728 
729 void EDA_3D_CANVAS::Request_refresh( bool aRedrawImmediately )
730 {
731  if( aRedrawImmediately )
732  {
733  // On some systems, just calling Refresh does not work always
734  // (Issue experienced on Win7 MSYS2)
735  //Refresh();
736  //Update();
737 
738  // Using PostEvent will take priority to other events like mouse movements, keys, etc.
739  wxPaintEvent redrawEvent;
740  wxPostEvent( this, redrawEvent );
741 
742  // This behaves the same
743  // wxQueueEvent( this,
744  // From wxWidget documentation: "The heap-allocated and
745  // non-NULL event to queue, the function takes ownership of it."
746  // new wxPaintEvent()
747  // );
748  }
749  else
750  {
751  // Schedule a timed redraw
752  m_redraw_trigger_timer.Start( 10 , wxTIMER_ONE_SHOT );
753  }
754 }
755 
756 
757 void EDA_3D_CANVAS::request_start_moving_camera( float aMovingSpeed, bool aRenderPivot )
758 {
759  wxASSERT( aMovingSpeed > FLT_EPSILON );
760 
761  // Fast forward the animation if the animation is disabled
762  if( !m_animation_enabled )
763  {
764  m_camera.Interpolate( 1.0f );
765  DisplayStatus();
766  Request_refresh();
767  return;
768  }
769 
770  // Map speed multipler option to actual multiplier value
771  // [1,2,3,4,5] -> [0.25, 0.5, 1, 2, 4]
772  aMovingSpeed *= ( 1 << m_moving_speed_multiplier ) / 8.0f;
773 
774  m_render_pivot = aRenderPivot;
775  m_camera_moving_speed = aMovingSpeed;
776 
778 
779  DisplayStatus();
780  Request_refresh();
781 
782  m_camera_is_moving = true;
783 
785 }
786 
787 
789 {
790  SFVEC3F rayOrigin;
791  SFVEC3F rayDir;
792 
793  // Generate a ray origin and direction based on current mouser position and camera
794  m_camera.MakeRayAtCurrrentMousePosition( rayOrigin, rayDir );
795 
796  RAY mouseRay;
797  mouseRay.Init( rayOrigin, rayDir );
798 
799  float hit_t;
800 
801  // Test it with the board bounding box
802  if( m_boardAdapter.GetBBox3DU().Intersect( mouseRay, &hit_t ) )
803  {
806  m_camera.SetLookAtPos_T1( mouseRay.at( hit_t ) );
808 
810  }
811 }
812 
813 
814 bool EDA_3D_CANVAS::SetView3D( int aKeycode )
815 {
816  if( m_camera_is_moving )
817  return false;
818 
819  const float delta_move = m_delta_move_step_factor * m_camera.ZoomGet();
820  const float arrow_moving_time_speed = 8.0f;
821  bool handled = false;
822 
823  switch( aKeycode )
824  {
825  case WXK_SPACE:
827  return true;
828 
829  case WXK_LEFT:
832  m_camera.Pan_T1( SFVEC3F( -delta_move, 0.0f, 0.0f ) );
833  request_start_moving_camera( arrow_moving_time_speed, false );
834  return true;
835 
836  case WXK_RIGHT:
839  m_camera.Pan_T1( SFVEC3F( +delta_move, 0.0f, 0.0f ) );
840  request_start_moving_camera( arrow_moving_time_speed, false );
841  return true;
842 
843  case WXK_UP:
846  m_camera.Pan_T1( SFVEC3F( 0.0f, +delta_move, 0.0f ) );
847  request_start_moving_camera( arrow_moving_time_speed, false );
848  return true;
849 
850  case WXK_DOWN:
853  m_camera.Pan_T1( SFVEC3F( 0.0f, -delta_move, 0.0f ) );
854  request_start_moving_camera( arrow_moving_time_speed, false );
855  return true;
856 
857  case WXK_HOME:
860  m_camera.Reset_T1();
861  request_start_moving_camera( glm::min( glm::max( m_camera.ZoomGet(), 1/1.26f ), 1.26f ) );
862  return true;
863 
864  case WXK_END:
865  break;
866 
867  case WXK_TAB:
870  m_camera.RotateZ_T1( glm::radians( 45.0f ) );
872  handled = true;
873  break;
874 
875  case WXK_F1:
878 
879  if( m_camera.Zoom_T1( 1.26f ) ) // 3 steps per doubling
881 
882  return true;
883 
884  case WXK_F2:
887 
888  if( m_camera.Zoom_T1( 1/1.26f ) ) // 3 steps per halving
890 
891  return true;
892 
893  case ID_VIEW3D_RESET:
896  m_camera.Reset_T1();
897  request_start_moving_camera( glm::min( glm::max( m_camera.ZoomGet(), 0.5f ), 1.125f ) );
898  return true;
899 
900  case ID_VIEW3D_RIGHT:
903  m_camera.Reset_T1();
904  m_camera.RotateZ_T1( glm::radians( -90.0f ) );
905  m_camera.RotateX_T1( glm::radians( -90.0f ) );
907  return true;
908 
909  case ID_VIEW3D_LEFT:
912  m_camera.Reset_T1();
913  m_camera.RotateZ_T1( glm::radians( 90.0f ) );
914  m_camera.RotateX_T1( glm::radians( -90.0f ) );
916  return true;
917 
918  case ID_VIEW3D_FRONT:
921  m_camera.Reset_T1();
922  m_camera.RotateX_T1( glm::radians( -90.0f ) );
924  return true;
925 
926  case ID_VIEW3D_BACK:
929  m_camera.Reset_T1();
930  m_camera.RotateX_T1( glm::radians( -90.0f ) );
931  // The rotation angle should be 180.
932  // We use 179.999 (180 - epsilon) to avoid a full 360 deg rotation when
933  // using 180 deg if the previous rotated position was already 180 deg
934  m_camera.RotateZ_T1( glm::radians( 179.999f ) );
936  return true;
937 
938  case ID_VIEW3D_TOP:
941  m_camera.Reset_T1();
942  request_start_moving_camera( glm::min( glm::max( m_camera.ZoomGet(), 0.5f ), 1.125f ) );
943  return true;
944 
945  case ID_VIEW3D_BOTTOM:
948  m_camera.Reset_T1();
949  m_camera.RotateY_T1( glm::radians( 179.999f ) ); // Rotation = 180 - epsilon
950  request_start_moving_camera( glm::min( glm::max( m_camera.ZoomGet(), 0.5f ), 1.125f ) );
951  return true;
952 
953  case ID_VIEW3D_FLIP:
956  m_camera.RotateY_T1( glm::radians( 179.999f ) );
958  return true;
959 
960  default:
961  return false;
962  }
963 
964  m_mouse_was_moved = true;
965 
967 
968  DisplayStatus();
969  Request_refresh();
970 
971  return handled;
972 }
973 
974 
976 {
977  switch( m_boardAdapter.RenderEngineGet() )
978  {
981  default: m_3d_render = NULL; break;
982  }
983 
984  if( m_3d_render )
986 
987  m_mouse_was_moved = false;
988 
989  Request_refresh();
990 }
C3D_RENDER_RAYTRACING * m_3d_render_raytracing
STATUSBAR_REPORTER is a wrapper for reporting to a specific text location in a statusbar.
Definition: reporter.h:258
void SetLookAtPos_T1(const SFVEC3F &aLookAtPos)
Definition: ccamera.h:128
virtual wxSize GetNativePixelSize() const
void OGL_GetScreenshot(wxImage &aDstImage)
OGL_GetScreenshot - got the pixel data of current OpenGL image.
Definition: ogl_utils.cpp:36
bool GetFlag(DISPLAY3D_FLG aFlag) const
GetFlag - get a configuration status of a flag.
void OnMouseMove(wxMouseEvent &event)
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:104
void OnResize(wxSizeEvent &event)
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:114
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:555
bool m_mouse_is_moving
void ResetXYpos_T1()
Definition: ccamera.cpp:412
unsigned m_strtime_camera_movement
WX_INFOBAR * m_parentInfoBar
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:40
wxStatusBar * m_parentStatusBar
virtual bool Redraw(bool aIsMoving, REPORTER *aStatusReporter=NULL, REPORTER *aWarningReporter=NULL)=0
Redraw - Ask to redraw the view.
Class CCAMERA is a virtual class used to derive CCAMERA objects from.
Definition: ccamera.h:79
wxGLContext * m_glRC
const CBBOX & GetBBox3DU() const noexcept
GetBBox3DU - Get the bbox of the pcb board.
void OnCloseWindow(wxCloseEvent &event)
wxGLCanvas wrapper for HiDPI/Retina support.
Implement a canvas based on a wxGLCanvas.
Definition: eda_3d_canvas.h:47
wxTimer m_editing_timeout_timer
static const wxChar * m_logTrace
Trace mask used to enable or disable the trace output of this class.
Definition: ray.h:67
void DisplayStatus()
DisplayStatus - Update the status bar with the position information.
C3D_RENDER_OGL_LEGACY * m_3d_render_ogl_legacy
S3D_CACHE.
Definition: 3d_cache.h:54
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 RenderEngineSet(RENDER_ENGINE aRenderEngine) noexcept
RenderEngineSet.
SFVEC3F at(float t) const
Definition: ray.h:89
void SetCurMousePosition(const wxPoint &aPosition)
It updates the current mouse position without make any new recalculations on camera.
Definition: ccamera.cpp:431
void SetBoard(BOARD *aBoard) noexcept
SetBoard - Set current board to be rendered.
BOARD_ADAPTER & m_boardAdapter
bool Zoom(float aFactor)
Definition: ccamera.cpp:474
void OnLeftDown(wxMouseEvent &event)
float m_camera_moving_speed
static GL_CONTEXT_MANAGER & Get()
Function Get returns the GL_CONTEXT_MANAGER instance (singleton).
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 SetEventDispatcher(TOOL_DISPATCHER *aEventDispatcher)
Function SetEventDispatcher() Sets a dispatcher that processes events and forwards them to tools.
float ZoomGet() const
Definition: ccamera.cpp:510
#define NULL
void OnMiddleDown(wxMouseEvent &event)
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
CCAMERA & m_camera
bool m_mouse_was_moved
void OnEraseBackground(wxEraseEvent &event)
void OnMouseWheel(wxMouseEvent &event)
void RenderEngineChanged()
RenderEngineChanged - Notify that the render engine was changed.
virtual void DispatchWxEvent(wxEvent &aEvent)
Function DispatchWxEvent() Processes wxEvents (mostly UI events), translates them to TOOL_EVENTs,...
bool m_camera_is_moving
virtual void Interpolate(float t)
Interpolate - It will update the matrix to interpolate between T0 and T1 values.
Definition: ccamera.cpp:569
virtual void Drag(const wxPoint &aNewMousePosition)=0
Calculate a new mouse drag position.
EVT_MIDDLE_DOWN(mpWindow::OnMouseMiddleDown) EVT_MOUSEWHEEL(mpWindow
Definition: mathplot.cpp:1683
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Function Report is a pure virtual function to override in the derived object.
Definition: reporter.cpp:146
RENDER_ENGINE RenderEngineGet() const noexcept
RenderEngineGet.
void Finalize()
Update the infobar with the reported text.
Definition: reporter.cpp:179
bool SetCurWindowSize(const wxSize &aSize)
SetCurWindowSize - update the windows size of the camera.
Definition: ccamera.cpp:448
void OnTimerTimeout_Redraw(wxTimerEvent &event)
bool Zoom_T1(float aFactor)
Definition: ccamera.cpp:493
virtual void Pan_T1(const SFVEC3F &aDeltaOffsetInc)=0
void RotateX_T1(float aAngleInRadians)
Definition: ccamera.cpp:537
SETTINGS_MANAGER * GetSettingsManager()
virtual void Reset_T1()
Definition: ccamera.cpp:103
bool m_animation_enabled
TOOL_DISPATCHER.
void LockCtx(wxGLContext *aContext, wxGLCanvas *aCanvas)
Function LockCtx sets a context as current and prevents other canvases from switching it.
void Set3DCacheManager(S3D_CACHE *aCachePointer) noexcept
Set3DCacheManager - Update the Cache manager pointer.
Definition: board_adapter.h:80
bool m_is_opengl_initialized
C3D_RENDER_BASE * m_3d_render
void OnMiddleUp(wxMouseEvent &event)
The C3D_RENDER_OGL_LEGACY class render the board using openGL legacy mode.
TOOL_DISPATCHER * m_eventDispatcher
void GetScreenshot(wxImage &aDstImage)
Request a screenshot and output it to the aDstImage.
int m_moving_speed_multiplier
const int scale
see class PGM_BASE
bool initializeOpenGL()
initializeOpenGL
void OnEvent(wxEvent &aEvent)
Used to forward events to the canvas from popups, etc.
bool m_opengl_supports_raytracing
Declaration of the eda_3d_viewer class.
void RotateY_T1(float aAngleInRadians)
Definition: ccamera.cpp:543
void MakeRayAtCurrrentMousePosition(SFVEC3F &aOutOrigin, SFVEC3F &aOutDirection) const
MakeRayAtCurrrentMousePosition - Make a ray based on the latest mouse position.
Definition: ccamera.cpp:380
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
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:180
virtual void DispatchWxCommand(wxCommandEvent &aEvent)
Function DispatchWxCommand() Processes wxCommands (mostly menu related events) and runs appropriate a...
#define _(s)
Definition: 3d_actions.cpp:33
bool Intersect(const RAY &aRay, float *t) const
Function Intersect.
Definition: cbbox_ray.cpp:46
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
bool SetView3D(int aKeycode)
SetView3D - Helper function to call view commands.
void OnPaint(wxPaintEvent &event)
void OnTimerTimeout_Editing(wxTimerEvent &event)
const SFVEC3F & GetCameraPos() const
Definition: ccamera.h:134
void DestroyCtx(wxGLContext *aContext)
Function DestroyCtx destroys a managed OpenGL context.
void SetColorSettings(COLOR_SETTINGS *aSettings) noexcept
wxGLContext * CreateCtx(wxGLCanvas *aCanvas, const wxGLContext *aOther=NULL)
Function CreateCtx creates a managed OpenGL context.
virtual void SetCurWindowSize(const wxSize &aSize)=0
SetCurWindowSize - Before each render, the canvas will tell the render what is the size of its window...
static int SetSwapInterval(int aVal)
Attempts to set the OpenGL swap interval.
Definition: gl_utils.h:41
void ReloadRequest()
ReloadRequest - !TODO: this must be reviewed to add flags to improve specific render.
void RotateZ_T1(float aAngleInRadians)
Definition: ccamera.cpp:549
wxTimer m_redraw_trigger_timer
Class BOARD_ADAPTER Helper class to handle information needed to display 3D board.
Definition: board_adapter.h:68
static const float m_delta_move_step_factor
void render_pivot(float t, float aScale)
render_pivot - render the pivot cursor
INFOBAR_REPORTER is a wrapper for reporting to a WX_INFOBAR UI element.
Definition: reporter.h:286
void SetInterpolateMode(CAMERA_INTERPOLATION aInterpolateMode)
Definition: ccamera.h:200
bool HasMessage() const override
Function HasMessage Returns true if the reporter client is non-empty.
Definition: reporter.cpp:154
bool ParametersChanged()
Function ParametersChanged.
Definition: ccamera.cpp:587
bool m_render_raytracing_was_requested