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) 2012 Kicad Developers, see change_log.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 
47 #ifndef CALLBACK
48 #define CALLBACK
49 #endif
50 
51 struct bitmap_glyph;
52 
53 namespace KIGFX
54 {
55 class SHADER;
56 class GL_BITMAP_CACHE;
57 
65 class OPENGL_GAL : public GAL, public HIDPI_GL_CANVAS
66 {
67 public:
83  OPENGL_GAL( GAL_DISPLAY_OPTIONS& aDisplayOptions, wxWindow* aParent,
84  wxEvtHandler* aMouseListener = nullptr, wxEvtHandler* aPaintListener = nullptr,
85  const wxString& aName = wxT( "GLCanvas" ) );
86 
87  virtual ~OPENGL_GAL();
88 
94  static wxString CheckFeatures( GAL_DISPLAY_OPTIONS& aOptions );
95 
96  virtual bool IsOpenGlEngine() override { return true; }
97 
99  virtual 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  virtual void DrawLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
117 
119  virtual void DrawSegment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint,
120  double aWidth ) override;
121 
123  virtual void DrawCircle( const VECTOR2D& aCenterPoint, double aRadius ) override;
124 
126  virtual void DrawArc( const VECTOR2D& aCenterPoint, double aRadius,
127  double aStartAngle, double aEndAngle ) override;
128 
130  virtual void DrawArcSegment( const VECTOR2D& aCenterPoint, double aRadius,
131  double aStartAngle, double aEndAngle, double aWidth ) override;
132 
134  virtual void DrawRectangle( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
135 
137  virtual void DrawPolyline( const std::deque<VECTOR2D>& aPointList ) override;
138  virtual void DrawPolyline( const VECTOR2D aPointList[], int aListSize ) override;
139  virtual void DrawPolyline( const SHAPE_LINE_CHAIN& aLineChain ) override;
140 
142  virtual void DrawPolygon( const std::deque<VECTOR2D>& aPointList ) override;
143  virtual void DrawPolygon( const VECTOR2D aPointList[], int aListSize ) override;
144  virtual void DrawPolygon( const SHAPE_POLY_SET& aPolySet ) override;
145  virtual void DrawPolygon( const SHAPE_LINE_CHAIN& aPolySet ) override;
146 
148  virtual void DrawCurve( const VECTOR2D& startPoint, const VECTOR2D& controlPointA,
149  const VECTOR2D& controlPointB, const VECTOR2D& endPoint,
150  double aFilterValue = 0.0 ) override;
151 
153  virtual void DrawBitmap( const BITMAP_BASE& aBitmap ) override;
154 
156  virtual void BitmapText( const wxString& aText, const VECTOR2D& aPosition,
157  double aRotationAngle ) override;
158 
160  virtual void DrawGrid() override;
161 
162  // --------------
163  // Screen methods
164  // --------------
165 
167  virtual void ResizeScreen( int aWidth, int aHeight ) override;
168 
170  virtual bool Show( bool aShow ) override;
171 
173  virtual void Flush() override;
174 
176  virtual void ClearScreen( ) override;
177 
178  // --------------
179  // Transformation
180  // --------------
181 
183  virtual void Transform( const MATRIX3x3D& aTransformation ) override;
184 
186  virtual void Rotate( double aAngle ) override;
187 
189  virtual void Translate( const VECTOR2D& aTranslation ) override;
190 
192  virtual void Scale( const VECTOR2D& aScale ) override;
193 
195  virtual void Save() override;
196 
198  virtual void Restore() override;
199 
200  // --------------------------------------------
201  // Group methods
202  // ---------------------------------------------
203 
205  virtual int BeginGroup() override;
206 
208  virtual void EndGroup() override;
209 
211  virtual void DrawGroup( int aGroupNumber ) override;
212 
214  virtual void ChangeGroupColor( int aGroupNumber, const COLOR4D& aNewColor ) override;
215 
217  virtual void ChangeGroupDepth( int aGroupNumber, int aDepth ) override;
218 
220  virtual void DeleteGroup( int aGroupNumber ) override;
221 
223  virtual void ClearCache() override;
224 
225  // --------------------------------------------------------
226  // Handling the world <-> screen transformation
227  // --------------------------------------------------------
228 
230  virtual void SetTarget( RENDER_TARGET aTarget ) override;
231 
233  virtual RENDER_TARGET GetTarget() const override;
234 
236  virtual void ClearTarget( RENDER_TARGET aTarget ) override;
237 
239  virtual void SetNegativeDrawMode( bool aSetting ) override {}
240 
241  virtual void ComputeWorldScreenMatrix() override;
242 
243  // -------
244  // Cursor
245  // -------
246 
248  virtual void DrawCursor( const VECTOR2D& aCursorPosition ) override;
249 
255  void PostPaint()
256  {
257  if( paintListener )
258  {
259  wxPaintEvent redrawEvent;
260  wxPostEvent( paintListener, redrawEvent );
261  }
262  }
263 
264  void SetMouseListener( wxEvtHandler* aMouseListener )
265  {
266  mouseListener = aMouseListener;
267  }
268 
269  void SetPaintListener( wxEvtHandler* aPaintListener )
270  {
271  paintListener = aPaintListener;
272  }
273 
274  virtual void EnableDepthTest( bool aEnabled = false ) override;
275 
277  typedef struct
278  {
281 
283  std::deque< boost::shared_array<GLdouble> >& intersectPoints;
284  } TessParams;
285 
286 private:
288  typedef GAL super;
289 
290  static const int CIRCLE_POINTS = 64;
291  static const int CURVE_POINTS = 32;
292 
293  static wxGLContext* glMainContext;
294  wxGLContext* glPrivContext;
295  static int instanceCounter;
296  wxEvtHandler* mouseListener;
297  wxEvtHandler* paintListener;
298 
299  static GLuint fontTexture;
300 
301  // Vertex buffer objects related fields
302  typedef std::unordered_map< unsigned int, std::shared_ptr<VERTEX_ITEM> > GROUPS_MAP;
304  unsigned int groupCounter;
309 
310  // Framebuffer & compositing
312  unsigned int mainBuffer;
313  unsigned int overlayBuffer;
315 
316  // Shader
318 
319  // Internal flags
321  static bool isBitmapFontLoaded;
324  bool isGrouping;
331 
332  std::unique_ptr<GL_BITMAP_CACHE> bitmapCache;
333 
334  void lockContext( int aClientCookie ) override;
335 
336  void unlockContext( int aClientCookie ) override;
337 
339  virtual void beginUpdate() override;
340 
342  virtual void endUpdate() override;
343 
345  virtual void beginDrawing() override;
346 
348  virtual void endDrawing() override;
349 
351  bool updatedGalDisplayOptions( const GAL_DISPLAY_OPTIONS& aOptions ) override;
352 
353  // Polygon tesselation
355  GLUtesselator* tesselator;
357  std::deque< boost::shared_array<GLdouble> > tessIntersects;
358 
365  void drawLineQuad( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint );
366 
376  void drawSemiCircle( const VECTOR2D& aCenterPoint, double aRadius, double aAngle );
377 
386  void drawFilledSemiCircle( const VECTOR2D& aCenterPoint, double aRadius, double aAngle );
387 
396  void drawStrokedSemiCircle( const VECTOR2D& aCenterPoint, double aRadius, double aAngle );
397 
403  void drawPolyline( const std::function<VECTOR2D (int)>& aPointGetter, int aPointCount );
404 
411  void drawPolygon( GLdouble* aPoints, int aPointCount );
412 
416  void drawTriangulatedPolyset( const SHAPE_POLY_SET& aPoly );
417 
418 
426  int drawBitmapChar( unsigned long aChar );
427 
437  void drawBitmapOverbar( double aLength, double aHeight );
438 
446  std::pair<VECTOR2D, float> computeBitmapTextSize( const UTF8& aText ) const;
447 
448  // Event handling
454  void onPaint( wxPaintEvent& aEvent );
455 
461  void skipMouseEvent( wxMouseEvent& aEvent );
462 
466  void blitCursor();
467 
473  unsigned int getNewGroupNumber();
474 
478  double calcAngleStep( double aRadius ) const
479  {
480  // Bigger arcs need smaller alpha increment to make them look smooth
481  return std::min( 1e6 / aRadius, 2.0 * M_PI / CIRCLE_POINTS );
482  }
483 
484  double getWorldPixelSize() const;
485 
487 
492  void init();
493 };
494 } // namespace KIGFX
495 
496 #endif // OPENGLGAL_H_
UTF8 is an 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to...
Definition: utf8.h:73
VERTEX_MANAGER * currentManager
Currently used VERTEX_MANAGER (for storing VERTEX_ITEMs)
Definition: opengl_gal.h:305
virtual void beginUpdate() override
Definition: opengl_gal.cpp:596
virtual void DrawBitmap(const BITMAP_BASE &aBitmap) override
Draw a bitmap image.
static wxGLContext * glMainContext
Parent OpenGL context.
Definition: opengl_gal.h:293
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:175
unsigned int overlayBuffer
Auxiliary rendering target (for menus etc.)
Definition: opengl_gal.h:313
std::deque< boost::shared_array< GLdouble > > & intersectPoints
Intersect points, that have to be freed after tessellation.
Definition: opengl_gal.h:283
virtual void Scale(const VECTOR2D &aScale) override
Scale the context.
virtual void ClearTarget(RENDER_TARGET aTarget) override
Clears the target for rendering.
virtual void DrawCurve(const VECTOR2D &startPoint, const VECTOR2D &controlPointA, const VECTOR2D &controlPointB, const VECTOR2D &endPoint, double aFilterValue=0.0) override
Draw a cubic bezier spline.
virtual void Translate(const VECTOR2D &aTranslation) override
Translate the context.
RENDER_TARGET currentTarget
Current rendering target.
Definition: opengl_gal.h:314
bool isBitmapFontInitialized
Is the shader set to use bitmap fonts?
Definition: opengl_gal.h:322
void drawPolygon(GLdouble *aPoints, int aPointCount)
Draws a filled polygon.
static GLuint fontTexture
Bitmap font texture handle (shared)
Definition: opengl_gal.h:299
void drawLineQuad(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a quad for the line.
VERTEX_MANAGER * overlayManager
Container for storing overlaid VERTEX_ITEMs.
Definition: opengl_gal.h:308
Class OpenGL_GAL is the OpenGL implementation of the Graphics Abstraction Layer.
Definition: opengl_gal.h:65
VECTOR2D getScreenPixelSize() const
Definition: opengl_gal.cpp:389
virtual void ComputeWorldScreenMatrix() override
Compute the world <-> screen transformation matrix.
virtual void Restore() override
Restore the context.
virtual void ChangeGroupDepth(int aGroupNumber, int aDepth) override
Changes the depth (Z-axis position) of the group.
std::unique_ptr< GL_BITMAP_CACHE > bitmapCache
Definition: opengl_gal.h:332
virtual void EnableDepthTest(bool aEnabled=false) override
Parameters passed to the GLU tesselator.
wxGLCanvas wrapper for HiDPI/Retina support.
virtual int BeginGroup() override
Begin a group.
virtual void DrawPolygon(const std::deque< VECTOR2D > &aPointList) override
Draw a polygon.
Definition: opengl_gal.cpp:954
Class to control vertex container and GPU with possibility of emulating old-style OpenGL 1....
bool isContextLocked
Used for assertion checking.
Definition: opengl_gal.h:326
void SetPaintListener(wxEvtHandler *aPaintListener)
Definition: opengl_gal.h:269
SHADER * shader
There is only one shader used for different objects.
Definition: opengl_gal.h:317
Class that handles multitarget rendering (ie.
bool isGrouping
Was a group started?
Definition: opengl_gal.h:325
std::unordered_map< unsigned int, std::shared_ptr< VERTEX_ITEM > > GROUPS_MAP
Definition: opengl_gal.h:302
OPENGL_GAL(GAL_DISPLAY_OPTIONS &aDisplayOptions, wxWindow *aParent, wxEvtHandler *aMouseListener=nullptr, wxEvtHandler *aPaintListener=nullptr, const wxString &aName=wxT("GLCanvas"))
Constructor OPENGL_GAL.
Definition: opengl_gal.cpp:193
virtual void ResizeScreen(int aWidth, int aHeight) override
Resizes the canvas.
void unlockContext(int aClientCookie) override
Definition: opengl_gal.cpp:582
virtual void Transform(const MATRIX3x3D &aTransformation) override
Transform the context.
VERTEX_MANAGER * nonCachedManager
Container for storing non-cached VERTEX_ITEMs.
Definition: opengl_gal.h:307
static int instanceCounter
GL GAL instance counter.
Definition: opengl_gal.h:295
VERTEX_MANAGER * vboManager
Manager used for storing new vertices.
Definition: opengl_gal.h:280
double getWorldPixelSize() const
Definition: opengl_gal.cpp:382
virtual void DrawArcSegment(const VECTOR2D &aCenterPoint, double aRadius, double aStartAngle, double aEndAngle, double aWidth) override
Draw an arc segment.
Definition: opengl_gal.cpp:797
virtual RENDER_TARGET GetTarget() const override
Gets the currently used target for rendering.
static const int CURVE_POINTS
The number of points for curve approximation.
Definition: opengl_gal.h:291
This class handle bitmap images in KiCad.
Definition: bitmap_base.h:51
virtual void endUpdate() override
Definition: opengl_gal.cpp:612
virtual void Save() override
Save the context.
void init()
Basic OpenGL initialization and feature checks.
OPENGL_COMPOSITOR * compositor
Handles multiple rendering targets.
Definition: opengl_gal.h:311
GROUPS_MAP groups
Stores informations about VBO objects (groups)
Definition: opengl_gal.h:303
unsigned int mainBuffer
Main rendering target.
Definition: opengl_gal.h:312
virtual bool Show(bool aShow) override
Shows/hides the GAL canvas.
void SetMouseListener(wxEvtHandler *aMouseListener)
Definition: opengl_gal.h:264
SHAPE_POLY_SET.
virtual void EndGroup() override
End the group.
virtual 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:478
wxEvtHandler * mouseListener
Definition: opengl_gal.h:296
bool updatedGalDisplayOptions(const GAL_DISPLAY_OPTIONS &aOptions) override
Function updatedGalDisplayOptions.
Definition: opengl_gal.cpp:355
virtual void DrawGrid() override
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint) override
Draw a line.
Definition: opengl_gal.cpp:621
void onPaint(wxPaintEvent &aEvent)
This is the OnPaint event handler.
GLint ufm_screenPixelSize
Definition: opengl_gal.h:329
virtual void endDrawing() override
Definition: opengl_gal.cpp:537
virtual void DrawGroup(int aGroupNumber) override
Draw the stored group.
bool isFramebufferInitialized
Are the framebuffers initialized?
Definition: opengl_gal.h:320
virtual bool IsInitialized() const override
Returns the initalization status for the canvas.
Definition: opengl_gal.h:99
virtual void ClearCache() override
Delete all data created during caching of graphic items.
void drawTriangulatedPolyset(const SHAPE_POLY_SET &aPoly)
Draws a set of polygons with a cached triangulation.
Definition: opengl_gal.cpp:988
Class SHADER provides the access to the OpenGL shaders.
Definition: shader.h:76
void lockContext(int aClientCookie) override
Private: use GAL_CONTEXT_LOCKER RAII object.
Definition: opengl_gal.cpp:572
GAL super
Super class definition.
Definition: opengl_gal.h:288
GLUtesselator * tesselator
The tessellator.
Definition: opengl_gal.h:355
virtual void Rotate(double aAngle) override
Rotate the context.
void drawStrokedSemiCircle(const VECTOR2D &aCenterPoint, double aRadius, double aAngle)
Draw a stroked semicircle.
void blitCursor()
Blits cursor into the current screen.
VERTEX_MANAGER * cachedManager
Container for storing cached VERTEX_ITEMs.
Definition: opengl_gal.h:306
virtual void DrawPolyline(const std::deque< VECTOR2D > &aPointList) override
Draw a polyline.
Definition: opengl_gal.cpp:931
virtual void DrawSegment(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint, double aWidth) override
Draw a rounded segment.
Definition: opengl_gal.cpp:629
GLint ufm_worldPixelSize
Definition: opengl_gal.h:328
wxEvtHandler * paintListener
Definition: opengl_gal.h:297
std::deque< boost::shared_array< GLdouble > > tessIntersects
Storage for intersecting points.
Definition: opengl_gal.h:357
virtual void DrawArc(const VECTOR2D &aCenterPoint, double aRadius, double aStartAngle, double aEndAngle) override
Draw an arc.
Definition: opengl_gal.cpp:731
virtual void SetTarget(RENDER_TARGET aTarget) override
Sets the target for rendering.
Class to store instances of VERTEX without caching.
bool isInitialized
Basic initialization flag, has to be done when the window is visible.
Definition: opengl_gal.h:323
void PostPaint()
Function PostPaint posts an event to m_paint_listener.
Definition: opengl_gal.h:255
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:325
SHAPE_LINE_CHAIN.
Class to handle an item held in a container.
unsigned int groupCounter
Counter used for generating keys for groups.
Definition: opengl_gal.h:304
int drawBitmapChar(unsigned long aChar)
Draws a single character using bitmap font.
CIRCLE_POINTS
virtual void ClearScreen() override
Clear the screen.
unsigned int getNewGroupNumber()
Returns a valid key that can be used as a new group number.
wxGLContext * glPrivContext
Canvas-specific OpenGL context.
Definition: opengl_gal.h:294
virtual void DrawRectangle(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint) override
Draw a rectangle.
Definition: opengl_gal.cpp:893
virtual void Flush() override
Force all remaining objects to be drawn.
virtual 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.
std::pair< VECTOR2D, float > computeBitmapTextSize(const UTF8 &aText) const
Computes a size of text drawn using bitmap font with current text setting applied.
RENDER_TARGET
RENDER_TARGET: Possible rendering targets.
Definition: definitions.h:46
virtual void BitmapText(const wxString &aText, const VECTOR2D &aPosition, double aRotationAngle) override
Draws a text using a bitmap font.
virtual void SetNegativeDrawMode(bool aSetting) override
Sets negative draw mode in the renderer.
Definition: opengl_gal.h:239
virtual ~OPENGL_GAL()
Definition: opengl_gal.cpp:280
bool IsVisible() const override
Definition: opengl_gal.h:106
virtual void beginDrawing() override
Definition: opengl_gal.cpp:396
GLint ufm_pixelSizeMultiplier
Definition: opengl_gal.h:330
virtual bool IsOpenGlEngine() override
Returns true if the GAL engine is a opengl based type.
Definition: opengl_gal.h:96
virtual void DrawCircle(const VECTOR2D &aCenterPoint, double aRadius) override
Draw a circle using world coordinates.
Definition: opengl_gal.cpp:675
static bool isBitmapFontLoaded
Is the bitmap font texture loaded?
Definition: opengl_gal.h:321
void skipMouseEvent(wxMouseEvent &aEvent)
Skip the mouse event to the parent.
void drawFilledSemiCircle(const VECTOR2D &aCenterPoint, double aRadius, double aAngle)
Draw a filled semicircle.
virtual void ChangeGroupColor(int aGroupNumber, const COLOR4D &aNewColor) override
Changes the color used to draw the group.
Class GAL is the abstract interface for drawing on a 2D-surface.
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:99
void drawBitmapOverbar(double aLength, double aHeight)
Draws an overbar over the currently drawn text.