KiCad PCB EDA Suite
Loading...
Searching...
No Matches
render_3d_opengl.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-2020 Mario Luzeiro <[email protected]>
5 * Copyright (C) 2023 CERN
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#include <cstdint>
27#include <gal/opengl/kiglew.h> // Must be included first
28
30#include "render_3d_opengl.h"
31#include "opengl_utils.h"
33#include <board.h>
34#include <footprint.h>
36#include <3d_math.h>
37#include <glm/geometric.hpp>
38#include <lset.h>
39#include <pgm_base.h>
40#include <math/util.h> // for KiROUND
41#include <utility>
42#include <vector>
43#include <wx/log.h>
44
45#include <base_units.h>
46
50#define UNITS3D_TO_UNITSPCB ( pcbIUScale.IU_PER_MM )
51
53 RENDER_3D_BASE( aAdapter, aCamera ),
54 m_canvas( aCanvas )
55{
56 wxLogTrace( m_logTrace, wxT( "RENDER_3D_OPENGL::RENDER_3D_OPENGL" ) );
57
58 m_layers.clear();
59 m_outerLayerHoles.clear();
60 m_innerLayerHoles.clear();
61 m_triangles.clear();
62 m_board = nullptr;
63 m_antiBoard = nullptr;
64
65 m_platedPadsFront = nullptr;
66 m_platedPadsBack = nullptr;
67 m_offboardPadsFront = nullptr;
68 m_offboardPadsBack = nullptr;
69
70 m_outerThroughHoles = nullptr;
72 m_outerViaThroughHoles = nullptr;
73 m_microviaHoles = nullptr;
74 m_padHoles = nullptr;
75 m_viaFrontCover = nullptr;
76 m_viaBackCover = nullptr;
77
79 m_grid = 0;
81 m_currentRollOverItem = nullptr;
82 m_boardWithHoles = nullptr;
83 m_postMachinePlugs = nullptr;
84
85 m_3dModelMap.clear();
86
87 m_spheres_gizmo = new SPHERES_GIZMO( 4, 4 );
88}
89
90
92{
93 wxLogTrace( m_logTrace, wxT( "RENDER_3D_OPENGL::RENDER_3D_OPENGL" ) );
94
96
97 glDeleteTextures( 1, &m_circleTexture );
98
99 delete m_spheres_gizmo;
100}
101
102
104{
105 return 50; // ms
106}
107
108
109void RENDER_3D_OPENGL::SetCurWindowSize( const wxSize& aSize )
110{
111 if( m_windowSize != aSize )
112 {
113 int viewport[4];
114 int fbWidth, fbHeight;
115 glGetIntegerv( GL_VIEWPORT, viewport );
116
117 m_windowSize = aSize;
118 glViewport( 0, 0, m_windowSize.x, m_windowSize.y );
120 // Initialize here any screen dependent data here
121 }
122}
123
124
126{
127 if( enabled )
128 glEnable( GL_LIGHT0 );
129 else
130 glDisable( GL_LIGHT0 );
131}
132
133
135{
136 if( enabled )
137 glEnable( GL_LIGHT1 );
138 else
139 glDisable( GL_LIGHT1 );
140}
141
142
144{
145 if( enabled )
146 glEnable( GL_LIGHT2 );
147 else
148 glDisable( GL_LIGHT2 );
149}
150
151
153{
154 m_spheres_gizmo->resetSelectedGizmoSphere();
155}
156
157
162
163
164void RENDER_3D_OPENGL::setGizmoViewport( int x, int y, int width, int height )
165{
166 m_spheres_gizmo->setViewport( x, y, width, height );
167}
168
169
170std::tuple<int, int, int, int> RENDER_3D_OPENGL::getGizmoViewport() const
171{
172 return m_spheres_gizmo->getViewport();
173}
174
175
176void RENDER_3D_OPENGL::handleGizmoMouseInput( int mouseX, int mouseY )
177{
178 m_spheres_gizmo->handleMouseInput( mouseX, mouseY );
179}
180
181
183{
184 m_materials = {};
185
186 // http://devernay.free.fr/cours/opengl/materials.html
187
188 // Plated copper
189 // Copper material mixed with the copper color
190 m_materials.m_Copper.m_Ambient = SFVEC3F( m_boardAdapter.m_CopperColor.r * 0.1f,
191 m_boardAdapter.m_CopperColor.g * 0.1f,
192 m_boardAdapter.m_CopperColor.b * 0.1f);
193
194 m_materials.m_Copper.m_Specular = SFVEC3F( m_boardAdapter.m_CopperColor.r * 0.75f + 0.25f,
195 m_boardAdapter.m_CopperColor.g * 0.75f + 0.25f,
196 m_boardAdapter.m_CopperColor.b * 0.75f + 0.25f );
197
198 // This guess the material type(ex: copper vs gold) to determine the
199 // shininess factor between 0.1 and 0.4
200 float shininessfactor = 0.40f - mapf( fabs( m_boardAdapter.m_CopperColor.r -
201 m_boardAdapter.m_CopperColor.g ),
202 0.15f, 1.00f,
203 0.00f, 0.30f );
204
205 m_materials.m_Copper.m_Shininess = shininessfactor * 128.0f;
206 m_materials.m_Copper.m_Emissive = SFVEC3F( 0.0f, 0.0f, 0.0f );
207
208
209 // Non plated copper (raw copper)
210 m_materials.m_NonPlatedCopper.m_Ambient = SFVEC3F( 0.191f, 0.073f, 0.022f );
211 m_materials.m_NonPlatedCopper.m_Diffuse = SFVEC3F( 184.0f / 255.0f, 115.0f / 255.0f,
212 50.0f / 255.0f );
213 m_materials.m_NonPlatedCopper.m_Specular = SFVEC3F( 0.256f, 0.137f, 0.086f );
214 m_materials.m_NonPlatedCopper.m_Shininess = 0.1f * 128.0f;
215 m_materials.m_NonPlatedCopper.m_Emissive = SFVEC3F( 0.0f, 0.0f, 0.0f );
216
217 // Paste material mixed with paste color
218 m_materials.m_Paste.m_Ambient = SFVEC3F( m_boardAdapter.m_SolderPasteColor.r,
219 m_boardAdapter.m_SolderPasteColor.g,
220 m_boardAdapter.m_SolderPasteColor.b );
221
222 m_materials.m_Paste.m_Specular = SFVEC3F( m_boardAdapter.m_SolderPasteColor.r *
223 m_boardAdapter.m_SolderPasteColor.r,
224 m_boardAdapter.m_SolderPasteColor.g *
225 m_boardAdapter.m_SolderPasteColor.g,
226 m_boardAdapter.m_SolderPasteColor.b *
227 m_boardAdapter.m_SolderPasteColor.b );
228
229 m_materials.m_Paste.m_Shininess = 0.1f * 128.0f;
230 m_materials.m_Paste.m_Emissive = SFVEC3F( 0.0f, 0.0f, 0.0f );
231
232 // Silk screen material mixed with silk screen color
233 m_materials.m_SilkSTop.m_Ambient = SFVEC3F( m_boardAdapter.m_SilkScreenColorTop.r,
234 m_boardAdapter.m_SilkScreenColorTop.g,
235 m_boardAdapter.m_SilkScreenColorTop.b );
236
237 m_materials.m_SilkSTop.m_Specular = SFVEC3F(
238 m_boardAdapter.m_SilkScreenColorTop.r * m_boardAdapter.m_SilkScreenColorTop.r + 0.10f,
239 m_boardAdapter.m_SilkScreenColorTop.g * m_boardAdapter.m_SilkScreenColorTop.g + 0.10f,
240 m_boardAdapter.m_SilkScreenColorTop.b * m_boardAdapter.m_SilkScreenColorTop.b + 0.10f );
241
242 m_materials.m_SilkSTop.m_Shininess = 0.078125f * 128.0f;
243 m_materials.m_SilkSTop.m_Emissive = SFVEC3F( 0.0f, 0.0f, 0.0f );
244
245 // Silk screen material mixed with silk screen color
246 m_materials.m_SilkSBot.m_Ambient = SFVEC3F( m_boardAdapter.m_SilkScreenColorBot.r,
247 m_boardAdapter.m_SilkScreenColorBot.g,
248 m_boardAdapter.m_SilkScreenColorBot.b );
249
250 m_materials.m_SilkSBot.m_Specular = SFVEC3F(
251 m_boardAdapter.m_SilkScreenColorBot.r * m_boardAdapter.m_SilkScreenColorBot.r + 0.10f,
252 m_boardAdapter.m_SilkScreenColorBot.g * m_boardAdapter.m_SilkScreenColorBot.g + 0.10f,
253 m_boardAdapter.m_SilkScreenColorBot.b * m_boardAdapter.m_SilkScreenColorBot.b + 0.10f );
254
255 m_materials.m_SilkSBot.m_Shininess = 0.078125f * 128.0f;
256 m_materials.m_SilkSBot.m_Emissive = SFVEC3F( 0.0f, 0.0f, 0.0f );
257
258 m_materials.m_SolderMask.m_Shininess = 0.8f * 128.0f;
259 m_materials.m_SolderMask.m_Emissive = SFVEC3F( 0.0f, 0.0f, 0.0f );
260
261 // Epoxy material
262 m_materials.m_EpoxyBoard.m_Ambient = SFVEC3F( 117.0f / 255.0f, 97.0f / 255.0f,
263 47.0f / 255.0f );
264
265 m_materials.m_EpoxyBoard.m_Specular = SFVEC3F( 18.0f / 255.0f, 3.0f / 255.0f,
266 20.0f / 255.0f );
267
268 m_materials.m_EpoxyBoard.m_Shininess = 0.1f * 128.0f;
269 m_materials.m_EpoxyBoard.m_Emissive = SFVEC3F( 0.0f, 0.0f, 0.0f );
270}
271
272
274{
275 if( m_boardAdapter.GetUseBoardEditorCopperLayerColors() && IsCopperLayer( aLayerID ) )
276 {
277 COLOR4D copper_color = m_boardAdapter.m_BoardEditorColors[aLayerID];
278 m_materials.m_Copper.m_Diffuse = SFVEC3F( copper_color.r, copper_color.g,
279 copper_color.b );
280 OglSetMaterial( m_materials.m_Copper, 1.0f );
281 m_materials.m_NonPlatedCopper.m_Diffuse = m_materials.m_Copper.m_Diffuse;
282 OglSetMaterial( m_materials.m_NonPlatedCopper, 1.0f );
283
284 return;
285 }
286
287 switch( aLayerID )
288 {
289 case F_Mask:
290 case B_Mask:
291 {
292 const SFVEC4F layerColor = aLayerID == F_Mask ? m_boardAdapter.m_SolderMaskColorTop
293 : m_boardAdapter.m_SolderMaskColorBot;
294
295 m_materials.m_SolderMask.m_Diffuse = layerColor;
296
297 // Convert Opacity to Transparency
298 m_materials.m_SolderMask.m_Transparency = 1.0f - layerColor.a;
299
300 m_materials.m_SolderMask.m_Ambient = m_materials.m_SolderMask.m_Diffuse * 0.3f;
301
302 m_materials.m_SolderMask.m_Specular = m_materials.m_SolderMask.m_Diffuse
303 * m_materials.m_SolderMask.m_Diffuse;
304
305 OglSetMaterial( m_materials.m_SolderMask, 1.0f );
306 break;
307 }
308
309 case B_Paste:
310 case F_Paste:
311 m_materials.m_Paste.m_Diffuse = m_boardAdapter.m_SolderPasteColor;
312 OglSetMaterial( m_materials.m_Paste, 1.0f );
313 break;
314
315 case B_SilkS:
316 m_materials.m_SilkSBot.m_Diffuse = m_boardAdapter.m_SilkScreenColorBot;
317 OglSetMaterial( m_materials.m_SilkSBot, 1.0f );
318 break;
319
320 case F_SilkS:
321 m_materials.m_SilkSTop.m_Diffuse = m_boardAdapter.m_SilkScreenColorTop;
322 OglSetMaterial( m_materials.m_SilkSTop, 1.0f );
323 break;
324
325 case B_Adhes:
326 case F_Adhes:
327 case Dwgs_User:
328 case Cmts_User:
329 case Eco1_User:
330 case Eco2_User:
331 case Edge_Cuts:
332 case Margin:
333 case B_CrtYd:
334 case F_CrtYd:
335 case B_Fab:
336 case F_Fab:
337 switch( aLayerID )
338 {
339 case Dwgs_User: m_materials.m_Plastic.m_Diffuse = m_boardAdapter.m_UserDrawingsColor; break;
340 case Cmts_User: m_materials.m_Plastic.m_Diffuse = m_boardAdapter.m_UserCommentsColor; break;
341 case Eco1_User: m_materials.m_Plastic.m_Diffuse = m_boardAdapter.m_ECO1Color; break;
342 case Eco2_User: m_materials.m_Plastic.m_Diffuse = m_boardAdapter.m_ECO2Color; break;
343 case Edge_Cuts: m_materials.m_Plastic.m_Diffuse = m_boardAdapter.m_UserDrawingsColor; break;
344 case Margin: m_materials.m_Plastic.m_Diffuse = m_boardAdapter.m_UserDrawingsColor; break;
345 default:
346 m_materials.m_Plastic.m_Diffuse = m_boardAdapter.GetLayerColor( aLayerID );
347 break;
348 }
349
350 m_materials.m_Plastic.m_Ambient = SFVEC3F( m_materials.m_Plastic.m_Diffuse.r * 0.05f,
351 m_materials.m_Plastic.m_Diffuse.g * 0.05f,
352 m_materials.m_Plastic.m_Diffuse.b * 0.05f );
353
354 m_materials.m_Plastic.m_Specular = SFVEC3F( m_materials.m_Plastic.m_Diffuse.r * 0.7f,
355 m_materials.m_Plastic.m_Diffuse.g * 0.7f,
356 m_materials.m_Plastic.m_Diffuse.b * 0.7f );
357
358 m_materials.m_Plastic.m_Shininess = 0.078125f * 128.0f;
359 m_materials.m_Plastic.m_Emissive = SFVEC3F( 0.0f, 0.0f, 0.0f );
360 OglSetMaterial( m_materials.m_Plastic, 1.0f );
361 break;
362
363 default:
364 {
365 int layer3D = MapPCBLayerTo3DLayer( aLayerID );
366
367 // Note: MUST do this in LAYER_3D space; User_1..User_45 are NOT contiguous
368 if( layer3D >= LAYER_3D_USER_1 && layer3D <= LAYER_3D_USER_45 )
369 {
370 int user_idx = layer3D - LAYER_3D_USER_1;
371
372 m_materials.m_Plastic.m_Diffuse = m_boardAdapter.m_UserDefinedLayerColor[ user_idx ];
373 m_materials.m_Plastic.m_Ambient = SFVEC3F( m_materials.m_Plastic.m_Diffuse.r * 0.05f,
374 m_materials.m_Plastic.m_Diffuse.g * 0.05f,
375 m_materials.m_Plastic.m_Diffuse.b * 0.05f );
376
377 m_materials.m_Plastic.m_Specular = SFVEC3F( m_materials.m_Plastic.m_Diffuse.r * 0.7f,
378 m_materials.m_Plastic.m_Diffuse.g * 0.7f,
379 m_materials.m_Plastic.m_Diffuse.b * 0.7f );
380
381 m_materials.m_Plastic.m_Shininess = 0.078125f * 128.0f;
382 m_materials.m_Plastic.m_Emissive = SFVEC3F( 0.0f, 0.0f, 0.0f );
383 OglSetMaterial( m_materials.m_Plastic, 1.0f );
384 break;
385 }
386
387 m_materials.m_Copper.m_Diffuse = m_boardAdapter.m_CopperColor;
388 OglSetMaterial( m_materials.m_Copper, 1.0f );
389 break;
390 }
391 }
392}
393
394
396{
397 // Setup light
398 // https://www.opengl.org/sdk/docs/man2/xhtml/glLight.xml
399 const GLfloat ambient[] = { 0.084f, 0.084f, 0.084f, 1.0f };
400 const GLfloat diffuse0[] = { 0.3f, 0.3f, 0.3f, 1.0f };
401 const GLfloat specular0[] = { 0.5f, 0.5f, 0.5f, 1.0f };
402
403 glLightfv( GL_LIGHT0, GL_AMBIENT, ambient );
404 glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse0 );
405 glLightfv( GL_LIGHT0, GL_SPECULAR, specular0 );
406
407 const GLfloat diffuse12[] = { 0.7f, 0.7f, 0.7f, 1.0f };
408 const GLfloat specular12[] = { 0.7f, 0.7f, 0.7f, 1.0f };
409
410 // defines a directional light that points along the negative z-axis
411 GLfloat position[4] = { 0.0f, 0.0f, 1.0f, 0.0f };
412
413 // This makes a vector slight not perpendicular with XZ plane
414 const SFVEC3F vectorLight = SphericalToCartesian( glm::pi<float>() * 0.03f,
415 glm::pi<float>() * 0.25f );
416
417 position[0] = vectorLight.x;
418 position[1] = vectorLight.y;
419 position[2] = vectorLight.z;
420
421 glLightfv( GL_LIGHT1, GL_AMBIENT, ambient );
422 glLightfv( GL_LIGHT1, GL_DIFFUSE, diffuse12 );
423 glLightfv( GL_LIGHT1, GL_SPECULAR, specular12 );
424 glLightfv( GL_LIGHT1, GL_POSITION, position );
425
426 // defines a directional light that points along the positive z-axis
427 position[2] = -position[2];
428
429 glLightfv( GL_LIGHT2, GL_AMBIENT, ambient );
430 glLightfv( GL_LIGHT2, GL_DIFFUSE, diffuse12 );
431 glLightfv( GL_LIGHT2, GL_SPECULAR, specular12 );
432 glLightfv( GL_LIGHT2, GL_POSITION, position );
433
434 const GLfloat lmodel_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
435
436 glLightModelfv( GL_LIGHT_MODEL_AMBIENT, lmodel_ambient );
437
438 glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE );
439}
440
441
443{
444 OglSetMaterial( m_materials.m_NonPlatedCopper, 1.0f );
445}
446
447
449{
450 glEnable( GL_POLYGON_OFFSET_FILL );
451 glPolygonOffset( -0.1f, -2.0f );
452 setLayerMaterial( aLayer_id );
453}
454
455
457{
458 glDisable( GL_POLYGON_OFFSET_FILL );
459}
460
461
462void RENDER_3D_OPENGL::renderBoardBody( bool aSkipRenderHoles )
463{
464 m_materials.m_EpoxyBoard.m_Diffuse = m_boardAdapter.m_BoardBodyColor;
465
466 // opacity to transparency
467 m_materials.m_EpoxyBoard.m_Transparency = 1.0f - m_boardAdapter.m_BoardBodyColor.a;
468
469 OglSetMaterial( m_materials.m_EpoxyBoard, 1.0f );
470
471 OPENGL_RENDER_LIST* ogl_disp_list = nullptr;
472
473 if( aSkipRenderHoles )
474 ogl_disp_list = m_board;
475 else
476 ogl_disp_list = m_boardWithHoles;
477
478 if( ogl_disp_list )
479 {
480 ogl_disp_list->ApplyScalePosition( -m_boardAdapter.GetBoardBodyThickness() / 2.0f,
481 m_boardAdapter.GetBoardBodyThickness() );
482
483 ogl_disp_list->SetItIsTransparent( true );
484 ogl_disp_list->DrawAll();
485 }
486
487 // Also render post-machining plugs (board material that remains after backdrill/counterbore/countersink)
488 if( !aSkipRenderHoles && m_postMachinePlugs )
489 {
490 m_postMachinePlugs->ApplyScalePosition( -m_boardAdapter.GetBoardBodyThickness() / 2.0f,
491 m_boardAdapter.GetBoardBodyThickness() );
492
493 m_postMachinePlugs->SetItIsTransparent( true );
494 m_postMachinePlugs->DrawAll();
495 }
496}
497
498
499static inline SFVEC4F premultiplyAlpha( const SFVEC4F& aInput )
500{
501 return SFVEC4F( aInput.r * aInput.a, aInput.g * aInput.a, aInput.b * aInput.a, aInput.a );
502}
503
504
505bool RENDER_3D_OPENGL::Redraw( bool aIsMoving, REPORTER* aStatusReporter,
506 REPORTER* aWarningReporter )
507{
508 // Initialize OpenGL
510 {
511 if( !initializeOpenGL() )
512 return false;
513 }
514
516
518 {
519 std::unique_ptr<BUSY_INDICATOR> busy = CreateBusyIndicator();
520
521 if( aStatusReporter )
522 aStatusReporter->Report( _( "Loading..." ) );
523
524 // Careful here!
525 // We are in the middle of rendering and the reload method may show
526 // a dialog box that requires the opengl context for a redraw
527 Pgm().GetGLContextManager()->RunWithoutCtxLock( [this, aStatusReporter, aWarningReporter]()
528 {
529 reload( aStatusReporter, aWarningReporter );
530 } );
531
532 // generate a new 3D grid as the size of the board may had changed
533 m_lastGridType = static_cast<GRID3D_TYPE>( cfg.grid_type );
535 }
536 else
537 {
538 // Check if grid was changed
539 if( cfg.grid_type != m_lastGridType )
540 {
541 // and generate a new one
542 m_lastGridType = static_cast<GRID3D_TYPE>( cfg.grid_type );
544 }
545 }
546
548
549 // Initial setup
550 glDepthFunc( GL_LESS );
551 glEnable( GL_CULL_FACE );
552 glFrontFace( GL_CCW ); // This is the OpenGL default
553 glEnable( GL_NORMALIZE ); // This allow OpenGL to normalize the normals after transformations
554 glViewport( 0, 0, m_windowSize.x, m_windowSize.y );
555
556 if( aIsMoving && cfg.opengl_AA_disableOnMove )
557 glDisable( GL_MULTISAMPLE );
558 else
559 glEnable( GL_MULTISAMPLE );
560
561 // clear color and depth buffers
562 glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
563 glClearDepth( 1.0f );
564 glClearStencil( 0x00 );
565 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
566
568
569 // Draw the background ( rectangle with color gradient)
571 premultiplyAlpha( m_boardAdapter.m_BgColorBot ) );
572
573 glEnable( GL_DEPTH_TEST );
574
575 // Set projection and modelview matrixes
576 glMatrixMode( GL_PROJECTION );
577 glLoadMatrixf( glm::value_ptr( m_camera.GetProjectionMatrix() ) );
578 glMatrixMode( GL_MODELVIEW );
579 glLoadIdentity();
580 glLoadMatrixf( glm::value_ptr( m_camera.GetViewMatrix() ) );
581
582 // Position the headlight
583 setLightFront( true );
584 setLightTop( true );
585 setLightBottom( true );
586
587 glEnable( GL_LIGHTING );
588
589 {
590 const SFVEC3F& cameraPos = m_camera.GetPos();
591
592 // Place the light at a minimum Z so the diffuse factor will not drop
593 // and the board will still look with good light.
594 float zpos;
595
596 if( cameraPos.z > 0.0f )
597 zpos = glm::max( cameraPos.z, 0.5f ) + cameraPos.z * cameraPos.z;
598 else
599 zpos = glm::min( cameraPos.z,-0.5f ) - cameraPos.z * cameraPos.z;
600
601 // This is a point light.
602 const GLfloat headlight_pos[] = { cameraPos.x, cameraPos.y, zpos, 1.0f };
603
604 glLightfv( GL_LIGHT0, GL_POSITION, headlight_pos );
605 }
606
607 bool skipThickness = aIsMoving && cfg.opengl_thickness_disableOnMove;
608 bool skipRenderHoles = aIsMoving && cfg.opengl_holes_disableOnMove;
609 bool skipRenderMicroVias = aIsMoving && cfg.opengl_microvias_disableOnMove;
610 bool showThickness = !skipThickness;
611
612 std::bitset<LAYER_3D_END> layerFlags = m_boardAdapter.GetVisibleLayers();
613
615
616 if( !( skipRenderMicroVias || skipRenderHoles ) && m_microviaHoles )
617 m_microviaHoles->DrawAll();
618
619 if( !skipRenderHoles && m_padHoles )
620 m_padHoles->DrawAll();
621
622 // Display copper and tech layers
623 for( MAP_OGL_DISP_LISTS::const_iterator ii = m_layers.begin(); ii != m_layers.end(); ++ii )
624 {
625 const PCB_LAYER_ID layer = ( PCB_LAYER_ID )( ii->first );
626 bool isSilkLayer = layer == F_SilkS || layer == B_SilkS;
627 bool isMaskLayer = layer == F_Mask || layer == B_Mask;
628 bool isPasteLayer = layer == F_Paste || layer == B_Paste;
629
630 // Mask layers are not processed here because they are a special case
631 if( isMaskLayer )
632 continue;
633
634 // Do not show inner layers when it is displaying the board and board body is opaque
635 // enough: the time to create inner layers can be *really significant*.
636 // So avoid creating them is they are not very visible
637 const double opacity_min = 0.8;
638
639 if( layerFlags.test( LAYER_3D_BOARD ) && m_boardAdapter.m_BoardBodyColor.a > opacity_min )
640 {
641 // generating internal copper layers is time consuming. so skip them
642 // if the board body is masking them (i.e. if the opacity is near 1.0)
643 // B_Cu is layer 2 and all inner layers are higher values
644 if( layer > B_Cu && IsCopperLayer( layer ) )
645 continue;
646 }
647
648 glPushMatrix();
649
650 OPENGL_RENDER_LIST* pLayerDispList = static_cast<OPENGL_RENDER_LIST*>( ii->second );
651
652 if( IsCopperLayer( layer ) )
653 {
654 if( cfg.DifferentiatePlatedCopper() )
656 else
657 setLayerMaterial( layer );
658
659 OPENGL_RENDER_LIST* outerTH = nullptr;
660 OPENGL_RENDER_LIST* viaHoles = nullptr;
661
662 if( !skipRenderHoles )
663 {
664 outerTH = m_outerThroughHoles;
665 viaHoles = m_outerLayerHoles[layer];
666 }
667
668 if( m_antiBoard )
669 m_antiBoard->ApplyScalePosition( pLayerDispList );
670
671 if( outerTH )
672 outerTH->ApplyScalePosition( pLayerDispList );
673
674 pLayerDispList->DrawCulled( showThickness, outerTH, viaHoles, m_antiBoard );
675
676 // Draw plated & offboard pads
677 if( layer == F_Cu && ( m_platedPadsFront || m_offboardPadsFront ) )
678 {
680
682 m_platedPadsFront->DrawCulled( showThickness, outerTH, viaHoles, m_antiBoard );
683
685 m_offboardPadsFront->DrawCulled( showThickness, outerTH, viaHoles );
686 }
687 else if( layer == B_Cu && ( m_platedPadsBack || m_offboardPadsBack ) )
688 {
690
691 if( m_platedPadsBack )
692 m_platedPadsBack->DrawCulled( showThickness, outerTH, viaHoles, m_antiBoard );
693
695 m_offboardPadsBack->DrawCulled( showThickness, outerTH, viaHoles );
696 }
697
699 }
700 else
701 {
702 setLayerMaterial( layer );
703
704 OPENGL_RENDER_LIST* throughHolesOuter = nullptr;
705 OPENGL_RENDER_LIST* anti_board = nullptr;
706 OPENGL_RENDER_LIST* solder_mask = nullptr;
707
708 if( !skipRenderHoles )
709 {
710 if( isSilkLayer && cfg.clip_silk_on_via_annuli )
711 throughHolesOuter = m_outerThroughHoleRings;
712 else
713 throughHolesOuter = m_outerThroughHoles;
714 }
715
716 if( isSilkLayer && cfg.show_off_board_silk )
717 anti_board = nullptr;
718 else if( LSET::PhysicalLayersMask().test( layer ) )
719 anti_board = m_antiBoard;
720
721 if( isSilkLayer && cfg.subtract_mask_from_silk && !cfg.show_off_board_silk )
722 solder_mask = m_layers[ ( layer == B_SilkS) ? B_Mask : F_Mask ];
723
724 if( throughHolesOuter )
725 throughHolesOuter->ApplyScalePosition( pLayerDispList );
726
727 if( anti_board )
728 anti_board->ApplyScalePosition( pLayerDispList );
729
730 if( solder_mask )
731 solder_mask->ApplyScalePosition( pLayerDispList );
732
733 pLayerDispList->DrawCulled( showThickness, solder_mask, throughHolesOuter, anti_board );
734 }
735
736 glPopMatrix();
737 }
738
739 glm::mat4 cameraViewMatrix;
740
741 glGetFloatv( GL_MODELVIEW_MATRIX, glm::value_ptr( cameraViewMatrix ) );
742
743 // Render 3D Models (Non-transparent)
744 renderOpaqueModels( cameraViewMatrix );
745
746 // Display board body
747 if( layerFlags.test( LAYER_3D_BOARD ) )
748 renderBoardBody( skipRenderHoles );
749
750 // Display transparent mask layers
751 if( layerFlags.test( LAYER_3D_SOLDERMASK_TOP )
752 || layerFlags.test( LAYER_3D_SOLDERMASK_BOTTOM ) )
753 {
754 // add a depth buffer offset, it will help to hide some artifacts
755 // on silkscreen where the SolderMask is removed
756 glEnable( GL_POLYGON_OFFSET_FILL );
757 glPolygonOffset( 0.0f, -2.0f );
758
759 if( m_camera.GetPos().z > 0 )
760 {
761 if( layerFlags.test( LAYER_3D_SOLDERMASK_BOTTOM ) )
762 {
764 showThickness, skipRenderHoles );
765 }
766
767 if( layerFlags.test( LAYER_3D_SOLDERMASK_TOP ) )
768 {
769 renderSolderMaskLayer( F_Mask, m_boardAdapter.GetLayerBottomZPos( F_Mask ),
770 showThickness, skipRenderHoles );
771 }
772 }
773 else
774 {
775 if( layerFlags.test( LAYER_3D_SOLDERMASK_TOP ) )
776 {
777 renderSolderMaskLayer( F_Mask, m_boardAdapter.GetLayerBottomZPos( F_Mask ),
778 showThickness, skipRenderHoles );
779 }
780
781 if( layerFlags.test( LAYER_3D_SOLDERMASK_BOTTOM ) )
782 {
784 showThickness, skipRenderHoles );
785 }
786 }
787
788 glDisable( GL_POLYGON_OFFSET_FILL );
789 glPolygonOffset( 0.0f, 0.0f );
790 }
791
792 // Render 3D Models (Transparent)
793 // !TODO: this can be optimized. If there are no transparent models (or no opacity),
794 // then there is no need to make this function call.
795 glDepthMask( GL_FALSE );
796 glEnable( GL_BLEND );
797 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
798
799 // Enables Texture Env so it can combine model transparency with each footprint opacity
800 glEnable( GL_TEXTURE_2D );
801 glActiveTexture( GL_TEXTURE0 );
802
803 // Uses an existent texture so the glTexEnv operations will work.
804 glBindTexture( GL_TEXTURE_2D, m_circleTexture );
805
806 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE );
807 glTexEnvf( GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE );
808 glTexEnvf( GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_MODULATE );
809
810 glTexEnvi( GL_TEXTURE_ENV, GL_SRC0_RGB, GL_PRIMARY_COLOR );
811 glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR );
812
813 glTexEnvi( GL_TEXTURE_ENV, GL_SRC1_RGB, GL_PREVIOUS );
814 glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR );
815
816 glTexEnvi( GL_TEXTURE_ENV, GL_SRC0_ALPHA, GL_PRIMARY_COLOR );
817 glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA );
818 glTexEnvi( GL_TEXTURE_ENV, GL_SRC1_ALPHA, GL_CONSTANT );
819 glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA );
820
821 renderTransparentModels( cameraViewMatrix );
822
823 glDisable( GL_BLEND );
825
826 glDepthMask( GL_TRUE );
827
828 // Render Grid
829 if( cfg.grid_type != GRID3D_TYPE::NONE )
830 {
831 glDisable( GL_LIGHTING );
832
833 if( glIsList( m_grid ) )
834 glCallList( m_grid );
835
836 glEnable( GL_LIGHTING );
837 }
838
839 // Render 3D arrows
840 if( cfg.show_navigator )
841 m_spheres_gizmo->render3dSpheresGizmo( m_camera.GetRotationMatrix() );
842
843 // Return back to the original viewport (this is important if we want
844 // to take a screenshot after the render)
845 glViewport( 0, 0, m_windowSize.x, m_windowSize.y );
846
847 return false;
848}
849
850
852{
853 glEnable( GL_LINE_SMOOTH );
854 glShadeModel( GL_SMOOTH );
855
856 // 4-byte pixel alignment
857 glPixelStorei( GL_UNPACK_ALIGNMENT, 4 );
858
859 // Initialize the open GL texture to draw the filled semi-circle of the segments
861
862 if( !circleImage )
863 return false;
864
865 unsigned int circleRadius = ( SIZE_OF_CIRCLE_TEXTURE / 2 ) - 4;
866
867 circleImage->CircleFilled( ( SIZE_OF_CIRCLE_TEXTURE / 2 ) - 0,
868 ( SIZE_OF_CIRCLE_TEXTURE / 2 ) - 0,
869 circleRadius,
870 0xFF );
871
872 IMAGE* circleImageBlured = new IMAGE( circleImage->GetWidth(), circleImage->GetHeight() );
873
874 circleImageBlured->EfxFilter_SkipCenter( circleImage, IMAGE_FILTER::GAUSSIAN_BLUR, circleRadius - 8 );
875
876 m_circleTexture = OglLoadTexture( *circleImageBlured );
877
878 delete circleImageBlured;
879 circleImageBlured = nullptr;
880
881 delete circleImage;
882 circleImage = nullptr;
883
884 init_lights();
885
886 // Use this mode if you want see the triangle lines (debug proposes)
887 //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
888 m_canvasInitialized = true;
889
890 return true;
891}
892
893
895{
896 glEnable( GL_COLOR_MATERIAL );
897 glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
898
899 const SFVEC4F ambient = SFVEC4F( 0.0f, 0.0f, 0.0f, 1.0f );
900 const SFVEC4F diffuse = SFVEC4F( 0.0f, 0.0f, 0.0f, 1.0f );
901 const SFVEC4F emissive = SFVEC4F( 0.0f, 0.0f, 0.0f, 1.0f );
902 const SFVEC4F specular = SFVEC4F( 0.1f, 0.1f, 0.1f, 1.0f );
903
904 glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, &specular.r );
905 glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, 96.0f );
906
907 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, &ambient.r );
908 glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, &diffuse.r );
909 glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, &emissive.r );
910}
911
912
914{
915#define DELETE_AND_FREE( ptr ) \
916 { \
917 delete ptr; \
918 ptr = nullptr; \
919 } \
920
921#define DELETE_AND_FREE_MAP( map ) \
922 { \
923 for( auto& [ layer, ptr ] : map ) \
924 delete ptr; \
925 \
926 map.clear(); \
927 }
928
929 if( glIsList( m_grid ) )
930 glDeleteLists( m_grid, 1 );
931
932 m_grid = 0;
933
935
940
943
945 delete list;
946
947 m_triangles.clear();
948
950
951 m_3dModelMatrixMap.clear();
952
957
961
966}
967
968
970 bool aShowThickness, bool aSkipRenderHoles )
971{
972 wxASSERT( (aLayerID == B_Mask) || (aLayerID == F_Mask) );
973
974 if( m_board )
975 {
976 OPENGL_RENDER_LIST* solder_mask = m_layers[ aLayerID ];
977 OPENGL_RENDER_LIST* via_holes = aSkipRenderHoles ? nullptr : m_outerThroughHoles;
978
979 if( via_holes )
980 via_holes->ApplyScalePosition( aZPos, m_boardAdapter.GetNonCopperLayerThickness() );
981
982 m_board->ApplyScalePosition( aZPos, m_boardAdapter.GetNonCopperLayerThickness() );
983
984 setLayerMaterial( aLayerID );
985 m_board->SetItIsTransparent( true );
986 m_board->DrawCulled( aShowThickness, solder_mask, via_holes );
987
988 if( aLayerID == F_Mask && m_viaFrontCover )
989 {
990 m_viaFrontCover->ApplyScalePosition( aZPos, 4 * m_boardAdapter.GetNonCopperLayerThickness() );
991 m_viaFrontCover->DrawTop();
992 }
993 else if( aLayerID == B_Mask && m_viaBackCover )
994 {
995 m_viaBackCover->ApplyScalePosition( aZPos, 4 * m_boardAdapter.GetNonCopperLayerThickness() );
996 m_viaBackCover->DrawBot();
997 }
998 }
999}
1000
1001
1002void RENDER_3D_OPENGL::get3dModelsSelected( std::list<MODELTORENDER> &aDstRenderList, bool aGetTop,
1003 bool aGetBot, bool aRenderTransparentOnly,
1004 bool aRenderSelectedOnly )
1005{
1006 wxASSERT( ( aGetTop == true ) || ( aGetBot == true ) );
1007
1008 if( !m_boardAdapter.GetBoard() )
1009 return;
1010
1012
1013 // Go for all footprints
1014 for( FOOTPRINT* fp : m_boardAdapter.GetBoard()->Footprints() )
1015 {
1016 bool highlight = false;
1017
1018 if( m_boardAdapter.m_IsBoardView )
1019 {
1020 if( fp->IsSelected() )
1021 highlight = true;
1022
1024 highlight = true;
1025
1026 if( aRenderSelectedOnly != highlight )
1027 continue;
1028 }
1029
1030 if( !fp->Models().empty() )
1031 {
1032 if( m_boardAdapter.IsFootprintShown( (FOOTPRINT_ATTR_T) fp->GetAttributes() ) )
1033 {
1034 const bool isFlipped = fp->IsFlipped();
1035
1036 if( aGetTop == !isFlipped || aGetBot == isFlipped )
1037 get3dModelsFromFootprint( aDstRenderList, fp, aRenderTransparentOnly,
1038 highlight );
1039 }
1040 }
1041 }
1042}
1043
1044
1045void RENDER_3D_OPENGL::get3dModelsFromFootprint( std::list<MODELTORENDER> &aDstRenderList,
1046 const FOOTPRINT* aFootprint,
1047 bool aRenderTransparentOnly, bool aIsSelected )
1048{
1049 if( !aFootprint->Models().empty() )
1050 {
1051 const double zpos = m_boardAdapter.GetFootprintZPos( aFootprint->IsFlipped() );
1052
1053 VECTOR2I pos = aFootprint->GetPosition();
1054
1055 glm::mat4 fpMatrix( 1.0f );
1056
1057 fpMatrix = glm::translate( fpMatrix, SFVEC3F( pos.x * m_boardAdapter.BiuTo3dUnits(),
1058 -pos.y * m_boardAdapter.BiuTo3dUnits(),
1059 zpos ) );
1060
1061 if( !aFootprint->GetOrientation().IsZero() )
1062 {
1063 fpMatrix = glm::rotate( fpMatrix, (float) aFootprint->GetOrientation().AsRadians(),
1064 SFVEC3F( 0.0f, 0.0f, 1.0f ) );
1065 }
1066
1067 if( aFootprint->IsFlipped() )
1068 {
1069 fpMatrix = glm::rotate( fpMatrix, glm::pi<float>(), SFVEC3F( 0.0f, 1.0f, 0.0f ) );
1070 fpMatrix = glm::rotate( fpMatrix, glm::pi<float>(), SFVEC3F( 0.0f, 0.0f, 1.0f ) );
1071 }
1072
1073 double modelunit_to_3d_units_factor = m_boardAdapter.BiuTo3dUnits() * UNITS3D_TO_UNITSPCB;
1074
1075 fpMatrix = glm::scale( fpMatrix, SFVEC3F( modelunit_to_3d_units_factor ) );
1076
1077 // Get the list of model files for this model
1078 for( const FP_3DMODEL& sM : aFootprint->Models() )
1079 {
1080 if( !sM.m_Show || sM.m_Filename.empty() )
1081 continue;
1082
1083 // Check if the model is present in our cache map
1084 auto cache_i = m_3dModelMap.find( sM.m_Filename );
1085
1086 if( cache_i == m_3dModelMap.end() )
1087 continue;
1088
1089 if( const MODEL_3D* modelPtr = cache_i->second )
1090 {
1091 bool opaque = sM.m_Opacity >= 1.0;
1092
1093 if( ( !aRenderTransparentOnly && modelPtr->HasOpaqueMeshes() && opaque ) ||
1094 ( aRenderTransparentOnly && ( modelPtr->HasTransparentMeshes() || !opaque ) ) )
1095 {
1096 glm::mat4 modelworldMatrix = fpMatrix;
1097
1098 const SFVEC3F offset = SFVEC3F( sM.m_Offset.x, sM.m_Offset.y, sM.m_Offset.z );
1099 const SFVEC3F rotation = SFVEC3F( sM.m_Rotation.x, sM.m_Rotation.y,
1100 sM.m_Rotation.z );
1101 const SFVEC3F scale = SFVEC3F( sM.m_Scale.x, sM.m_Scale.y, sM.m_Scale.z );
1102
1103 std::vector<float> key = { offset.x, offset.y, offset.z,
1104 rotation.x, rotation.y, rotation.z,
1105 scale.x, scale.y, scale.z };
1106
1107 auto it = m_3dModelMatrixMap.find( key );
1108
1109 if( it != m_3dModelMatrixMap.end() )
1110 {
1111 modelworldMatrix *= it->second;
1112 }
1113 else
1114 {
1115 glm::mat4 mtx( 1.0f );
1116 mtx = glm::translate( mtx, offset );
1117 mtx = glm::rotate( mtx, glm::radians( -rotation.z ), { 0.0f, 0.0f, 1.0f } );
1118 mtx = glm::rotate( mtx, glm::radians( -rotation.y ), { 0.0f, 1.0f, 0.0f } );
1119 mtx = glm::rotate( mtx, glm::radians( -rotation.x ), { 1.0f, 0.0f, 0.0f } );
1120 mtx = glm::scale( mtx, scale );
1121 m_3dModelMatrixMap[ key ] = mtx;
1122
1123 modelworldMatrix *= mtx;
1124 }
1125
1126 aDstRenderList.emplace_back( modelworldMatrix, modelPtr,
1127 aRenderTransparentOnly ? sM.m_Opacity : 1.0f,
1128 aRenderTransparentOnly,
1129 aFootprint->IsSelected() || aIsSelected );
1130 }
1131 }
1132 }
1133 }
1134}
1135
1136
1137void RENDER_3D_OPENGL::renderOpaqueModels( const glm::mat4 &aCameraViewMatrix )
1138{
1140
1141 const SFVEC3F selColor = m_boardAdapter.GetColor( cfg.opengl_selection_color );
1142
1143 glPushMatrix();
1144
1145 std::list<MODELTORENDER> renderList;
1146
1147 if( m_boardAdapter.m_IsBoardView )
1148 {
1149 renderList.clear();
1150
1151 get3dModelsSelected( renderList, true, true, false, true );
1152
1153 if( !renderList.empty() )
1154 {
1155 MODEL_3D::BeginDrawMulti( false );
1156
1157 for( const MODELTORENDER& mtr : renderList )
1158 renderModel( aCameraViewMatrix, mtr, selColor, nullptr );
1159
1161 }
1162 }
1163
1164 renderList.clear();
1165 get3dModelsSelected( renderList, true, true, false, false );
1166
1167 if( !renderList.empty() )
1168 {
1170
1171 for( const MODELTORENDER& mtr : renderList )
1172 renderModel( aCameraViewMatrix, mtr, selColor, nullptr );
1173
1175 }
1176
1177 glPopMatrix();
1178}
1179
1180
1181void RENDER_3D_OPENGL::renderTransparentModels( const glm::mat4 &aCameraViewMatrix )
1182{
1184
1185 const SFVEC3F selColor = m_boardAdapter.GetColor( cfg.opengl_selection_color );
1186
1187 std::list<MODELTORENDER> renderListModels; // do not clear it until this function returns
1188
1189 if( m_boardAdapter.m_IsBoardView )
1190 {
1191 // Get Transparent Selected
1192 get3dModelsSelected( renderListModels, true, true, true, true );
1193 }
1194
1195 // Get Transparent Not Selected
1196 get3dModelsSelected( renderListModels, true, true, true, false );
1197
1198 if( renderListModels.empty() )
1199 return;
1200
1201 std::vector<std::pair<const MODELTORENDER *, float>> transparentModelList;
1202
1203 transparentModelList.reserve( renderListModels.size() );
1204
1205 // Calculate the distance to the camera for each model
1206 const SFVEC3F &cameraPos = m_camera.GetPos();
1207
1208 for( const MODELTORENDER& mtr : renderListModels )
1209 {
1210 const BBOX_3D& bBox = mtr.m_model->GetBBox();
1211 const SFVEC3F& bBoxCenter = bBox.GetCenter();
1212 const SFVEC3F bBoxWorld = mtr.m_modelWorldMat * glm::vec4( bBoxCenter, 1.0f );
1213
1214 const float distanceToCamera = glm::length( cameraPos - bBoxWorld );
1215
1216 transparentModelList.emplace_back( &mtr, distanceToCamera );
1217 }
1218
1219 // Sort from back to front
1220 std::sort( transparentModelList.begin(), transparentModelList.end(),
1221 [&]( std::pair<const MODELTORENDER *, float>& a,
1222 std::pair<const MODELTORENDER *, float>& b )
1223 {
1224 if( a.second != b.second )
1225 return a.second > b.second;
1226
1227 return a.first > b.first; // use pointers as a last resort
1228 } );
1229
1230 // Start rendering calls
1231 glPushMatrix();
1232
1233 bool isUsingColorInformation = !( transparentModelList.begin()->first->m_isSelected &&
1234 m_boardAdapter.m_IsBoardView );
1235
1236 MODEL_3D::BeginDrawMulti( isUsingColorInformation );
1237
1238 for( const std::pair<const MODELTORENDER *, float>& mtr : transparentModelList )
1239 {
1240 if( m_boardAdapter.m_IsBoardView )
1241 {
1242 // Toggle between using model color or the select color
1243 if( !isUsingColorInformation && !mtr.first->m_isSelected )
1244 {
1245 isUsingColorInformation = true;
1246
1247 glEnableClientState( GL_COLOR_ARRAY );
1248 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
1249 glEnable( GL_COLOR_MATERIAL );
1250 }
1251 else if( isUsingColorInformation && mtr.first->m_isSelected )
1252 {
1253 isUsingColorInformation = false;
1254
1255 glDisableClientState( GL_COLOR_ARRAY );
1256 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1257 glDisable( GL_COLOR_MATERIAL );
1258 }
1259 }
1260
1261 // Render model, sort each individuall material group
1262 // by passing cameraPos
1263 renderModel( aCameraViewMatrix, *mtr.first, selColor, &cameraPos );
1264 }
1265
1267
1268 glPopMatrix();
1269}
1270
1271
1272void RENDER_3D_OPENGL::renderModel( const glm::mat4 &aCameraViewMatrix,
1273 const MODELTORENDER &aModelToRender,
1274 const SFVEC3F &aSelColor, const SFVEC3F *aCameraWorldPos )
1275{
1277
1278 const glm::mat4 modelviewMatrix = aCameraViewMatrix * aModelToRender.m_modelWorldMat;
1279
1280 glLoadMatrixf( glm::value_ptr( modelviewMatrix ) );
1281
1282 aModelToRender.m_model->Draw( aModelToRender.m_isTransparent, aModelToRender.m_opacity,
1283 aModelToRender.m_isSelected, aSelColor,
1284 &aModelToRender.m_modelWorldMat, aCameraWorldPos );
1285
1286 if( cfg.show_model_bbox )
1287 {
1288 const bool wasBlendEnabled = glIsEnabled( GL_BLEND );
1289
1290 if( !wasBlendEnabled )
1291 {
1292 glEnable( GL_BLEND );
1293 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
1294 }
1295
1296 glDisable( GL_LIGHTING );
1297
1298 glLineWidth( 1 );
1299 aModelToRender.m_model->DrawBboxes();
1300
1301 glLineWidth( 4 );
1302 aModelToRender.m_model->DrawBbox();
1303
1304 glEnable( GL_LIGHTING );
1305
1306 if( !wasBlendEnabled )
1307 glDisable( GL_BLEND );
1308 }
1309}
1310
1311
1313{
1314 if( glIsList( m_grid ) )
1315 glDeleteLists( m_grid, 1 );
1316
1317 m_grid = 0;
1318
1319 if( aGridType == GRID3D_TYPE::NONE )
1320 return;
1321
1322 m_grid = glGenLists( 1 );
1323
1324 if( !glIsList( m_grid ) )
1325 return;
1326
1327 glNewList( m_grid, GL_COMPILE );
1328
1329 glEnable( GL_BLEND );
1330 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
1331
1332 const double zpos = 0.0;
1333
1334 // Color of grid lines
1335 const SFVEC3F gridColor = m_boardAdapter.GetColor( DARKGRAY );
1336
1337 // Color of grid lines every 5 lines
1338 const SFVEC3F gridColor_marker = m_boardAdapter.GetColor( LIGHTBLUE );
1339 const double scale = m_boardAdapter.BiuTo3dUnits();
1340 const GLfloat transparency = 0.35f;
1341
1342 double griSizeMM = 0.0;
1343
1344 switch( aGridType )
1345 {
1346 case GRID3D_TYPE::GRID_1MM: griSizeMM = 1.0; break;
1347 case GRID3D_TYPE::GRID_2P5MM: griSizeMM = 2.5; break;
1348 case GRID3D_TYPE::GRID_5MM: griSizeMM = 5.0; break;
1349 case GRID3D_TYPE::GRID_10MM: griSizeMM = 10.0; break;
1350
1351 default:
1352 case GRID3D_TYPE::NONE: return;
1353 }
1354
1355 glNormal3f( 0.0, 0.0, 1.0 );
1356
1357 const VECTOR2I brd_size = m_boardAdapter.GetBoardSize();
1358 VECTOR2I brd_center_pos = m_boardAdapter.GetBoardPos();
1359
1360 brd_center_pos.y = -brd_center_pos.y;
1361
1362 const int xsize = std::max( brd_size.x, pcbIUScale.mmToIU( 100 ) ) * 1.2;
1363 const int ysize = std::max( brd_size.y, pcbIUScale.mmToIU( 100 ) ) * 1.2;
1364
1365 // Grid limits, in 3D units
1366 double xmin = ( brd_center_pos.x - xsize / 2 ) * scale;
1367 double xmax = ( brd_center_pos.x + xsize / 2 ) * scale;
1368 double ymin = ( brd_center_pos.y - ysize / 2 ) * scale;
1369 double ymax = ( brd_center_pos.y + ysize / 2 ) * scale;
1370 double zmin = pcbIUScale.mmToIU( -50 ) * scale;
1371 double zmax = pcbIUScale.mmToIU( 100 ) * scale;
1372
1373 // Set rasterised line width (min value = 1)
1374 glLineWidth( 1 );
1375
1376 // Draw horizontal grid centered on 3D origin (center of the board)
1377 for( int ii = 0; ; ii++ )
1378 {
1379 if( (ii % 5) )
1380 glColor4f( gridColor.r, gridColor.g, gridColor.b, transparency );
1381 else
1382 glColor4f( gridColor_marker.r, gridColor_marker.g, gridColor_marker.b,
1383 transparency );
1384
1385 const int delta = KiROUND( ii * griSizeMM * pcbIUScale.IU_PER_MM );
1386
1387 if( delta <= xsize / 2 ) // Draw grid lines parallel to X axis
1388 {
1389 glBegin( GL_LINES );
1390 glVertex3f( (brd_center_pos.x + delta) * scale, -ymin, zpos );
1391 glVertex3f( (brd_center_pos.x + delta) * scale, -ymax, zpos );
1392 glEnd();
1393
1394 if( ii != 0 )
1395 {
1396 glBegin( GL_LINES );
1397 glVertex3f( (brd_center_pos.x - delta) * scale, -ymin, zpos );
1398 glVertex3f( (brd_center_pos.x - delta) * scale, -ymax, zpos );
1399 glEnd();
1400 }
1401 }
1402
1403 if( delta <= ysize / 2 ) // Draw grid lines parallel to Y axis
1404 {
1405 glBegin( GL_LINES );
1406 glVertex3f( xmin, -( brd_center_pos.y + delta ) * scale, zpos );
1407 glVertex3f( xmax, -( brd_center_pos.y + delta ) * scale, zpos );
1408 glEnd();
1409
1410 if( ii != 0 )
1411 {
1412 glBegin( GL_LINES );
1413 glVertex3f( xmin, -( brd_center_pos.y - delta ) * scale, zpos );
1414 glVertex3f( xmax, -( brd_center_pos.y - delta ) * scale, zpos );
1415 glEnd();
1416 }
1417 }
1418
1419 if( ( delta > ysize / 2 ) && ( delta > xsize / 2 ) )
1420 break;
1421 }
1422
1423 // Draw vertical grid on Z axis
1424 glNormal3f( 0.0, -1.0, 0.0 );
1425
1426 // Draw vertical grid lines (parallel to Z axis)
1427 double posy = -brd_center_pos.y * scale;
1428
1429 for( int ii = 0; ; ii++ )
1430 {
1431 if( (ii % 5) )
1432 glColor4f( gridColor.r, gridColor.g, gridColor.b, transparency );
1433 else
1434 glColor4f( gridColor_marker.r, gridColor_marker.g, gridColor_marker.b,
1435 transparency );
1436
1437 const double delta = ii * griSizeMM * pcbIUScale.IU_PER_MM;
1438
1439 glBegin( GL_LINES );
1440 xmax = ( brd_center_pos.x + delta ) * scale;
1441
1442 glVertex3f( xmax, posy, zmin );
1443 glVertex3f( xmax, posy, zmax );
1444 glEnd();
1445
1446 if( ii != 0 )
1447 {
1448 glBegin( GL_LINES );
1449 xmin = ( brd_center_pos.x - delta ) * scale;
1450 glVertex3f( xmin, posy, zmin );
1451 glVertex3f( xmin, posy, zmax );
1452 glEnd();
1453 }
1454
1455 if( delta > xsize / 2.0f )
1456 break;
1457 }
1458
1459 // Draw horizontal grid lines on Z axis (parallel to X axis)
1460 for( int ii = 0; ; ii++ )
1461 {
1462 if( ii % 5 )
1463 glColor4f( gridColor.r, gridColor.g, gridColor.b, transparency );
1464 else
1465 glColor4f( gridColor_marker.r, gridColor_marker.g, gridColor_marker.b, transparency );
1466
1467 const double delta = ii * griSizeMM * pcbIUScale.IU_PER_MM * scale;
1468
1469 if( delta <= zmax )
1470 {
1471 // Draw grid lines on Z axis (positive Z axis coordinates)
1472 glBegin( GL_LINES );
1473 glVertex3f( xmin, posy, delta );
1474 glVertex3f( xmax, posy, delta );
1475 glEnd();
1476 }
1477
1478 if( delta <= -zmin && ( ii != 0 ) )
1479 {
1480 // Draw grid lines on Z axis (negative Z axis coordinates)
1481 glBegin( GL_LINES );
1482 glVertex3f( xmin, posy, -delta );
1483 glVertex3f( xmax, posy, -delta );
1484 glEnd();
1485 }
1486
1487 if( ( delta > zmax ) && ( delta > -zmin ) )
1488 break;
1489 }
1490
1491 glDisable( GL_BLEND );
1492
1493 glEndList();
1494}
GRID3D_TYPE
Grid types.
Definition 3d_enums.h:54
Defines math related functions.
float mapf(float x, float in_min, float in_max, float out_min, float out_max)
Definition 3d_math.h:133
SFVEC3F SphericalToCartesian(float aInclination, float aAzimuth)
https://en.wikipedia.org/wiki/Spherical_coordinate_system
Definition 3d_math.h:43
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:112
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:990
Helper class to handle information needed to display 3D board.
A class used to derive camera objects from.
Definition camera.h:103
Implement a canvas based on a wxGLCanvas.
bool IsZero() const
Definition eda_angle.h:136
double AsRadians() const
Definition eda_angle.h:120
bool IsSelected() const
Definition eda_item.h:127
EDA_ANGLE GetOrientation() const
Definition footprint.h:248
bool IsFlipped() const
Definition footprint.h:434
std::vector< FP_3DMODEL > & Models()
Definition footprint.h:241
VECTOR2I GetPosition() const override
Definition footprint.h:245
auto RunWithoutCtxLock(Func &&aFunction, Args &&... args)
Run the given function first releasing the GL context lock, then restoring it.
Manage an 8-bit channel image.
Definition image.h:90
void CircleFilled(int aCx, int aCy, int aRadius, unsigned char aValue)
Definition image.cpp:173
void EfxFilter_SkipCenter(IMAGE *aInImg, IMAGE_FILTER aFilterType, unsigned int aRadius)
Apply a filter to the input image and store it in the image class.
Definition image.cpp:527
unsigned int GetHeight() const
Definition image.h:214
unsigned int GetWidth() const
Definition image.h:213
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:105
double r
Red component.
Definition color4d.h:393
double g
Green component.
Definition color4d.h:394
double b
Blue component.
Definition color4d.h:395
static const LSET & PhysicalLayersMask()
Return a mask holding all layers which are physically realized.
Definition lset.cpp:680
void DrawBbox() const
Draw main bounding box of the model.
Definition 3d_model.cpp:571
static void EndDrawMulti()
Cleanup render states after drawing multiple models.
Definition 3d_model.cpp:405
void Draw(bool aTransparent, float aOpacity, bool aUseSelectedMaterial, const SFVEC3F &aSelectionColor, const glm::mat4 *aModelWorldMatrix, const SFVEC3F *aCameraWorldPos) const
Render the model into the current context.
Definition 3d_model.cpp:418
static void BeginDrawMulti(bool aUseColorInformation)
Set some basic render states before drawing multiple models.
Definition 3d_model.cpp:389
void DrawBboxes() const
Draw individual bounding boxes of each mesh.
Definition 3d_model.cpp:590
Store the OpenGL display lists to related with a layer.
void ApplyScalePosition(float aZposition, float aZscale)
void SetItIsTransparent(bool aSetTransparent)
void DrawCulled(bool aDrawMiddle, const OPENGL_RENDER_LIST *aSubtractList=nullptr, const OPENGL_RENDER_LIST *bSubtractList=nullptr, const OPENGL_RENDER_LIST *cSubtractList=nullptr, const OPENGL_RENDER_LIST *dSubtractList=nullptr) const
Draw all layers if they are visible by the camera if camera position is above the layer.
void DrawAll(bool aDrawMiddle=true) const
Call to draw all the display lists.
GL_CONTEXT_MANAGER * GetGLContextManager()
Definition pgm_base.h:119
std::unique_ptr< BUSY_INDICATOR > CreateBusyIndicator() const
Return a created busy indicator, if a factory has been set, else a null pointer.
RENDER_3D_BASE(BOARD_ADAPTER &aBoardAdapter, CAMERA &aCamera)
bool m_canvasInitialized
Flag if the canvas specific for this render was already initialized.
wxSize m_windowSize
The window size that this camera is working.
BOARD_ADAPTER & m_boardAdapter
Settings reference in use for this render.
OPENGL_RENDER_LIST * m_board
OPENGL_RENDER_LIST * m_outerThroughHoleRings
OPENGL_RENDER_LIST * m_offboardPadsFront
SPHERES_GIZMO::GizmoSphereSelection getSelectedGizmoSphere() const
GRID3D_TYPE m_lastGridType
Stores the last grid type.
std::tuple< int, int, int, int > getGizmoViewport() const
OPENGL_RENDER_LIST * m_microviaHoles
void renderOpaqueModels(const glm::mat4 &aCameraViewMatrix)
void generate3dGrid(GRID3D_TYPE aGridType)
Create a 3D grid to an OpenGL display list.
void setLightFront(bool enabled)
bool Redraw(bool aIsMoving, REPORTER *aStatusReporter, REPORTER *aWarningReporter) override
Redraw the view.
MAP_OGL_DISP_LISTS m_layers
MAP_OGL_DISP_LISTS m_innerLayerHoles
OPENGL_RENDER_LIST * m_boardWithHoles
RENDER_3D_OPENGL(EDA_3D_CANVAS *aCanvas, BOARD_ADAPTER &aAdapter, CAMERA &aCamera)
MAP_OGL_DISP_LISTS m_outerLayerHoles
OPENGL_RENDER_LIST * m_offboardPadsBack
BOARD_ITEM * m_currentRollOverItem
void renderBoardBody(bool aSkipRenderHoles)
std::map< std::vector< float >, glm::mat4 > m_3dModelMatrixMap
std::map< wxString, MODEL_3D * > m_3dModelMap
OPENGL_RENDER_LIST * m_viaBackCover
OPENGL_RENDER_LIST * m_viaFrontCover
LIST_TRIANGLES m_triangles
store pointers so can be deleted latter
OPENGL_RENDER_LIST * m_outerViaThroughHoles
OPENGL_RENDER_LIST * m_outerThroughHoles
void setLayerMaterial(PCB_LAYER_ID aLayerID)
OPENGL_RENDER_LIST * m_platedPadsFront
struct RENDER_3D_OPENGL::@136145154067207014164113243162246125147361200233 m_materials
void renderModel(const glm::mat4 &aCameraViewMatrix, const MODELTORENDER &aModelToRender, const SFVEC3F &aSelColor, const SFVEC3F *aCameraWorldPos)
int GetWaitForEditingTimeOut() override
Give the interface the time (in ms) that it should wait for editing or movements before (this works f...
void renderSolderMaskLayer(PCB_LAYER_ID aLayerID, float aZPos, bool aShowThickness, bool aSkipRenderHoles)
void renderTransparentModels(const glm::mat4 &aCameraViewMatrix)
void get3dModelsSelected(std::list< MODELTORENDER > &aDstRenderList, bool aGetTop, bool aGetBot, bool aRenderTransparentOnly, bool aRenderSelectedOnly)
OPENGL_RENDER_LIST * m_postMachinePlugs
Board material plugs for backdrill/counterbore/countersink.
void setPlatedCopperAndDepthOffset(PCB_LAYER_ID aLayer_id)
OPENGL_RENDER_LIST * m_antiBoard
void SetCurWindowSize(const wxSize &aSize) override
Before each render, the canvas will tell the render what is the size of its windows,...
EDA_3D_CANVAS * m_canvas
OPENGL_RENDER_LIST * m_padHoles
SPHERES_GIZMO * m_spheres_gizmo
GLuint m_grid
oGL list that stores current grid
OPENGL_RENDER_LIST * m_platedPadsBack
void get3dModelsFromFootprint(std::list< MODELTORENDER > &aDstRenderList, const FOOTPRINT *aFootprint, bool aRenderTransparentOnly, bool aIsSelected)
void handleGizmoMouseInput(int mouseX, int mouseY)
void setLightBottom(bool enabled)
void setGizmoViewport(int x, int y, int width, int height)
void setLightTop(bool enabled)
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:102
Renders a set of colored spheres in 3D space that act as a directional orientation gizmo.
GizmoSphereSelection
Enum to indicate which sphere (direction) is selected.
Store arrays of triangles to be used to create display lists.
@ LIGHTBLUE
Definition color4d.h:62
@ DARKGRAY
Definition color4d.h:46
#define DELETE_AND_FREE_MAP(map)
#define DELETE_AND_FREE(ptr)
#define _(s)
#define UNITS3D_TO_UNITSPCB
Implements a model viewer canvas.
FOOTPRINT_ATTR_T
The set of attributes allowed within a FOOTPRINT, using FOOTPRINT::SetAttributes() and FOOTPRINT::Get...
Definition footprint.h:80
static const wxChar * m_logTrace
Trace mask used to enable or disable the trace output of this class.
@ GAUSSIAN_BLUR
Definition image.h:65
int MapPCBLayerTo3DLayer(PCB_LAYER_ID aLayer)
Definition layer_id.cpp:334
@ LAYER_3D_USER_1
Definition layer_ids.h:567
@ LAYER_3D_SOLDERMASK_TOP
Definition layer_ids.h:560
@ LAYER_3D_SOLDERMASK_BOTTOM
Definition layer_ids.h:559
@ LAYER_3D_BOARD
Definition layer_ids.h:554
@ LAYER_3D_USER_45
Definition layer_ids.h:611
bool IsCopperLayer(int aLayerId)
Test whether a layer is a copper layer.
Definition layer_ids.h:677
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
@ F_CrtYd
Definition layer_ids.h:116
@ B_Adhes
Definition layer_ids.h:103
@ Edge_Cuts
Definition layer_ids.h:112
@ Dwgs_User
Definition layer_ids.h:107
@ F_Paste
Definition layer_ids.h:104
@ Cmts_User
Definition layer_ids.h:108
@ F_Adhes
Definition layer_ids.h:102
@ B_Mask
Definition layer_ids.h:98
@ B_Cu
Definition layer_ids.h:65
@ Eco1_User
Definition layer_ids.h:109
@ F_Mask
Definition layer_ids.h:97
@ B_Paste
Definition layer_ids.h:105
@ F_Fab
Definition layer_ids.h:119
@ Margin
Definition layer_ids.h:113
@ F_SilkS
Definition layer_ids.h:100
@ B_CrtYd
Definition layer_ids.h:115
@ Eco2_User
Definition layer_ids.h:110
@ B_SilkS
Definition layer_ids.h:101
@ F_Cu
Definition layer_ids.h:64
@ B_Fab
Definition layer_ids.h:118
void OglResetTextureState()
Reset to default state the texture settings.
void OglSetMaterial(const SMATERIAL &aMaterial, float aOpacity, bool aUseSelectedMaterial, SFVEC3F aSelectionColor)
Set OpenGL materials.
GLuint OglLoadTexture(const IMAGE &aImage)
Generate a new OpenGL texture.
Definition ogl_utils.cpp:96
void OglDrawBackground(const SFVEC4F &aTopColor, const SFVEC4F &aBotColor)
Define generic OpenGL functions that are common to any OpenGL target.
PGM_BASE & Pgm()
The global program "get" accessor.
Definition pgm_base.cpp:946
see class PGM_BASE
void init_lights()
static SFVEC4F premultiplyAlpha(const SFVEC4F &aInput)
#define SIZE_OF_CIRCLE_TEXTURE
const int scale
Manage a bounding box defined by two SFVEC3F min max points.
Definition bbox_3d.h:43
SFVEC3F GetCenter() const
Return the center point of the bounding box.
Definition bbox_3d.cpp:132
bool DifferentiatePlatedCopper()
return true if platted copper aeras and non platted copper areas must be drawn using a different colo...
int delta
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695
glm::vec3 SFVEC3F
Definition xv3d_types.h:44
glm::vec4 SFVEC4F
Definition xv3d_types.h:46