32#include <wx/dcclient.h>
51#define UNITS3D_TO_UNITSPCB ( pcbIUScale.IU_PER_MM )
76#ifdef USE_OSX_MAGNIFY_EVENT
77 EVT_MAGNIFY( EDA_3D_MODEL_VIEWER::OnMagnify )
88#define RANGE_SCALE_3D 8.0f
94 wxDefaultPosition, wxDefaultSize,
95 wxFULL_REPAINT_ON_RESIZE ),
99 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_MODEL_VIEWER::EDA_3D_MODEL_VIEWER" ) );
113 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_MODEL_VIEWER::~EDA_3D_MODEL_VIEWER" ) );
121 std::optional<ORPHANED_GL_OBJECTS> orphaned;
137 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_MODEL_VIEWER::Set3DModel with a S3DMODEL" ) );
141 wxASSERT( a3DModel.
m_Meshes !=
nullptr );
164 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_MODEL_VIEWER::Set3DModel with a wxString" ) );
196 const int glVersion = gladLoaderLoadGL();
200 wxLogTrace(
m_logTrace, wxT(
"Failed to load OpenGL via loader" ) );
204 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_MODEL_VIEWER::ogl_initialize Using OpenGL version %s" ),
205 From_UTF8( (
char*) glGetString( GL_VERSION ) ) );
207 SetOpenGLInfo( (
const char*) glGetString( GL_VENDOR ), (
const char*) glGetString( GL_RENDERER ),
208 (
const char*) glGetString( GL_VERSION ) );
210 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
211 glHint( GL_LINE_SMOOTH_HINT, GL_NICEST );
212 glHint( GL_POLYGON_SMOOTH_HINT, GL_NICEST );
214 glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
215 glEnable( GL_DEPTH_TEST );
216 glEnable( GL_CULL_FACE );
217 glShadeModel( GL_SMOOTH );
218 glEnable( GL_LINE_SMOOTH );
219 glEnable( GL_NORMALIZE );
223 const GLfloat ambient[] = { 0.01f, 0.01f, 0.01f, 1.0f };
224 const GLfloat diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
225 const GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
228 const GLfloat position[] = { 0.0f, 0.0f, 2.0f *
RANGE_SCALE_3D, 0.0f };
230 const GLfloat lmodel_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
232 glLightfv( GL_LIGHT0, GL_AMBIENT, ambient );
233 glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse );
234 glLightfv( GL_LIGHT0, GL_SPECULAR, specular );
235 glLightfv( GL_LIGHT0, GL_POSITION, position );
236 glLightModelfv( GL_LIGHT_MODEL_AMBIENT, lmodel_ambient );
244 glEnable( GL_COLOR_MATERIAL );
245 glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
249 glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, &specular.r );
250 glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, 96.0f );
257 wxPaintDC
dc(
this );
263 if( !IsShownOnScreen() )
265 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_MODEL_VIEWER::OnPaint !IsShown" ) );
283 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_MODEL_VIEWER::OnPaint creating gl context failed" ) );
311 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_MODEL_VIEWER::OnPaint m_reload_is_needed" ) );
323 glViewport( 0, 0, clientSize.x, clientSize.y );
328 glEnable( GL_DEPTH_TEST );
329 glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
330 glClearDepth( 1.0f );
331 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
334 glMatrixMode( GL_PROJECTION );
336 glMatrixMode( GL_MODELVIEW );
338 glEnable( GL_LIGHTING );
339 glEnable( GL_LIGHT0 );
348 glScaled( modelunit_to_3d_units_factor, modelunit_to_3d_units_factor,
349 modelunit_to_3d_units_factor );
354 glTranslatef( -model_center.x, -model_center.y, -model_center.z );
360 glDepthMask( GL_FALSE );
361 glEnable( GL_BLEND );
362 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
366 glDisable( GL_BLEND );
367 glDepthMask( GL_TRUE );
375 glViewport( 0, 0, clientSize.y / 8 , clientSize.y / 8 );
376 glClear( GL_DEPTH_BUFFER_BIT );
378 glMatrixMode( GL_PROJECTION );
382 glMatrixMode( GL_MODELVIEW );
385 const glm::mat4 TranslationMatrix = glm::translate( glm::mat4(1.0f),
388 const glm::mat4 ViewMatrix = TranslationMatrix *
m_trackBallCamera.GetRotationMatrix();
390 glLoadMatrixf( glm::value_ptr( ViewMatrix ) );
394 glColor3f( 0.9f, 0.0f, 0.0f );
398 glColor3f( 0.0f, 0.9f, 0.0f );
402 glColor3f( 0.0f, 0.0f, 0.9f );
417 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_MODEL_VIEWER::OnEraseBackground" ) );
424 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_MODEL_VIEWER::OnMouseWheel" ) );
426 if( event.ShiftDown() )
433 else if( event.ControlDown() )
452#ifdef USE_OSX_MAGNIFY_EVENT
453void EDA_3D_MODEL_VIEWER::OnMagnify( wxMouseEvent& event )
466 if( event.Dragging() )
468 if( event.LeftIsDown() )
@ NORMAL
Use all material properties from model file.
#define RANGE_SCALE_3D
This defines the range that all coord will have to be rendered.
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.
Implement a canvas based on a wxGLCanvas.
MODEL_3D * m_ogl_3dmodel
Class holder for 3d model to display on openGL.
void OnMiddleDown(wxMouseEvent &event)
EDA_3D_MODEL_VIEWER(wxWindow *aParent, const wxGLAttributes &aGLAttribs, S3D_CACHE *aCacheManager=nullptr)
Create a new 3D Canvas with a attribute list.
void OnMouseMove(wxMouseEvent &event)
void OnEraseBackground(wxEraseEvent &event)
S3D_CACHE * m_cacheManager
Optional cache manager.
void ogl_set_arrow_material()
void Clear3DModel()
Unload the displayed 3D model.
bool m_reload_is_needed
Flag that we have a new model and it need to be reloaded when the paint is called.
void OnLeftDown(wxMouseEvent &event)
double m_BiuTo3dUnits
factor to convert the model or any other items to keep it in relation to the +/-RANGE_SCALE_3D (it is...
void OnRightClick(wxMouseEvent &event)
void Set3DModel(const S3DMODEL &a3DModel)
Set this model to be displayed.
void OnPaint(wxPaintEvent &event)
bool m_ogl_initialized
Flag if open gl was initialized.
const S3DMODEL * m_3d_model
Original 3d model data.
TRACK_BALL m_trackBallCamera
Camera used in this canvas.
wxGLContext * m_glRC
openGL context
void OnMiddleUp(wxMouseEvent &event)
void OnLeftUp(wxMouseEvent &event)
void OnMouseWheel(wxMouseEvent &event)
void UnlockCtx(wxGLContext *aContext)
Allow other canvases to bind an OpenGL context.
void DestroyCtx(wxGLContext *aContext)
Destroy a managed OpenGL context.
bool 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)
HIDPI_GL_CANVAS(const KIGFX::VC_SETTINGS &aSettings, wxWindow *aParent, const wxGLAttributes &aGLAttribs, wxWindowID aId=wxID_ANY, const wxPoint &aPos=wxDefaultPosition, const wxSize &aSize=wxDefaultSize, long aStyle=0, const wxString &aName=wxGLCanvasName, const wxPalette &aPalette=wxNullPalette)
virtual wxSize GetNativePixelSize() const
wxPoint GetNativePosition(const wxPoint &aPoint) const
Convert the given point from client coordinates to native pixel coordinates.
GL_CONTEXT_MANAGER * GetGLContextManager()
Cache for storing the 3D shapes.
#define UNITS3D_TO_UNITSPCB
Implements a model viewer canvas.
Implements a model viewer canvas.
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 DrawRoundArrow(SFVEC3F aPosition, SFVEC3F aTargetPos, float aSize)
Draw a round arrow.
PGM_BASE & Pgm()
The global program "get" accessor.
wxString From_UTF8(const char *cstring)
Store the a model based on meshes and materials.
SMATERIAL * m_Materials
The materials list of this model.
unsigned int m_MeshesSize
Number of meshes in the array.
SMESH * m_Meshes
The meshes list of this model.
unsigned int m_MaterialsSize
Number of materials in the material array.
DATABASE_CONNECTION dc(odbcBackend(cs))