27#include <wx/tokenzr.h>
29#include "../common_ogl/ogl_utils.h"
75#if wxCHECK_VERSION( 3, 1, 0 ) || defined( USE_OSX_MAGNIFY_EVENT )
76 EVT_MAGNIFY( EDA_3D_CANVAS::OnMagnify )
92 wxDefaultSize, wxFULL_REPAINT_ON_RESIZE ),
93 m_eventDispatcher(
nullptr ),
94 m_parentStatusBar(
nullptr ),
95 m_parentInfoBar(
nullptr ),
97 m_is_opengl_initialized( false ),
98 m_is_opengl_version_supported( true ),
99 m_editing_timeout_timer( this, wxID_HIGHEST + 1 ),
100 m_redraw_trigger_timer( this, wxID_HIGHEST + 2 ),
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_3d_render(
nullptr ),
108 m_opengl_supports_raytracing( true ),
109 m_render_raytracing_was_requested( false ),
110 m_accelerator3DShapes(
nullptr ),
111 m_currentRollOverItem(
nullptr )
113 wxLogTrace( m_logTrace, wxT(
"EDA_3D_CANVAS::EDA_3D_CANVAS" ) );
115 m_editing_timeout_timer.SetOwner(
this );
116 Connect( m_editing_timeout_timer.GetId(), wxEVT_TIMER,
119 m_redraw_trigger_timer.SetOwner(
this );
120 Connect( m_redraw_trigger_timer.GetId(), wxEVT_TIMER,
123 m_is_currently_painting.clear();
126 m_3d_render_opengl =
new RENDER_3D_OPENGL(
this, m_boardAdapter, m_camera );
128 wxASSERT( m_3d_render_raytracing !=
nullptr );
129 wxASSERT( m_3d_render_opengl !=
nullptr );
131 auto busy_indicator_factory =
134 return std::make_unique<WX_BUSY_INDICATOR>();
137 m_3d_render_raytracing->SetBusyIndicatorFactory( busy_indicator_factory );
138 m_3d_render_opengl->SetBusyIndicatorFactory( busy_indicator_factory );
142 m_3d_render = m_3d_render_opengl;
144 m_boardAdapter.ReloadColorSettings();
146 wxASSERT( a3DCachePointer !=
nullptr );
147 m_boardAdapter.Set3dCacheManager( a3DCachePointer );
149 const wxEventType events[] =
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 )
162 wxEVT_MENU_OPEN, wxEVT_MENU_CLOSE, wxEVT_MENU_HIGHLIGHT
165 for( wxEventType eventType : events )
172 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::~EDA_3D_CANVAS" ) );
219 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::initializeOpenGL" ) );
221 const GLenum err = glewInit();
225 const wxString msgError = (
const char*) glewGetErrorString( err );
227 wxLogMessage( msgError );
233 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::initializeOpenGL Using GLEW version %s" ),
234 FROM_UTF8( (
char*) glewGetString( GLEW_VERSION ) ) );
237 wxString version =
FROM_UTF8( (
char *) glGetString( GL_VERSION ) );
239 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::%s OpenGL version string %s." ),
240 __WXFUNCTION__, version );
246 wxStringTokenizer tokenizer( version );
248 if( tokenizer.HasMoreTokens() )
253 tmp = tokenizer.GetNextToken();
255 tokenizer.SetString( tmp, wxString( wxT(
"." ) ) );
257 if( tokenizer.HasMoreTokens() )
258 tokenizer.GetNextToken().ToLong( &major );
260 if( tokenizer.HasMoreTokens() )
261 tokenizer.GetNextToken().ToLong( &minor );
263 if( major < 2 || ( ( major == 2 ) && ( minor < 1 ) ) )
265 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::%s OpenGL ray tracing not supported." ),
271 GetParent()->ProcessWindowEvent( evt );
277 if( ( major == 1 ) && ( minor < 5 ) )
279 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::%s OpenGL not supported." ),
301 if( aCachePointer !=
nullptr )
304 if( aBoard !=
nullptr )
334 m_parentStatusBar->SetStatusText( msg,
static_cast<int>( EDA_3D_VIEWER_STATUSBAR::X_POS ) );
337 m_parentStatusBar->SetStatusText( msg,
static_cast<int>( EDA_3D_VIEWER_STATUSBAR::Y_POS ) );
355 if( !IsShownOnScreen() )
357 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::DoRePaint !IsShown" ) );
368 if( !GetParent()->GetParent()->IsShown() )
371 wxString err_messages;
421 warningReporter.
Report(
_(
"Your OpenGL version is not supported. Minimum required "
430 glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
431 glClear( GL_COLOR_BUFFER_BIT );
457 || windows_size_changed )
465 float curtime_delta_s = 0.0f;
473 if( curtime_delta_s > 1.0f )
489 bool requested_redraw =
false;
497 bool reloadRaytracingForCalculations =
false;
502 reloadRaytracingForCalculations =
true;
506 &activityReporter, &warningReporter );
515 if( reloadRaytracingForCalculations )
518 catch( std::runtime_error& )
555 activityReporter.
Report( wxString::Format(
_(
"Last render time %.0f ms" ),
556 calculation_time ) );
565 if( !err_messages.IsEmpty() )
566 wxLogMessage( err_messages );
597 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::OnEraseBackground" ) );
604 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::OnMouseWheel" ) );
617#if wxCHECK_VERSION( 3, 1, 0 ) || defined( USE_OSX_MAGNIFY_EVENT )
618void EDA_3D_CANVAS::OnMagnify( wxMouseEvent& event )
628 float magnification = (
event.GetMagnification() + 1.0f );
662 return wxString::Format(
_(
"Net %s\tNet class %s" ),
663 aItem->GetNet()->GetNetname(),
664 aItem->GetNet()->GetNetClass()->GetName() );
679 switch( rollOverItem->
Type() )
683 PAD*
pad =
static_cast<PAD*
>( rollOverItem );
685 if( !
pad->GetNumber().IsEmpty() )
686 msg += wxString::Format(
_(
"Pad %s\t" ),
pad->GetNumber() );
688 if(
pad->IsOnCopperLayer() )
689 msg += printNetInfo(
pad );
706 msg += printNetInfo( track );
712 ZONE* zone =
static_cast<ZONE*
>( rollOverItem );
717 msg += wxString::Format(
_(
"Rule area %s\t" ), zone->
GetZoneName() );
719 msg += wxString::Format(
_(
"Zone %s\t" ), zone->
GetZoneName() );
723 msg += printNetInfo( zone );
742 reporter.
Report( wxEmptyString );
852 if( aRedrawImmediately )
859 wxPostEvent(
this, redrawEvent );
871 wxASSERT( aMovingSpeed > FLT_EPSILON );
925 const float arrow_moving_time_speed = 8.0f;
926 bool handled =
false;
1111 mouseRay.
Init( rayOrigin, rayDir );
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
Helper class to handle information needed to display 3D board.
const BBOX_3D & GetBBox() const noexcept
Get the board outling bounding box.
void ReloadColorSettings() noexcept
void SetBoard(BOARD *aBoard) noexcept
Set current board to be rendered.
EDA_3D_VIEWER_SETTINGS * m_Cfg
void Set3dCacheManager(S3D_CACHE *aCacheMgr) noexcept
Update the cache manager pointer.
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Information pertinent to a Pcbnew printed circuit board.
A class used to derive camera objects from.
void RotateY_T1(float aAngleInRadians)
bool Zoom_T1(float aFactor)
virtual void Pan_T1(const SFVEC3F &aDeltaOffsetInc)=0
virtual void Interpolate(float t)
It will update the matrix to interpolate between T0 and T1 values.
bool SetCurWindowSize(const wxSize &aSize)
Update the windows size of the camera.
void SetInterpolateMode(CAMERA_INTERPOLATION aInterpolateMode)
virtual void SetT0_and_T1_current_T()
This will set T0 and T1 with the current values.
void MakeRayAtCurrentMousePosition(SFVEC3F &aOutOrigin, SFVEC3F &aOutDirection) const
Make a ray based on the latest mouse position.
void RotateX_T1(float aAngleInRadians)
void SetLookAtPos_T1(const SFVEC3F &aLookAtPos)
const SFVEC3F & GetCameraPos() const
void RotateZ_T1(float aAngleInRadians)
Implement a canvas based on a wxGLCanvas.
RENDER_3D_OPENGL * m_3d_render_opengl
TOOL_DISPATCHER * m_eventDispatcher
void OnEvent(wxEvent &aEvent)
Used to forward events to the canvas from popups, etc.
unsigned m_strtime_camera_movement
RENDER_3D_RAYTRACE * m_3d_render_raytracing
void restart_editingTimeOut_Timer()
Reset the editing timer.
BOARD_ITEM * m_currentRollOverItem
RENDER_3D_BASE * m_3d_render
bool m_is_opengl_initialized
void OnResize(wxSizeEvent &event)
bool m_render3dmousePivot
void OnMouseWheel(wxMouseEvent &event)
WX_INFOBAR * m_parentInfoBar
wxTimer m_editing_timeout_timer
void OnLeftDown(wxMouseEvent &event)
ACCELERATOR_3D * m_accelerator3DShapes
void OnTimerTimeout_Redraw(wxTimerEvent &event)
wxStatusBar * m_parentStatusBar
void DoRePaint()
The actual function to repaint the canvas.
int m_moving_speed_multiplier
bool SetView3D(int aKeycode)
Helper function to call view commands.
bool m_is_opengl_version_supported
wxTimer m_redraw_trigger_timer
BOARD_ADAPTER & m_boardAdapter
void DisplayStatus()
Update the status bar with the position information.
void render3dmousePivot(float aScale)
Render the 3dmouse pivot cursor.
void OnPaint(wxPaintEvent &aEvent)
Called by a wxPaintEvent event.
void RenderRaytracingRequest()
Request to render the current view in Raytracing mode.
void SetEventDispatcher(TOOL_DISPATCHER *aEventDispatcher)
Set a dispatcher that processes events and forwards them to tools.
bool m_render_raytracing_was_requested
float m_camera_moving_speed
void OnLeftUp(wxMouseEvent &event)
void ReloadRequest(BOARD *aBoard=nullptr, S3D_CACHE *aCachePointer=nullptr)
RAY getRayAtCurrentMousePosition()
void OnCloseWindow(wxCloseEvent &event)
void OnMiddleDown(wxMouseEvent &event)
void GetScreenshot(wxImage &aDstImage)
Request a screenshot and output it to the aDstImage.
void OnMiddleUp(wxMouseEvent &event)
void render_pivot(float t, float aScale)
Render the pivot cursor.
void request_start_moving_camera(float aMovingSpeed=2.0f, bool aRenderPivot=true)
Start a camera movement.
void RenderEngineChanged()
Notify that the render engine was changed.
std::atomic_flag m_is_currently_painting
void OnRefreshRequest(wxEvent &aEvent)
void OnEraseBackground(wxEraseEvent &event)
void releaseOpenGL()
Free created targets and openGL context.
void Request_refresh(bool aRedrawImmediately=true)
Schedule a refresh update of the canvas.
bool m_opengl_supports_raytracing
void OnMouseMove(wxMouseEvent &event)
void move_pivot_based_on_cur_mouse_position()
This function hits a ray to the board and start a movement.
void OnTimerTimeout_Editing(wxTimerEvent &event)
void stop_editingTimeOut_Timer()
Stop the editing time so it will not timeout.
KICAD_T Type() const
Returns the type of object.
void UnlockCtx(wxGLContext *aContext)
Allow other canvases to bind an OpenGL context.
void DestroyCtx(wxGLContext *aContext)
Destroy a managed OpenGL context.
void LockCtx(wxGLContext *aContext, wxGLCanvas *aCanvas)
Set a context as current and prevents other canvases from switching it.
static GL_CONTEXT_MANAGER & Get()
Return the GL_CONTEXT_MANAGER instance (singleton).
wxGLContext * CreateCtx(wxGLCanvas *aCanvas, const wxGLContext *aOther=nullptr)
Create a managed OpenGL context.
static int SetSwapInterval(int aVal)
Attempts to set the OpenGL swap interval.
Provides basic 3D controls ( zoom, rotate, translate, ... )
static const float m_delta_move_step_factor
void OnMouseWheelCamera(wxMouseEvent &event, bool aPan)
void OnMouseMoveCamera(wxMouseEvent &event)
virtual wxSize GetNativePixelSize() const
A wrapper for reporting to a WX_INFOBAR UI element.
void Finalize()
Update the infobar with the reported text.
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
virtual bool Redraw(bool aIsMoving, REPORTER *aStatusReporter=nullptr, REPORTER *aWarningReporter=nullptr)=0
Redraw the view.
virtual int GetWaitForEditingTimeOut()=0
Give the interface the time (in ms) that it should wait for editing or movements before (this works f...
virtual void SetCurWindowSize(const wxSize &aSize)=0
Before each render, the canvas will tell the render what is the size of its windows,...
bool IsReloadRequestPending() const
Query if there is a pending reload request.
Object to render the board using openGL.
void SetCurrentRollOverItem(BOARD_ITEM *aRollOverItem)
BOARD_ITEM * IntersectBoardItem(const RAY &aRay)
void Reload(REPORTER *aStatusReporter, REPORTER *aWarningReporter, bool aOnlyLoadCopperAndShapes)
Cache for storing the 3D shapes.
T * GetAppSettings()
Returns a handle to the a given settings by type If the settings have already been loaded,...
A wrapper for reporting to a specific text location in a statusbar.
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
bool HasMessage() const override
Returns true if the reporter client is non-empty.
Handle a list of polygons defining a copper zone.
bool GetIsRuleArea() const
Accessors to parameters used in Rule Area zones:
wxString GetZoneName() const
bool IsOnCopperLayer() const override
wxDEFINE_EVENT(wxEVT_REFRESH_CUSTOM_COMMAND, wxEvent)
Declaration of the eda_3d_viewer class.
static const wxChar * m_logTrace
Trace mask used to enable or disable the trace output of this class.
This file contains miscellaneous commonly used macros and functions.
static wxString FROM_UTF8(const char *cstring)
Convert a UTF8 encoded C string to a wxString for all wxWidgets build modes.
void OglGetScreenshot(wxImage &aDstImage)
Get the pixel data of current OpenGL image.
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
unsigned GetRunningMicroSecs()
An alternate way to calculate an elapsed time (in microsecondes) to class PROF_COUNTER.
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
bool Intersect(const RAY &aRay, float *t) const
void Init(const SFVEC3F &o, const SFVEC3F &d)
SFVEC3F at(float t) const
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
@ PCB_ZONE_T
class ZONE, a copper pour area
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
@ PCB_PAD_T
class PAD, a pad in a footprint
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)