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 
42 #include <wx/glcanvas.h>
43 
44 #include <map>
45 #include <boost/smart_ptr/shared_array.hpp>
46 #include <memory>
47 
48 #ifndef CALLBACK
49 #define CALLBACK
50 #endif
51 
52 struct bitmap_glyph;
53 
54 namespace KIGFX
55 {
56 class SHADER;
57 
65 class OPENGL_GAL : public GAL, public wxGLCanvas
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 
90  virtual bool IsInitialized() const override
91  {
92  // is*Initialized flags, but it is enough for OpenGL to show up
93  return IsShownOnScreen();
94  }
95 
97  bool IsVisible() const override
98  {
99  return IsShownOnScreen();
100  }
101 
102  // ---------------
103  // Drawing methods
104  // ---------------
105 
107  virtual void BeginDrawing() override;
108 
110  virtual void EndDrawing() override;
111 
113  virtual void BeginUpdate() override;
114 
116  virtual void EndUpdate() override;
117 
119  virtual void DrawLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
120 
122  virtual void DrawSegment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint,
123  double aWidth ) override;
124 
126  virtual void DrawCircle( const VECTOR2D& aCenterPoint, double aRadius ) override;
127 
129  virtual void DrawArc( const VECTOR2D& aCenterPoint, double aRadius,
130  double aStartAngle, double aEndAngle ) override;
131 
133  virtual void DrawArcSegment( const VECTOR2D& aCenterPoint, double aRadius,
134  double aStartAngle, double aEndAngle, double aWidth ) override;
135 
137  virtual void DrawRectangle( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
138 
140  virtual void DrawPolyline( const std::deque<VECTOR2D>& aPointList ) override;
141  virtual void DrawPolyline( const VECTOR2D aPointList[], int aListSize ) override;
142  virtual void DrawPolyline( const SHAPE_LINE_CHAIN& aLineChain ) override;
143 
145  virtual void DrawPolygon( const std::deque<VECTOR2D>& aPointList ) override;
146  virtual void DrawPolygon( const VECTOR2D aPointList[], int aListSize ) override;
147  virtual void DrawPolygon( const SHAPE_POLY_SET& aPolySet ) override;
148 
150  virtual void DrawCurve( const VECTOR2D& startPoint, const VECTOR2D& controlPointA,
151  const VECTOR2D& controlPointB, const VECTOR2D& endPoint ) override;
152 
154  virtual void BitmapText( const wxString& aText, const VECTOR2D& aPosition,
155  double aRotationAngle ) override;
156 
158  virtual void DrawGrid() override;
159 
160  // --------------
161  // Screen methods
162  // --------------
163 
165  virtual void ResizeScreen( int aWidth, int aHeight ) override;
166 
168  virtual bool Show( bool aShow ) override;
169 
171  virtual void Flush() override;
172 
174  virtual void ClearScreen( const COLOR4D& aColor ) override;
175 
176  // --------------
177  // Transformation
178  // --------------
179 
181  virtual void Transform( const MATRIX3x3D& aTransformation ) override;
182 
184  virtual void Rotate( double aAngle ) override;
185 
187  virtual void Translate( const VECTOR2D& aTranslation ) override;
188 
190  virtual void Scale( const VECTOR2D& aScale ) override;
191 
193  virtual void Save() override;
194 
196  virtual void Restore() override;
197 
198  // --------------------------------------------
199  // Group methods
200  // ---------------------------------------------
201 
203  virtual int BeginGroup() override;
204 
206  virtual void EndGroup() override;
207 
209  virtual void DrawGroup( int aGroupNumber ) override;
210 
212  virtual void ChangeGroupColor( int aGroupNumber, const COLOR4D& aNewColor ) override;
213 
215  virtual void ChangeGroupDepth( int aGroupNumber, int aDepth ) override;
216 
218  virtual void DeleteGroup( int aGroupNumber ) override;
219 
221  virtual void ClearCache() override;
222 
223  // --------------------------------------------------------
224  // Handling the world <-> screen transformation
225  // --------------------------------------------------------
226 
228  virtual void SaveScreen() override;
229 
231  virtual void RestoreScreen() override;
232 
234  virtual void SetTarget( RENDER_TARGET aTarget ) override;
235 
237  virtual RENDER_TARGET GetTarget() const override;
238 
240  virtual void ClearTarget( RENDER_TARGET aTarget ) override;
241 
242  // -------
243  // Cursor
244  // -------
245 
247  virtual void DrawCursor( const VECTOR2D& aCursorPosition ) override;
248 
254  void PostPaint()
255  {
256  if( paintListener )
257  {
258  wxPaintEvent redrawEvent;
259  wxPostEvent( paintListener, redrawEvent );
260  }
261  }
262 
263  void SetMouseListener( wxEvtHandler* aMouseListener )
264  {
265  mouseListener = aMouseListener;
266  }
267 
268  void SetPaintListener( wxEvtHandler* aPaintListener )
269  {
270  paintListener = aPaintListener;
271  }
272 
274  typedef struct
275  {
278 
280  std::deque< boost::shared_array<GLdouble> >& intersectPoints;
281  } TessParams;
282 
283 private:
285  typedef GAL super;
286 
287  static const int CIRCLE_POINTS = 64;
288  static const int CURVE_POINTS = 32;
289 
290  static wxGLContext* glMainContext;
291  wxGLContext* glPrivContext;
292  static int instanceCounter;
293  wxEvtHandler* mouseListener;
294  wxEvtHandler* paintListener;
295 
296  static GLuint fontTexture;
297 
298  // Vertex buffer objects related fields
299  typedef std::map< unsigned int, std::shared_ptr<VERTEX_ITEM> > GROUPS_MAP;
300  GROUPS_MAP groups;
301  unsigned int groupCounter;
306 
307  // Framebuffer & compositing
309  unsigned int mainBuffer;
310  unsigned int overlayBuffer;
312 
313  // Shader
314  static SHADER* shader;
315 
316  // Internal flags
318  static bool isBitmapFontLoaded;
321  bool isGrouping;
323 
325  bool updatedGalDisplayOptions( const GAL_DISPLAY_OPTIONS& aOptions ) override;
326 
327  // Polygon tesselation
329  GLUtesselator* tesselator;
331  std::deque< boost::shared_array<GLdouble> > tessIntersects;
332 
339  void drawLineQuad( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint );
340 
350  void drawSemiCircle( const VECTOR2D& aCenterPoint, double aRadius, double aAngle );
351 
360  void drawFilledSemiCircle( const VECTOR2D& aCenterPoint, double aRadius, double aAngle );
361 
370  void drawStrokedSemiCircle( const VECTOR2D& aCenterPoint, double aRadius, double aAngle );
371 
377  void drawPolyline( std::function<VECTOR2D (int)> aPointGetter, int aPointCount );
378 
385  void drawPolygon( GLdouble* aPoints, int aPointCount );
386 
394  int drawBitmapChar( unsigned long aChar );
395 
405  void drawBitmapOverbar( double aLength, double aHeight );
406 
414  std::pair<VECTOR2D, float> computeBitmapTextSize( const wxString& aText ) const;
415 
416  // Event handling
422  void onPaint( wxPaintEvent& aEvent );
423 
429  void skipMouseEvent( wxMouseEvent& aEvent );
430 
434  void blitCursor();
435 
441  unsigned int getNewGroupNumber();
442 
446  double calcAngleStep( double aRadius ) const
447  {
448  // Bigger arcs need smaller alpha increment to make them look smooth
449  return std::min( 1e6 / aRadius, 2.0 * M_PI / CIRCLE_POINTS );
450  }
451 
455  void init();
456 };
457 } // namespace KIGFX
458 
459 #endif // OPENGLGAL_H_
VERTEX_MANAGER * currentManager
Currently used VERTEX_MANAGER (for storing VERTEX_ITEMs)
Definition: opengl_gal.h:302
static SHADER * shader
There is only one shader used for different objects.
Definition: opengl_gal.h:314
virtual void BeginUpdate() override
Enables item update mode.
Definition: opengl_gal.cpp:380
static wxGLContext * glMainContext
Parent OpenGL context.
Definition: opengl_gal.h:290
double calcAngleStep(double aRadius) const
Compute the angle step when drawing arcs/circles approximated with lines.
Definition: opengl_gal.h:446
virtual void EndUpdate() override
Disables item update mode.
Definition: opengl_gal.cpp:393
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: class_module.h:58
unsigned int overlayBuffer
Auxiliary rendering target (for menus etc.)
Definition: opengl_gal.h:310
std::deque< boost::shared_array< GLdouble > > & intersectPoints
Intersect points, that have to be freed after tessellation.
Definition: opengl_gal.h:280
void drawPolyline(std::function< VECTOR2D(int)> aPointGetter, int aPointCount)
Generic way of drawing a polyline stored in different containers.
virtual void Scale(const VECTOR2D &aScale) override
Scale the context.
virtual void ClearTarget(RENDER_TARGET aTarget) override
Clears the target for rendering.
virtual void Translate(const VECTOR2D &aTranslation) override
Translate the context.
virtual void ClearScreen(const COLOR4D &aColor) override
Clear the screen.
RENDER_TARGET currentTarget
Current rendering target.
Definition: opengl_gal.h:311
bool isBitmapFontInitialized
Is the shader set to use bitmap fonts?
Definition: opengl_gal.h:319
void drawPolygon(GLdouble *aPoints, int aPointCount)
Draws a filled polygon.
static GLuint fontTexture
Bitmap font texture handle (shared)
Definition: opengl_gal.h:296
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:305
virtual void EndDrawing() override
End the drawing, needs to be called for every new frame.
Definition: opengl_gal.cpp:346
Class OpenGL_GAL is the OpenGL implementation of the Graphics Abstraction Layer.
Definition: opengl_gal.h:65
virtual void Restore() override
Restore the context.
virtual void ChangeGroupDepth(int aGroupNumber, int aDepth) override
Changes the depth (Z-axis position) of the group.
virtual int BeginGroup() override
Begin a group.
virtual void DrawPolygon(const std::deque< VECTOR2D > &aPointList) override
Draw a polygon.
Definition: opengl_gal.cpp:743
Class to control vertex container and GPU with possibility of emulating old-style OpenGL 1...
void SetPaintListener(wxEvtHandler *aPaintListener)
Parameters passed to the GLU tesselator.
Definition: opengl_gal.h:268
Class to store instances of VERTEX with caching.
CIRCLE_POINTS
Class that handles multitarget rendering (ie.
bool isGrouping
Was a group started?
Definition: opengl_gal.h:322
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:66
virtual void ResizeScreen(int aWidth, int aHeight) override
Resizes the canvas.
virtual void Transform(const MATRIX3x3D &aTransformation) override
Transform the context.
VERTEX_MANAGER * nonCachedManager
Container for storing non-cached VERTEX_ITEMs.
Definition: opengl_gal.h:304
static int instanceCounter
GL GAL instance counter.
Definition: opengl_gal.h:292
VERTEX_MANAGER * vboManager
Manager used for storing new vertices.
Definition: opengl_gal.h:277
virtual void DrawArcSegment(const VECTOR2D &aCenterPoint, double aRadius, double aStartAngle, double aEndAngle, double aWidth) override
Draw an arc segment.
Definition: opengl_gal.cpp:592
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:288
virtual void Save() override
Save the context.
void init()
Basic OpenGL initialization.
OPENGL_COMPOSITOR * compositor
Handles multiple rendering targets.
Definition: opengl_gal.h:308
GROUPS_MAP groups
Stores informations about VBO objects (groups)
Definition: opengl_gal.h:300
virtual void BeginDrawing() override
Begin the drawing, needs to be called for every new frame.
Definition: opengl_gal.cpp:209
virtual void RestoreScreen() override
Restore the screen contents.
unsigned int mainBuffer
Main rendering target.
Definition: opengl_gal.h:309
virtual bool Show(bool aShow) override
Shows/hides the GAL canvas.
void SetMouseListener(wxEvtHandler *aMouseListener)
Definition: opengl_gal.h:263
Class SHAPE_POLY_SET.
virtual void EndGroup() override
End the group.
virtual void DeleteGroup(int aGroupNumber) override
Delete the group from the memory.
wxEvtHandler * mouseListener
Definition: opengl_gal.h:293
bool updatedGalDisplayOptions(const GAL_DISPLAY_OPTIONS &aOptions) override
Function updatedGalDisplayOptions.
Definition: opengl_gal.cpp:188
virtual void DrawGrid() override
>
Definition: opengl_gal.cpp:939
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint) override
Draw a line.
Definition: opengl_gal.cpp:403
void onPaint(wxPaintEvent &aEvent)
This is the OnPaint event handler.
virtual void DrawGroup(int aGroupNumber) override
Draw the stored group.
bool isFramebufferInitialized
Are the framebuffers initialized?
Definition: opengl_gal.h:317
virtual bool IsInitialized() const override
Returns the initalization status for the canvas.
Definition: opengl_gal.h:90
virtual void ClearCache() override
Delete all data created during caching of graphic items.
Class SHADER provides the access to the OpenGL shaders.
Definition: shader.h:74
GAL super
Super class definition.
Definition: opengl_gal.h:285
GLUtesselator * tesselator
The tessellator.
Definition: opengl_gal.h:329
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:303
virtual void DrawPolyline(const std::deque< VECTOR2D > &aPointList) override
Draw a polyline.
Definition: opengl_gal.cpp:720
virtual void DrawSegment(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint, double aWidth) override
Draw a rounded segment.
Definition: opengl_gal.cpp:421
wxEvtHandler * paintListener
Definition: opengl_gal.h:294
std::deque< boost::shared_array< GLdouble > > tessIntersects
Storage for intersecting points.
Definition: opengl_gal.h:331
virtual void DrawArc(const VECTOR2D &aCenterPoint, double aRadius, double aStartAngle, double aEndAngle) override
Draw an arc.
Definition: opengl_gal.cpp:526
virtual void SaveScreen() override
Save the screen contents.
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:320
void PostPaint()
Function PostPaint posts an event to m_paint_listener.
Definition: opengl_gal.h:254
void drawSemiCircle(const VECTOR2D &aCenterPoint, double aRadius, double aAngle)
Draw a semicircle.
Class 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:301
int drawBitmapChar(unsigned long aChar)
Draws a single character using bitmap font.
unsigned int getNewGroupNumber()
Returns a valid key that can be used as a new group number.
std::pair< VECTOR2D, float > computeBitmapTextSize(const wxString &aText) const
Computes a size of text drawn using bitmap font with current text setting applied.
wxGLContext * glPrivContext
Canvas-specific OpenGL context.
Definition: opengl_gal.h:291
virtual void DrawRectangle(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint) override
Draw a rectangle.
Definition: opengl_gal.cpp:682
virtual void Flush() override
Force all remaining objects to be drawn.
std::map< unsigned int, std::shared_ptr< VERTEX_ITEM > > GROUPS_MAP
Definition: opengl_gal.h:299
virtual void DrawCursor(const VECTOR2D &aCursorPosition) override
Draw the cursor.
RENDER_TARGET
RENDER_TARGET: Possible rendering targets.
Definition: definitions.h:39
virtual void BitmapText(const wxString &aText, const VECTOR2D &aPosition, double aRotationAngle) override
Draws a text using a bitmap font.
Definition: opengl_gal.cpp:831
virtual ~OPENGL_GAL()
Definition: opengl_gal.cpp:147
bool IsVisible() const override
>
Definition: opengl_gal.h:97
virtual void DrawCircle(const VECTOR2D &aCenterPoint, double aRadius) override
Draw a circle using world coordinates.
Definition: opengl_gal.cpp:466
static bool isBitmapFontLoaded
Is the bitmap font texture loaded?
Definition: opengl_gal.h:318
virtual void DrawCurve(const VECTOR2D &startPoint, const VECTOR2D &controlPointA, const VECTOR2D &controlPointB, const VECTOR2D &endPoint) override
Draw a cubic bezier spline.
Definition: opengl_gal.cpp:799
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.
#define min(a, b)
Definition: auxiliary.h:85
Class GAL is the abstract interface for drawing on a 2D-surface.
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39
void drawBitmapOverbar(double aLength, double aHeight)
Draws an overbar over the currently drawn text.