KiCad PCB EDA Suite
vertex_manager.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) 2013-2016 CERN
5  * @author Maciej Suminski <maciej.suminski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
31 #ifndef VERTEX_MANAGER_H_
32 #define VERTEX_MANAGER_H_
33 
34 #define GLM_FORCE_RADIANS
35 #include <glm/gtc/matrix_transform.hpp>
36 #include <glm/glm.hpp>
38 #include <gal/color4d.h>
39 #include <stack>
40 #include <memory>
41 #include <wx/log.h>
42 
43 namespace KIGFX
44 {
45 class SHADER;
46 class VERTEX_ITEM;
47 class VERTEX_CONTAINER;
48 class GPU_MANAGER;
49 
51 {
52 public:
59  VERTEX_MANAGER( bool aCached );
60 
65  void Map();
66 
71  void Unmap();
72 
80  bool Reserve( unsigned int aSize );
81 
92  inline bool Vertex( const VERTEX& aVertex )
93  {
94  return Vertex( aVertex.x, aVertex.y, aVertex.z );
95  }
96 
107  bool Vertex( GLfloat aX, GLfloat aY, GLfloat aZ );
108 
118  bool Vertex( const VECTOR2D& aXY, GLfloat aZ )
119  {
120  return Vertex( aXY.x, aXY.y, aZ );
121  }
122 
135  bool Vertices( const VERTEX aVertices[], unsigned int aSize );
136 
143  inline void Color( const COLOR4D& aColor )
144  {
145  m_color[0] = aColor.r * 255.0;
146  m_color[1] = aColor.g * 255.0;
147  m_color[2] = aColor.b * 255.0;
148  m_color[3] = aColor.a * 255.0;
149  }
150 
160  inline void Color( GLfloat aRed, GLfloat aGreen, GLfloat aBlue, GLfloat aAlpha )
161  {
162  m_color[0] = aRed * 255.0;
163  m_color[1] = aGreen * 255.0;
164  m_color[2] = aBlue * 255.0;
165  m_color[3] = aAlpha * 255.0;
166  }
167 
180  inline void Shader( GLfloat aShaderType, GLfloat aParam1 = 0.0f,
181  GLfloat aParam2 = 0.0f, GLfloat aParam3 = 0.0f )
182  {
183  m_shader[0] = aShaderType;
184  m_shader[1] = aParam1;
185  m_shader[2] = aParam2;
186  m_shader[3] = aParam3;
187  }
188 
198  inline void Translate( GLfloat aX, GLfloat aY, GLfloat aZ )
199  {
200  m_transform = glm::translate( m_transform, glm::vec3( aX, aY, aZ ) );
201  }
202 
213  inline void Rotate( GLfloat aAngle, GLfloat aX, GLfloat aY, GLfloat aZ )
214  {
215  m_transform = glm::rotate( m_transform, aAngle, glm::vec3( aX, aY, aZ ) );
216  }
217 
227  inline void Scale( GLfloat aX, GLfloat aY, GLfloat aZ )
228  {
229  m_transform = glm::scale( m_transform, glm::vec3( aX, aY, aZ ) );
230  }
231 
237  inline void PushMatrix()
238  {
240 
241  // Every transformation starts with PushMatrix
242  m_noTransform = false;
243  }
244 
250  void PopMatrix()
251  {
252  wxASSERT( !m_transformStack.empty() );
253 
255  m_transformStack.pop();
256 
257  if( m_transformStack.empty() )
258  {
259  // We return back to the identity matrix, thus no vertex transformation is needed
260  m_noTransform = true;
261  }
262  }
263 
271  void SetItem( VERTEX_ITEM& aItem ) const;
272 
277  void FinishItem() const;
278 
285  void FreeItem( VERTEX_ITEM& aItem ) const;
286 
294  void ChangeItemColor( const VERTEX_ITEM& aItem, const COLOR4D& aColor ) const;
295 
303  void ChangeItemDepth( const VERTEX_ITEM& aItem, GLfloat aDepth ) const;
304 
312  VERTEX* GetVertices( const VERTEX_ITEM& aItem ) const;
313 
314  const glm::mat4& GetTransformation() const
315  {
316  return m_transform;
317  }
318 
324  void SetShader( SHADER& aShader ) const;
325 
330  void Clear() const;
331 
336  void BeginDrawing() const;
337 
344  void DrawItem( const VERTEX_ITEM& aItem ) const;
345 
350  void EndDrawing() const;
351 
356  void EnableDepthTest( bool aEnabled );
357 
358 protected:
369  void putVertex( VERTEX& aTarget, GLfloat aX, GLfloat aY, GLfloat aZ ) const;
370 
372  std::shared_ptr<VERTEX_CONTAINER> m_container;
374  std::shared_ptr<GPU_MANAGER> m_gpu;
375 
380  glm::mat4 m_transform;
382  std::stack<glm::mat4> m_transformStack;
387 
390 
392  unsigned int m_reservedSpace;
393 };
394 
395 } // namespace KIGFX
396 
397 #endif /* VERTEX_MANAGER_H_ */
void FreeItem(VERTEX_ITEM &aItem) const
Function FreeItem() frees the memory occupied by the item, so it is no longer stored in the container...
VERTEX * GetVertices(const VERTEX_ITEM &aItem) const
Function GetVertices() returns a pointer to the vertices owned by an item.
void Clear() const
Function Clear() removes all the stored vertices from the container.
void ChangeItemDepth(const VERTEX_ITEM &aItem, GLfloat aDepth) const
Function ChangeItemDepth() changes the depth of all vertices owned by an item.
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: class_module.h:58
void PushMatrix()
Function PushMatrix() pushes the current transformation matrix stack.
Data structure for vertices {X,Y,Z,R,G,B,A,shader&param}
Definition: vertex_common.h:56
std::shared_ptr< VERTEX_CONTAINER > m_container
Container for vertices, may be cached or noncached.
bool Reserve(unsigned int aSize)
Function Reserve() allocates space for vertices, so it will be used with subsequent Vertex() calls...
void Color(GLfloat aRed, GLfloat aGreen, GLfloat aBlue, GLfloat aAlpha)
Function Color() changes currently used color that will be applied to newly added vertices...
GLfloat m_shader[SHADER_STRIDE]
Currently used shader and its parameters.
bool Vertex(const VERTEX &aVertex)
Function Vertex() adds a vertex with the given coordinates to the currently set item.
void Rotate(GLfloat aAngle, GLfloat aX, GLfloat aY, GLfloat aZ)
Function Rotate() multiplies the current matrix by a rotation matrix, so the newly vertices will be r...
double g
Green component.
Definition: color4d.h:288
void SetItem(VERTEX_ITEM &aItem) const
Function SetItem() sets an item to start its modifications.
bool m_noTransform
State machine variables True in case there is no need to transform vertices.
double b
Blue component.
Definition: color4d.h:289
glm::mat4 m_transform
Currently used transform matrix.
double a
Alpha component.
Definition: color4d.h:290
void Unmap()
Function Unmap() unmaps vertex buffer.
std::stack< glm::mat4 > m_transformStack
Stack of transformation matrices, used for Push/PopMatrix.
void EndDrawing() const
Function EndDrawing() finishes drawing operations.
void Translate(GLfloat aX, GLfloat aY, GLfloat aZ)
Function Translate() multiplies the current matrix by a translation matrix, so newly vertices will be...
void Map()
Function Map() maps vertex buffer.
bool Vertex(const VECTOR2D &aXY, GLfloat aZ)
Function Vertex() adds a vertex with the given coordinates to the currently set item.
Class SHADER provides the access to the OpenGL shaders.
Definition: shader.h:74
void putVertex(VERTEX &aTarget, GLfloat aX, GLfloat aY, GLfloat aZ) const
Function putVertex() applies all transformation to the given coordinates and store them at the specif...
void BeginDrawing() const
Function BeginDrawing() prepares buffers and items to start drawing.
void Color(const COLOR4D &aColor)
Function Color() changes currently used color that will be applied to newly added vertices...
bool Vertices(const VERTEX aVertices[], unsigned int aSize)
Function Vertices() adds one or more vertices to the currently set item.
void PopMatrix()
Function PopMatrix() pops the current transformation matrix stack.
Common defines and consts used in vertex related classes.
const int scale
void ChangeItemColor(const VERTEX_ITEM &aItem, const COLOR4D &aColor) const
Function ChangeItemColor() changes the color of all vertices owned by an item.
GLubyte m_color[COLOR_STRIDE]
Currently used color.
void EnableDepthTest(bool aEnabled)
Function EnableDepthTest() Enables/disables Z buffer depth test.
void FinishItem() const
Function FinishItem() does the cleaning after adding an item.
const glm::mat4 & GetTransformation() const
void Scale(GLfloat aX, GLfloat aY, GLfloat aZ)
Function Scale() multiplies the current matrix by a scaling matrix, so the newly vertices will be sca...
VERTEX_MANAGER(bool aCached)
Constructor.
static constexpr size_t SHADER_STRIDE
Definition: vertex_common.h:77
void Shader(GLfloat aShaderType, GLfloat aParam1=0.0f, GLfloat aParam2=0.0f, GLfloat aParam3=0.0f)
Function Shader() changes currently used shader and its parameters that will be applied to newly adde...
double r
Red component.
Definition: color4d.h:287
std::shared_ptr< GPU_MANAGER > m_gpu
GPU manager for data transfers and drawing operations.
void DrawItem(const VERTEX_ITEM &aItem) const
Function DrawItem() draws an item to the buffer.
VERTEX * m_reserved
Currently reserved chunk to store vertices.
unsigned int m_reservedSpace
Currently available reserved space.
void SetShader(SHADER &aShader) const
Function SetShader() sets a shader program that is going to be used during rendering.
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39
static constexpr size_t COLOR_STRIDE
Definition: vertex_common.h:72