KiCad PCB EDA Suite
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 (C) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors.
6  * Copyright (C) 2013-2017 CERN
7  * @author Maciej Suminski <maciej.suminski@cern.ch>
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
35 #include <gal/opengl/shader.h>
37 #include <gal/opengl/vertex_item.h>
41 #include <gal/hidpi_gl_canvas.h>
42 
43 #include <unordered_map>
44 #include <boost/smart_ptr/shared_array.hpp>
45 #include <memory>
46 #include <wx/event.h>
47 
48 #ifndef CALLBACK
49 #define CALLBACK
50 #endif
51 
52 struct bitmap_glyph;
53 
54 namespace KIGFX
55 {
56 class SHADER;
57 class GL_BITMAP_CACHE;
58 
66 class OPENGL_GAL : public GAL, public HIDPI_GL_CANVAS
67 {
68 public:
82  OPENGL_GAL( GAL_DISPLAY_OPTIONS& aDisplayOptions, wxWindow* aParent,
83  wxEvtHandler* aMouseListener = nullptr, wxEvtHandler* aPaintListener = nullptr,
84  const wxString& aName = wxT( "GLCanvas" ) );
85 
86  ~OPENGL_GAL();
87 
94  static wxString CheckFeatures( GAL_DISPLAY_OPTIONS& aOptions );
95 
96  bool IsOpenGlEngine() override { return true; }
97 
99  bool IsInitialized() const override
100  {
101  // is*Initialized flags, but it is enough for OpenGL to show up
102  return IsShownOnScreen() && !GetClientRect().IsEmpty();
103  }
104 
106  bool IsVisible() const override
107  {
108  return IsShownOnScreen() && !GetClientRect().IsEmpty();
109  }
110 
111  // ---------------
112  // Drawing methods
113  // ---------------
114 
116  void DrawLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
117 
119  void DrawSegment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint,
120  double aWidth ) override;
121 
123  void DrawCircle( const VECTOR2D& aCenterPoint, double aRadius ) override;
124 
126  void DrawArc( const VECTOR2D& aCenterPoint, double aRadius,
127  double aStartAngle, double aEndAngle ) override;
128 
130  void DrawArcSegment( const VECTOR2D& aCenterPoint, double aRadius,
131  double aStartAngle, double aEndAngle, double aWidth ) override;
132 
134  void DrawRectangle( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
135 
137  void DrawPolyline( const std::deque<VECTOR2D>& aPointList ) override;
138  void DrawPolyline( const VECTOR2D aPointList[], int aListSize ) override;
139  void DrawPolyline( const SHAPE_LINE_CHAIN& aLineChain ) override;
140 
142  void DrawPolygon( const std::deque<VECTOR2D>& aPointList ) override;
143  void DrawPolygon( const VECTOR2D aPointList[], int aListSize ) override;
144  void DrawPolygon( const SHAPE_POLY_SET& aPolySet ) override;
145  void DrawPolygon( const SHAPE_LINE_CHAIN& aPolySet ) override;
146 
148  void DrawCurve( const VECTOR2D& startPoint, const VECTOR2D& controlPointA,
149  const VECTOR2D& controlPointB, const VECTOR2D& endPoint,
150  double aFilterValue = 0.0 ) override;
151 
153  void DrawBitmap( const BITMAP_BASE& aBitmap ) override;
154 
156  void BitmapText( const wxString& aText, const VECTOR2D& aPosition,
157  double aRotationAngle ) override;
158 
160  void DrawGrid() override;
161 
162  // --------------
163  // Screen methods
164  // --------------
165 
167  void ResizeScreen( int aWidth, int aHeight ) override;
168 
170  bool Show( bool aShow ) override;
171 
173  void Flush() override;
174 
176  void ClearScreen( ) override;
177 
178  // --------------
179  // Transformation
180  // --------------
181 
183  void Transform( const MATRIX3x3D& aTransformation ) override;
184 
186  void Rotate( double aAngle ) override;
187 
189  void Translate( const VECTOR2D& aTranslation ) override;
190 
192  void Scale( const VECTOR2D& aScale ) override;
193 
195  void Save() override;
196 
198  void Restore() override;
199 
200  // --------------------------------------------
201  // Group methods
202  // ---------------------------------------------
203 
205  int BeginGroup() override;
206 
208  void EndGroup() override;
209 
211  void DrawGroup( int aGroupNumber ) override;
212 
214  void ChangeGroupColor( int aGroupNumber, const COLOR4D& aNewColor ) override;
215 
217  void ChangeGroupDepth( int aGroupNumber, int aDepth ) override;
218 
220  void DeleteGroup( int aGroupNumber ) override;
221 
223  void ClearCache() override;
224 
225  // --------------------------------------------------------
226  // Handling the world <-> screen transformation
227  // --------------------------------------------------------
228 
230  void SetTarget( RENDER_TARGET aTarget ) override;
231 
233  RENDER_TARGET GetTarget() const override;
234 
236  void ClearTarget( RENDER_TARGET aTarget ) override;
237 
239  virtual bool HasTarget( RENDER_TARGET aTarget ) override;
240 
242  void SetNegativeDrawMode( bool aSetting ) override {}
243 
244  void ComputeWorldScreenMatrix() override;
245 
246  // -------
247  // Cursor
248  // -------
249 
251  bool SetNativeCursorStyle( KICURSOR aCursor ) override;
252 
254  void DrawCursor( const VECTOR2D& aCursorPosition ) override;
255 
262  void PostPaint( wxPaintEvent& aEvent );
263 
264  void SetMouseListener( wxEvtHandler* aMouseListener )
265  {
266  m_mouseListener = aMouseListener;
267  }
268 
269  void SetPaintListener( wxEvtHandler* aPaintListener )
270  {
271  m_paintListener = aPaintListener;
272  }
273 
274  void EnableDepthTest( bool aEnabled = false ) override;
275 
276  bool IsContextLocked() override
277  {
278  return m_isContextLocked;
279  }
280 
282  typedef struct
283  {
286 
288  std::deque< boost::shared_array<GLdouble> >& intersectPoints;
289  } TessParams;
290 
291 private:
293  typedef GAL super;
294 
295  static const int CIRCLE_POINTS = 64;
296  static const int CURVE_POINTS = 32;
297 
298  static wxGLContext* m_glMainContext;
299  wxGLContext* m_glPrivContext;
300  static int m_instanceCounter;
301  wxEvtHandler* m_mouseListener;
302  wxEvtHandler* m_paintListener;
303 
304  static GLuint g_fontTexture;
305 
306  // Vertex buffer objects related fields
307  typedef std::unordered_map< unsigned int, std::shared_ptr<VERTEX_ITEM> > GROUPS_MAP;
308 
310  unsigned int m_groupCounter;
316 
317  // Framebuffer & compositing
319  unsigned int m_mainBuffer;
320  unsigned int m_overlayBuffer;
322 
323  // Shader
325 
327  // Internal flags
329  static bool m_isBitmapFontLoaded;
332  bool m_isGrouping;
339 
340  wxCursor m_currentwxCursor;
341 
342  std::unique_ptr<GL_BITMAP_CACHE> m_bitmapCache;
343 
344  // Polygon tesselation
345  GLUtesselator* m_tesselator;
346  std::deque< boost::shared_array<GLdouble> > m_tessIntersects;
347 
348  void lockContext( int aClientCookie ) override;
349 
350  void unlockContext( int aClientCookie ) override;
351 
353  void beginUpdate() override;
354 
356  void endUpdate() override;
357 
359  void beginDrawing() override;
360 
362  void endDrawing() override;
363 
365  bool updatedGalDisplayOptions( const GAL_DISPLAY_OPTIONS& aOptions ) override;
366 
373  void drawLineQuad( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint );
374 
386  void drawSemiCircle( const VECTOR2D& aCenterPoint, double aRadius, double aAngle );
387 
396  void drawFilledSemiCircle( const VECTOR2D& aCenterPoint, double aRadius, double aAngle );
397 
406  void drawStrokedSemiCircle( const VECTOR2D& aCenterPoint, double aRadius, double aAngle );
407 
414  void drawPolyline( const std::function<VECTOR2D (int)>& aPointGetter, int aPointCount );
415 
423  void drawPolygon( GLdouble* aPoints, int aPointCount );
424 
428  void drawTriangulatedPolyset( const SHAPE_POLY_SET& aPoly );
429 
430 
439  int drawBitmapChar( unsigned long aChar );
440 
451  void drawBitmapOverbar( double aLength, double aHeight );
452 
460  std::pair<VECTOR2D, float> computeBitmapTextSize( const UTF8& aText ) const;
461 
462  // Event handling
468  void onPaint( wxPaintEvent& aEvent );
469 
475  void skipMouseEvent( wxMouseEvent& aEvent );
476 
482  void onSetNativeCursor( wxSetCursorEvent& aEvent );
483 
487  void blitCursor();
488 
494  unsigned int getNewGroupNumber();
495 
499  double calcAngleStep( double aRadius ) const
500  {
501  // Bigger arcs need smaller alpha increment to make them look smooth
502  return std::min( 1e6 / aRadius, 2.0 * M_PI / CIRCLE_POINTS );
503  }
504 
505  double getWorldPixelSize() const;
506 
508 
514  void init();
515 };
516 } // namespace KIGFX
517 
518 #endif // OPENGLGAL_H_
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition: utf8.h:70
void onSetNativeCursor(wxSetCursorEvent &aEvent)
Give the correct cursor image when the native widget asks for it.
void beginUpdate() override
Definition: opengl_gal.cpp:629
unsigned int m_overlayBuffer
Auxiliary rendering target (for menus etc.)
Definition: opengl_gal.h:320
GROUPS_MAP m_groups
Stores information about VBO objects (groups)
Definition: opengl_gal.h:309
void DrawBitmap(const BITMAP_BASE &aBitmap) override
Draw a bitmap image.
wxEvtHandler * m_paintListener
Definition: opengl_gal.h:302
static bool m_isBitmapFontLoaded
Is the bitmap font texture loaded?
Definition: opengl_gal.h:329
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:175
std::deque< boost::shared_array< GLdouble > > & intersectPoints
Intersect points, that have to be freed after tessellation.
Definition: opengl_gal.h:288
void Scale(const VECTOR2D &aScale) override
Scale the context.
void ClearTarget(RENDER_TARGET aTarget) override
Clear the target for rendering.
static int m_instanceCounter
GL GAL instance counter.
Definition: opengl_gal.h:300
void DrawCurve(const VECTOR2D &startPoint, const VECTOR2D &controlPointA, const VECTOR2D &controlPointB, const VECTOR2D &endPoint, double aFilterValue=0.0) override
Draw a cubic bezier spline.
void Translate(const VECTOR2D &aTranslation) override
Translate the context.
void drawPolygon(GLdouble *aPoints, int aPointCount)
Draw a filled polygon.
VERTEX_MANAGER * m_overlayManager
Container for storing overlaid VERTEX_ITEMs.
Definition: opengl_gal.h:315
void drawLineQuad(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a quad for the line.
std::deque< boost::shared_array< GLdouble > > m_tessIntersects
Definition: opengl_gal.h:346
OpenGL implementation of the Graphics Abstraction Layer.
Definition: opengl_gal.h:66
VECTOR2D getScreenPixelSize() const
Definition: opengl_gal.cpp:406
void ComputeWorldScreenMatrix() override
Compute the world <-> screen transformation matrix.
void Restore() override
Restore the context.
void ChangeGroupDepth(int aGroupNumber, int aDepth) override
Change the depth (Z-axis position) of the group.
void EnableDepthTest(bool aEnabled=false) override
wxGLCanvas wrapper for HiDPI/Retina support.
int BeginGroup() override
Begin a group.
void DrawPolygon(const std::deque< VECTOR2D > &aPointList) override
Draw a polygon.
void SetPaintListener(wxEvtHandler *aPaintListener)
Definition: opengl_gal.h:269
KICURSOR
Definition: cursors.h:33
unsigned int m_mainBuffer
Main rendering target.
Definition: opengl_gal.h:319
Handle multitarget rendering (ie.
VERTEX_MANAGER * m_cachedManager
Container for storing cached VERTEX_ITEMs.
Definition: opengl_gal.h:313
std::unordered_map< unsigned int, std::shared_ptr< VERTEX_ITEM > > GROUPS_MAP
Definition: opengl_gal.h:307
bool IsInitialized() const override
Return the initialization status for the canvas.
Definition: opengl_gal.h:99
OPENGL_GAL(GAL_DISPLAY_OPTIONS &aDisplayOptions, wxWindow *aParent, wxEvtHandler *aMouseListener=nullptr, wxEvtHandler *aPaintListener=nullptr, const wxString &aName=wxT("GLCanvas"))
Definition: opengl_gal.cpp:196
void ResizeScreen(int aWidth, int aHeight) override
Resizes the canvas.
void unlockContext(int aClientCookie) override
Definition: opengl_gal.cpp:615
void Transform(const MATRIX3x3D &aTransformation) override
Transform the context.
VERTEX_MANAGER * vboManager
Manager used for storing new vertices.
Definition: opengl_gal.h:285
double getWorldPixelSize() const
Definition: opengl_gal.cpp:399
RENDER_TARGET m_currentTarget
Current rendering target.
Definition: opengl_gal.h:321
void DrawArcSegment(const VECTOR2D &aCenterPoint, double aRadius, double aStartAngle, double aEndAngle, double aWidth) override
Draw an arc segment.
Definition: opengl_gal.cpp:842
RENDER_TARGET GetTarget() const override
Get the currently used target for rendering.
static const int CURVE_POINTS
The number of points for curve approximation.
Definition: opengl_gal.h:296
This class handle bitmap images in KiCad.
Definition: bitmap_base.h:51
wxEvtHandler * m_mouseListener
Definition: opengl_gal.h:301
void endUpdate() override
Definition: opengl_gal.cpp:645
bool SetNativeCursorStyle(KICURSOR aCursor) override
Set the cursor in the native panel.
void Save() override
Save the context.
void init()
Basic OpenGL initialization and feature checks.
VERTEX_MANAGER * m_currentManager
Currently used VERTEX_MANAGER (for storing VERTEX_ITEMs).
Definition: opengl_gal.h:311
static wxGLContext * m_glMainContext
Parent OpenGL context.
Definition: opengl_gal.h:298
bool Show(bool aShow) override
Shows/hides the GAL canvas.
void SetMouseListener(wxEvtHandler *aMouseListener)
Definition: opengl_gal.h:264
Represent a set of closed polygons.
void SetNegativeDrawMode(bool aSetting) override
Set negative draw mode in the renderer.
Definition: opengl_gal.h:242
void EndGroup() override
End the group.
void DeleteGroup(int aGroupNumber) override
Delete the group from the memory.
double calcAngleStep(double aRadius) const
Compute the angle step when drawing arcs/circles approximated with lines.
Definition: opengl_gal.h:499
SHADER * m_shader
There is only one shader used for different objects.
Definition: opengl_gal.h:324
bool m_isInitialized
Basic initialization flag, has to be done when the window is visible.
Definition: opengl_gal.h:331
bool updatedGalDisplayOptions(const GAL_DISPLAY_OPTIONS &aOptions) override
Handle updating display options.
Definition: opengl_gal.cpp:372
OPENGL_COMPOSITOR * m_compositor
Handles multiple rendering targets.
Definition: opengl_gal.h:318
void DrawGrid() override
void PostPaint(wxPaintEvent &aEvent)
Function PostPaint posts an event to m_paint_listener.
Definition: opengl_gal.cpp:364
void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint) override
Draw a line.
Definition: opengl_gal.cpp:654
GLUtesselator * m_tesselator
Definition: opengl_gal.h:345
void onPaint(wxPaintEvent &aEvent)
This is the OnPaint event handler.
GLint ufm_screenPixelSize
Definition: opengl_gal.h:337
void endDrawing() override
Definition: opengl_gal.cpp:564
void DrawGroup(int aGroupNumber) override
Draw the stored group.
void ClearCache() override
Delete all data created during caching of graphic items.
bool IsOpenGlEngine() override
Return true if the GAL engine is a OpenGL based type.
Definition: opengl_gal.h:96
void drawTriangulatedPolyset(const SHAPE_POLY_SET &aPoly)
Draw a set of polygons with a cached triangulation.
bool IsContextLocked() override
Parameters passed to the GLU tesselator.
Definition: opengl_gal.h:276
Provide the access to the OpenGL shaders.
Definition: shader.h:76
void lockContext(int aClientCookie) override
Use GAL_CONTEXT_LOCKER RAII object.
Definition: opengl_gal.cpp:605
GAL super
Super class definition.
Definition: opengl_gal.h:293
void Rotate(double aAngle) override
Rotate the context.
void drawStrokedSemiCircle(const VECTOR2D &aCenterPoint, double aRadius, double aAngle)
Draw a stroked semicircle.
bool m_isContextLocked
Used for assertion checking.
Definition: opengl_gal.h:334
void blitCursor()
Blit cursor into the current screen.
static GLuint g_fontTexture
Bitmap font texture handle (shared)
Definition: opengl_gal.h:304
void DrawPolyline(const std::deque< VECTOR2D > &aPointList) override
Draw a polyline.
Definition: opengl_gal.cpp:988
void DrawSegment(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint, double aWidth) override
Draw a rounded segment.
Definition: opengl_gal.cpp:662
GLint ufm_worldPixelSize
Definition: opengl_gal.h:336
void DrawArc(const VECTOR2D &aCenterPoint, double aRadius, double aStartAngle, double aEndAngle) override
Draw an arc.
Definition: opengl_gal.cpp:773
void SetTarget(RENDER_TARGET aTarget) override
Set the target for rendering.
wxGLContext * m_glPrivContext
Canvas-specific OpenGL context.
Definition: opengl_gal.h:299
Class to store instances of VERTEX without caching.
bool m_isFramebufferInitialized
Are the framebuffers initialized?
Definition: opengl_gal.h:328
wxCursor m_currentwxCursor
wxCursor showing the current native cursor
Definition: opengl_gal.h:340
void drawSemiCircle(const VECTOR2D &aCenterPoint, double aRadius, double aAngle)
Draw a semicircle.
static wxString CheckFeatures(GAL_DISPLAY_OPTIONS &aOptions)
Checks OpenGL features.
Definition: opengl_gal.cpp:332
SHAPE_LINE_CHAIN.
Class to handle an item held in a container.
virtual bool HasTarget(RENDER_TARGET aTarget) override
Return true if the target exists.
int drawBitmapChar(unsigned long aChar)
Draw a single character using bitmap font.
CIRCLE_POINTS
void ClearScreen() override
Clear the screen.
std::unique_ptr< GL_BITMAP_CACHE > m_bitmapCache
Definition: opengl_gal.h:342
unsigned int getNewGroupNumber()
Return a valid key that can be used as a new group number.
void DrawRectangle(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint) override
Draw a rectangle.
Definition: opengl_gal.cpp:949
void Flush() override
Force all remaining objects to be drawn.
void DrawCursor(const VECTOR2D &aCursorPosition) override
Draw the cursor.
void drawPolyline(const std::function< VECTOR2D(int)> &aPointGetter, int aPointCount)
Generic way of drawing a polyline stored in different containers.
Class to control vertex container and GPU with possibility of emulating old-style OpenGL 1....
unsigned int m_groupCounter
Counter used for generating keys for groups.
Definition: opengl_gal.h:310
std::pair< VECTOR2D, float > computeBitmapTextSize(const UTF8 &aText) const
Compute a size of text drawn using bitmap font with current text setting applied.
RENDER_TARGET
RENDER_TARGET: Possible rendering targets.
Definition: definitions.h:46
void BitmapText(const wxString &aText, const VECTOR2D &aPosition, double aRotationAngle) override
Draw a text using a bitmap font.
bool m_isBitmapFontInitialized
Is the shader set to use bitmap fonts?
Definition: opengl_gal.h:330
VERTEX_MANAGER * m_nonCachedManager
Container for storing non-cached VERTEX_ITEMs.
Definition: opengl_gal.h:314
bool IsVisible() const override
Return true if the GAL canvas is visible on the screen.
Definition: opengl_gal.h:106
void beginDrawing() override
Definition: opengl_gal.cpp:413
GLint ufm_pixelSizeMultiplier
Definition: opengl_gal.h:338
void DrawCircle(const VECTOR2D &aCenterPoint, double aRadius) override
Draw a circle using world coordinates.
Definition: opengl_gal.cpp:716
void skipMouseEvent(wxMouseEvent &aEvent)
Skip the mouse event to the parent.
bool m_isGrouping
Was a group started?
Definition: opengl_gal.h:333
void drawFilledSemiCircle(const VECTOR2D &aCenterPoint, double aRadius, double aAngle)
Draw a filled semicircle.
void ChangeGroupColor(int aGroupNumber, const COLOR4D &aNewColor) override
Change the color used to draw the group.
Abstract interface for drawing on a 2D-surface.
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:98
void drawBitmapOverbar(double aLength, double aHeight)
Draw an overbar over the currently drawn text.