29#include <wx/tokenzr.h>
31#include <fmt/format.h>
111 wxDefaultSize, wxFULL_REPAINT_ON_RESIZE ),
116 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::EDA_3D_CANVAS" ) );
134 auto busy_indicator_factory =
137 return std::make_unique<WX_BUSY_INDICATOR>();
149 wxASSERT( a3DCachePointer !=
nullptr );
152#if defined( __WXMSW__ )
153 EnableTouchEvents( wxTOUCH_ZOOM_GESTURE | wxTOUCH_ROTATE_GESTURE | wxTOUCH_PAN_GESTURES );
154#elif defined( __WXGTK__ )
155 EnableTouchEvents( wxTOUCH_ZOOM_GESTURE | wxTOUCH_ROTATE_GESTURE );
158 const wxEventType events[] =
164 wxEVT_LEFT_UP, wxEVT_LEFT_DOWN, wxEVT_LEFT_DCLICK,
165 wxEVT_RIGHT_UP, wxEVT_RIGHT_DOWN, wxEVT_RIGHT_DCLICK,
166 wxEVT_MIDDLE_UP, wxEVT_MIDDLE_DOWN, wxEVT_MIDDLE_DCLICK,
167 wxEVT_MOTION, wxEVT_MOUSEWHEEL, wxEVT_CHAR, wxEVT_CHAR_HOOK,
169 wxEVT_MENU_OPEN, wxEVT_MENU_CLOSE, wxEVT_MENU_HIGHLIGHT
172 for( wxEventType eventType : events )
179 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::~EDA_3D_CANVAS" ) );
227 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::initializeOpenGL" ) );
231 const int glVersion = gladLoaderLoadGL();
235 wxLogMessage( wxT(
"Failed to load OpenGL via loader" ) );
241 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::initializeOpenGL Using OpenGL version %s" ),
242 From_UTF8( (
char*) glGetString( GL_VERSION ) ) );
245 SetOpenGLInfo( (
const char*) glGetString( GL_VENDOR ), (
const char*) glGetString( GL_RENDERER ),
246 (
const char*) glGetString( GL_VERSION ) );
248 wxString version =
From_UTF8( (
char *) glGetString( GL_VERSION ) );
250 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::%s OpenGL version string %s." ),
251 __WXFUNCTION__, version );
257 wxStringTokenizer tokenizer( version,
" \t\r\n" );
259 if( tokenizer.HasMoreTokens() )
264 tmp = tokenizer.GetNextToken();
266 tokenizer.SetString( tmp, wxString( wxT(
"." ) ) );
268 if( tokenizer.HasMoreTokens() )
269 tokenizer.GetNextToken().ToLong( &major );
271 if( tokenizer.HasMoreTokens() )
272 tokenizer.GetNextToken().ToLong( &minor );
274 if( major < 2 || ( ( major == 2 ) && ( minor < 1 ) ) )
276 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::%s OpenGL ray tracing not supported." ),
282 GetParent()->ProcessWindowEvent( evt );
288 if( ( major == 1 ) && ( minor < 5 ) )
290 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::%s OpenGL not supported." ),
297#if wxCHECK_VERSION( 3, 3, 3 )
298 wxGLCanvas::SetSwapInterval( -1 );
317 if( aCachePointer !=
nullptr )
320 if( aBoard !=
nullptr )
349 msg.Printf( wxT(
"dx %3.2f" ),
m_camera.GetCameraPos().x );
352 msg.Printf( wxT(
"dy %3.2f" ),
m_camera.GetCameraPos().y );
355 msg.Printf( wxT(
"zoom %3.2f" ), 1 /
m_camera.GetZoom() );
375 if( !IsShownOnScreen() )
377 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::DoRePaint !IsShown" ) );
388 if( !GetParent()->GetParent()->IsShownOnScreen() )
391 wxString err_messages;
424 const bool windows_size_changed =
m_camera.SetCurWindowSize( clientSize );
439 warningReporter.
Report(
_(
"Your OpenGL version is not supported. Minimum required "
448 glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
449 glClear( GL_COLOR_BUFFER_BIT );
470 const bool was_camera_changed =
m_camera.ParametersChanged();
475 || windows_size_changed )
483 float curtime_delta_s = 0.0f;
489 curtime_delta_s =
static_cast<float>(
static_cast<double>( curtime_delta ) / 1e6 )
491 m_camera.Interpolate( curtime_delta_s );
493 if( curtime_delta_s > 1.0f )
509 bool requested_redraw =
false;
517 bool reloadRaytracingForCalculations =
false;
522 reloadRaytracingForCalculations =
true;
526 &activityReporter, &warningReporter );
535 if( reloadRaytracingForCalculations )
538 catch( std::runtime_error& )
575 activityReporter.
Report( wxString::Format(
_(
"Last render time %.0f ms" ),
576 calculation_time ) );
584 if( !err_messages.IsEmpty() )
585 wxLogMessage( err_messages );
598 int index =
static_cast<int>( selectedGizmoSphere );
599 if(
index >= 0 &&
index <
static_cast<int>( viewTable.size() ) )
616 if( !buffer || width <= 0 || height <= 0 )
624 if( !GetParent() || !GetParent()->GetParent() || !GetParent()->GetParent()->IsShownOnScreen() )
630 wxString err_messages;
640 wxLogError(
_(
"OpenGL context creation error" ) );
648 GLuint framebuffer = 0;
649 GLuint colorTexture = 0;
650 GLuint depthStencilBuffer = 0;
651 GLint oldFramebuffer = 0;
652 GLint oldViewport[4];
655 glGetIntegerv( GL_FRAMEBUFFER_BINDING, &oldFramebuffer );
656 glGetIntegerv( GL_VIEWPORT, oldViewport );
659 glGenFramebuffers( 1, &framebuffer );
660 glBindFramebuffer( GL_FRAMEBUFFER, framebuffer );
663 glGenTextures( 1, &colorTexture );
664 glBindTexture( GL_TEXTURE_2D, colorTexture );
665 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
nullptr );
666 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
667 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
668 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
669 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
670 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorTexture, 0 );
675 glGenRenderbuffers( 1, &depthStencilBuffer );
676 glBindRenderbuffer( GL_RENDERBUFFER, depthStencilBuffer );
677 glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height );
678 glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
679 depthStencilBuffer );
681 auto resetState = std::unique_ptr<void, std::function<void(
void*)>>(
682 reinterpret_cast<void*
>(1),
684 glBindFramebuffer( GL_FRAMEBUFFER, oldFramebuffer );
685 glViewport( oldViewport[0], oldViewport[1], oldViewport[2], oldViewport[3] );
686 glDeleteFramebuffers( 1, &framebuffer );
687 glDeleteTextures( 1, &colorTexture );
688 glDeleteRenderbuffers( 1, &depthStencilBuffer );
695 GLenum framebufferStatus = glCheckFramebufferStatus( GL_FRAMEBUFFER );
697 if( framebufferStatus != GL_FRAMEBUFFER_COMPLETE )
699 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::RenderToFrameBuffer Framebuffer incomplete: 0x%04X" ),
706 glViewport( 0, 0, width, height );
709 wxSize clientSize( width, height );
710 const bool windows_size_changed =
m_camera.SetCurWindowSize( clientSize );
717 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::RenderToFrameBuffer OpenGL initialization failed." ) );
723 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::RenderToFrameBuffer OpenGL version not supported." ) );
729 glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
730 glClear( GL_COLOR_BUFFER_BIT );
733 glReadPixels( 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer );
747 const bool was_camera_changed =
m_camera.ParametersChanged();
758 float curtime_delta_s = 0.0f;
762 curtime_delta_s =
static_cast<float>(
static_cast<double>( curtime_delta ) / 1e6 )
764 m_camera.Interpolate( curtime_delta_s );
766 if( curtime_delta_s > 1.0f )
775 bool requested_redraw =
false;
782 bool reloadRaytracingForCalculations =
false;
786 reloadRaytracingForCalculations =
true;
789 requested_redraw =
m_3d_render->Redraw(
false,
nullptr,
nullptr );
791 if( reloadRaytracingForCalculations )
794 catch( std::runtime_error& )
805 glReadPixels( 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer );
808 GLenum error = glGetError();
809 if( error != GL_NO_ERROR )
811 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::RenderToFrameBuffer OpenGL error: 0x%04X" ), error );
812 err_messages += wxString::Format(
_(
"OpenGL error during off-screen rendering: 0x%04X\n" ), error );
818 if( !err_messages.IsEmpty() )
819 wxLogMessage( err_messages );
842 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::OnEraseBackground" ) );
849 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::OnMouseWheel" ) );
872 float magnification = (
event.GetMagnification() + 1.0f );
885 if( aEvent.IsGestureStart() )
888 m_camera.SetCurMousePosition( aEvent.GetPosition() );
896 m_camera.Pan( aEvent.GetPosition() );
897 m_camera.SetCurMousePosition( aEvent.GetPosition() );
912 if( aEvent.IsGestureStart() )
913 m_camera.SetCurMousePosition( aEvent.GetPosition() );
918 m_camera.Pan( aEvent.GetPosition() );
919 m_camera.SetCurMousePosition( aEvent.GetPosition() );
930 if( aEvent.IsGestureStart() )
933 m_camera.SetCurMousePosition( aEvent.GetPosition() );
977 return wxString::Format(
_(
"Net %s\tNet class %s" ), aItem->GetNet()->GetNetname(),
978 aItem->GetNet()->GetNetClass()->GetHumanReadableName() );
993 switch( rollOverItem->
Type() )
997 PAD*
pad =
static_cast<PAD*
>( rollOverItem );
999 if( !
pad->GetNumber().IsEmpty() )
1000 msg += wxString::Format(
_(
"Pad %s\t" ),
pad->GetNumber() );
1002 if(
pad->IsOnCopperLayer() )
1003 msg += printNetInfo(
pad );
1020 msg += printNetInfo( track );
1026 ZONE* zone =
static_cast<ZONE*
>( rollOverItem );
1031 msg += wxString::Format(
_(
"Rule area %s\t" ), zone->
GetZoneName() );
1033 msg += wxString::Format(
_(
"Zone %s\t" ), zone->
GetZoneName() );
1037 msg += printNetInfo( zone );
1056 reporter.
Report( wxEmptyString );
1081 if( intersectedBoardItem )
1085 switch( intersectedBoardItem->
Type() )
1088 footprint =
static_cast<FOOTPRINT*
>( intersectedBoardItem );
1092 footprint =
static_cast<PAD*
>( intersectedBoardItem )->GetParentFootprint();
1096 footprint =
static_cast<PCB_FIELD*
>( intersectedBoardItem )->GetParentFootprint();
1112 std::string command = fmt::format(
"$SELECT: 0,F{}",
1135 wxSize logicalSize = GetClientSize();
1136 int logicalW = logicalSize.GetWidth();
1137 int logicalH = logicalSize.GetHeight();
1139 int gizmo_x = 0, gizmo_y = 0, gizmo_width = 0, gizmo_height = 0;
1140 std::tie( gizmo_x, gizmo_y, gizmo_width, gizmo_height ) =
m_3d_render_opengl->getGizmoViewport();
1142 float scaleX =
static_cast<float>(
static_cast<double>( gizmo_width ) /
static_cast<double>( logicalW ) );
1143 float scaleY =
static_cast<float>(
static_cast<double>( gizmo_height ) /
static_cast<double>( logicalH ) );
1145 int scaledMouseX =
static_cast<int>(
static_cast<float>(
event.GetX() ) * scaleX );
1146 int scaledMouseY =
static_cast<int>(
static_cast<float>( logicalH -
event.GetY() ) * scaleY );
1248 if( aRedrawImmediately )
1255 wxPostEvent(
this, redrawEvent );
1267 wxASSERT( aMovingSpeed > FLT_EPSILON );
1307 m_camera.SetLookAtPos_T1( mouseRay.
at( hit_t ) );
1321 const float arrow_moving_time_speed = 8.0f;
1323 switch( aRequestedView )
1389 m_camera.ViewCommand_T1( aRequestedView );
1397 m_camera.ViewCommand_T1( aRequestedView );
1411 switch( cfg->m_Render.engine )
1434 m_camera.MakeRayAtCurrentMousePosition( rayOrigin, rayDir );
1437 mouseRay.
Init( rayOrigin, rayDir );
void SetOpenGLInfo(const char *aVendor, const char *aRenderer, const char *aVersion)
A setter for OpenGL info when it's initialized.
void SetOpenGLBackendInfo(wxString aBackend)
A setter for OpenGL backend info after the canvas is created.
Helper class to handle information needed to display 3D board.
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.
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.
void OnMagnify(wxMouseEvent &event)
void restart_editingTimeOut_Timer()
Reset the editing timer.
void RenderToFrameBuffer(unsigned char *aBuffer, int aWidth, int aHeight)
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
int64_t m_strtime_camera_movement
void OnLeftDown(wxMouseEvent &event)
void OnPanGesture(wxPanGestureEvent &event)
void OnRightUp(wxMouseEvent &event)
ACCELERATOR_3D * m_accelerator3DShapes
void OnTimerTimeout_Redraw(wxTimerEvent &event)
wxStatusBar * m_parentStatusBar
void DoRePaint()
The actual function to repaint the canvas.
void OnRightDown(wxMouseEvent &event)
int m_moving_speed_multiplier
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)
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()
double m_gestureLastZoomFactor
Used to track gesture events.
void OnCloseWindow(wxCloseEvent &event)
bool SetView3D(VIEW3D_TYPE aRequestedView)
Select a specific 3D view or operation.
void OnMiddleDown(wxMouseEvent &event)
void GetScreenshot(wxImage &aDstImage)
Request a screenshot and output it to the aDstImage.
RENDER_3D_RAYTRACE_GL * m_3d_render_raytracing
void OnZoomGesture(wxZoomGestureEvent &event)
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
~EDA_3D_CANVAS() override
void OnMouseMove(wxMouseEvent &event)
void OnRotateGesture(wxRotateGestureEvent &event)
EDA_3D_CANVAS(wxWindow *aParent, const wxGLAttributes &aGLAttribs, BOARD_ADAPTER &aSettings, CAMERA &aCamera, S3D_CACHE *a3DCachePointer)
Create a new 3D Canvas with an attribute list.
void move_pivot_based_on_cur_mouse_position()
This function hits a ray to the board and start a movement.
double m_gestureLastAngle
void OnTimerTimeout_Editing(wxTimerEvent &event)
void stop_editingTimeOut_Timer()
Stop the editing time so it will not timeout.
Create and handle a window for the 3d viewer connected to a Kiway and a pcbboard.
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.
wxGLContext * CreateCtx(wxGLCanvas *aCanvas, const wxGLContext *aOther=nullptr)
Create a managed OpenGL context.
static int SetSwapInterval(wxGLCanvas *aCanvas, int aVal)
Attempt to set the OpenGL swap interval.
static wxString DetectGLBackend(wxGLCanvas *aCanvas)
Provides basic 3D controls ( zoom, rotate, translate, ... )
static const float m_delta_move_step_factor
HIDPI_GL_3D_CANVAS(const KIGFX::VC_SETTINGS &aVcSettings, CAMERA &aCamera, wxWindow *parent, const wxGLAttributes &aGLAttribs, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=0, const wxString &name=wxGLCanvasName, const wxPalette &palette=wxNullPalette)
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.
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
virtual void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=nullptr, bool aFromOtherThread=false)
Send aPayload to aDestination from aSource.
GL_CONTEXT_MANAGER * GetGLContextManager()
Object to render the board using openGL.
Cache for storing the 3D shapes.
GizmoSphereSelection
Enum to indicate which sphere (direction) is selected.
@ Count
Number of selectable spheres.
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.
Handle a list of polygons defining a copper zone.
bool GetIsRuleArea() const
Accessors to parameters used in Rule Area zones:
const 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.
void OglGetScreenshot(wxImage &aDstImage)
Get the pixel data of current OpenGL image.
Define generic OpenGL functions that are common to any OpenGL target.
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
PGM_BASE & Pgm()
The global program "get" accessor.
int64_t GetRunningMicroSecs()
An alternate way to calculate an elapsed time (in microsecondes) to class PROF_COUNTER.
T * GetAppSettings(const char *aFilename)
wxString From_UTF8(const char *cstring)
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
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_FIELD_T
class PCB_FIELD, text associated with a footprint property
@ 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)