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
47#include <glm/gtc/type_ptr.hpp>
48
58static float TransparencyControl( float aGrayColorValue, float aTransparency )
59{
60 const float aaa = aTransparency * aTransparency * aTransparency;
61
62 // 1.00-1.05*(1.0-x)^3
63 float ca = 1.0f - aTransparency;
64 ca = 1.00f - 1.05f * ca * ca * ca;
65
66 return glm::clamp( aGrayColorValue * ca + aaa, 0.0f, 1.0f );
67}
68
72#define UNITS3D_TO_UNITSPCB ( pcbIUScale.IU_PER_MM )
73
75 RENDER_3D_BASE( aAdapter, aCamera ),
76 m_canvas( aCanvas )
77{
78 wxLogTrace( m_logTrace, wxT( "RENDER_3D_OPENGL::RENDER_3D_OPENGL" ) );
79
80 m_layers.clear();
81 m_outerLayerHoles.clear();
82 m_innerLayerHoles.clear();
83 m_triangles.clear();
84 m_board = nullptr;
85 m_antiBoard = nullptr;
86
87 m_platedPadsFront = nullptr;
88 m_platedPadsBack = nullptr;
89 m_offboardPadsFront = nullptr;
90 m_offboardPadsBack = nullptr;
91
92 m_outerThroughHoles = nullptr;
94 m_outerViaThroughHoles = nullptr;
95 m_microviaHoles = nullptr;
96 m_padHoles = nullptr;
97 m_viaFrontCover = nullptr;
98 m_viaBackCover = nullptr;
99
100 m_circleTexture = 0;
101 m_grid = 0;
103 m_currentRollOverItem = nullptr;
104 m_boardWithHoles = nullptr;
105 m_postMachinePlugs = nullptr;
106
107 m_3dModelMap.clear();
108
109 m_spheres_gizmo = new SPHERES_GIZMO( 4, 4 );
110}
111
112
114{
115 wxLogTrace( m_logTrace, wxT( "RENDER_3D_OPENGL::RENDER_3D_OPENGL" ) );
116
117 freeAllLists();
118
119 glDeleteTextures( 1, &m_circleTexture );
120
121 delete m_spheres_gizmo;
122}
123
124
126{
127 return 50; // ms
128}
129
130
131void RENDER_3D_OPENGL::SetCurWindowSize( const wxSize& aSize )
132{
133 if( m_windowSize != aSize )
134 {
135 int viewport[4];
136 int fbWidth, fbHeight;
137 glGetIntegerv( GL_VIEWPORT, viewport );
138
139 m_windowSize = aSize;
140 glViewport( 0, 0, m_windowSize.x, m_windowSize.y );
142 // Initialize here any screen dependent data here
143 }
144}
145
146
148{
149 if( enabled )
150 glEnable( GL_LIGHT0 );
151 else
152 glDisable( GL_LIGHT0 );
153}
154
155
157{
158 if( enabled )
159 glEnable( GL_LIGHT1 );
160 else
161 glDisable( GL_LIGHT1 );
162}
163
164
166{
167 if( enabled )
168 glEnable( GL_LIGHT2 );
169 else
170 glDisable( GL_LIGHT2 );
171}
172
173
175{
176 m_spheres_gizmo->resetSelectedGizmoSphere();
177}
178
179
184
185
186void RENDER_3D_OPENGL::setGizmoViewport( int x, int y, int width, int height )
187{
188 m_spheres_gizmo->setViewport( x, y, width, height );
189}
190
191
192std::tuple<int, int, int, int> RENDER_3D_OPENGL::getGizmoViewport() const
193{
194 return m_spheres_gizmo->getViewport();
195}
196
197
198void RENDER_3D_OPENGL::handleGizmoMouseInput( int mouseX, int mouseY )
199{
200 m_spheres_gizmo->handleMouseInput( mouseX, mouseY );
201}
202
203
205{
206 m_materials = {};
207
208 // http://devernay.free.fr/cours/opengl/materials.html
209
210 // Plated copper
211 // Copper material mixed with the copper color
212 m_materials.m_Copper.m_Ambient = SFVEC3F( m_boardAdapter.m_CopperColor.r * 0.1f,
213 m_boardAdapter.m_CopperColor.g * 0.1f,
214 m_boardAdapter.m_CopperColor.b * 0.1f);
215
216 m_materials.m_Copper.m_Specular = SFVEC3F( m_boardAdapter.m_CopperColor.r * 0.75f + 0.25f,
217 m_boardAdapter.m_CopperColor.g * 0.75f + 0.25f,
218 m_boardAdapter.m_CopperColor.b * 0.75f + 0.25f );
219
220 // This guess the material type(ex: copper vs gold) to determine the
221 // shininess factor between 0.1 and 0.4
222 float shininessfactor = 0.40f - mapf( fabs( m_boardAdapter.m_CopperColor.r -
223 m_boardAdapter.m_CopperColor.g ),
224 0.15f, 1.00f,
225 0.00f, 0.30f );
226
227 m_materials.m_Copper.m_Shininess = shininessfactor * 128.0f;
228 m_materials.m_Copper.m_Emissive = SFVEC3F( 0.0f, 0.0f, 0.0f );
229
230
231 // Non plated copper (raw copper)
232 m_materials.m_NonPlatedCopper.m_Ambient = SFVEC3F( 0.191f, 0.073f, 0.022f );
233 m_materials.m_NonPlatedCopper.m_Diffuse = SFVEC3F( 184.0f / 255.0f, 115.0f / 255.0f,
234 50.0f / 255.0f );
235 m_materials.m_NonPlatedCopper.m_Specular = SFVEC3F( 0.256f, 0.137f, 0.086f );
236 m_materials.m_NonPlatedCopper.m_Shininess = 0.1f * 128.0f;
237 m_materials.m_NonPlatedCopper.m_Emissive = SFVEC3F( 0.0f, 0.0f, 0.0f );
238
239 // Paste material mixed with paste color
240 m_materials.m_Paste.m_Ambient = SFVEC3F( m_boardAdapter.m_SolderPasteColor.r,
241 m_boardAdapter.m_SolderPasteColor.g,
242 m_boardAdapter.m_SolderPasteColor.b );
243
244 m_materials.m_Paste.m_Specular = SFVEC3F( m_boardAdapter.m_SolderPasteColor.r *
245 m_boardAdapter.m_SolderPasteColor.r,
246 m_boardAdapter.m_SolderPasteColor.g *
247 m_boardAdapter.m_SolderPasteColor.g,
248 m_boardAdapter.m_SolderPasteColor.b *
249 m_boardAdapter.m_SolderPasteColor.b );
250
251 m_materials.m_Paste.m_Shininess = 0.1f * 128.0f;
252 m_materials.m_Paste.m_Emissive = SFVEC3F( 0.0f, 0.0f, 0.0f );
253
254 // Silk screen material mixed with silk screen color
255 m_materials.m_SilkSTop.m_Ambient = SFVEC3F( m_boardAdapter.m_SilkScreenColorTop.r,
256 m_boardAdapter.m_SilkScreenColorTop.g,
257 m_boardAdapter.m_SilkScreenColorTop.b );
258
259 m_materials.m_SilkSTop.m_Specular = SFVEC3F(
260 m_boardAdapter.m_SilkScreenColorTop.r * m_boardAdapter.m_SilkScreenColorTop.r + 0.10f,
261 m_boardAdapter.m_SilkScreenColorTop.g * m_boardAdapter.m_SilkScreenColorTop.g + 0.10f,
262 m_boardAdapter.m_SilkScreenColorTop.b * m_boardAdapter.m_SilkScreenColorTop.b + 0.10f );
263
264 m_materials.m_SilkSTop.m_Shininess = 0.078125f * 128.0f;
265 m_materials.m_SilkSTop.m_Emissive = SFVEC3F( 0.0f, 0.0f, 0.0f );
266
267 // Silk screen material mixed with silk screen color
268 m_materials.m_SilkSBot.m_Ambient = SFVEC3F( m_boardAdapter.m_SilkScreenColorBot.r,
269 m_boardAdapter.m_SilkScreenColorBot.g,
270 m_boardAdapter.m_SilkScreenColorBot.b );
271
272 m_materials.m_SilkSBot.m_Specular = SFVEC3F(
273 m_boardAdapter.m_SilkScreenColorBot.r * m_boardAdapter.m_SilkScreenColorBot.r + 0.10f,
274 m_boardAdapter.m_SilkScreenColorBot.g * m_boardAdapter.m_SilkScreenColorBot.g + 0.10f,
275 m_boardAdapter.m_SilkScreenColorBot.b * m_boardAdapter.m_SilkScreenColorBot.b + 0.10f );
276
277 m_materials.m_SilkSBot.m_Shininess = 0.078125f * 128.0f;
278 m_materials.m_SilkSBot.m_Emissive = SFVEC3F( 0.0f, 0.0f, 0.0f );
279
280 // Shininess is computed dynamically in setLayerMaterial() based on color darkness
281 m_materials.m_SolderMask.m_Shininess = 0.85f * 128.0f;
282 m_materials.m_SolderMask.m_Emissive = SFVEC3F( 0.0f, 0.0f, 0.0f );
283
284 // Epoxy material
285 m_materials.m_EpoxyBoard.m_Ambient = SFVEC3F( 117.0f / 255.0f, 97.0f / 255.0f,
286 47.0f / 255.0f );
287
288 m_materials.m_EpoxyBoard.m_Specular = SFVEC3F( 18.0f / 255.0f, 3.0f / 255.0f,
289 20.0f / 255.0f );
290
291 m_materials.m_EpoxyBoard.m_Shininess = 0.1f * 128.0f;
292 m_materials.m_EpoxyBoard.m_Emissive = SFVEC3F( 0.0f, 0.0f, 0.0f );
293}
294
295
297{
298 if( m_boardAdapter.GetUseBoardEditorCopperLayerColors() && IsCopperLayer( aLayerID ) )
299 {
300 COLOR4D copper_color = m_boardAdapter.m_BoardEditorColors[aLayerID];
301 m_materials.m_Copper.m_Diffuse = SFVEC3F( copper_color.r, copper_color.g,
302 copper_color.b );
303 OglSetMaterial( m_materials.m_Copper, 1.0f );
304 m_materials.m_NonPlatedCopper.m_Diffuse = m_materials.m_Copper.m_Diffuse;
305 OglSetMaterial( m_materials.m_NonPlatedCopper, 1.0f );
306
307 return;
308 }
309
310 switch( aLayerID )
311 {
312 case F_Mask:
313 case B_Mask:
314 {
315 const SFVEC4F layerColor = aLayerID == F_Mask ? m_boardAdapter.m_SolderMaskColorTop
316 : m_boardAdapter.m_SolderMaskColorBot;
317
318 m_materials.m_SolderMask.m_Diffuse = layerColor;
319
320 // Compute gray value for material property adjustments based on color darkness
321 const float solderMask_gray = ( layerColor.r + layerColor.g + layerColor.b ) / 3.0f;
322
323 // Use TransparencyControl to make darker colors more opaque, preventing copper
324 // show-through on dark solder masks
325 const float baseTransparency = 1.0f - layerColor.a;
326 m_materials.m_SolderMask.m_Transparency = TransparencyControl( solderMask_gray,
327 baseTransparency );
328
329 m_materials.m_SolderMask.m_Ambient = m_materials.m_SolderMask.m_Diffuse * 0.3f;
330
331 // Darker solder masks need a higher specular floor to avoid washed-out appearance
332 const SFVEC3F baseSpecular = m_materials.m_SolderMask.m_Diffuse
333 * m_materials.m_SolderMask.m_Diffuse;
334 m_materials.m_SolderMask.m_Specular = glm::max( baseSpecular, SFVEC3F( 0.30f ) );
335
336 // Darker colors get higher shininess for a tighter specular highlight, matching
337 // how dark solder masks appear in real life
338 const float minSolderMaskShininess = 0.85f * 128.0f;
339 const float maxSolderMaskShininess = 512.0f;
340 m_materials.m_SolderMask.m_Shininess = minSolderMaskShininess
341 + ( maxSolderMaskShininess - minSolderMaskShininess ) * ( 1.0f - solderMask_gray );
342
343 OglSetMaterial( m_materials.m_SolderMask, 1.0f );
344 break;
345 }
346
347 case B_Paste:
348 case F_Paste:
349 m_materials.m_Paste.m_Diffuse = m_boardAdapter.m_SolderPasteColor;
350 OglSetMaterial( m_materials.m_Paste, 1.0f );
351 break;
352
353 case B_SilkS:
354 m_materials.m_SilkSBot.m_Diffuse = m_boardAdapter.m_SilkScreenColorBot;
355 OglSetMaterial( m_materials.m_SilkSBot, 1.0f );
356 break;
357
358 case F_SilkS:
359 m_materials.m_SilkSTop.m_Diffuse = m_boardAdapter.m_SilkScreenColorTop;
360 OglSetMaterial( m_materials.m_SilkSTop, 1.0f );
361 break;
362
363 case B_Adhes:
364 case F_Adhes:
365 case Dwgs_User:
366 case Cmts_User:
367 case Eco1_User:
368 case Eco2_User:
369 case Edge_Cuts:
370 case Margin:
371 case B_CrtYd:
372 case F_CrtYd:
373 case B_Fab:
374 case F_Fab:
375 switch( aLayerID )
376 {
377 case Dwgs_User: m_materials.m_Plastic.m_Diffuse = m_boardAdapter.m_UserDrawingsColor; break;
378 case Cmts_User: m_materials.m_Plastic.m_Diffuse = m_boardAdapter.m_UserCommentsColor; break;
379 case Eco1_User: m_materials.m_Plastic.m_Diffuse = m_boardAdapter.m_ECO1Color; break;
380 case Eco2_User: m_materials.m_Plastic.m_Diffuse = m_boardAdapter.m_ECO2Color; break;
381 case Edge_Cuts: m_materials.m_Plastic.m_Diffuse = m_boardAdapter.m_UserDrawingsColor; break;
382 case Margin: m_materials.m_Plastic.m_Diffuse = m_boardAdapter.m_UserDrawingsColor; break;
383 default:
384 m_materials.m_Plastic.m_Diffuse = m_boardAdapter.GetLayerColor( aLayerID );
385 break;
386 }
387
388 m_materials.m_Plastic.m_Ambient = SFVEC3F( m_materials.m_Plastic.m_Diffuse.r * 0.05f,
389 m_materials.m_Plastic.m_Diffuse.g * 0.05f,
390 m_materials.m_Plastic.m_Diffuse.b * 0.05f );
391
392 m_materials.m_Plastic.m_Specular = SFVEC3F( m_materials.m_Plastic.m_Diffuse.r * 0.7f,
393 m_materials.m_Plastic.m_Diffuse.g * 0.7f,
394 m_materials.m_Plastic.m_Diffuse.b * 0.7f );
395
396 m_materials.m_Plastic.m_Shininess = 0.078125f * 128.0f;
397 m_materials.m_Plastic.m_Emissive = SFVEC3F( 0.0f, 0.0f, 0.0f );
398 OglSetMaterial( m_materials.m_Plastic, 1.0f );
399 break;
400
401 default:
402 {
403 int layer3D = MapPCBLayerTo3DLayer( aLayerID );
404
405 // Note: MUST do this in LAYER_3D space; User_1..User_45 are NOT contiguous
406 if( layer3D >= LAYER_3D_USER_1 && layer3D <= LAYER_3D_USER_45 )
407 {
408 int user_idx = layer3D - LAYER_3D_USER_1;
409
410 m_materials.m_Plastic.m_Diffuse = m_boardAdapter.m_UserDefinedLayerColor[ user_idx ];
411 m_materials.m_Plastic.m_Ambient = SFVEC3F( m_materials.m_Plastic.m_Diffuse.r * 0.05f,
412 m_materials.m_Plastic.m_Diffuse.g * 0.05f,
413 m_materials.m_Plastic.m_Diffuse.b * 0.05f );
414
415 m_materials.m_Plastic.m_Specular = SFVEC3F( m_materials.m_Plastic.m_Diffuse.r * 0.7f,
416 m_materials.m_Plastic.m_Diffuse.g * 0.7f,
417 m_materials.m_Plastic.m_Diffuse.b * 0.7f );
418
419 m_materials.m_Plastic.m_Shininess = 0.078125f * 128.0f;
420 m_materials.m_Plastic.m_Emissive = SFVEC3F( 0.0f, 0.0f, 0.0f );
421 OglSetMaterial( m_materials.m_Plastic, 1.0f );
422 break;
423 }
424
425 m_materials.m_Copper.m_Diffuse = m_boardAdapter.m_CopperColor;
426 OglSetMaterial( m_materials.m_Copper, 1.0f );
427 break;
428 }
429 }
430}
431
432
434{
435 // Setup light
436 // https://www.opengl.org/sdk/docs/man2/xhtml/glLight.xml
437 const GLfloat ambient[] = { 0.084f, 0.084f, 0.084f, 1.0f };
438 const GLfloat diffuse0[] = { 0.3f, 0.3f, 0.3f, 1.0f };
439 const GLfloat specular0[] = { 0.5f, 0.5f, 0.5f, 1.0f };
440
441 glLightfv( GL_LIGHT0, GL_AMBIENT, ambient );
442 glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse0 );
443 glLightfv( GL_LIGHT0, GL_SPECULAR, specular0 );
444
445 const GLfloat diffuse12[] = { 0.7f, 0.7f, 0.7f, 1.0f };
446 const GLfloat specular12[] = { 0.7f, 0.7f, 0.7f, 1.0f };
447
448 // defines a directional light that points along the negative z-axis
449 GLfloat position[4] = { 0.0f, 0.0f, 1.0f, 0.0f };
450
451 // This makes a vector slight not perpendicular with XZ plane
452 const SFVEC3F vectorLight = SphericalToCartesian( glm::pi<float>() * 0.03f,
453 glm::pi<float>() * 0.25f );
454
455 position[0] = vectorLight.x;
456 position[1] = vectorLight.y;
457 position[2] = vectorLight.z;
458
459 glLightfv( GL_LIGHT1, GL_AMBIENT, ambient );
460 glLightfv( GL_LIGHT1, GL_DIFFUSE, diffuse12 );
461 glLightfv( GL_LIGHT1, GL_SPECULAR, specular12 );
462 glLightfv( GL_LIGHT1, GL_POSITION, position );
463
464 // defines a directional light that points along the positive z-axis
465 position[2] = -position[2];
466
467 glLightfv( GL_LIGHT2, GL_AMBIENT, ambient );
468 glLightfv( GL_LIGHT2, GL_DIFFUSE, diffuse12 );
469 glLightfv( GL_LIGHT2, GL_SPECULAR, specular12 );
470 glLightfv( GL_LIGHT2, GL_POSITION, position );
471
472 const GLfloat lmodel_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
473
474 glLightModelfv( GL_LIGHT_MODEL_AMBIENT, lmodel_ambient );
475
476 glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE );
477}
478
479
481{
482 OglSetMaterial( m_materials.m_NonPlatedCopper, 1.0f );
483}
484
485
487{
488 glEnable( GL_POLYGON_OFFSET_FILL );
489 glPolygonOffset( -0.1f, -2.0f );
490 setLayerMaterial( aLayer_id );
491}
492
493
495{
496 glDisable( GL_POLYGON_OFFSET_FILL );
497}
498
499
500void RENDER_3D_OPENGL::renderBoardBody( bool aSkipRenderHoles )
501{
502 m_materials.m_EpoxyBoard.m_Diffuse = m_boardAdapter.m_BoardBodyColor;
503
504 // opacity to transparency
505 m_materials.m_EpoxyBoard.m_Transparency = 1.0f - m_boardAdapter.m_BoardBodyColor.a;
506
507 OglSetMaterial( m_materials.m_EpoxyBoard, 1.0f );
508
509 OPENGL_RENDER_LIST* ogl_disp_list = nullptr;
510
511 if( aSkipRenderHoles )
512 ogl_disp_list = m_board;
513 else
514 ogl_disp_list = m_boardWithHoles;
515
516 if( ogl_disp_list )
517 {
518 ogl_disp_list->ApplyScalePosition( -m_boardAdapter.GetBoardBodyThickness() / 2.0f,
519 m_boardAdapter.GetBoardBodyThickness() );
520
521 ogl_disp_list->SetItIsTransparent( true );
522 ogl_disp_list->DrawAll();
523 }
524
525 // Also render post-machining plugs (board material that remains after backdrill/counterbore/countersink)
526 if( !aSkipRenderHoles && m_postMachinePlugs )
527 {
528 m_postMachinePlugs->ApplyScalePosition( -m_boardAdapter.GetBoardBodyThickness() / 2.0f,
529 m_boardAdapter.GetBoardBodyThickness() );
530
531 m_postMachinePlugs->SetItIsTransparent( true );
532 m_postMachinePlugs->DrawAll();
533 }
534}
535
536
537static inline SFVEC4F premultiplyAlpha( const SFVEC4F& aInput )
538{
539 return SFVEC4F( aInput.r * aInput.a, aInput.g * aInput.a, aInput.b * aInput.a, aInput.a );
540}
541
542
543bool RENDER_3D_OPENGL::Redraw( bool aIsMoving, REPORTER* aStatusReporter,
544 REPORTER* aWarningReporter )
545{
546 // Initialize OpenGL
548 {
549 if( !initializeOpenGL() )
550 return false;
551 }
552
554
556 {
557 std::unique_ptr<BUSY_INDICATOR> busy = CreateBusyIndicator();
558
559 if( aStatusReporter )
560 aStatusReporter->Report( _( "Loading..." ) );
561
562 // Careful here!
563 // We are in the middle of rendering and the reload method may show
564 // a dialog box that requires the opengl context for a redraw
565 Pgm().GetGLContextManager()->RunWithoutCtxLock( [this, aStatusReporter, aWarningReporter]()
566 {
567 reload( aStatusReporter, aWarningReporter );
568 } );
569
570 // generate a new 3D grid as the size of the board may had changed
571 m_lastGridType = static_cast<GRID3D_TYPE>( cfg.grid_type );
573 }
574 else
575 {
576 // Check if grid was changed
577 if( cfg.grid_type != m_lastGridType )
578 {
579 // and generate a new one
580 m_lastGridType = static_cast<GRID3D_TYPE>( cfg.grid_type );
582 }
583 }
584
586
587 // Initial setup
588 glDepthFunc( GL_LESS );
589 glEnable( GL_CULL_FACE );
590 glFrontFace( GL_CCW ); // This is the OpenGL default
591 glEnable( GL_NORMALIZE ); // This allow OpenGL to normalize the normals after transformations
592 glViewport( 0, 0, m_windowSize.x, m_windowSize.y );
593
594 if( aIsMoving && cfg.opengl_AA_disableOnMove )
595 glDisable( GL_MULTISAMPLE );
596 else
597 glEnable( GL_MULTISAMPLE );
598
599 // clear color and depth buffers
600 glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
601 glClearDepth( 1.0f );
602 glClearStencil( 0x00 );
603 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
604
606
607 // Draw the background ( rectangle with color gradient)
609 premultiplyAlpha( m_boardAdapter.m_BgColorBot ) );
610
611 glEnable( GL_DEPTH_TEST );
612
613 // Set projection and modelview matrixes
614 glMatrixMode( GL_PROJECTION );
615 glLoadMatrixf( glm::value_ptr( m_camera.GetProjectionMatrix() ) );
616 glMatrixMode( GL_MODELVIEW );
617 glLoadIdentity();
618 glLoadMatrixf( glm::value_ptr( m_camera.GetViewMatrix() ) );
619
620 // Position the headlight
621 setLightFront( true );
622 setLightTop( true );
623 setLightBottom( true );
624
625 glEnable( GL_LIGHTING );
626
627 {
628 const SFVEC3F& cameraPos = m_camera.GetPos();
629
630 // Place the light at a minimum Z so the diffuse factor will not drop
631 // and the board will still look with good light.
632 float zpos;
633
634 if( cameraPos.z > 0.0f )
635 zpos = glm::max( cameraPos.z, 0.5f ) + cameraPos.z * cameraPos.z;
636 else
637 zpos = glm::min( cameraPos.z,-0.5f ) - cameraPos.z * cameraPos.z;
638
639 // This is a point light.
640 const GLfloat headlight_pos[] = { cameraPos.x, cameraPos.y, zpos, 1.0f };
641
642 glLightfv( GL_LIGHT0, GL_POSITION, headlight_pos );
643 }
644
645 bool skipThickness = aIsMoving && cfg.opengl_thickness_disableOnMove;
646 bool skipRenderHoles = aIsMoving && cfg.opengl_holes_disableOnMove;
647 bool skipRenderMicroVias = aIsMoving && cfg.opengl_microvias_disableOnMove;
648 bool showThickness = !skipThickness;
649
650 std::bitset<LAYER_3D_END> layerFlags = m_boardAdapter.GetVisibleLayers();
651
653
654 if( !( skipRenderMicroVias || skipRenderHoles ) && m_microviaHoles )
655 m_microviaHoles->DrawAll();
656
657 if( !skipRenderHoles && m_padHoles )
658 m_padHoles->DrawAll();
659
660 // Display copper and tech layers
661 for( MAP_OGL_DISP_LISTS::const_iterator ii = m_layers.begin(); ii != m_layers.end(); ++ii )
662 {
663 const PCB_LAYER_ID layer = ( PCB_LAYER_ID )( ii->first );
664 bool isSilkLayer = layer == F_SilkS || layer == B_SilkS;
665 bool isMaskLayer = layer == F_Mask || layer == B_Mask;
666 bool isPasteLayer = layer == F_Paste || layer == B_Paste;
667
668 // Mask layers are not processed here because they are a special case
669 if( isMaskLayer )
670 continue;
671
672 // Do not show inner layers when it is displaying the board and board body is opaque
673 // enough: the time to create inner layers can be *really significant*.
674 // So avoid creating them is they are not very visible
675 const double opacity_min = 0.8;
676
677 if( layerFlags.test( LAYER_3D_BOARD ) && m_boardAdapter.m_BoardBodyColor.a > opacity_min )
678 {
679 // generating internal copper layers is time consuming. so skip them
680 // if the board body is masking them (i.e. if the opacity is near 1.0)
681 // B_Cu is layer 2 and all inner layers are higher values
682 if( layer > B_Cu && IsCopperLayer( layer ) )
683 continue;
684 }
685
686 glPushMatrix();
687
688 OPENGL_RENDER_LIST* pLayerDispList = static_cast<OPENGL_RENDER_LIST*>( ii->second );
689
690 if( IsCopperLayer( layer ) )
691 {
692 if( cfg.DifferentiatePlatedCopper() )
694 else
695 setLayerMaterial( layer );
696
697 OPENGL_RENDER_LIST* outerTH = nullptr;
698 OPENGL_RENDER_LIST* viaHoles = nullptr;
699
700 if( !skipRenderHoles )
701 {
702 outerTH = m_outerThroughHoles;
703 viaHoles = m_outerLayerHoles[layer];
704 }
705
706 if( m_antiBoard )
707 m_antiBoard->ApplyScalePosition( pLayerDispList );
708
709 if( outerTH )
710 outerTH->ApplyScalePosition( pLayerDispList );
711
712 pLayerDispList->DrawCulled( showThickness, outerTH, viaHoles, m_antiBoard );
713
714 // Draw plated & offboard pads
715 if( layer == F_Cu && ( m_platedPadsFront || m_offboardPadsFront ) )
716 {
718
720 m_platedPadsFront->DrawCulled( showThickness, outerTH, viaHoles, m_antiBoard );
721
723 m_offboardPadsFront->DrawCulled( showThickness, outerTH, viaHoles );
724 }
725 else if( layer == B_Cu && ( m_platedPadsBack || m_offboardPadsBack ) )
726 {
728
729 if( m_platedPadsBack )
730 m_platedPadsBack->DrawCulled( showThickness, outerTH, viaHoles, m_antiBoard );
731
733 m_offboardPadsBack->DrawCulled( showThickness, outerTH, viaHoles );
734 }
735
737 }
738 else
739 {
740 setLayerMaterial( layer );
741
742 OPENGL_RENDER_LIST* throughHolesOuter = nullptr;
743 OPENGL_RENDER_LIST* anti_board = nullptr;
744 OPENGL_RENDER_LIST* solder_mask = nullptr;
745
746 if( !skipRenderHoles )
747 {
748 if( isSilkLayer && cfg.clip_silk_on_via_annuli )
749 throughHolesOuter = m_outerThroughHoleRings;
750 else
751 throughHolesOuter = m_outerThroughHoles;
752 }
753
754 if( isSilkLayer && cfg.show_off_board_silk )
755 anti_board = nullptr;
756 else if( LSET::PhysicalLayersMask().test( layer ) )
757 anti_board = m_antiBoard;
758
759 if( isSilkLayer && cfg.subtract_mask_from_silk && !cfg.show_off_board_silk )
760 solder_mask = m_layers[ ( layer == B_SilkS) ? B_Mask : F_Mask ];
761
762 if( throughHolesOuter )
763 throughHolesOuter->ApplyScalePosition( pLayerDispList );
764
765 if( anti_board )
766 anti_board->ApplyScalePosition( pLayerDispList );
767
768 if( solder_mask )
769 solder_mask->ApplyScalePosition( pLayerDispList );
770
771 pLayerDispList->DrawCulled( showThickness, solder_mask, throughHolesOuter, anti_board );
772 }
773
774 glPopMatrix();
775 }
776
777 glm::mat4 cameraViewMatrix;
778
779 glGetFloatv( GL_MODELVIEW_MATRIX, glm::value_ptr( cameraViewMatrix ) );
780
781 // Render 3D Models (Non-transparent)
782 renderOpaqueModels( cameraViewMatrix );
783
784 // Display board body
785 if( layerFlags.test( LAYER_3D_BOARD ) )
786 renderBoardBody( skipRenderHoles );
787
788 // Display transparent mask layers
789 if( layerFlags.test( LAYER_3D_SOLDERMASK_TOP )
790 || layerFlags.test( LAYER_3D_SOLDERMASK_BOTTOM ) )
791 {
792 // add a depth buffer offset, it will help to hide some artifacts
793 // on silkscreen where the SolderMask is removed
794 glEnable( GL_POLYGON_OFFSET_FILL );
795 glPolygonOffset( 0.0f, -2.0f );
796
797 if( m_camera.GetPos().z > 0 )
798 {
799 if( layerFlags.test( LAYER_3D_SOLDERMASK_BOTTOM ) )
800 {
802 showThickness, skipRenderHoles );
803 }
804
805 if( layerFlags.test( LAYER_3D_SOLDERMASK_TOP ) )
806 {
807 renderSolderMaskLayer( F_Mask, m_boardAdapter.GetLayerBottomZPos( F_Mask ),
808 showThickness, skipRenderHoles );
809 }
810 }
811 else
812 {
813 if( layerFlags.test( LAYER_3D_SOLDERMASK_TOP ) )
814 {
815 renderSolderMaskLayer( F_Mask, m_boardAdapter.GetLayerBottomZPos( F_Mask ),
816 showThickness, skipRenderHoles );
817 }
818
819 if( layerFlags.test( LAYER_3D_SOLDERMASK_BOTTOM ) )
820 {
822 showThickness, skipRenderHoles );
823 }
824 }
825
826 glDisable( GL_POLYGON_OFFSET_FILL );
827 glPolygonOffset( 0.0f, 0.0f );
828 }
829
830 // Render 3D Models (Transparent)
831 // !TODO: this can be optimized. If there are no transparent models (or no opacity),
832 // then there is no need to make this function call.
833 glDepthMask( GL_FALSE );
834 glEnable( GL_BLEND );
835 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
836
837 // Enables Texture Env so it can combine model transparency with each footprint opacity
838 glEnable( GL_TEXTURE_2D );
839 glActiveTexture( GL_TEXTURE0 );
840
841 // Uses an existent texture so the glTexEnv operations will work.
842 glBindTexture( GL_TEXTURE_2D, m_circleTexture );
843
844 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE );
845 glTexEnvf( GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE );
846 glTexEnvf( GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_MODULATE );
847
848 glTexEnvi( GL_TEXTURE_ENV, GL_SRC0_RGB, GL_PRIMARY_COLOR );
849 glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR );
850
851 glTexEnvi( GL_TEXTURE_ENV, GL_SRC1_RGB, GL_PREVIOUS );
852 glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR );
853
854 glTexEnvi( GL_TEXTURE_ENV, GL_SRC0_ALPHA, GL_PRIMARY_COLOR );
855 glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA );
856 glTexEnvi( GL_TEXTURE_ENV, GL_SRC1_ALPHA, GL_CONSTANT );
857 glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA );
858
859 renderTransparentModels( cameraViewMatrix );
860
861 glDisable( GL_BLEND );
863
864 glDepthMask( GL_TRUE );
865
866 // Render Grid
867 if( cfg.grid_type != GRID3D_TYPE::NONE )
868 {
869 glDisable( GL_LIGHTING );
870
871 if( glIsList( m_grid ) )
872 glCallList( m_grid );
873
874 glEnable( GL_LIGHTING );
875 }
876
877 // Render 3D arrows
878 if( cfg.show_navigator )
879 m_spheres_gizmo->render3dSpheresGizmo( m_camera.GetRotationMatrix() );
880
881 // Return back to the original viewport (this is important if we want
882 // to take a screenshot after the render)
883 glViewport( 0, 0, m_windowSize.x, m_windowSize.y );
884
885 return false;
886}
887
888
890{
891 glEnable( GL_LINE_SMOOTH );
892 glShadeModel( GL_SMOOTH );
893
894 // 4-byte pixel alignment
895 glPixelStorei( GL_UNPACK_ALIGNMENT, 4 );
896
897 // Initialize the open GL texture to draw the filled semi-circle of the segments
899
900 if( !circleImage )
901 return false;
902
903 unsigned int circleRadius = ( SIZE_OF_CIRCLE_TEXTURE / 2 ) - 4;
904
905 circleImage->CircleFilled( ( SIZE_OF_CIRCLE_TEXTURE / 2 ) - 0,
906 ( SIZE_OF_CIRCLE_TEXTURE / 2 ) - 0,
907 circleRadius,
908 0xFF );
909
910 IMAGE* circleImageBlured = new IMAGE( circleImage->GetWidth(), circleImage->GetHeight() );
911
912 circleImageBlured->EfxFilter_SkipCenter( circleImage, IMAGE_FILTER::GAUSSIAN_BLUR, circleRadius - 8 );
913
914 m_circleTexture = OglLoadTexture( *circleImageBlured );
915
916 delete circleImageBlured;
917 circleImageBlured = nullptr;
918
919 delete circleImage;
920 circleImage = nullptr;
921
922 init_lights();
923
924 // Use this mode if you want see the triangle lines (debug proposes)
925 //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
926 m_canvasInitialized = true;
927
928 return true;
929}
930
931
933{
934 glEnable( GL_COLOR_MATERIAL );
935 glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
936
937 const SFVEC4F ambient = SFVEC4F( 0.0f, 0.0f, 0.0f, 1.0f );
938 const SFVEC4F diffuse = SFVEC4F( 0.0f, 0.0f, 0.0f, 1.0f );
939 const SFVEC4F emissive = SFVEC4F( 0.0f, 0.0f, 0.0f, 1.0f );
940 const SFVEC4F specular = SFVEC4F( 0.1f, 0.1f, 0.1f, 1.0f );
941
942 glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, &specular.r );
943 glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, 96.0f );
944
945 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, &ambient.r );
946 glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, &diffuse.r );
947 glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, &emissive.r );
948}
949
950
952{
953#define DELETE_AND_FREE( ptr ) \
954 { \
955 delete ptr; \
956 ptr = nullptr; \
957 } \
958
959#define DELETE_AND_FREE_MAP( map ) \
960 { \
961 for( auto& [ layer, ptr ] : map ) \
962 delete ptr; \
963 \
964 map.clear(); \
965 }
966
967 if( glIsList( m_grid ) )
968 glDeleteLists( m_grid, 1 );
969
970 m_grid = 0;
971
973
978
981
983 delete list;
984
985 m_triangles.clear();
986
988
989 m_3dModelMatrixMap.clear();
990
995
999
1004}
1005
1006
1008 bool aShowThickness, bool aSkipRenderHoles )
1009{
1010 wxASSERT( (aLayerID == B_Mask) || (aLayerID == F_Mask) );
1011
1012 if( m_board )
1013 {
1014 OPENGL_RENDER_LIST* solder_mask = m_layers[ aLayerID ];
1015 OPENGL_RENDER_LIST* via_holes = aSkipRenderHoles ? nullptr : m_outerThroughHoles;
1016
1017 if( via_holes )
1018 via_holes->ApplyScalePosition( aZPos, m_boardAdapter.GetNonCopperLayerThickness() );
1019
1020 m_board->ApplyScalePosition( aZPos, m_boardAdapter.GetNonCopperLayerThickness() );
1021
1022 setLayerMaterial( aLayerID );
1023 m_board->SetItIsTransparent( true );
1024 m_board->DrawCulled( aShowThickness, solder_mask, via_holes );
1025
1026 if( aLayerID == F_Mask && m_viaFrontCover )
1027 {
1028 m_viaFrontCover->ApplyScalePosition( aZPos, 4 * m_boardAdapter.GetNonCopperLayerThickness() );
1029 m_viaFrontCover->DrawTop();
1030 }
1031 else if( aLayerID == B_Mask && m_viaBackCover )
1032 {
1033 m_viaBackCover->ApplyScalePosition( aZPos, 4 * m_boardAdapter.GetNonCopperLayerThickness() );
1034 m_viaBackCover->DrawBot();
1035 }
1036 }
1037}
1038
1039
1040void RENDER_3D_OPENGL::get3dModelsSelected( std::list<MODELTORENDER> &aDstRenderList, bool aGetTop,
1041 bool aGetBot, bool aRenderTransparentOnly,
1042 bool aRenderSelectedOnly )
1043{
1044 wxASSERT( ( aGetTop == true ) || ( aGetBot == true ) );
1045
1046 if( !m_boardAdapter.GetBoard() )
1047 return;
1048
1050 const wxString currentVariant = m_boardAdapter.GetBoard()->GetCurrentVariant();
1051
1052 // Go for all footprints
1053 for( FOOTPRINT* fp : m_boardAdapter.GetBoard()->Footprints() )
1054 {
1055 bool highlight = false;
1056
1057 if( m_boardAdapter.m_IsBoardView )
1058 {
1059 if( fp->IsSelected() )
1060 highlight = true;
1061
1063 highlight = true;
1064
1065 if( aRenderSelectedOnly != highlight )
1066 continue;
1067 }
1068
1069 if( !fp->Models().empty() )
1070 {
1071 if( m_boardAdapter.IsFootprintShown( fp ) )
1072 {
1073 // Skip 3D models for footprints that are DNP in the current variant
1074 if( fp->GetDNPForVariant( currentVariant ) )
1075 continue;
1076
1077 const bool isFlipped = fp->IsFlipped();
1078
1079 if( aGetTop == !isFlipped || aGetBot == isFlipped )
1080 get3dModelsFromFootprint( aDstRenderList, fp, aRenderTransparentOnly,
1081 highlight );
1082 }
1083 }
1084 }
1085}
1086
1087
1088void RENDER_3D_OPENGL::get3dModelsFromFootprint( std::list<MODELTORENDER> &aDstRenderList,
1089 const FOOTPRINT* aFootprint,
1090 bool aRenderTransparentOnly, bool aIsSelected )
1091{
1092 if( !aFootprint->Models().empty() )
1093 {
1094 const double zpos = m_boardAdapter.GetFootprintZPos( aFootprint->IsFlipped() );
1095
1096 VECTOR2I pos = aFootprint->GetPosition();
1097
1098 glm::mat4 fpMatrix( 1.0f );
1099
1100 fpMatrix = glm::translate( fpMatrix, SFVEC3F( pos.x * m_boardAdapter.BiuTo3dUnits(),
1101 -pos.y * m_boardAdapter.BiuTo3dUnits(),
1102 zpos ) );
1103
1104 if( !aFootprint->GetOrientation().IsZero() )
1105 {
1106 fpMatrix = glm::rotate( fpMatrix, (float) aFootprint->GetOrientation().AsRadians(),
1107 SFVEC3F( 0.0f, 0.0f, 1.0f ) );
1108 }
1109
1110 if( aFootprint->IsFlipped() )
1111 {
1112 fpMatrix = glm::rotate( fpMatrix, glm::pi<float>(), SFVEC3F( 0.0f, 1.0f, 0.0f ) );
1113 fpMatrix = glm::rotate( fpMatrix, glm::pi<float>(), SFVEC3F( 0.0f, 0.0f, 1.0f ) );
1114 }
1115
1116 double modelunit_to_3d_units_factor = m_boardAdapter.BiuTo3dUnits() * UNITS3D_TO_UNITSPCB;
1117
1118 fpMatrix = glm::scale( fpMatrix, SFVEC3F( modelunit_to_3d_units_factor ) );
1119
1120 // Get the list of model files for this model
1121 for( const FP_3DMODEL& sM : aFootprint->Models() )
1122 {
1123 if( !sM.m_Show || sM.m_Filename.empty() )
1124 continue;
1125
1126 // Check if the model is present in our cache map
1127 auto cache_i = m_3dModelMap.find( sM.m_Filename );
1128
1129 if( cache_i == m_3dModelMap.end() )
1130 continue;
1131
1132 if( const MODEL_3D* modelPtr = cache_i->second )
1133 {
1134 bool opaque = sM.m_Opacity >= 1.0;
1135
1136 if( ( !aRenderTransparentOnly && modelPtr->HasOpaqueMeshes() && opaque ) ||
1137 ( aRenderTransparentOnly && ( modelPtr->HasTransparentMeshes() || !opaque ) ) )
1138 {
1139 glm::mat4 modelworldMatrix = fpMatrix;
1140
1141 const SFVEC3F offset = SFVEC3F( sM.m_Offset.x, sM.m_Offset.y, sM.m_Offset.z );
1142 const SFVEC3F rotation = SFVEC3F( sM.m_Rotation.x, sM.m_Rotation.y,
1143 sM.m_Rotation.z );
1144 const SFVEC3F scale = SFVEC3F( sM.m_Scale.x, sM.m_Scale.y, sM.m_Scale.z );
1145
1146 std::vector<float> key = { offset.x, offset.y, offset.z,
1147 rotation.x, rotation.y, rotation.z,
1148 scale.x, scale.y, scale.z };
1149
1150 auto it = m_3dModelMatrixMap.find( key );
1151
1152 if( it != m_3dModelMatrixMap.end() )
1153 {
1154 modelworldMatrix *= it->second;
1155 }
1156 else
1157 {
1158 glm::mat4 mtx( 1.0f );
1159 mtx = glm::translate( mtx, offset );
1160 mtx = glm::rotate( mtx, glm::radians( -rotation.z ), { 0.0f, 0.0f, 1.0f } );
1161 mtx = glm::rotate( mtx, glm::radians( -rotation.y ), { 0.0f, 1.0f, 0.0f } );
1162 mtx = glm::rotate( mtx, glm::radians( -rotation.x ), { 1.0f, 0.0f, 0.0f } );
1163 mtx = glm::scale( mtx, scale );
1164 m_3dModelMatrixMap[ key ] = mtx;
1165
1166 modelworldMatrix *= mtx;
1167 }
1168
1169 aDstRenderList.emplace_back( modelworldMatrix, modelPtr,
1170 aRenderTransparentOnly ? sM.m_Opacity : 1.0f,
1171 aRenderTransparentOnly,
1172 aFootprint->IsSelected() || aIsSelected );
1173 }
1174 }
1175 }
1176 }
1177}
1178
1179
1180void RENDER_3D_OPENGL::renderOpaqueModels( const glm::mat4 &aCameraViewMatrix )
1181{
1183
1184 const SFVEC3F selColor = m_boardAdapter.GetColor( cfg.opengl_selection_color );
1185
1186 glPushMatrix();
1187
1188 std::list<MODELTORENDER> renderList;
1189
1190 if( m_boardAdapter.m_IsBoardView )
1191 {
1192 renderList.clear();
1193
1194 get3dModelsSelected( renderList, true, true, false, true );
1195
1196 if( !renderList.empty() )
1197 {
1198 MODEL_3D::BeginDrawMulti( false );
1199
1200 for( const MODELTORENDER& mtr : renderList )
1201 renderModel( aCameraViewMatrix, mtr, selColor, nullptr );
1202
1204 }
1205 }
1206
1207 renderList.clear();
1208 get3dModelsSelected( renderList, true, true, false, false );
1209
1210 if( !renderList.empty() )
1211 {
1213
1214 for( const MODELTORENDER& mtr : renderList )
1215 renderModel( aCameraViewMatrix, mtr, selColor, nullptr );
1216
1218 }
1219
1220 glPopMatrix();
1221}
1222
1223
1224void RENDER_3D_OPENGL::renderTransparentModels( const glm::mat4 &aCameraViewMatrix )
1225{
1227
1228 const SFVEC3F selColor = m_boardAdapter.GetColor( cfg.opengl_selection_color );
1229
1230 std::list<MODELTORENDER> renderListModels; // do not clear it until this function returns
1231
1232 if( m_boardAdapter.m_IsBoardView )
1233 {
1234 // Get Transparent Selected
1235 get3dModelsSelected( renderListModels, true, true, true, true );
1236 }
1237
1238 // Get Transparent Not Selected
1239 get3dModelsSelected( renderListModels, true, true, true, false );
1240
1241 if( renderListModels.empty() )
1242 return;
1243
1244 std::vector<std::pair<const MODELTORENDER *, float>> transparentModelList;
1245
1246 transparentModelList.reserve( renderListModels.size() );
1247
1248 // Calculate the distance to the camera for each model
1249 const SFVEC3F &cameraPos = m_camera.GetPos();
1250
1251 for( const MODELTORENDER& mtr : renderListModels )
1252 {
1253 const BBOX_3D& bBox = mtr.m_model->GetBBox();
1254 const SFVEC3F& bBoxCenter = bBox.GetCenter();
1255 const SFVEC3F bBoxWorld = mtr.m_modelWorldMat * glm::vec4( bBoxCenter, 1.0f );
1256
1257 const float distanceToCamera = glm::length( cameraPos - bBoxWorld );
1258
1259 transparentModelList.emplace_back( &mtr, distanceToCamera );
1260 }
1261
1262 // Sort from back to front
1263 std::sort( transparentModelList.begin(), transparentModelList.end(),
1264 [&]( std::pair<const MODELTORENDER *, float>& a,
1265 std::pair<const MODELTORENDER *, float>& b )
1266 {
1267 if( a.second != b.second )
1268 return a.second > b.second;
1269
1270 return a.first > b.first; // use pointers as a last resort
1271 } );
1272
1273 // Start rendering calls
1274 glPushMatrix();
1275
1276 bool isUsingColorInformation = !( transparentModelList.begin()->first->m_isSelected &&
1277 m_boardAdapter.m_IsBoardView );
1278
1279 MODEL_3D::BeginDrawMulti( isUsingColorInformation );
1280
1281 for( const std::pair<const MODELTORENDER *, float>& mtr : transparentModelList )
1282 {
1283 if( m_boardAdapter.m_IsBoardView )
1284 {
1285 // Toggle between using model color or the select color
1286 if( !isUsingColorInformation && !mtr.first->m_isSelected )
1287 {
1288 isUsingColorInformation = true;
1289
1290 glEnableClientState( GL_COLOR_ARRAY );
1291 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
1292 glEnable( GL_COLOR_MATERIAL );
1293 }
1294 else if( isUsingColorInformation && mtr.first->m_isSelected )
1295 {
1296 isUsingColorInformation = false;
1297
1298 glDisableClientState( GL_COLOR_ARRAY );
1299 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1300 glDisable( GL_COLOR_MATERIAL );
1301 }
1302 }
1303
1304 // Render model, sort each individuall material group
1305 // by passing cameraPos
1306 renderModel( aCameraViewMatrix, *mtr.first, selColor, &cameraPos );
1307 }
1308
1310
1311 glPopMatrix();
1312}
1313
1314
1315void RENDER_3D_OPENGL::renderModel( const glm::mat4 &aCameraViewMatrix,
1316 const MODELTORENDER &aModelToRender,
1317 const SFVEC3F &aSelColor, const SFVEC3F *aCameraWorldPos )
1318{
1320
1321 const glm::mat4 modelviewMatrix = aCameraViewMatrix * aModelToRender.m_modelWorldMat;
1322
1323 glLoadMatrixf( glm::value_ptr( modelviewMatrix ) );
1324
1325 aModelToRender.m_model->Draw( aModelToRender.m_isTransparent, aModelToRender.m_opacity,
1326 aModelToRender.m_isSelected, aSelColor,
1327 &aModelToRender.m_modelWorldMat, aCameraWorldPos );
1328
1329 if( cfg.show_model_bbox )
1330 {
1331 const bool wasBlendEnabled = glIsEnabled( GL_BLEND );
1332
1333 if( !wasBlendEnabled )
1334 {
1335 glEnable( GL_BLEND );
1336 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
1337 }
1338
1339 glDisable( GL_LIGHTING );
1340
1341 glLineWidth( 1 );
1342 aModelToRender.m_model->DrawBboxes();
1343
1344 glLineWidth( 4 );
1345 aModelToRender.m_model->DrawBbox();
1346
1347 glEnable( GL_LIGHTING );
1348
1349 if( !wasBlendEnabled )
1350 glDisable( GL_BLEND );
1351 }
1352}
1353
1354
1356{
1357 if( glIsList( m_grid ) )
1358 glDeleteLists( m_grid, 1 );
1359
1360 m_grid = 0;
1361
1362 if( aGridType == GRID3D_TYPE::NONE )
1363 return;
1364
1365 m_grid = glGenLists( 1 );
1366
1367 if( !glIsList( m_grid ) )
1368 return;
1369
1370 glNewList( m_grid, GL_COMPILE );
1371
1372 glEnable( GL_BLEND );
1373 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
1374
1375 const double zpos = 0.0;
1376
1377 // Color of grid lines
1378 const SFVEC3F gridColor = m_boardAdapter.GetColor( DARKGRAY );
1379
1380 // Color of grid lines every 5 lines
1381 const SFVEC3F gridColor_marker = m_boardAdapter.GetColor( LIGHTBLUE );
1382 const double scale = m_boardAdapter.BiuTo3dUnits();
1383 const GLfloat transparency = 0.35f;
1384
1385 double griSizeMM = 0.0;
1386
1387 switch( aGridType )
1388 {
1389 case GRID3D_TYPE::GRID_1MM: griSizeMM = 1.0; break;
1390 case GRID3D_TYPE::GRID_2P5MM: griSizeMM = 2.5; break;
1391 case GRID3D_TYPE::GRID_5MM: griSizeMM = 5.0; break;
1392 case GRID3D_TYPE::GRID_10MM: griSizeMM = 10.0; break;
1393
1394 default:
1395 case GRID3D_TYPE::NONE: return;
1396 }
1397
1398 glNormal3f( 0.0, 0.0, 1.0 );
1399
1400 const VECTOR2I brd_size = m_boardAdapter.GetBoardSize();
1401 VECTOR2I brd_center_pos = m_boardAdapter.GetBoardPos();
1402
1403 brd_center_pos.y = -brd_center_pos.y;
1404
1405 const int xsize = std::max( brd_size.x, pcbIUScale.mmToIU( 100 ) ) * 1.2;
1406 const int ysize = std::max( brd_size.y, pcbIUScale.mmToIU( 100 ) ) * 1.2;
1407
1408 // Grid limits, in 3D units
1409 double xmin = ( brd_center_pos.x - xsize / 2 ) * scale;
1410 double xmax = ( brd_center_pos.x + xsize / 2 ) * scale;
1411 double ymin = ( brd_center_pos.y - ysize / 2 ) * scale;
1412 double ymax = ( brd_center_pos.y + ysize / 2 ) * scale;
1413 double zmin = pcbIUScale.mmToIU( -50 ) * scale;
1414 double zmax = pcbIUScale.mmToIU( 100 ) * scale;
1415
1416 // Set rasterised line width (min value = 1)
1417 glLineWidth( 1 );
1418
1419 // Draw horizontal grid centered on 3D origin (center of the board)
1420 for( int ii = 0; ; ii++ )
1421 {
1422 if( (ii % 5) )
1423 glColor4f( gridColor.r, gridColor.g, gridColor.b, transparency );
1424 else
1425 glColor4f( gridColor_marker.r, gridColor_marker.g, gridColor_marker.b,
1426 transparency );
1427
1428 const int delta = KiROUND( ii * griSizeMM * pcbIUScale.IU_PER_MM );
1429
1430 if( delta <= xsize / 2 ) // Draw grid lines parallel to X axis
1431 {
1432 glBegin( GL_LINES );
1433 glVertex3f( (brd_center_pos.x + delta) * scale, -ymin, zpos );
1434 glVertex3f( (brd_center_pos.x + delta) * scale, -ymax, zpos );
1435 glEnd();
1436
1437 if( ii != 0 )
1438 {
1439 glBegin( GL_LINES );
1440 glVertex3f( (brd_center_pos.x - delta) * scale, -ymin, zpos );
1441 glVertex3f( (brd_center_pos.x - delta) * scale, -ymax, zpos );
1442 glEnd();
1443 }
1444 }
1445
1446 if( delta <= ysize / 2 ) // Draw grid lines parallel to Y axis
1447 {
1448 glBegin( GL_LINES );
1449 glVertex3f( xmin, -( brd_center_pos.y + delta ) * scale, zpos );
1450 glVertex3f( xmax, -( brd_center_pos.y + delta ) * scale, zpos );
1451 glEnd();
1452
1453 if( ii != 0 )
1454 {
1455 glBegin( GL_LINES );
1456 glVertex3f( xmin, -( brd_center_pos.y - delta ) * scale, zpos );
1457 glVertex3f( xmax, -( brd_center_pos.y - delta ) * scale, zpos );
1458 glEnd();
1459 }
1460 }
1461
1462 if( ( delta > ysize / 2 ) && ( delta > xsize / 2 ) )
1463 break;
1464 }
1465
1466 // Draw vertical grid on Z axis
1467 glNormal3f( 0.0, -1.0, 0.0 );
1468
1469 // Draw vertical grid lines (parallel to Z axis)
1470 double posy = -brd_center_pos.y * scale;
1471
1472 for( int ii = 0; ; ii++ )
1473 {
1474 if( (ii % 5) )
1475 glColor4f( gridColor.r, gridColor.g, gridColor.b, transparency );
1476 else
1477 glColor4f( gridColor_marker.r, gridColor_marker.g, gridColor_marker.b,
1478 transparency );
1479
1480 const double delta = ii * griSizeMM * pcbIUScale.IU_PER_MM;
1481
1482 glBegin( GL_LINES );
1483 xmax = ( brd_center_pos.x + delta ) * scale;
1484
1485 glVertex3f( xmax, posy, zmin );
1486 glVertex3f( xmax, posy, zmax );
1487 glEnd();
1488
1489 if( ii != 0 )
1490 {
1491 glBegin( GL_LINES );
1492 xmin = ( brd_center_pos.x - delta ) * scale;
1493 glVertex3f( xmin, posy, zmin );
1494 glVertex3f( xmin, posy, zmax );
1495 glEnd();
1496 }
1497
1498 if( delta > xsize / 2.0f )
1499 break;
1500 }
1501
1502 // Draw horizontal grid lines on Z axis (parallel to X axis)
1503 for( int ii = 0; ; ii++ )
1504 {
1505 if( ii % 5 )
1506 glColor4f( gridColor.r, gridColor.g, gridColor.b, transparency );
1507 else
1508 glColor4f( gridColor_marker.r, gridColor_marker.g, gridColor_marker.b, transparency );
1509
1510 const double delta = ii * griSizeMM * pcbIUScale.IU_PER_MM * scale;
1511
1512 if( delta <= zmax )
1513 {
1514 // Draw grid lines on Z axis (positive Z axis coordinates)
1515 glBegin( GL_LINES );
1516 glVertex3f( xmin, posy, delta );
1517 glVertex3f( xmax, posy, delta );
1518 glEnd();
1519 }
1520
1521 if( delta <= -zmin && ( ii != 0 ) )
1522 {
1523 // Draw grid lines on Z axis (negative Z axis coordinates)
1524 glBegin( GL_LINES );
1525 glVertex3f( xmin, posy, -delta );
1526 glVertex3f( xmax, posy, -delta );
1527 glEnd();
1528 }
1529
1530 if( ( delta > zmax ) && ( delta > -zmin ) )
1531 break;
1532 }
1533
1534 glDisable( GL_BLEND );
1535
1536 glEndList();
1537}
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:128
EDA_ANGLE GetOrientation() const
Definition footprint.h:330
bool IsFlipped() const
Definition footprint.h:524
std::vector< FP_3DMODEL > & Models()
Definition footprint.h:323
VECTOR2I GetPosition() const override
Definition footprint.h:327
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:697
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:121
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.
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.
see class PGM_BASE
void init_lights()
static SFVEC4F premultiplyAlpha(const SFVEC4F &aInput)
static float TransparencyControl(float aGrayColorValue, float aTransparency)
Attempt to control the transparency based on the gray value of the color.
#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