27#include <wx/tokenzr.h>
55#include <fmt/format.h>
107 wxDefaultSize, wxFULL_REPAINT_ON_RESIZE ),
112 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::EDA_3D_CANVAS" ) );
130 auto busy_indicator_factory =
133 return std::make_unique<WX_BUSY_INDICATOR>();
145 wxASSERT( a3DCachePointer !=
nullptr );
148#if defined( __WXMSW__ )
149 EnableTouchEvents( wxTOUCH_ZOOM_GESTURE | wxTOUCH_ROTATE_GESTURE | wxTOUCH_PAN_GESTURES );
150#elif defined( __WXGTK__ )
151 EnableTouchEvents( wxTOUCH_ZOOM_GESTURE | wxTOUCH_ROTATE_GESTURE );
154 const wxEventType events[] =
160 wxEVT_LEFT_UP, wxEVT_LEFT_DOWN, wxEVT_LEFT_DCLICK,
161 wxEVT_RIGHT_UP, wxEVT_RIGHT_DOWN, wxEVT_RIGHT_DCLICK,
162 wxEVT_MIDDLE_UP, wxEVT_MIDDLE_DOWN, wxEVT_MIDDLE_DCLICK,
163 wxEVT_MOTION, wxEVT_MOUSEWHEEL, wxEVT_CHAR, wxEVT_CHAR_HOOK,
165 wxEVT_MENU_OPEN, wxEVT_MENU_CLOSE, wxEVT_MENU_HIGHLIGHT
168 for( wxEventType eventType : events )
175 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::~EDA_3D_CANVAS" ) );
223 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::initializeOpenGL" ) );
227 const GLenum err = glewInit();
231 const wxString msgError = (
const char*) glewGetErrorString( err );
233 wxLogMessage( msgError );
239 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::initializeOpenGL Using GLEW version %s" ),
240 From_UTF8( (
char*) glewGetString( GLEW_VERSION ) ) );
243 SetOpenGLInfo( (
const char*) glGetString( GL_VENDOR ), (
const char*) glGetString( GL_RENDERER ),
244 (
const char*) glGetString( GL_VERSION ) );
246 wxString version =
From_UTF8( (
char *) glGetString( GL_VERSION ) );
248 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::%s OpenGL version string %s." ),
249 __WXFUNCTION__, version );
255 wxStringTokenizer tokenizer( version,
" \t\r\n" );
257 if( tokenizer.HasMoreTokens() )
262 tmp = tokenizer.GetNextToken();
264 tokenizer.SetString( tmp, wxString( wxT(
"." ) ) );
266 if( tokenizer.HasMoreTokens() )
267 tokenizer.GetNextToken().ToLong( &major );
269 if( tokenizer.HasMoreTokens() )
270 tokenizer.GetNextToken().ToLong( &minor );
272 if( major < 2 || ( ( major == 2 ) && ( minor < 1 ) ) )
274 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::%s OpenGL ray tracing not supported." ),
280 GetParent()->ProcessWindowEvent( evt );
286 if( ( major == 1 ) && ( minor < 5 ) )
288 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::%s OpenGL not supported." ),
295#if wxCHECK_VERSION( 3, 3, 3 )
296 wxGLCanvas::SetSwapInterval( -1 );
315 if( aCachePointer !=
nullptr )
318 if( aBoard !=
nullptr )
347 msg.Printf( wxT(
"dx %3.2f" ),
m_camera.GetCameraPos().x );
350 msg.Printf( wxT(
"dy %3.2f" ),
m_camera.GetCameraPos().y );
353 msg.Printf( wxT(
"zoom %3.2f" ), 1 /
m_camera.GetZoom() );
373 if( !IsShownOnScreen() )
375 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::DoRePaint !IsShown" ) );
386 if( !GetParent()->GetParent()->IsShownOnScreen() )
389 wxString err_messages;
422 const bool windows_size_changed =
m_camera.SetCurWindowSize( clientSize );
437 warningReporter.
Report(
_(
"Your OpenGL version is not supported. Minimum required "
446 glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
447 glClear( GL_COLOR_BUFFER_BIT );
468 const bool was_camera_changed =
m_camera.ParametersChanged();
473 || windows_size_changed )
481 float curtime_delta_s = 0.0f;
487 curtime_delta_s =
static_cast<float>(
static_cast<double>( curtime_delta ) / 1e6 )
489 m_camera.Interpolate( curtime_delta_s );
491 if( curtime_delta_s > 1.0f )
507 bool requested_redraw =
false;
515 bool reloadRaytracingForCalculations =
false;
520 reloadRaytracingForCalculations =
true;
524 &activityReporter, &warningReporter );
533 if( reloadRaytracingForCalculations )
536 catch( std::runtime_error& )
573 activityReporter.
Report( wxString::Format(
_(
"Last render time %.0f ms" ),
574 calculation_time ) );
582 if( !err_messages.IsEmpty() )
583 wxLogMessage( err_messages );
596 int index =
static_cast<int>( selectedGizmoSphere );
597 if(
index >= 0 &&
index <
static_cast<int>( viewTable.size() ) )
614 if( !buffer || width <= 0 || height <= 0 )
622 if( !GetParent() || !GetParent()->GetParent() || !GetParent()->GetParent()->IsShownOnScreen() )
628 wxString err_messages;
638 wxLogError(
_(
"OpenGL context creation error" ) );
646 GLuint framebuffer = 0;
647 GLuint colorTexture = 0;
648 GLuint depthStencilBuffer = 0;
649 GLint oldFramebuffer = 0;
650 GLint oldViewport[4];
653 glGetIntegerv( GL_FRAMEBUFFER_BINDING, &oldFramebuffer );
654 glGetIntegerv( GL_VIEWPORT, oldViewport );
657 glGenFramebuffers( 1, &framebuffer );
658 glBindFramebuffer( GL_FRAMEBUFFER, framebuffer );
661 glGenTextures( 1, &colorTexture );
662 glBindTexture( GL_TEXTURE_2D, colorTexture );
663 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
nullptr );
664 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
665 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
666 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
667 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
668 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorTexture, 0 );
673 glGenRenderbuffers( 1, &depthStencilBuffer );
674 glBindRenderbuffer( GL_RENDERBUFFER, depthStencilBuffer );
675 glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height );
676 glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
677 depthStencilBuffer );
679 auto resetState = std::unique_ptr<void, std::function<void(
void*)>>(
680 reinterpret_cast<void*
>(1),
682 glBindFramebuffer( GL_FRAMEBUFFER, oldFramebuffer );
683 glViewport( oldViewport[0], oldViewport[1], oldViewport[2], oldViewport[3] );
684 glDeleteFramebuffers( 1, &framebuffer );
685 glDeleteTextures( 1, &colorTexture );
686 glDeleteRenderbuffers( 1, &depthStencilBuffer );
693 GLenum framebufferStatus = glCheckFramebufferStatus( GL_FRAMEBUFFER );
695 if( framebufferStatus != GL_FRAMEBUFFER_COMPLETE )
697 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::RenderToFrameBuffer Framebuffer incomplete: 0x%04X" ),
704 glViewport( 0, 0, width, height );
707 wxSize clientSize( width, height );
708 const bool windows_size_changed =
m_camera.SetCurWindowSize( clientSize );
715 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::RenderToFrameBuffer OpenGL initialization failed." ) );
721 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::RenderToFrameBuffer OpenGL version not supported." ) );
727 glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
728 glClear( GL_COLOR_BUFFER_BIT );
731 glReadPixels( 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer );
745 const bool was_camera_changed =
m_camera.ParametersChanged();
756 float curtime_delta_s = 0.0f;
760 curtime_delta_s =
static_cast<float>(
static_cast<double>( curtime_delta ) / 1e6 )
762 m_camera.Interpolate( curtime_delta_s );
764 if( curtime_delta_s > 1.0f )
773 bool requested_redraw =
false;
780 bool reloadRaytracingForCalculations =
false;
784 reloadRaytracingForCalculations =
true;
787 requested_redraw =
m_3d_render->Redraw(
false,
nullptr,
nullptr );
789 if( reloadRaytracingForCalculations )
792 catch( std::runtime_error& )
803 glReadPixels( 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer );
806 GLenum error = glGetError();
807 if( error != GL_NO_ERROR )
809 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::RenderToFrameBuffer OpenGL error: 0x%04X" ), error );
810 err_messages += wxString::Format(
_(
"OpenGL error during off-screen rendering: 0x%04X\n" ), error );
816 if( !err_messages.IsEmpty() )
817 wxLogMessage( err_messages );
840 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::OnEraseBackground" ) );
847 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_CANVAS::OnMouseWheel" ) );
870 float magnification = (
event.GetMagnification() + 1.0f );
883 if( aEvent.IsGestureStart() )
886 m_camera.SetCurMousePosition( aEvent.GetPosition() );
894 m_camera.Pan( aEvent.GetPosition() );
895 m_camera.SetCurMousePosition( aEvent.GetPosition() );
910 if( aEvent.IsGestureStart() )
911 m_camera.SetCurMousePosition( aEvent.GetPosition() );
916 m_camera.Pan( aEvent.GetPosition() );
917 m_camera.SetCurMousePosition( aEvent.GetPosition() );
928 if( aEvent.IsGestureStart() )
931 m_camera.SetCurMousePosition( aEvent.GetPosition() );
975 return wxString::Format(
_(
"Net %s\tNet class %s" ), aItem->GetNet()->GetNetname(),
976 aItem->GetNet()->GetNetClass()->GetHumanReadableName() );
991 switch( rollOverItem->
Type() )
995 PAD*
pad =
static_cast<PAD*
>( rollOverItem );
997 if( !
pad->GetNumber().IsEmpty() )
998 msg += wxString::Format(
_(
"Pad %s\t" ),
pad->GetNumber() );
1000 if(
pad->IsOnCopperLayer() )
1001 msg += printNetInfo(
pad );
1018 msg += printNetInfo( track );
1024 ZONE* zone =
static_cast<ZONE*
>( rollOverItem );
1029 msg += wxString::Format(
_(
"Rule area %s\t" ), zone->
GetZoneName() );
1031 msg += wxString::Format(
_(
"Zone %s\t" ), zone->
GetZoneName() );
1035 msg += printNetInfo( zone );
1054 reporter.
Report( wxEmptyString );
1079 if( intersectedBoardItem )
1083 switch( intersectedBoardItem->
Type() )
1086 footprint =
static_cast<FOOTPRINT*
>( intersectedBoardItem );
1090 footprint =
static_cast<PAD*
>( intersectedBoardItem )->GetParentFootprint();
1094 footprint =
static_cast<PCB_FIELD*
>( intersectedBoardItem )->GetParentFootprint();
1110 std::string command = fmt::format(
"$SELECT: 0,F{}",
1133 wxSize logicalSize = GetClientSize();
1134 int logicalW = logicalSize.GetWidth();
1135 int logicalH = logicalSize.GetHeight();
1137 int gizmo_x = 0, gizmo_y = 0, gizmo_width = 0, gizmo_height = 0;
1138 std::tie( gizmo_x, gizmo_y, gizmo_width, gizmo_height ) =
m_3d_render_opengl->getGizmoViewport();
1140 float scaleX =
static_cast<float>(
static_cast<double>( gizmo_width ) /
static_cast<double>( logicalW ) );
1141 float scaleY =
static_cast<float>(
static_cast<double>( gizmo_height ) /
static_cast<double>( logicalH ) );
1143 int scaledMouseX =
static_cast<int>(
static_cast<float>(
event.GetX() ) * scaleX );
1144 int scaledMouseY =
static_cast<int>(
static_cast<float>( logicalH -
event.GetY() ) * scaleY );
1246 if( aRedrawImmediately )
1253 wxPostEvent(
this, redrawEvent );
1265 wxASSERT( aMovingSpeed > FLT_EPSILON );
1305 m_camera.SetLookAtPos_T1( mouseRay.
at( hit_t ) );
1319 const float arrow_moving_time_speed = 8.0f;
1321 switch( aRequestedView )
1387 m_camera.ViewCommand_T1( aRequestedView );
1395 m_camera.ViewCommand_T1( aRequestedView );
1409 switch( cfg->m_Render.engine )
1432 m_camera.MakeRayAtCurrentMousePosition( rayOrigin, rayDir );
1435 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 wxString DetectGLBackend(wxGLCanvas *aCanvas)
static int SetSwapInterval(int aVal)
Attempt to set the OpenGL swap interval.
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)