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;
56class wxImage;
57
58struct bitmap_glyph;
59
60namespace KIGFX
61{
62class SHADER;
63class GL_BITMAP_CACHE;
64
65
73class GAL_API OPENGL_GAL : public GAL, public HIDPI_GL_CANVAS
74{
75public:
89 OPENGL_GAL( const KIGFX::VC_SETTINGS& aVcSettings, GAL_DISPLAY_OPTIONS& aDisplayOptions,
90 wxWindow* aParent,
91 wxEvtHandler* aMouseListener = nullptr, wxEvtHandler* aPaintListener = nullptr,
92 const wxString& aName = wxT( "GLCanvas" ) );
93
95
102 static wxString CheckFeatures( GAL_DISPLAY_OPTIONS& aOptions );
103
104 bool IsOpenGlEngine() override { return true; }
105
107 bool IsInitialized() const override
108 {
109 // is*Initialized flags, but it is enough for OpenGL to show up
110 return IsShownOnScreen() && !GetClientRect().IsEmpty();
111 }
112
114 bool IsVisible() const override
115 {
116 return IsShownOnScreen() && !GetClientRect().IsEmpty();
117 }
118
119 void SetMinLineWidth( float aLineWidth ) override;
120
121 // ---------------
122 // Drawing methods
123 // ---------------
124
126 void DrawLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
127
129 void DrawSegment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint,
130 double aWidth ) override;
131
133 void DrawSegmentChain( const std::vector<VECTOR2D>& aPointList, double aWidth ) override;
134 void DrawSegmentChain( const SHAPE_LINE_CHAIN& aLineChain, double aWidth ) override;
135
137 void DrawCircle( const VECTOR2D& aCenterPoint, double aRadius ) override;
138
140 void DrawHoleWall( const VECTOR2D& aCenterPoint, double aHoleRadius,
141 double aWallWidth ) override;
142
144 void DrawArc( const VECTOR2D& aCenterPoint, double aRadius, const EDA_ANGLE& aStartAngle,
145 const EDA_ANGLE& aAngle ) override;
146
148 void DrawArcSegment( const VECTOR2D& aCenterPoint, double aRadius, const EDA_ANGLE& aStartAngle,
149 const EDA_ANGLE& aAngle, double aWidth, double aMaxError ) override;
150
152 void DrawEllipse( const VECTOR2D& aCenterPoint, double aMajorRadius, double aMinorRadius,
153 const EDA_ANGLE& aRotation ) override;
154
156 void DrawEllipseArc( const VECTOR2D& aCenterPoint, double aMajorRadius, double aMinorRadius,
157 const EDA_ANGLE& aRotation, const EDA_ANGLE& aStartAngle,
158 const EDA_ANGLE& aEndAngle ) override;
159
161 void DrawRectangle( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
162
164 void DrawPolyline( const std::deque<VECTOR2D>& aPointList ) override;
165 void DrawPolyline( const std::vector<VECTOR2D>& aPointList ) override;
166 void DrawPolyline( const VECTOR2D aPointList[], int aListSize ) override;
167 void DrawPolyline( const SHAPE_LINE_CHAIN& aLineChain ) override;
168
170 void DrawPolylines( const std::vector<std::vector<VECTOR2D>>& aPointLists ) override;
171
173 void DrawPolygon( const std::deque<VECTOR2D>& aPointList ) override;
174 void DrawPolygon( const VECTOR2D aPointList[], int aListSize ) override;
175 void DrawPolygon( const SHAPE_POLY_SET& aPolySet, bool aStrokeTriangulation = false ) override;
176 void DrawPolygon( const SHAPE_LINE_CHAIN& aPolySet ) override;
177
179 virtual void DrawGlyph( const KIFONT::GLYPH& aGlyph, int aNth, int aTotal ) override;
180
182 virtual void DrawGlyphs( const std::vector<std::unique_ptr<KIFONT::GLYPH>>& aGlyphs ) override;
183
185 void DrawCurve( const VECTOR2D& startPoint, const VECTOR2D& controlPointA,
186 const VECTOR2D& controlPointB, const VECTOR2D& endPoint,
187 double aFilterValue = 0.0 ) override;
188
190 void DrawBitmap( const BITMAP_BASE& aBitmap, double alphaBlend = 1.0 ) override;
191
193 void BitmapText( const wxString& aText, const VECTOR2I& aPosition,
194 const EDA_ANGLE& aAngle ) override;
195
197 void DrawGrid() override;
198
199 // --------------
200 // Screen methods
201 // --------------
202
204 void ResizeScreen( int aWidth, int aHeight ) override;
205
207 bool Show( bool aShow ) override;
208
210 int GetSwapInterval() const override { return m_swapInterval; };
211
213 void Flush() override;
214
216 void ClearScreen( ) override;
217
218 // --------------
219 // Transformation
220 // --------------
221
223 void Transform( const MATRIX3x3D& aTransformation ) override;
224
226 void Rotate( double aAngle ) override;
227
229 void Translate( const VECTOR2D& aTranslation ) override;
230
232 void Scale( const VECTOR2D& aScale ) override;
233
235 void Save() override;
236
238 void Restore() override;
239
240 // --------------------------------------------
241 // Group methods
242 // ---------------------------------------------
243
245 int BeginGroup() override;
246
248 void EndGroup() override;
249
251 void DrawGroup( int aGroupNumber ) override;
252
254 void ChangeGroupColor( int aGroupNumber, const COLOR4D& aNewColor ) override;
255
257 void ChangeGroupDepth( int aGroupNumber, int aDepth ) override;
258
260 void DeleteGroup( int aGroupNumber ) override;
261
263 void ClearCache() override;
264
265 // --------------------------------------------------------
266 // Handling the world <-> screen transformation
267 // --------------------------------------------------------
268
270 void SetTarget( RENDER_TARGET aTarget ) override;
271
273 RENDER_TARGET GetTarget() const override;
274
276 void ClearTarget( RENDER_TARGET aTarget ) override;
277
279 virtual bool HasTarget( RENDER_TARGET aTarget ) override;
280
282 void SetNegativeDrawMode( bool aSetting ) override {}
283
285 void StartDiffLayer() override;
286 //
288 void EndDiffLayer() override;
289
290 void ComputeWorldScreenMatrix() override;
291
292 // -------
293 // Cursor
294 // -------
295
297 bool SetNativeCursorStyle( KICURSOR aCursor, bool aHiDPI ) override;
298
300 void DrawCursor( const VECTOR2D& aCursorPosition ) override;
301
308 void PostPaint( wxPaintEvent& aEvent );
309
310 void SetMouseListener( wxEvtHandler* aMouseListener )
311 {
312 m_mouseListener = aMouseListener;
313 }
314
315 void SetPaintListener( wxEvtHandler* aPaintListener )
316 {
317 m_paintListener = aPaintListener;
318 }
319
320 void EnableDepthTest( bool aEnabled = false ) override;
321
322 bool IsContextLocked() override
323 {
324 return m_isContextLocked;
325 }
326
327 void LockContext( int aClientCookie ) override;
328
329 void UnlockContext( int aClientCookie ) override;
330
332 void BeginDrawing() override;
333
335 void EndDrawing() override;
336
337 bool GetScreenshot( wxImage& aDstImage );
338
341 {
344
346 std::deque<std::shared_ptr<GLdouble>>& intersectPoints;
347 };
348
349private:
351 typedef GAL super;
352
353 static wxGLContext* m_glMainContext;
354 wxGLContext* m_glPrivContext;
356 static int m_instanceCounter;
357 wxEvtHandler* m_mouseListener;
358 wxEvtHandler* m_paintListener;
359
360 static GLuint g_fontTexture;
361
362 // Vertex buffer objects related fields
363 typedef std::unordered_map< unsigned int, std::shared_ptr<VERTEX_ITEM> > GROUPS_MAP;
364
366 unsigned int m_groupCounter;
372
375
376 // Framebuffer & compositing
378 unsigned int m_mainBuffer;
379 unsigned int m_overlayBuffer;
380 unsigned int m_tempBuffer;
382
383 // Shader
386
387 // Internal flags
403
406
407 std::unique_ptr<GL_BITMAP_CACHE> m_bitmapCache;
408
409 // Polygon tesselation
410 GLUtesselator* m_tesselator;
411 std::deque<std::shared_ptr<GLdouble>> m_tessIntersects;
412
414 void beginUpdate() override;
415
417 void endUpdate() override;
418
420 bool updatedGalDisplayOptions( const GAL_DISPLAY_OPTIONS& aOptions ) override;
421
430 void drawLineQuad( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint,
431 bool aReserve = true );
432
438 void reserveLineQuads( const int aLineCount );
439
451 void drawSemiCircle( const VECTOR2D& aCenterPoint, double aRadius, double aAngle );
452
461 void drawFilledSemiCircle( const VECTOR2D& aCenterPoint, double aRadius, double aAngle );
462
472 void drawStrokedSemiCircle( const VECTOR2D& aCenterPoint, double aRadius, double aAngle,
473 bool aReserve = true );
474
480 void drawCircle( const VECTOR2D& aCenterPoint, double aRadius, bool aReserve = true );
481
489 void drawPolyline( const std::function<VECTOR2D( int )>& aPointGetter, int aPointCount,
490 bool aReserve = true );
491
499 void drawSegmentChain( const std::function<VECTOR2D( int )>& aPointGetter, int aPointCount,
500 double aWidth, bool aReserve = true );
501
505 void drawSegment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint, double aWidth,
506 bool aReserve = true );
507
515 void drawPolygon( GLdouble* aPoints, int aPointCount );
516
523 void drawTriangulatedPolyset( const SHAPE_POLY_SET& aPoly, bool aStrokeTriangulation );
524
534 int drawBitmapChar( unsigned long aChar, bool aReserve = true );
535
547 void drawBitmapOverbar( double aLength, double aHeight, bool aReserve = true );
548
556 std::pair<VECTOR2D, float> computeBitmapTextSize( const UTF8& aText ) const;
557
558 // Event handling
564 void onPaint( wxPaintEvent& aEvent );
565
571 void skipMouseEvent( wxMouseEvent& aEvent );
572
578 void skipGestureEvent( wxGestureEvent& aEvent );
579
585 void onSetNativeCursor( wxSetCursorEvent& aEvent );
586
590 void blitCursor();
591
597 unsigned int getNewGroupNumber();
598
602 double calcAngleStep( double aRadius ) const
603 {
604 // Bigger arcs need smaller alpha increment to make them look smooth
605 return std::min( 1e6 / aRadius, 2.0 * M_PI / SEG_PER_CIRCLE_COUNT );
606 }
607
608 double getWorldPixelSize() const;
609
610 VECTOR2D getScreenPixelSize() const;
611
617 void setupShaderParameters();
618
624 void init();
625};
626} // namespace KIGFX
627
628#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:366
std::unordered_map< unsigned int, std::shared_ptr< VERTEX_ITEM > > GROUPS_MAP
Definition opengl_gal.h:363
VERTEX_MANAGER * m_overlayManager
Container for storing overlaid VERTEX_ITEMs.
Definition opengl_gal.h:371
bool m_isInitialized
Basic initialization flag, has to be done when the window is visible.
Definition opengl_gal.h:391
VERTEX_MANAGER * m_currentManager
Currently used VERTEX_MANAGER (for storing VERTEX_ITEMs).
Definition opengl_gal.h:367
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:411
void SetNegativeDrawMode(bool aSetting) override
Set negative draw mode in the renderer.
Definition opengl_gal.h:282
bool IsInitialized() const override
Return the initialization status for the canvas.
Definition opengl_gal.h:107
WX_CURSOR_TYPE m_currentwxCursor
wx cursor showing the current native cursor.
Definition opengl_gal.h:405
unsigned int m_mainBuffer
Main rendering target.
Definition opengl_gal.h:378
std::unique_ptr< GL_BITMAP_CACHE > m_bitmapCache
Definition opengl_gal.h:407
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:322
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:390
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:379
OPENGL_COMPOSITOR * m_compositor
Handles multiple rendering targets.
Definition opengl_gal.h:377
VERTEX_MANAGER * m_cachedManager
Container for storing cached VERTEX_ITEMs.
Definition opengl_gal.h:369
void SetPaintListener(wxEvtHandler *aPaintListener)
Definition opengl_gal.h:315
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:400
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:399
bool IsVisible() const override
Return true if the GAL canvas is visible on the screen.
Definition opengl_gal.h:114
wxEvtHandler * m_mouseListener
Definition opengl_gal.h:357
int m_swapInterval
Used to store swap interval information.
Definition opengl_gal.h:355
GROUPS_MAP m_groups
Stores information about VBO objects (groups)
Definition opengl_gal.h:365
void endUpdate() override
GLint ufm_fontTextureWidth
Definition opengl_gal.h:402
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:210
GAL super
Super class definition.
Definition opengl_gal.h:351
void SetMouseListener(wxEvtHandler *aMouseListener)
Definition opengl_gal.h:310
VERTEX_MANAGER * m_nonCachedManager
Container for storing non-cached VERTEX_ITEMs.
Definition opengl_gal.h:370
int drawBitmapChar(unsigned long aChar, bool aReserve=true)
Draw a single character using bitmap font.
GLUtesselator * m_tesselator
Definition opengl_gal.h:410
wxEvtHandler * m_paintListener
Definition opengl_gal.h:358
bool m_isContextLocked
Used for assertion checking.
Definition opengl_gal.h:394
bool m_isFramebufferInitialized
Are the framebuffers initialized?
Definition opengl_gal.h:388
static GLuint g_fontTexture
Bitmap font texture handle (shared)
Definition opengl_gal.h:360
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:602
bool m_isGrouping
Was a group started?
Definition opengl_gal.h:393
GLint ufm_screenPixelSize
Definition opengl_gal.h:397
GLint ufm_pixelSizeMultiplier
Definition opengl_gal.h:398
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:374
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:385
RENDER_TARGET m_currentTarget
Current rendering target.
Definition opengl_gal.h:381
wxGLContext * m_glPrivContext
Canvas-specific OpenGL context.
Definition opengl_gal.h:354
static bool m_isBitmapFontLoaded
Is the bitmap font texture loaded?
Definition opengl_gal.h:389
static wxGLContext * m_glMainContext
Parent OpenGL context.
Definition opengl_gal.h:353
unsigned int m_tempBuffer
Temporary rendering target (for diffing etc.)
Definition opengl_gal.h:380
static int m_instanceCounter
GL GAL instance counter.
Definition opengl_gal.h:356
bool IsOpenGlEngine() override
Return true if the GAL engine is a OpenGL based type.
Definition opengl_gal.h:104
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:343
std::deque< std::shared_ptr< GLdouble > > & intersectPoints
Intersect points, that have to be freed after tessellation.
Definition opengl_gal.h:346
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.