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, 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
55class GLUtesselator;
56
57struct bitmap_glyph;
58
59namespace KIGFX
60{
61class SHADER;
62class GL_BITMAP_CACHE;
63
64
72class GAL_API OPENGL_GAL : public GAL, public HIDPI_GL_CANVAS
73{
74public:
88 OPENGL_GAL( const KIGFX::VC_SETTINGS& aVcSettings, GAL_DISPLAY_OPTIONS& aDisplayOptions,
89 wxWindow* aParent,
90 wxEvtHandler* aMouseListener = nullptr, wxEvtHandler* aPaintListener = nullptr,
91 const wxString& aName = wxT( "GLCanvas" ) );
92
94
101 static wxString CheckFeatures( GAL_DISPLAY_OPTIONS& aOptions );
102
103 bool IsOpenGlEngine() override { return true; }
104
106 bool IsInitialized() const override
107 {
108 // is*Initialized flags, but it is enough for OpenGL to show up
109 return IsShownOnScreen() && !GetClientRect().IsEmpty();
110 }
111
113 bool IsVisible() const override
114 {
115 return IsShownOnScreen() && !GetClientRect().IsEmpty();
116 }
117
118 void SetMinLineWidth( float aLineWidth ) override;
119
120 // ---------------
121 // Drawing methods
122 // ---------------
123
125 void DrawLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
126
128 void DrawSegment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint,
129 double aWidth ) override;
130
132 void DrawSegmentChain( const std::vector<VECTOR2D>& aPointList, double aWidth ) override;
133 void DrawSegmentChain( const SHAPE_LINE_CHAIN& aLineChain, double aWidth ) override;
134
136 void DrawCircle( const VECTOR2D& aCenterPoint, double aRadius ) override;
137
139 void DrawHoleWall( const VECTOR2D& aCenterPoint, double aHoleRadius,
140 double aWallWidth ) override;
141
143 void DrawArc( const VECTOR2D& aCenterPoint, double aRadius, const EDA_ANGLE& aStartAngle,
144 const EDA_ANGLE& aAngle ) override;
145
147 void DrawArcSegment( const VECTOR2D& aCenterPoint, double aRadius, const EDA_ANGLE& aStartAngle,
148 const EDA_ANGLE& aAngle, double aWidth, double aMaxError ) override;
149
151 void DrawEllipse( const VECTOR2D& aCenterPoint, double aMajorRadius, double aMinorRadius,
152 const EDA_ANGLE& aRotation ) override;
153
155 void DrawEllipseArc( const VECTOR2D& aCenterPoint, double aMajorRadius, double aMinorRadius,
156 const EDA_ANGLE& aRotation, const EDA_ANGLE& aStartAngle,
157 const EDA_ANGLE& aEndAngle ) override;
158
160 void DrawRectangle( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
161
163 void DrawPolyline( const std::deque<VECTOR2D>& aPointList ) override;
164 void DrawPolyline( const std::vector<VECTOR2D>& aPointList ) override;
165 void DrawPolyline( const VECTOR2D aPointList[], int aListSize ) override;
166 void DrawPolyline( const SHAPE_LINE_CHAIN& aLineChain ) override;
167
169 void DrawPolylines( const std::vector<std::vector<VECTOR2D>>& aPointLists ) override;
170
172 void DrawPolygon( const std::deque<VECTOR2D>& aPointList ) override;
173 void DrawPolygon( const VECTOR2D aPointList[], int aListSize ) override;
174 void DrawPolygon( const SHAPE_POLY_SET& aPolySet, bool aStrokeTriangulation = false ) override;
175 void DrawPolygon( const SHAPE_LINE_CHAIN& aPolySet ) override;
176
178 virtual void DrawGlyph( const KIFONT::GLYPH& aGlyph, int aNth, int aTotal ) override;
179
181 virtual void DrawGlyphs( const std::vector<std::unique_ptr<KIFONT::GLYPH>>& aGlyphs ) override;
182
184 void DrawCurve( const VECTOR2D& startPoint, const VECTOR2D& controlPointA,
185 const VECTOR2D& controlPointB, const VECTOR2D& endPoint,
186 double aFilterValue = 0.0 ) override;
187
189 void DrawBitmap( const BITMAP_BASE& aBitmap, double alphaBlend = 1.0 ) override;
190
192 void BitmapText( const wxString& aText, const VECTOR2I& aPosition,
193 const EDA_ANGLE& aAngle ) override;
194
196 void DrawGrid() override;
197
198 // --------------
199 // Screen methods
200 // --------------
201
203 void ResizeScreen( int aWidth, int aHeight ) override;
204
206 bool Show( bool aShow ) override;
207
209 int GetSwapInterval() const override { return m_swapInterval; };
210
212 void Flush() override;
213
215 void ClearScreen( ) override;
216
217 // --------------
218 // Transformation
219 // --------------
220
222 void Transform( const MATRIX3x3D& aTransformation ) override;
223
225 void Rotate( double aAngle ) override;
226
228 void Translate( const VECTOR2D& aTranslation ) override;
229
231 void Scale( const VECTOR2D& aScale ) override;
232
234 void Save() override;
235
237 void Restore() override;
238
239 // --------------------------------------------
240 // Group methods
241 // ---------------------------------------------
242
244 int BeginGroup() override;
245
247 void EndGroup() override;
248
250 void DrawGroup( int aGroupNumber ) override;
251
253 void ChangeGroupColor( int aGroupNumber, const COLOR4D& aNewColor ) override;
254
256 void ChangeGroupDepth( int aGroupNumber, int aDepth ) override;
257
259 void DeleteGroup( int aGroupNumber ) override;
260
262 void ClearCache() override;
263
264 // --------------------------------------------------------
265 // Handling the world <-> screen transformation
266 // --------------------------------------------------------
267
269 void SetTarget( RENDER_TARGET aTarget ) override;
270
272 RENDER_TARGET GetTarget() const override;
273
275 void ClearTarget( RENDER_TARGET aTarget ) override;
276
278 virtual bool HasTarget( RENDER_TARGET aTarget ) override;
279
281 void SetNegativeDrawMode( bool aSetting ) override {}
282
284 void StartDiffLayer() override;
285 //
287 void EndDiffLayer() override;
288
289 void ComputeWorldScreenMatrix() override;
290
291 // -------
292 // Cursor
293 // -------
294
296 bool SetNativeCursorStyle( KICURSOR aCursor, bool aHiDPI ) override;
297
299 void DrawCursor( const VECTOR2D& aCursorPosition ) override;
300
307 void PostPaint( wxPaintEvent& aEvent );
308
309 void SetMouseListener( wxEvtHandler* aMouseListener )
310 {
311 m_mouseListener = aMouseListener;
312 }
313
314 void SetPaintListener( wxEvtHandler* aPaintListener )
315 {
316 m_paintListener = aPaintListener;
317 }
318
319 void EnableDepthTest( bool aEnabled = false ) override;
320
321 bool IsContextLocked() override
322 {
323 return m_isContextLocked;
324 }
325
326 void LockContext( int aClientCookie ) override;
327
328 void UnlockContext( int aClientCookie ) override;
329
331 void BeginDrawing() override;
332
334 void EndDrawing() override;
335
338 {
341
343 std::deque<std::shared_ptr<GLdouble>>& intersectPoints;
344 };
345
346private:
348 typedef GAL super;
349
350 static wxGLContext* m_glMainContext;
351 wxGLContext* m_glPrivContext;
353 static int m_instanceCounter;
354 wxEvtHandler* m_mouseListener;
355 wxEvtHandler* m_paintListener;
356
357 static GLuint g_fontTexture;
358
359 // Vertex buffer objects related fields
360 typedef std::unordered_map< unsigned int, std::shared_ptr<VERTEX_ITEM> > GROUPS_MAP;
361
363 unsigned int m_groupCounter;
369
372
373 // Framebuffer & compositing
375 unsigned int m_mainBuffer;
376 unsigned int m_overlayBuffer;
377 unsigned int m_tempBuffer;
379
380 // Shader
383
384 // Internal flags
400
403
404 std::unique_ptr<GL_BITMAP_CACHE> m_bitmapCache;
405
406 // Polygon tesselation
407 GLUtesselator* m_tesselator;
408 std::deque<std::shared_ptr<GLdouble>> m_tessIntersects;
409
411 void beginUpdate() override;
412
414 void endUpdate() override;
415
417 bool updatedGalDisplayOptions( const GAL_DISPLAY_OPTIONS& aOptions ) override;
418
427 void drawLineQuad( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint,
428 bool aReserve = true );
429
435 void reserveLineQuads( const int aLineCount );
436
448 void drawSemiCircle( const VECTOR2D& aCenterPoint, double aRadius, double aAngle );
449
458 void drawFilledSemiCircle( const VECTOR2D& aCenterPoint, double aRadius, double aAngle );
459
469 void drawStrokedSemiCircle( const VECTOR2D& aCenterPoint, double aRadius, double aAngle,
470 bool aReserve = true );
471
477 void drawCircle( const VECTOR2D& aCenterPoint, double aRadius, bool aReserve = true );
478
486 void drawPolyline( const std::function<VECTOR2D( int )>& aPointGetter, int aPointCount,
487 bool aReserve = true );
488
496 void drawSegmentChain( const std::function<VECTOR2D( int )>& aPointGetter, int aPointCount,
497 double aWidth, bool aReserve = true );
498
502 void drawSegment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint, double aWidth,
503 bool aReserve = true );
504
512 void drawPolygon( GLdouble* aPoints, int aPointCount );
513
520 void drawTriangulatedPolyset( const SHAPE_POLY_SET& aPoly, bool aStrokeTriangulation );
521
531 int drawBitmapChar( unsigned long aChar, bool aReserve = true );
532
544 void drawBitmapOverbar( double aLength, double aHeight, bool aReserve = true );
545
553 std::pair<VECTOR2D, float> computeBitmapTextSize( const UTF8& aText ) const;
554
555 // Event handling
561 void onPaint( wxPaintEvent& aEvent );
562
568 void skipMouseEvent( wxMouseEvent& aEvent );
569
575 void skipGestureEvent( wxGestureEvent& aEvent );
576
582 void onSetNativeCursor( wxSetCursorEvent& aEvent );
583
587 void blitCursor();
588
594 unsigned int getNewGroupNumber();
595
599 double calcAngleStep( double aRadius ) const
600 {
601 // Bigger arcs need smaller alpha increment to make them look smooth
602 return std::min( 1e6 / aRadius, 2.0 * M_PI / SEG_PER_CIRCLE_COUNT );
603 }
604
605 double getWorldPixelSize() const;
606
607 VECTOR2D getScreenPixelSize() const;
608
614 void setupShaderParameters();
615
621 void init();
622};
623} // namespace KIGFX
624
625#endif // OPENGLGAL_H_
This class handle bitmap images in KiCad.
Definition bitmap_base.h:49
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:105
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:363
std::unordered_map< unsigned int, std::shared_ptr< VERTEX_ITEM > > GROUPS_MAP
Definition opengl_gal.h:360
VERTEX_MANAGER * m_overlayManager
Container for storing overlaid VERTEX_ITEMs.
Definition opengl_gal.h:368
bool m_isInitialized
Basic initialization flag, has to be done when the window is visible.
Definition opengl_gal.h:388
VERTEX_MANAGER * m_currentManager
Currently used VERTEX_MANAGER (for storing VERTEX_ITEMs).
Definition opengl_gal.h:364
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:408
void SetNegativeDrawMode(bool aSetting) override
Set negative draw mode in the renderer.
Definition opengl_gal.h:281
bool IsInitialized() const override
Return the initialization status for the canvas.
Definition opengl_gal.h:106
WX_CURSOR_TYPE m_currentwxCursor
wx cursor showing the current native cursor.
Definition opengl_gal.h:402
unsigned int m_mainBuffer
Main rendering target.
Definition opengl_gal.h:375
std::unique_ptr< GL_BITMAP_CACHE > m_bitmapCache
Definition opengl_gal.h:404
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:321
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:387
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:376
OPENGL_COMPOSITOR * m_compositor
Handles multiple rendering targets.
Definition opengl_gal.h:374
VERTEX_MANAGER * m_cachedManager
Container for storing cached VERTEX_ITEMs.
Definition opengl_gal.h:366
void SetPaintListener(wxEvtHandler *aPaintListener)
Definition opengl_gal.h:314
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:397
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:396
bool IsVisible() const override
Return true if the GAL canvas is visible on the screen.
Definition opengl_gal.h:113
wxEvtHandler * m_mouseListener
Definition opengl_gal.h:354
int m_swapInterval
Used to store swap interval information.
Definition opengl_gal.h:352
GROUPS_MAP m_groups
Stores information about VBO objects (groups)
Definition opengl_gal.h:362
void endUpdate() override
GLint ufm_fontTextureWidth
Definition opengl_gal.h:399
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:209
GAL super
Super class definition.
Definition opengl_gal.h:348
void SetMouseListener(wxEvtHandler *aMouseListener)
Definition opengl_gal.h:309
VERTEX_MANAGER * m_nonCachedManager
Container for storing non-cached VERTEX_ITEMs.
Definition opengl_gal.h:367
int drawBitmapChar(unsigned long aChar, bool aReserve=true)
Draw a single character using bitmap font.
GLUtesselator * m_tesselator
Definition opengl_gal.h:407
wxEvtHandler * m_paintListener
Definition opengl_gal.h:355
bool m_isContextLocked
Used for assertion checking.
Definition opengl_gal.h:391
bool m_isFramebufferInitialized
Are the framebuffers initialized?
Definition opengl_gal.h:385
static GLuint g_fontTexture
Bitmap font texture handle (shared)
Definition opengl_gal.h:357
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:599
bool m_isGrouping
Was a group started?
Definition opengl_gal.h:390
GLint ufm_screenPixelSize
Definition opengl_gal.h:394
GLint ufm_pixelSizeMultiplier
Definition opengl_gal.h:395
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:371
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:382
RENDER_TARGET m_currentTarget
Current rendering target.
Definition opengl_gal.h:378
wxGLContext * m_glPrivContext
Canvas-specific OpenGL context.
Definition opengl_gal.h:351
static bool m_isBitmapFontLoaded
Is the bitmap font texture loaded?
Definition opengl_gal.h:386
static wxGLContext * m_glMainContext
Parent OpenGL context.
Definition opengl_gal.h:350
unsigned int m_tempBuffer
Temporary rendering target (for diffing etc.)
Definition opengl_gal.h:377
static int m_instanceCounter
GL GAL instance counter.
Definition opengl_gal.h:353
bool IsOpenGlEngine() override
Return true if the GAL engine is a OpenGL based type.
Definition opengl_gal.h:103
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:71
KICURSOR
Definition cursors.h:44
wxCursor WX_CURSOR_TYPE
Represents either a wxCursorBundle for wx 3.3+ or a wxCursor for older versions.
Definition cursors.h:39
#define GAL_API
Definition gal.h:27
MATRIX3x3< double > MATRIX3x3D
Definition matrix3x3.h:473
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:33
RENDER_TARGET
RENDER_TARGET: Possible rendering targets.
Definition definitions.h:36
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:340
std::deque< std::shared_ptr< GLdouble > > & intersectPoints
Intersect points, that have to be freed after tessellation.
Definition opengl_gal.h:343
Structure to keep VIEW_CONTROLS settings for easy store/restore operations.
#define M_PI
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687
VECTOR2< double > VECTOR2D
Definition vector2d.h:686
Class to handle an item held in a container.