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 DrawRectangle( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
152
154 void DrawPolyline( const std::deque<VECTOR2D>& aPointList ) override;
155 void DrawPolyline( const std::vector<VECTOR2D>& aPointList ) override;
156 void DrawPolyline( const VECTOR2D aPointList[], int aListSize ) override;
157 void DrawPolyline( const SHAPE_LINE_CHAIN& aLineChain ) override;
158
160 void DrawPolylines( const std::vector<std::vector<VECTOR2D>>& aPointLists ) override;
161
163 void DrawPolygon( const std::deque<VECTOR2D>& aPointList ) override;
164 void DrawPolygon( const VECTOR2D aPointList[], int aListSize ) override;
165 void DrawPolygon( const SHAPE_POLY_SET& aPolySet, bool aStrokeTriangulation = false ) override;
166 void DrawPolygon( const SHAPE_LINE_CHAIN& aPolySet ) override;
167
169 virtual void DrawGlyph( const KIFONT::GLYPH& aGlyph, int aNth, int aTotal ) override;
170
172 virtual void DrawGlyphs( const std::vector<std::unique_ptr<KIFONT::GLYPH>>& aGlyphs ) override;
173
175 void DrawCurve( const VECTOR2D& startPoint, const VECTOR2D& controlPointA,
176 const VECTOR2D& controlPointB, const VECTOR2D& endPoint,
177 double aFilterValue = 0.0 ) override;
178
180 void DrawBitmap( const BITMAP_BASE& aBitmap, double alphaBlend = 1.0 ) override;
181
183 void BitmapText( const wxString& aText, const VECTOR2I& aPosition,
184 const EDA_ANGLE& aAngle ) override;
185
187 void DrawGrid() override;
188
189 // --------------
190 // Screen methods
191 // --------------
192
194 void ResizeScreen( int aWidth, int aHeight ) override;
195
197 bool Show( bool aShow ) override;
198
200 int GetSwapInterval() const override { return m_swapInterval; };
201
203 void Flush() override;
204
206 void ClearScreen( ) override;
207
208 // --------------
209 // Transformation
210 // --------------
211
213 void Transform( const MATRIX3x3D& aTransformation ) override;
214
216 void Rotate( double aAngle ) override;
217
219 void Translate( const VECTOR2D& aTranslation ) override;
220
222 void Scale( const VECTOR2D& aScale ) override;
223
225 void Save() override;
226
228 void Restore() override;
229
230 // --------------------------------------------
231 // Group methods
232 // ---------------------------------------------
233
235 int BeginGroup() override;
236
238 void EndGroup() override;
239
241 void DrawGroup( int aGroupNumber ) override;
242
244 void ChangeGroupColor( int aGroupNumber, const COLOR4D& aNewColor ) override;
245
247 void ChangeGroupDepth( int aGroupNumber, int aDepth ) override;
248
250 void DeleteGroup( int aGroupNumber ) override;
251
253 void ClearCache() override;
254
255 // --------------------------------------------------------
256 // Handling the world <-> screen transformation
257 // --------------------------------------------------------
258
260 void SetTarget( RENDER_TARGET aTarget ) override;
261
263 RENDER_TARGET GetTarget() const override;
264
266 void ClearTarget( RENDER_TARGET aTarget ) override;
267
269 virtual bool HasTarget( RENDER_TARGET aTarget ) override;
270
272 void SetNegativeDrawMode( bool aSetting ) override {}
273
275 void StartDiffLayer() override;
276 //
278 void EndDiffLayer() override;
279
280 void ComputeWorldScreenMatrix() override;
281
282 // -------
283 // Cursor
284 // -------
285
287 bool SetNativeCursorStyle( KICURSOR aCursor, bool aHiDPI ) override;
288
290 void DrawCursor( const VECTOR2D& aCursorPosition ) override;
291
298 void PostPaint( wxPaintEvent& aEvent );
299
300 void SetMouseListener( wxEvtHandler* aMouseListener )
301 {
302 m_mouseListener = aMouseListener;
303 }
304
305 void SetPaintListener( wxEvtHandler* aPaintListener )
306 {
307 m_paintListener = aPaintListener;
308 }
309
310 void EnableDepthTest( bool aEnabled = false ) override;
311
312 bool IsContextLocked() override
313 {
314 return m_isContextLocked;
315 }
316
317 void LockContext( int aClientCookie ) override;
318
319 void UnlockContext( int aClientCookie ) override;
320
322 void BeginDrawing() override;
323
325 void EndDrawing() override;
326
329 {
332
334 std::deque<std::shared_ptr<GLdouble>>& intersectPoints;
335 };
336
337private:
339 typedef GAL super;
340
341 static wxGLContext* m_glMainContext;
342 wxGLContext* m_glPrivContext;
344 static int m_instanceCounter;
345 wxEvtHandler* m_mouseListener;
346 wxEvtHandler* m_paintListener;
347
348 static GLuint g_fontTexture;
349
350 // Vertex buffer objects related fields
351 typedef std::unordered_map< unsigned int, std::shared_ptr<VERTEX_ITEM> > GROUPS_MAP;
352
354 unsigned int m_groupCounter;
360
363
364 // Framebuffer & compositing
366 unsigned int m_mainBuffer;
367 unsigned int m_overlayBuffer;
368 unsigned int m_tempBuffer;
370
371 // Shader
374
375 // Internal flags
391
394
395 std::unique_ptr<GL_BITMAP_CACHE> m_bitmapCache;
396
397 // Polygon tesselation
398 GLUtesselator* m_tesselator;
399 std::deque<std::shared_ptr<GLdouble>> m_tessIntersects;
400
402 void beginUpdate() override;
403
405 void endUpdate() override;
406
408 bool updatedGalDisplayOptions( const GAL_DISPLAY_OPTIONS& aOptions ) override;
409
418 void drawLineQuad( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint,
419 bool aReserve = true );
420
426 void reserveLineQuads( const int aLineCount );
427
439 void drawSemiCircle( const VECTOR2D& aCenterPoint, double aRadius, double aAngle );
440
449 void drawFilledSemiCircle( const VECTOR2D& aCenterPoint, double aRadius, double aAngle );
450
460 void drawStrokedSemiCircle( const VECTOR2D& aCenterPoint, double aRadius, double aAngle,
461 bool aReserve = true );
462
468 void drawCircle( const VECTOR2D& aCenterPoint, double aRadius, bool aReserve = true );
469
477 void drawPolyline( const std::function<VECTOR2D( int )>& aPointGetter, int aPointCount,
478 bool aReserve = true );
479
487 void drawSegmentChain( const std::function<VECTOR2D( int )>& aPointGetter, int aPointCount,
488 double aWidth, bool aReserve = true );
489
493 void drawSegment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint, double aWidth,
494 bool aReserve = true );
495
503 void drawPolygon( GLdouble* aPoints, int aPointCount );
504
511 void drawTriangulatedPolyset( const SHAPE_POLY_SET& aPoly, bool aStrokeTriangulation );
512
522 int drawBitmapChar( unsigned long aChar, bool aReserve = true );
523
535 void drawBitmapOverbar( double aLength, double aHeight, bool aReserve = true );
536
544 std::pair<VECTOR2D, float> computeBitmapTextSize( const UTF8& aText ) const;
545
546 // Event handling
552 void onPaint( wxPaintEvent& aEvent );
553
559 void skipMouseEvent( wxMouseEvent& aEvent );
560
566 void skipGestureEvent( wxGestureEvent& aEvent );
567
573 void onSetNativeCursor( wxSetCursorEvent& aEvent );
574
578 void blitCursor();
579
585 unsigned int getNewGroupNumber();
586
590 double calcAngleStep( double aRadius ) const
591 {
592 // Bigger arcs need smaller alpha increment to make them look smooth
593 return std::min( 1e6 / aRadius, 2.0 * M_PI / SEG_PER_CIRCLE_COUNT );
594 }
595
596 double getWorldPixelSize() const;
597
598 VECTOR2D getScreenPixelSize() const;
599
605 void setupShaderParameters();
606
612 void init();
613};
614} // namespace KIGFX
615
616#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:354
std::unordered_map< unsigned int, std::shared_ptr< VERTEX_ITEM > > GROUPS_MAP
Definition opengl_gal.h:351
VERTEX_MANAGER * m_overlayManager
Container for storing overlaid VERTEX_ITEMs.
Definition opengl_gal.h:359
bool m_isInitialized
Basic initialization flag, has to be done when the window is visible.
Definition opengl_gal.h:379
VERTEX_MANAGER * m_currentManager
Currently used VERTEX_MANAGER (for storing VERTEX_ITEMs).
Definition opengl_gal.h:355
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:399
void SetNegativeDrawMode(bool aSetting) override
Set negative draw mode in the renderer.
Definition opengl_gal.h:272
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:393
unsigned int m_mainBuffer
Main rendering target.
Definition opengl_gal.h:366
std::unique_ptr< GL_BITMAP_CACHE > m_bitmapCache
Definition opengl_gal.h:395
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:312
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:378
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:367
OPENGL_COMPOSITOR * m_compositor
Handles multiple rendering targets.
Definition opengl_gal.h:365
VERTEX_MANAGER * m_cachedManager
Container for storing cached VERTEX_ITEMs.
Definition opengl_gal.h:357
void SetPaintListener(wxEvtHandler *aPaintListener)
Definition opengl_gal.h:305
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:388
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:387
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:345
int m_swapInterval
Used to store swap interval information.
Definition opengl_gal.h:343
GROUPS_MAP m_groups
Stores information about VBO objects (groups)
Definition opengl_gal.h:353
void endUpdate() override
GLint ufm_fontTextureWidth
Definition opengl_gal.h:390
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:200
GAL super
Super class definition.
Definition opengl_gal.h:339
void SetMouseListener(wxEvtHandler *aMouseListener)
Definition opengl_gal.h:300
VERTEX_MANAGER * m_nonCachedManager
Container for storing non-cached VERTEX_ITEMs.
Definition opengl_gal.h:358
int drawBitmapChar(unsigned long aChar, bool aReserve=true)
Draw a single character using bitmap font.
GLUtesselator * m_tesselator
Definition opengl_gal.h:398
wxEvtHandler * m_paintListener
Definition opengl_gal.h:346
bool m_isContextLocked
Used for assertion checking.
Definition opengl_gal.h:382
bool m_isFramebufferInitialized
Are the framebuffers initialized?
Definition opengl_gal.h:376
static GLuint g_fontTexture
Bitmap font texture handle (shared)
Definition opengl_gal.h:348
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:590
bool m_isGrouping
Was a group started?
Definition opengl_gal.h:381
GLint ufm_screenPixelSize
Definition opengl_gal.h:385
GLint ufm_pixelSizeMultiplier
Definition opengl_gal.h:386
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:362
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:373
RENDER_TARGET m_currentTarget
Current rendering target.
Definition opengl_gal.h:369
wxGLContext * m_glPrivContext
Canvas-specific OpenGL context.
Definition opengl_gal.h:342
static bool m_isBitmapFontLoaded
Is the bitmap font texture loaded?
Definition opengl_gal.h:377
static wxGLContext * m_glMainContext
Parent OpenGL context.
Definition opengl_gal.h:341
unsigned int m_tempBuffer
Temporary rendering target (for diffing etc.)
Definition opengl_gal.h:368
static int m_instanceCounter
GL GAL instance counter.
Definition opengl_gal.h:344
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: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:331
std::deque< std::shared_ptr< GLdouble > > & intersectPoints
Intersect points, that have to be freed after tessellation.
Definition opengl_gal.h:334
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.