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
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 void SetMinLineWidth( float aLineWidth ) override;
117
118 // ---------------
119 // Drawing methods
120 // ---------------
121
123 void DrawLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
124
126 void DrawSegment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint,
127 double aWidth ) override;
128
130 void DrawSegmentChain( const std::vector<VECTOR2D>& aPointList, double aWidth ) override;
131 void DrawSegmentChain( const SHAPE_LINE_CHAIN& aLineChain, double aWidth ) override;
132
134 void DrawCircle( const VECTOR2D& aCenterPoint, double aRadius ) override;
135
137 void DrawHoleWall( const VECTOR2D& aCenterPoint, double aHoleRadius,
138 double aWallWidth ) override;
139
141 void DrawArc( const VECTOR2D& aCenterPoint, double aRadius, const EDA_ANGLE& aStartAngle,
142 const EDA_ANGLE& aAngle ) override;
143
145 void DrawArcSegment( const VECTOR2D& aCenterPoint, double aRadius, const EDA_ANGLE& aStartAngle,
146 const EDA_ANGLE& aAngle, double aWidth, double aMaxError ) override;
147
149 void DrawRectangle( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
150
152 void DrawPolyline( const std::deque<VECTOR2D>& aPointList ) override;
153 void DrawPolyline( const std::vector<VECTOR2D>& aPointList ) override;
154 void DrawPolyline( const VECTOR2D aPointList[], int aListSize ) override;
155 void DrawPolyline( const SHAPE_LINE_CHAIN& aLineChain ) override;
156
158 void DrawPolylines( const std::vector<std::vector<VECTOR2D>>& aPointLists ) override;
159
161 void DrawPolygon( const std::deque<VECTOR2D>& aPointList ) override;
162 void DrawPolygon( const VECTOR2D aPointList[], int aListSize ) override;
163 void DrawPolygon( const SHAPE_POLY_SET& aPolySet, bool aStrokeTriangulation = false ) override;
164 void DrawPolygon( const SHAPE_LINE_CHAIN& aPolySet ) override;
165
167 virtual void DrawGlyph( const KIFONT::GLYPH& aGlyph, int aNth, int aTotal ) override;
168
170 virtual void DrawGlyphs( const std::vector<std::unique_ptr<KIFONT::GLYPH>>& aGlyphs ) override;
171
173 void DrawCurve( const VECTOR2D& startPoint, const VECTOR2D& controlPointA,
174 const VECTOR2D& controlPointB, const VECTOR2D& endPoint,
175 double aFilterValue = 0.0 ) override;
176
178 void DrawBitmap( const BITMAP_BASE& aBitmap, double alphaBlend = 1.0 ) override;
179
181 void BitmapText( const wxString& aText, const VECTOR2I& aPosition,
182 const EDA_ANGLE& aAngle ) override;
183
185 void DrawGrid() override;
186
187 // --------------
188 // Screen methods
189 // --------------
190
192 void ResizeScreen( int aWidth, int aHeight ) override;
193
195 bool Show( bool aShow ) override;
196
198 int GetSwapInterval() const override { return m_swapInterval; };
199
201 void Flush() override;
202
204 void ClearScreen( ) override;
205
206 // --------------
207 // Transformation
208 // --------------
209
211 void Transform( const MATRIX3x3D& aTransformation ) override;
212
214 void Rotate( double aAngle ) override;
215
217 void Translate( const VECTOR2D& aTranslation ) override;
218
220 void Scale( const VECTOR2D& aScale ) override;
221
223 void Save() override;
224
226 void Restore() override;
227
228 // --------------------------------------------
229 // Group methods
230 // ---------------------------------------------
231
233 int BeginGroup() override;
234
236 void EndGroup() override;
237
239 void DrawGroup( int aGroupNumber ) override;
240
242 void ChangeGroupColor( int aGroupNumber, const COLOR4D& aNewColor ) override;
243
245 void ChangeGroupDepth( int aGroupNumber, int aDepth ) override;
246
248 void DeleteGroup( int aGroupNumber ) override;
249
251 void ClearCache() override;
252
253 // --------------------------------------------------------
254 // Handling the world <-> screen transformation
255 // --------------------------------------------------------
256
258 void SetTarget( RENDER_TARGET aTarget ) override;
259
261 RENDER_TARGET GetTarget() const override;
262
264 void ClearTarget( RENDER_TARGET aTarget ) override;
265
267 virtual bool HasTarget( RENDER_TARGET aTarget ) override;
268
270 void SetNegativeDrawMode( bool aSetting ) override {}
271
273 void StartDiffLayer() override;
274 //
276 void EndDiffLayer() override;
277
278 void ComputeWorldScreenMatrix() override;
279
280 // -------
281 // Cursor
282 // -------
283
285 bool SetNativeCursorStyle( KICURSOR aCursor, bool aHiDPI ) override;
286
288 void DrawCursor( const VECTOR2D& aCursorPosition ) override;
289
296 void PostPaint( wxPaintEvent& aEvent );
297
298 void SetMouseListener( wxEvtHandler* aMouseListener )
299 {
300 m_mouseListener = aMouseListener;
301 }
302
303 void SetPaintListener( wxEvtHandler* aPaintListener )
304 {
305 m_paintListener = aPaintListener;
306 }
307
308 void EnableDepthTest( bool aEnabled = false ) override;
309
310 bool IsContextLocked() override
311 {
312 return m_isContextLocked;
313 }
314
315 void LockContext( int aClientCookie ) override;
316
317 void UnlockContext( int aClientCookie ) override;
318
320 void BeginDrawing() override;
321
323 void EndDrawing() override;
324
327 {
330
332 std::deque<std::shared_ptr<GLdouble>>& intersectPoints;
333 };
334
335private:
337 typedef GAL super;
338
339 static wxGLContext* m_glMainContext;
340 wxGLContext* m_glPrivContext;
342 static int m_instanceCounter;
343 wxEvtHandler* m_mouseListener;
344 wxEvtHandler* m_paintListener;
345
346 static GLuint g_fontTexture;
347
348 // Vertex buffer objects related fields
349 typedef std::unordered_map< unsigned int, std::shared_ptr<VERTEX_ITEM> > GROUPS_MAP;
350
352 unsigned int m_groupCounter;
358
361
362 // Framebuffer & compositing
364 unsigned int m_mainBuffer;
365 unsigned int m_overlayBuffer;
366 unsigned int m_tempBuffer;
368
369 // Shader
372
373 // Internal flags
389
392
393 std::unique_ptr<GL_BITMAP_CACHE> m_bitmapCache;
394
395 // Polygon tesselation
396 GLUtesselator* m_tesselator;
397 std::deque<std::shared_ptr<GLdouble>> m_tessIntersects;
398
400 void beginUpdate() override;
401
403 void endUpdate() override;
404
406 bool updatedGalDisplayOptions( const GAL_DISPLAY_OPTIONS& aOptions ) override;
407
416 void drawLineQuad( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint,
417 bool aReserve = true );
418
424 void reserveLineQuads( const int aLineCount );
425
437 void drawSemiCircle( const VECTOR2D& aCenterPoint, double aRadius, double aAngle );
438
447 void drawFilledSemiCircle( const VECTOR2D& aCenterPoint, double aRadius, double aAngle );
448
458 void drawStrokedSemiCircle( const VECTOR2D& aCenterPoint, double aRadius, double aAngle,
459 bool aReserve = true );
460
466 void drawCircle( const VECTOR2D& aCenterPoint, double aRadius, bool aReserve = true );
467
475 void drawPolyline( const std::function<VECTOR2D( int )>& aPointGetter, int aPointCount,
476 bool aReserve = true );
477
485 void drawSegmentChain( const std::function<VECTOR2D( int )>& aPointGetter, int aPointCount,
486 double aWidth, bool aReserve = true );
487
491 void drawSegment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint, double aWidth,
492 bool aReserve = true );
493
501 void drawPolygon( GLdouble* aPoints, int aPointCount );
502
509 void drawTriangulatedPolyset( const SHAPE_POLY_SET& aPoly, bool aStrokeTriangulation );
510
520 int drawBitmapChar( unsigned long aChar, bool aReserve = true );
521
533 void drawBitmapOverbar( double aLength, double aHeight, bool aReserve = true );
534
542 std::pair<VECTOR2D, float> computeBitmapTextSize( const UTF8& aText ) const;
543
544 // Event handling
550 void onPaint( wxPaintEvent& aEvent );
551
557 void skipMouseEvent( wxMouseEvent& aEvent );
558
564 void skipGestureEvent( wxGestureEvent& aEvent );
565
571 void onSetNativeCursor( wxSetCursorEvent& aEvent );
572
576 void blitCursor();
577
583 unsigned int getNewGroupNumber();
584
588 double calcAngleStep( double aRadius ) const
589 {
590 // Bigger arcs need smaller alpha increment to make them look smooth
591 return std::min( 1e6 / aRadius, 2.0 * M_PI / SEG_PER_CIRCLE_COUNT );
592 }
593
594 double getWorldPixelSize() const;
595
596 VECTOR2D getScreenPixelSize() const;
597
603 void setupShaderParameters();
604
610 void init();
611};
612} // namespace KIGFX
613
614#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:104
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:352
std::unordered_map< unsigned int, std::shared_ptr< VERTEX_ITEM > > GROUPS_MAP
Definition opengl_gal.h:349
VERTEX_MANAGER * m_overlayManager
Container for storing overlaid VERTEX_ITEMs.
Definition opengl_gal.h:357
bool m_isInitialized
Basic initialization flag, has to be done when the window is visible.
Definition opengl_gal.h:377
VERTEX_MANAGER * m_currentManager
Currently used VERTEX_MANAGER (for storing VERTEX_ITEMs).
Definition opengl_gal.h:353
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:397
void SetNegativeDrawMode(bool aSetting) override
Set negative draw mode in the renderer.
Definition opengl_gal.h:270
bool IsInitialized() const override
Return the initialization status for the canvas.
Definition opengl_gal.h:104
WX_CURSOR_TYPE m_currentwxCursor
wx cursor showing the current native cursor.
Definition opengl_gal.h:391
unsigned int m_mainBuffer
Main rendering target.
Definition opengl_gal.h:364
std::unique_ptr< GL_BITMAP_CACHE > m_bitmapCache
Definition opengl_gal.h:393
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:310
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:376
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:365
OPENGL_COMPOSITOR * m_compositor
Handles multiple rendering targets.
Definition opengl_gal.h:363
VERTEX_MANAGER * m_cachedManager
Container for storing cached VERTEX_ITEMs.
Definition opengl_gal.h:355
void SetPaintListener(wxEvtHandler *aPaintListener)
Definition opengl_gal.h:303
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:386
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:385
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:343
int m_swapInterval
Used to store swap interval information.
Definition opengl_gal.h:341
GROUPS_MAP m_groups
Stores information about VBO objects (groups)
Definition opengl_gal.h:351
void endUpdate() override
GLint ufm_fontTextureWidth
Definition opengl_gal.h:388
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:198
GAL super
Super class definition.
Definition opengl_gal.h:337
void SetMouseListener(wxEvtHandler *aMouseListener)
Definition opengl_gal.h:298
VERTEX_MANAGER * m_nonCachedManager
Container for storing non-cached VERTEX_ITEMs.
Definition opengl_gal.h:356
int drawBitmapChar(unsigned long aChar, bool aReserve=true)
Draw a single character using bitmap font.
GLUtesselator * m_tesselator
Definition opengl_gal.h:396
wxEvtHandler * m_paintListener
Definition opengl_gal.h:344
bool m_isContextLocked
Used for assertion checking.
Definition opengl_gal.h:380
bool m_isFramebufferInitialized
Are the framebuffers initialized?
Definition opengl_gal.h:374
static GLuint g_fontTexture
Bitmap font texture handle (shared)
Definition opengl_gal.h:346
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:588
bool m_isGrouping
Was a group started?
Definition opengl_gal.h:379
GLint ufm_screenPixelSize
Definition opengl_gal.h:383
GLint ufm_pixelSizeMultiplier
Definition opengl_gal.h:384
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:360
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:371
RENDER_TARGET m_currentTarget
Current rendering target.
Definition opengl_gal.h:367
wxGLContext * m_glPrivContext
Canvas-specific OpenGL context.
Definition opengl_gal.h:340
static bool m_isBitmapFontLoaded
Is the bitmap font texture loaded?
Definition opengl_gal.h:375
static wxGLContext * m_glMainContext
Parent OpenGL context.
Definition opengl_gal.h:339
unsigned int m_tempBuffer
Temporary rendering target (for diffing etc.)
Definition opengl_gal.h:366
static int m_instanceCounter
GL GAL instance counter.
Definition opengl_gal.h:342
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: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:28
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:329
std::deque< std::shared_ptr< GLdouble > > & intersectPoints
Intersect points, that have to be freed after tessellation.
Definition opengl_gal.h:332
Structure to keep VIEW_CONTROLS settings for easy store/restore operations.
#define M_PI
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695
VECTOR2< double > VECTOR2D
Definition vector2d.h:694
Class to handle an item held in a container.