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 
121  bool Vertices( const VERTEX aVertices[], unsigned int aSize );
122 
129  inline void Color( const COLOR4D& aColor )
130  {
131  m_color[0] = aColor.r * 255.0;
132  m_color[1] = aColor.g * 255.0;
133  m_color[2] = aColor.b * 255.0;
134  m_color[3] = aColor.a * 255.0;
135  }
136 
146  inline void Color( GLfloat aRed, GLfloat aGreen, GLfloat aBlue, GLfloat aAlpha )
147  {
148  m_color[0] = aRed * 255.0;
149  m_color[1] = aGreen * 255.0;
150  m_color[2] = aBlue * 255.0;
151  m_color[3] = aAlpha * 255.0;
152  }
153 
166  inline void Shader( GLfloat aShaderType, GLfloat aParam1 = 0.0f,
167  GLfloat aParam2 = 0.0f, GLfloat aParam3 = 0.0f )
168  {
169  m_shader[0] = aShaderType;
170  m_shader[1] = aParam1;
171  m_shader[2] = aParam2;
172  m_shader[3] = aParam3;
173  }
174 
184  inline void Translate( GLfloat aX, GLfloat aY, GLfloat aZ )
185  {
186  m_transform = glm::translate( m_transform, glm::vec3( aX, aY, aZ ) );
187  }
188 
199  inline void Rotate( GLfloat aAngle, GLfloat aX, GLfloat aY, GLfloat aZ )
200  {
201  m_transform = glm::rotate( m_transform, aAngle, glm::vec3( aX, aY, aZ ) );
202  }
203 
213  inline void Scale( GLfloat aX, GLfloat aY, GLfloat aZ )
214  {
215  m_transform = glm::scale( m_transform, glm::vec3( aX, aY, aZ ) );
216  }
217 
223  inline void PushMatrix()
224  {
226 
227  // Every transformation starts with PushMatrix
228  m_noTransform = false;
229  }
230 
236  void PopMatrix()
237  {
238  wxASSERT( !m_transformStack.empty() );
239 
241  m_transformStack.pop();
242 
243  if( m_transformStack.empty() )
244  {
245  // We return back to the identity matrix, thus no vertex transformation is needed
246  m_noTransform = true;
247  }
248  }
249 
257  void SetItem( VERTEX_ITEM& aItem ) const;
258 
263  void FinishItem() const;
264 
271  void FreeItem( VERTEX_ITEM& aItem ) const;
272 
280  void ChangeItemColor( const VERTEX_ITEM& aItem, const COLOR4D& aColor ) const;
281 
289  void ChangeItemDepth( const VERTEX_ITEM& aItem, GLfloat aDepth ) const;
290 
298  VERTEX* GetVertices( const VERTEX_ITEM& aItem ) const;
299 
300  const glm::mat4& GetTransformation() const
301  {
302  return m_transform;
303  }
304 
310  void SetShader( SHADER& aShader ) const;
311 
316  void Clear() const;
317 
322  void BeginDrawing() const;
323 
330  void DrawItem( const VERTEX_ITEM& aItem ) const;
331 
336  void EndDrawing() const;
337 
338 protected:
349  void putVertex( VERTEX& aTarget, GLfloat aX, GLfloat aY, GLfloat aZ ) const;
350 
352  std::shared_ptr<VERTEX_CONTAINER> m_container;
354  std::shared_ptr<GPU_MANAGER> m_gpu;
355 
360  glm::mat4 m_transform;
362  std::stack<glm::mat4> m_transformStack;
367 
370 
372  unsigned int m_reservedSpace;
373 };
374 
375 } // namespace KIGFX
376 
377 #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.
GLfloat m_shader[ShaderStride]
Currently used shader and its parameters.
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:57
void PushMatrix()
Function PushMatrix() pushes the current transformation matrix stack.
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...
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:282
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:283
glm::mat4 m_transform
Currently used transform matrix.
double a
Alpha component.
Definition: color4d.h:284
void Unmap()
Function Unmap() unmaps vertex buffer.
GLubyte m_color[ColorStride]
Currently used color.
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.
const size_t ShaderStride
Definition: vertex_common.h:72
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.
void FinishItem() const
Function FinishItem() does the cleaning after adding an item.
const size_t ColorStride
Definition: vertex_common.h:67
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.
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:281
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