KiCad PCB EDA Suite
Loading...
Searching...
No Matches
opengl_gal.h
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) 2012 Torsten Hueter, torstenhtr <at> gmx.de
5 * Copyright (C) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors.
6 * Copyright (C) 2013-2017 CERN
7 * @author Maciej Suminski <[email protected]>
8 *
9 * Graphics Abstraction Layer (GAL) for OpenGL
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, you may find one here:
23 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
24 * or you may search the http://www.gnu.org website for the version 2 license,
25 * or you may write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27 */
28
29#ifndef OPENGLGAL_H_
30#define OPENGLGAL_H_
31
32// GAL imports
33#include <gal/gal.h>
36#include <gal/opengl/shader.h>
42#include <gal/hidpi_gl_canvas.h>
43
44#include <unordered_map>
45#include <memory>
46#include <wx/event.h>
47
48#ifndef CALLBACK
49#define CALLBACK
50#endif
51
53#define SEG_PER_CIRCLE_COUNT 64
54
55struct bitmap_glyph;
56
57namespace KIGFX
58{
59class SHADER;
60class GL_BITMAP_CACHE;
61
62
70class GAL_API OPENGL_GAL : public GAL, public HIDPI_GL_CANVAS
71{
72public:
86 OPENGL_GAL( const KIGFX::VC_SETTINGS& aVcSettings, GAL_DISPLAY_OPTIONS& aDisplayOptions,
87 wxWindow* aParent,
88 wxEvtHandler* aMouseListener = nullptr, wxEvtHandler* aPaintListener = nullptr,
89 const wxString& aName = wxT( "GLCanvas" ) );
90
92
99 static wxString CheckFeatures( GAL_DISPLAY_OPTIONS& aOptions );
100
101 bool IsOpenGlEngine() override { return true; }
102
104 bool IsInitialized() const override
105 {
106 // is*Initialized flags, but it is enough for OpenGL to show up
107 return IsShownOnScreen() && !GetClientRect().IsEmpty();
108 }
109
111 bool IsVisible() const override
112 {
113 return IsShownOnScreen() && !GetClientRect().IsEmpty();
114 }
115
116 // ---------------
117 // Drawing methods
118 // ---------------
119
121 void DrawLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
122
124 void DrawSegment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint,
125 double aWidth ) override;
126
128 void DrawSegmentChain( const std::vector<VECTOR2D>& aPointList, double aWidth ) override;
129 void DrawSegmentChain( const SHAPE_LINE_CHAIN& aLineChain, double aWidth ) override;
130
132 void DrawCircle( const VECTOR2D& aCenterPoint, double aRadius ) override;
133
135 void DrawArc( const VECTOR2D& aCenterPoint, double aRadius, const EDA_ANGLE& aStartAngle,
136 const EDA_ANGLE& aAngle ) override;
137
139 void DrawArcSegment( const VECTOR2D& aCenterPoint, double aRadius, const EDA_ANGLE& aStartAngle,
140 const EDA_ANGLE& aAngle, double aWidth, double aMaxError ) override;
141
143 void DrawRectangle( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
144
146 void DrawPolyline( const std::deque<VECTOR2D>& aPointList ) override;
147 void DrawPolyline( const std::vector<VECTOR2D>& aPointList ) override;
148 void DrawPolyline( const VECTOR2D aPointList[], int aListSize ) override;
149 void DrawPolyline( const SHAPE_LINE_CHAIN& aLineChain ) override;
150
152 void DrawPolylines( const std::vector<std::vector<VECTOR2D>>& aPointLists ) override;
153
155 void DrawPolygon( const std::deque<VECTOR2D>& aPointList ) override;
156 void DrawPolygon( const VECTOR2D aPointList[], int aListSize ) override;
157 void DrawPolygon( const SHAPE_POLY_SET& aPolySet, bool aStrokeTriangulation = false ) override;
158 void DrawPolygon( const SHAPE_LINE_CHAIN& aPolySet ) override;
159
161 virtual void DrawGlyph( const KIFONT::GLYPH& aGlyph, int aNth, int aTotal ) override;
162
164 virtual void DrawGlyphs( const std::vector<std::unique_ptr<KIFONT::GLYPH>>& aGlyphs ) override;
165
167 void DrawCurve( const VECTOR2D& startPoint, const VECTOR2D& controlPointA,
168 const VECTOR2D& controlPointB, const VECTOR2D& endPoint,
169 double aFilterValue = 0.0 ) override;
170
172 void DrawBitmap( const BITMAP_BASE& aBitmap, double alphaBlend = 1.0 ) override;
173
175 void BitmapText( const wxString& aText, const VECTOR2I& aPosition,
176 const EDA_ANGLE& aAngle ) override;
177
179 void DrawGrid() override;
180
181 // --------------
182 // Screen methods
183 // --------------
184
186 void ResizeScreen( int aWidth, int aHeight ) override;
187
189 bool Show( bool aShow ) override;
190
192 int GetSwapInterval() const override { return m_swapInterval; };
193
195 void Flush() override;
196
198 void ClearScreen( ) override;
199
200 // --------------
201 // Transformation
202 // --------------
203
205 void Transform( const MATRIX3x3D& aTransformation ) override;
206
208 void Rotate( double aAngle ) override;
209
211 void Translate( const VECTOR2D& aTranslation ) override;
212
214 void Scale( const VECTOR2D& aScale ) override;
215
217 void Save() override;
218
220 void Restore() override;
221
222 // --------------------------------------------
223 // Group methods
224 // ---------------------------------------------
225
227 int BeginGroup() override;
228
230 void EndGroup() override;
231
233 void DrawGroup( int aGroupNumber ) override;
234
236 void ChangeGroupColor( int aGroupNumber, const COLOR4D& aNewColor ) override;
237
239 void ChangeGroupDepth( int aGroupNumber, int aDepth ) override;
240
242 void DeleteGroup( int aGroupNumber ) override;
243
245 void ClearCache() override;
246
247 // --------------------------------------------------------
248 // Handling the world <-> screen transformation
249 // --------------------------------------------------------
250
252 void SetTarget( RENDER_TARGET aTarget ) override;
253
255 RENDER_TARGET GetTarget() const override;
256
258 void ClearTarget( RENDER_TARGET aTarget ) override;
259
261 virtual bool HasTarget( RENDER_TARGET aTarget ) override;
262
264 void SetNegativeDrawMode( bool aSetting ) override {}
265
267 void StartDiffLayer() override;
268 //
270 void EndDiffLayer() override;
271
272 void ComputeWorldScreenMatrix() override;
273
274 // -------
275 // Cursor
276 // -------
277
279 bool SetNativeCursorStyle( KICURSOR aCursor ) override;
280
282 void DrawCursor( const VECTOR2D& aCursorPosition ) override;
283
290 void PostPaint( wxPaintEvent& aEvent );
291
292 void SetMouseListener( wxEvtHandler* aMouseListener )
293 {
294 m_mouseListener = aMouseListener;
295 }
296
297 void SetPaintListener( wxEvtHandler* aPaintListener )
298 {
299 m_paintListener = aPaintListener;
300 }
301
302 void EnableDepthTest( bool aEnabled = false ) override;
303
304 bool IsContextLocked() override
305 {
306 return m_isContextLocked;
307 }
308
309 void LockContext( int aClientCookie ) override;
310
311 void UnlockContext( int aClientCookie ) override;
312
314 void BeginDrawing() override;
315
317 void EndDrawing() override;
318
321 {
324
326 std::deque<std::shared_ptr<GLdouble>>& intersectPoints;
327 };
328
329private:
331 typedef GAL super;
332
333 static wxGLContext* m_glMainContext;
334 wxGLContext* m_glPrivContext;
336 static int m_instanceCounter;
337 wxEvtHandler* m_mouseListener;
338 wxEvtHandler* m_paintListener;
339
340 static GLuint g_fontTexture;
341
342 // Vertex buffer objects related fields
343 typedef std::unordered_map< unsigned int, std::shared_ptr<VERTEX_ITEM> > GROUPS_MAP;
344
346 unsigned int m_groupCounter;
353
354 // Framebuffer & compositing
356 unsigned int m_mainBuffer;
357 unsigned int m_overlayBuffer;
358 unsigned int m_tempBuffer;
360
361 // Shader
364
365 // Internal flags
378
380
381 std::unique_ptr<GL_BITMAP_CACHE> m_bitmapCache;
382
383 // Polygon tesselation
384 GLUtesselator* m_tesselator;
385 std::deque<std::shared_ptr<GLdouble>> m_tessIntersects;
386
388 void beginUpdate() override;
389
391 void endUpdate() override;
392
394 bool updatedGalDisplayOptions( const GAL_DISPLAY_OPTIONS& aOptions ) override;
395
404 void drawLineQuad( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint,
405 bool aReserve = true );
406
412 void reserveLineQuads( const int aLineCount );
413
425 void drawSemiCircle( const VECTOR2D& aCenterPoint, double aRadius, double aAngle );
426
435 void drawFilledSemiCircle( const VECTOR2D& aCenterPoint, double aRadius, double aAngle );
436
446 void drawStrokedSemiCircle( const VECTOR2D& aCenterPoint, double aRadius, double aAngle,
447 bool aReserve = true );
448
454 void drawCircle( const VECTOR2D& aCenterPoint, double aRadius, bool aReserve = true );
455
463 void drawPolyline( const std::function<VECTOR2D( int )>& aPointGetter, int aPointCount,
464 bool aReserve = true );
465
473 void drawSegmentChain( const std::function<VECTOR2D( int )>& aPointGetter, int aPointCount,
474 double aWidth, bool aReserve = true );
475
479 void drawSegment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint, double aWidth,
480 bool aReserve = true );
481
489 void drawPolygon( GLdouble* aPoints, int aPointCount );
490
497 void drawTriangulatedPolyset( const SHAPE_POLY_SET& aPoly, bool aStrokeTriangulation );
498
508 int drawBitmapChar( unsigned long aChar, bool aReserve = true );
509
521 void drawBitmapOverbar( double aLength, double aHeight, bool aReserve = true );
522
530 std::pair<VECTOR2D, float> computeBitmapTextSize( const UTF8& aText ) const;
531
532 // Event handling
538 void onPaint( wxPaintEvent& aEvent );
539
545 void skipMouseEvent( wxMouseEvent& aEvent );
546
552 void onSetNativeCursor( wxSetCursorEvent& aEvent );
553
557 void blitCursor();
558
564 unsigned int getNewGroupNumber();
565
569 double calcAngleStep( double aRadius ) const
570 {
571 // Bigger arcs need smaller alpha increment to make them look smooth
572 return std::min( 1e6 / aRadius, 2.0 * M_PI / SEG_PER_CIRCLE_COUNT );
573 }
574
575 double getWorldPixelSize() const;
576
577 VECTOR2D getScreenPixelSize() const;
578
584 void init();
585};
586} // namespace KIGFX
587
588#endif // OPENGLGAL_H_
This class handle bitmap images in KiCad.
Definition: bitmap_base.h:48
wxGLCanvas wrapper for HiDPI/Retina support.
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
Abstract interface for drawing on a 2D-surface.
OpenGL implementation of the Graphics Abstraction Layer.
Definition: opengl_gal.h:71
unsigned int m_groupCounter
Counter used for generating keys for groups.
Definition: opengl_gal.h:346
std::unordered_map< unsigned int, std::shared_ptr< VERTEX_ITEM > > GROUPS_MAP
Definition: opengl_gal.h:343
VERTEX_MANAGER * m_overlayManager
Container for storing overlaid VERTEX_ITEMs.
Definition: opengl_gal.h:351
bool m_isInitialized
Basic initialization flag, has to be done when the window is visible.
Definition: opengl_gal.h:369
VERTEX_MANAGER * m_currentManager
Currently used VERTEX_MANAGER (for storing VERTEX_ITEMs).
Definition: opengl_gal.h:347
std::deque< std::shared_ptr< GLdouble > > m_tessIntersects
Definition: opengl_gal.h:385
void SetNegativeDrawMode(bool aSetting) override
Set negative draw mode in the renderer.
Definition: opengl_gal.h:264
bool IsInitialized() const override
Return the initialization status for the canvas.
Definition: opengl_gal.h:104
unsigned int m_mainBuffer
Main rendering target.
Definition: opengl_gal.h:356
std::unique_ptr< GL_BITMAP_CACHE > m_bitmapCache
Definition: opengl_gal.h:381
bool IsContextLocked() override
Checks the state of the context lock.
Definition: opengl_gal.h:304
bool m_isBitmapFontInitialized
Is the shader set to use bitmap fonts?
Definition: opengl_gal.h:368
unsigned int m_overlayBuffer
Auxiliary rendering target (for menus etc.)
Definition: opengl_gal.h:357
OPENGL_COMPOSITOR * m_compositor
Handles multiple rendering targets.
Definition: opengl_gal.h:355
VERTEX_MANAGER * m_cachedManager
Container for storing cached VERTEX_ITEMs.
Definition: opengl_gal.h:349
void SetPaintListener(wxEvtHandler *aPaintListener)
Definition: opengl_gal.h:297
GLint ufm_worldPixelSize
Definition: opengl_gal.h:374
GLint ufm_antialiasingOffset
Definition: opengl_gal.h:377
bool IsVisible() const override
Return true if the GAL canvas is visible on the screen.
Definition: opengl_gal.h:111
wxEvtHandler * m_mouseListener
Definition: opengl_gal.h:337
int m_swapInterval
Used to store swap interval information.
Definition: opengl_gal.h:335
GROUPS_MAP m_groups
Stores information about VBO objects (groups)
Definition: opengl_gal.h:345
int GetSwapInterval() const override
Return the swap interval. -1 for adaptive, 0 for disabled/unknown.
Definition: opengl_gal.h:192
GAL super
Super class definition.
Definition: opengl_gal.h:331
void SetMouseListener(wxEvtHandler *aMouseListener)
Definition: opengl_gal.h:292
VERTEX_MANAGER * m_nonCachedManager
Container for storing non-cached VERTEX_ITEMs.
Definition: opengl_gal.h:350
GLUtesselator * m_tesselator
Definition: opengl_gal.h:384
wxEvtHandler * m_paintListener
Definition: opengl_gal.h:338
bool m_isContextLocked
Used for assertion checking.
Definition: opengl_gal.h:372
wxCursor m_currentwxCursor
wxCursor showing the current native cursor
Definition: opengl_gal.h:379
bool m_isFramebufferInitialized
Are the framebuffers initialized?
Definition: opengl_gal.h:366
static GLuint g_fontTexture
Bitmap font texture handle (shared)
Definition: opengl_gal.h:340
double calcAngleStep(double aRadius) const
Compute the angle step when drawing arcs/circles approximated with lines.
Definition: opengl_gal.h:569
bool m_isGrouping
Was a group started?
Definition: opengl_gal.h:371
GLint ufm_screenPixelSize
Definition: opengl_gal.h:375
GLint ufm_pixelSizeMultiplier
Definition: opengl_gal.h:376
VERTEX_MANAGER * m_tempManager
Container for storing temp (diff mode) VERTEX_ITEMs.
Definition: opengl_gal.h:352
SHADER * m_shader
There is only one shader used for different objects.
Definition: opengl_gal.h:362
RENDER_TARGET m_currentTarget
Current rendering target.
Definition: opengl_gal.h:359
wxGLContext * m_glPrivContext
Canvas-specific OpenGL context.
Definition: opengl_gal.h:334
static bool m_isBitmapFontLoaded
Is the bitmap font texture loaded?
Definition: opengl_gal.h:367
static wxGLContext * m_glMainContext
Parent OpenGL context.
Definition: opengl_gal.h:333
unsigned int m_tempBuffer
Temporary rendering target (for diffing etc.)
Definition: opengl_gal.h:358
static int m_instanceCounter
GL GAL instance counter.
Definition: opengl_gal.h:336
bool IsOpenGlEngine() override
Return true if the GAL engine is a OpenGL based type.
Definition: opengl_gal.h:101
Provide the access to the OpenGL shaders.
Definition: shader.h:77
Class to control vertex container and GPU with possibility of emulating old-style OpenGL 1....
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
Represent a set of closed polygons.
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition: utf8.h:72
KICURSOR
Definition: cursors.h:34
#define GAL_API
Definition: gal.h:28
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:247
RENDER_TARGET
RENDER_TARGET: Possible rendering targets.
Definition: definitions.h:47
Class to store instances of VERTEX without caching.
Handle multitarget rendering (ie.
#define SEG_PER_CIRCLE_COUNT
Definition: opengl_gal.h:53
void DrawSegment(const ROUND_SEGMENT_2D &aSegment, unsigned int aNrSidesPerCircle)
Draw a thick line segment with rounded ends.
VERTEX_MANAGER * vboManager
Manager used for storing new vertices.
Definition: opengl_gal.h:323
std::deque< std::shared_ptr< GLdouble > > & intersectPoints
Intersect points, that have to be freed after tessellation.
Definition: opengl_gal.h:326
Structure to keep VIEW_CONTROLS settings for easy store/restore operations.
Definition: view_controls.h:43
VECTOR2< double > VECTOR2D
Definition: vector2d.h:587
Class to handle an item held in a container.