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 The 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, see <https://www.gnu.org/licenses/>.
23 */
24
25#ifndef OPENGLGAL_H_
26#define OPENGLGAL_H_
27
28// GAL imports
29#include <gal/gal.h>
32#include <gal/opengl/shader.h>
38#include <gal/hidpi_gl_canvas.h>
39
40#include <unordered_map>
41#include <memory>
42#include <wx/event.h>
43
44#ifndef CALLBACK
45#define CALLBACK
46#endif
47
49#define SEG_PER_CIRCLE_COUNT 64
50
51class GLUtesselator;
52class wxImage;
53
54struct bitmap_glyph;
55
56namespace KIGFX
57{
58class SHADER;
59class GL_BITMAP_CACHE;
60
61
69class GAL_API OPENGL_GAL : public GAL, public HIDPI_GL_CANVAS
70{
71public:
85 OPENGL_GAL( const KIGFX::VC_SETTINGS& aVcSettings, GAL_DISPLAY_OPTIONS& aDisplayOptions,
86 wxWindow* aParent,
87 wxEvtHandler* aMouseListener = nullptr, wxEvtHandler* aPaintListener = nullptr,
88 const wxString& aName = wxT( "GLCanvas" ) );
89
91
98 static wxString CheckFeatures( GAL_DISPLAY_OPTIONS& aOptions );
99
100 bool IsOpenGlEngine() override { return true; }
101
103 bool IsInitialized() const override
104 {
105 // is*Initialized flags, but it is enough for OpenGL to show up
106 return IsShownOnScreen() && !GetClientRect().IsEmpty();
107 }
108
110 bool IsVisible() const override
111 {
112 return IsShownOnScreen() && !GetClientRect().IsEmpty();
113 }
114
115 void SetMinLineWidth( float aLineWidth ) override;
116
117 // ---------------
118 // Drawing methods
119 // ---------------
120
122 void DrawLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
123
125 void DrawSegment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint,
126 double aWidth ) override;
127
129 void DrawSegmentChain( const std::vector<VECTOR2D>& aPointList, double aWidth ) override;
130 void DrawSegmentChain( const SHAPE_LINE_CHAIN& aLineChain, double aWidth ) override;
131
133 void DrawCircle( const VECTOR2D& aCenterPoint, double aRadius ) override;
134
136 void DrawHoleWall( const VECTOR2D& aCenterPoint, double aHoleRadius,
137 double aWallWidth ) override;
138
140 void DrawArc( const VECTOR2D& aCenterPoint, double aRadius, const EDA_ANGLE& aStartAngle,
141 const EDA_ANGLE& aAngle ) override;
142
144 void DrawArcSegment( const VECTOR2D& aCenterPoint, double aRadius, const EDA_ANGLE& aStartAngle,
145 const EDA_ANGLE& aAngle, double aWidth, double aMaxError ) override;
146
148 void DrawEllipse( const VECTOR2D& aCenterPoint, double aMajorRadius, double aMinorRadius,
149 const EDA_ANGLE& aRotation ) override;
150
152 void DrawEllipseArc( const VECTOR2D& aCenterPoint, double aMajorRadius, double aMinorRadius,
153 const EDA_ANGLE& aRotation, const EDA_ANGLE& aStartAngle,
154 const EDA_ANGLE& aEndAngle ) override;
155
157 void DrawRectangle( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
158
160 void DrawPolyline( const std::deque<VECTOR2D>& aPointList ) override;
161 void DrawPolyline( const std::vector<VECTOR2D>& aPointList ) override;
162 void DrawPolyline( const VECTOR2D aPointList[], int aListSize ) override;
163 void DrawPolyline( const SHAPE_LINE_CHAIN& aLineChain ) override;
164
166 void DrawPolylines( const std::vector<std::vector<VECTOR2D>>& aPointLists ) override;
167
169 void DrawPolygon( const std::deque<VECTOR2D>& aPointList ) override;
170 void DrawPolygon( const VECTOR2D aPointList[], int aListSize ) override;
171 void DrawPolygon( const SHAPE_POLY_SET& aPolySet, bool aStrokeTriangulation = false ) override;
172 void DrawPolygon( const SHAPE_LINE_CHAIN& aPolySet ) override;
173
175 virtual void DrawGlyph( const KIFONT::GLYPH& aGlyph, int aNth, int aTotal ) override;
176
178 virtual void DrawGlyphs( const std::vector<std::unique_ptr<KIFONT::GLYPH>>& aGlyphs ) override;
179
181 void DrawCurve( const VECTOR2D& startPoint, const VECTOR2D& controlPointA,
182 const VECTOR2D& controlPointB, const VECTOR2D& endPoint,
183 double aFilterValue = 0.0 ) override;
184
186 void DrawBitmap( const BITMAP_BASE& aBitmap, double alphaBlend = 1.0 ) override;
187
189 void BitmapText( const wxString& aText, const VECTOR2I& aPosition,
190 const EDA_ANGLE& aAngle ) override;
191
193 void DrawGrid() override;
194
195 // --------------
196 // Screen methods
197 // --------------
198
200 void ResizeScreen( int aWidth, int aHeight ) override;
201
203 bool Show( bool aShow ) override;
204
206 int GetSwapInterval() const override { return m_swapInterval; };
207
209 void Flush() override;
210
212 void ClearScreen( ) override;
213
214 // --------------
215 // Transformation
216 // --------------
217
219 void Transform( const MATRIX3x3D& aTransformation ) override;
220
222 void Rotate( double aAngle ) override;
223
225 void Translate( const VECTOR2D& aTranslation ) override;
226
228 void Scale( const VECTOR2D& aScale ) override;
229
231 void Save() override;
232
234 void Restore() override;
235
236 // --------------------------------------------
237 // Group methods
238 // ---------------------------------------------
239
241 int BeginGroup() override;
242
244 void EndGroup() override;
245
247 void DrawGroup( int aGroupNumber ) override;
248
250 void ChangeGroupColor( int aGroupNumber, const COLOR4D& aNewColor ) override;
251
253 void ChangeGroupDepth( int aGroupNumber, int aDepth ) override;
254
256 void DeleteGroup( int aGroupNumber ) override;
257
259 void ClearCache() override;
260
261 // --------------------------------------------------------
262 // Handling the world <-> screen transformation
263 // --------------------------------------------------------
264
266 void SetTarget( RENDER_TARGET aTarget ) override;
267
269 RENDER_TARGET GetTarget() const override;
270
272 void ClearTarget( RENDER_TARGET aTarget ) override;
273
275 virtual bool HasTarget( RENDER_TARGET aTarget ) override;
276
278 void SetNegativeDrawMode( bool aSetting ) override {}
279
281 void StartDiffLayer() override;
282 //
284 void EndDiffLayer() override;
285
286 void ComputeWorldScreenMatrix() override;
287
288 // -------
289 // Cursor
290 // -------
291
293 bool SetNativeCursorStyle( KICURSOR aCursor, bool aHiDPI ) override;
294
296 void DrawCursor( const VECTOR2D& aCursorPosition ) override;
297
304 void PostPaint( wxPaintEvent& aEvent );
305
306 void SetMouseListener( wxEvtHandler* aMouseListener )
307 {
308 m_mouseListener = aMouseListener;
309 }
310
311 void SetPaintListener( wxEvtHandler* aPaintListener )
312 {
313 m_paintListener = aPaintListener;
314 }
315
316 void EnableDepthTest( bool aEnabled = false ) override;
317
318 bool IsContextLocked() override
319 {
320 return m_isContextLocked;
321 }
322
323 void LockContext( int aClientCookie ) override;
324
325 void UnlockContext( int aClientCookie ) override;
326
328 void BeginDrawing() override;
329
331 void EndDrawing() override;
332
333 bool GetScreenshot( wxImage& aDstImage );
334
337 {
340
342 std::deque<std::shared_ptr<GLdouble>>& intersectPoints;
343 };
344
345private:
347 typedef GAL super;
348
349 static wxGLContext* m_glMainContext;
350 wxGLContext* m_glPrivContext;
352 static int m_instanceCounter;
353 wxEvtHandler* m_mouseListener;
354 wxEvtHandler* m_paintListener;
355
356 static GLuint g_fontTexture;
357
358 // Vertex buffer objects related fields
359 typedef std::unordered_map< unsigned int, std::shared_ptr<VERTEX_ITEM> > GROUPS_MAP;
360
362 unsigned int m_groupCounter;
368
371
372 // Framebuffer & compositing
374 unsigned int m_mainBuffer;
375 unsigned int m_overlayBuffer;
376 unsigned int m_tempBuffer;
378
379 // Shader
382
383 // Internal flags
399
402
403 std::unique_ptr<GL_BITMAP_CACHE> m_bitmapCache;
404
405 // Polygon tesselation
406 GLUtesselator* m_tesselator;
407 std::deque<std::shared_ptr<GLdouble>> m_tessIntersects;
408
410 void beginUpdate() override;
411
413 void endUpdate() override;
414
416 bool updatedGalDisplayOptions( const GAL_DISPLAY_OPTIONS& aOptions ) override;
417
426 void drawLineQuad( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint,
427 bool aReserve = true );
428
434 void reserveLineQuads( const int aLineCount );
435
447 void drawSemiCircle( const VECTOR2D& aCenterPoint, double aRadius, double aAngle );
448
457 void drawFilledSemiCircle( const VECTOR2D& aCenterPoint, double aRadius, double aAngle );
458
468 void drawStrokedSemiCircle( const VECTOR2D& aCenterPoint, double aRadius, double aAngle,
469 bool aReserve = true );
470
476 void drawCircle( const VECTOR2D& aCenterPoint, double aRadius, bool aReserve = true );
477
485 void drawPolyline( const std::function<VECTOR2D( int )>& aPointGetter, int aPointCount,
486 bool aReserve = true );
487
495 void drawSegmentChain( const std::function<VECTOR2D( int )>& aPointGetter, int aPointCount,
496 double aWidth, bool aReserve = true );
497
501 void drawSegment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint, double aWidth,
502 bool aReserve = true );
503
511 void drawPolygon( GLdouble* aPoints, int aPointCount );
512
519 void drawTriangulatedPolyset( const SHAPE_POLY_SET& aPoly, bool aStrokeTriangulation );
520
530 int drawBitmapChar( unsigned long aChar, bool aReserve = true );
531
543 void drawBitmapOverbar( double aLength, double aHeight, bool aReserve = true );
544
552 std::pair<VECTOR2D, float> computeBitmapTextSize( const UTF8& aText ) const;
553
554 // Event handling
560 void onPaint( wxPaintEvent& aEvent );
561
567 void skipMouseEvent( wxMouseEvent& aEvent );
568
574 void skipGestureEvent( wxGestureEvent& aEvent );
575
581 void onSetNativeCursor( wxSetCursorEvent& aEvent );
582
586 void blitCursor();
587
593 unsigned int getNewGroupNumber();
594
598 double calcAngleStep( double aRadius ) const
599 {
600 // Bigger arcs need smaller alpha increment to make them look smooth
601 return std::min( 1e6 / aRadius, 2.0 * M_PI / SEG_PER_CIRCLE_COUNT );
602 }
603
604 double getWorldPixelSize() const;
605
606 VECTOR2D getScreenPixelSize() const;
607
613 void setupShaderParameters();
614
620 void init();
621};
622} // namespace KIGFX
623
624#endif // OPENGLGAL_H_
This class handle bitmap images in KiCad.
Definition bitmap_base.h:45
HIDPI_GL_CANVAS(const KIGFX::VC_SETTINGS &aSettings, wxWindow *aParent, const wxGLAttributes &aGLAttribs, wxWindowID aId=wxID_ANY, const wxPoint &aPos=wxDefaultPosition, const wxSize &aSize=wxDefaultSize, long aStyle=0, const wxString &aName=wxGLCanvasName, const wxPalette &aPalette=wxNullPalette)
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
GAL(GAL_DISPLAY_OPTIONS &aOptions)
void drawPolygon(GLdouble *aPoints, int aPointCount)
Draw a filled polygon.
void skipMouseEvent(wxMouseEvent &aEvent)
Skip the mouse event to the parent.
void drawSegment(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint, double aWidth, bool aReserve=true)
Internal method for segment drawing.
unsigned int m_groupCounter
Counter used for generating keys for groups.
Definition opengl_gal.h:362
std::unordered_map< unsigned int, std::shared_ptr< VERTEX_ITEM > > GROUPS_MAP
Definition opengl_gal.h:359
VERTEX_MANAGER * m_overlayManager
Container for storing overlaid VERTEX_ITEMs.
Definition opengl_gal.h:367
bool m_isInitialized
Basic initialization flag, has to be done when the window is visible.
Definition opengl_gal.h:387
VERTEX_MANAGER * m_currentManager
Currently used VERTEX_MANAGER (for storing VERTEX_ITEMs).
Definition opengl_gal.h:363
void drawCircle(const VECTOR2D &aCenterPoint, double aRadius, bool aReserve=true)
Internal method for circle drawing.
std::deque< std::shared_ptr< GLdouble > > m_tessIntersects
Definition opengl_gal.h:407
void SetNegativeDrawMode(bool aSetting) override
Set negative draw mode in the renderer.
Definition opengl_gal.h:278
bool IsInitialized() const override
Return the initialization status for the canvas.
Definition opengl_gal.h:103
WX_CURSOR_TYPE m_currentwxCursor
wx cursor showing the current native cursor.
Definition opengl_gal.h:401
unsigned int m_mainBuffer
Main rendering target.
Definition opengl_gal.h:374
std::unique_ptr< GL_BITMAP_CACHE > m_bitmapCache
Definition opengl_gal.h:403
std::pair< VECTOR2D, float > computeBitmapTextSize(const UTF8 &aText) const
Compute a size of text drawn using bitmap font with current text setting applied.
bool IsContextLocked() override
Checks the state of the context lock.
Definition opengl_gal.h:318
void blitCursor()
Blit cursor into the current screen.
static wxString CheckFeatures(GAL_DISPLAY_OPTIONS &aOptions)
Checks OpenGL features.
void drawBitmapOverbar(double aLength, double aHeight, bool aReserve=true)
Draw an overbar over the currently drawn text.
bool m_isBitmapFontInitialized
Is the shader set to use bitmap fonts?
Definition opengl_gal.h:386
void drawSegmentChain(const std::function< VECTOR2D(int)> &aPointGetter, int aPointCount, double aWidth, bool aReserve=true)
Generic way of drawing a chain of segments stored in different containers.
bool updatedGalDisplayOptions(const GAL_DISPLAY_OPTIONS &aOptions) override
Handle updating display options.
unsigned int m_overlayBuffer
Auxiliary rendering target (for menus etc.)
Definition opengl_gal.h:375
OPENGL_COMPOSITOR * m_compositor
Handles multiple rendering targets.
Definition opengl_gal.h:373
VERTEX_MANAGER * m_cachedManager
Container for storing cached VERTEX_ITEMs.
Definition opengl_gal.h:365
void SetPaintListener(wxEvtHandler *aPaintListener)
Definition opengl_gal.h:311
void drawFilledSemiCircle(const VECTOR2D &aCenterPoint, double aRadius, double aAngle)
Draw a filled semicircle.
void onPaint(wxPaintEvent &aEvent)
This is the OnPaint event handler.
GLint ufm_minLinePixelWidth
Definition opengl_gal.h:396
void drawStrokedSemiCircle(const VECTOR2D &aCenterPoint, double aRadius, double aAngle, bool aReserve=true)
Draw a stroked semicircle.
unsigned int getNewGroupNumber()
Return a valid key that can be used as a new group number.
GLint ufm_antialiasingOffset
Definition opengl_gal.h:395
bool IsVisible() const override
Return true if the GAL canvas is visible on the screen.
Definition opengl_gal.h:110
wxEvtHandler * m_mouseListener
Definition opengl_gal.h:353
int m_swapInterval
Used to store swap interval information.
Definition opengl_gal.h:351
GROUPS_MAP m_groups
Stores information about VBO objects (groups)
Definition opengl_gal.h:361
void endUpdate() override
GLint ufm_fontTextureWidth
Definition opengl_gal.h:398
void drawTriangulatedPolyset(const SHAPE_POLY_SET &aPoly, bool aStrokeTriangulation)
Draw a set of polygons with a cached triangulation.
int GetSwapInterval() const override
Return the swap interval. -1 for adaptive, 0 for disabled/unknown.
Definition opengl_gal.h:206
GAL super
Super class definition.
Definition opengl_gal.h:347
void SetMouseListener(wxEvtHandler *aMouseListener)
Definition opengl_gal.h:306
VERTEX_MANAGER * m_nonCachedManager
Container for storing non-cached VERTEX_ITEMs.
Definition opengl_gal.h:366
int drawBitmapChar(unsigned long aChar, bool aReserve=true)
Draw a single character using bitmap font.
GLUtesselator * m_tesselator
Definition opengl_gal.h:406
wxEvtHandler * m_paintListener
Definition opengl_gal.h:354
bool m_isContextLocked
Used for assertion checking.
Definition opengl_gal.h:390
bool m_isFramebufferInitialized
Are the framebuffers initialized?
Definition opengl_gal.h:384
static GLuint g_fontTexture
Bitmap font texture handle (shared)
Definition opengl_gal.h:356
void reserveLineQuads(const int aLineCount)
Reserve specified number of line quads.
OPENGL_GAL(const KIGFX::VC_SETTINGS &aVcSettings, GAL_DISPLAY_OPTIONS &aDisplayOptions, wxWindow *aParent, wxEvtHandler *aMouseListener=nullptr, wxEvtHandler *aPaintListener=nullptr, const wxString &aName=wxT("GLCanvas"))
void beginUpdate() override
double calcAngleStep(double aRadius) const
Compute the angle step when drawing arcs/circles approximated with lines.
Definition opengl_gal.h:598
bool m_isGrouping
Was a group started?
Definition opengl_gal.h:389
GLint ufm_screenPixelSize
Definition opengl_gal.h:393
GLint ufm_pixelSizeMultiplier
Definition opengl_gal.h:394
void drawPolyline(const std::function< VECTOR2D(int)> &aPointGetter, int aPointCount, bool aReserve=true)
Generic way of drawing a polyline stored in different containers.
void skipGestureEvent(wxGestureEvent &aEvent)
Skip the gesture event to the parent.
void drawSemiCircle(const VECTOR2D &aCenterPoint, double aRadius, double aAngle)
Draw a semicircle.
void drawLineQuad(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint, bool aReserve=true)
Draw a quad for the line.
VERTEX_MANAGER * m_tempManager
Container for storing temp (diff mode) VERTEX_ITEMs.
Definition opengl_gal.h:370
void onSetNativeCursor(wxSetCursorEvent &aEvent)
Give the correct cursor image when the native widget asks for it.
SHADER * m_shader
There is only one shader used for different objects.
Definition opengl_gal.h:381
RENDER_TARGET m_currentTarget
Current rendering target.
Definition opengl_gal.h:377
wxGLContext * m_glPrivContext
Canvas-specific OpenGL context.
Definition opengl_gal.h:350
static bool m_isBitmapFontLoaded
Is the bitmap font texture loaded?
Definition opengl_gal.h:385
static wxGLContext * m_glMainContext
Parent OpenGL context.
Definition opengl_gal.h:349
unsigned int m_tempBuffer
Temporary rendering target (for diffing etc.)
Definition opengl_gal.h:376
static int m_instanceCounter
GL GAL instance counter.
Definition opengl_gal.h:352
bool IsOpenGlEngine() override
Return true if the GAL engine is a OpenGL based type.
Definition opengl_gal.h:100
Provide the access to the OpenGL shaders.
Definition shader.h:73
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:67
KICURSOR
Definition cursors.h:40
wxCursor WX_CURSOR_TYPE
Represents either a wxCursorBundle for wx 3.3+ or a wxCursor for older versions.
Definition cursors.h:35
#define GAL_API
Definition gal.h:27
MATRIX3x3< double > MATRIX3x3D
Definition matrix3x3.h:469
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:29
RENDER_TARGET
RENDER_TARGET: Possible rendering targets.
Definition definitions.h:32
Class to store instances of VERTEX without caching.
Handle multitarget rendering (ie.
#define SEG_PER_CIRCLE_COUNT
Definition opengl_gal.h:49
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:339
std::deque< std::shared_ptr< GLdouble > > & intersectPoints
Intersect points, that have to be freed after tessellation.
Definition opengl_gal.h:342
Structure to keep VIEW_CONTROLS settings for easy store/restore operations.
#define M_PI
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
VECTOR2< double > VECTOR2D
Definition vector2d.h:682
Class to handle an item held in a container.