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  * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * @author Maciej Suminski <maciej.suminski@cern.ch>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
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 
42 namespace KIGFX
43 {
44 class SHADER;
45 class VERTEX_ITEM;
46 class VERTEX_CONTAINER;
47 class GPU_MANAGER;
48 
54 {
55 public:
60  VERTEX_MANAGER( bool aCached );
61 
65  void Map();
66 
70  void Unmap();
71 
78  bool Reserve( unsigned int aSize );
79 
90  inline bool Vertex( const VERTEX& aVertex )
91  {
92  return Vertex( aVertex.x, aVertex.y, aVertex.z );
93  }
94 
105  bool Vertex( GLfloat aX, GLfloat aY, GLfloat aZ );
106 
116  bool Vertex( const VECTOR2D& aXY, GLfloat aZ )
117  {
118  return Vertex( aXY.x, aXY.y, aZ );
119  }
120 
133  bool Vertices( const VERTEX aVertices[], unsigned int aSize );
134 
140  inline void Color( const COLOR4D& aColor )
141  {
142  m_color[0] = aColor.r * 255.0;
143  m_color[1] = aColor.g * 255.0;
144  m_color[2] = aColor.b * 255.0;
145  m_color[3] = aColor.a * 255.0;
146  }
147 
158  inline void Color( GLfloat aRed, GLfloat aGreen, GLfloat aBlue, GLfloat aAlpha )
159  {
160  m_color[0] = aRed * 255.0;
161  m_color[1] = aGreen * 255.0;
162  m_color[2] = aBlue * 255.0;
163  m_color[3] = aAlpha * 255.0;
164  }
165 
179  inline void Shader( GLfloat aShaderType, GLfloat aParam1 = 0.0f, GLfloat aParam2 = 0.0f,
180  GLfloat aParam3 = 0.0f )
181  {
182  m_shader[0] = aShaderType;
183  m_shader[1] = aParam1;
184  m_shader[2] = aParam2;
185  m_shader[3] = aParam3;
186  }
187 
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 
214  inline void Rotate( GLfloat aAngle, GLfloat aX, GLfloat aY, GLfloat aZ )
215  {
216  m_transform = glm::rotate( m_transform, aAngle, glm::vec3( aX, aY, aZ ) );
217  }
218 
229  inline void Scale( GLfloat aX, GLfloat aY, GLfloat aZ )
230  {
231  m_transform = glm::scale( m_transform, glm::vec3( aX, aY, aZ ) );
232  }
233 
239  inline void PushMatrix()
240  {
242 
243  // Every transformation starts with PushMatrix
244  m_noTransform = false;
245  }
246 
252  void PopMatrix()
253  {
254  wxASSERT( !m_transformStack.empty() );
255 
257  m_transformStack.pop();
258 
259  if( m_transformStack.empty() )
260  {
261  // We return back to the identity matrix, thus no vertex transformation is needed
262  m_noTransform = true;
263  }
264  }
265 
273  void SetItem( VERTEX_ITEM& aItem ) const;
274 
278  void FinishItem() const;
279 
285  void FreeItem( VERTEX_ITEM& aItem ) const;
286 
293  void ChangeItemColor( const VERTEX_ITEM& aItem, const COLOR4D& aColor ) const;
294 
301  void ChangeItemDepth( const VERTEX_ITEM& aItem, GLfloat aDepth ) const;
302 
309  VERTEX* GetVertices( const VERTEX_ITEM& aItem ) const;
310 
311  const glm::mat4& GetTransformation() const
312  {
313  return m_transform;
314  }
315 
321  void SetShader( SHADER& aShader ) const;
322 
326  void Clear() const;
327 
331  void BeginDrawing() const;
332 
338  void DrawItem( const VERTEX_ITEM& aItem ) const;
339 
343  void EndDrawing() const;
344 
348  void EnableDepthTest( bool aEnabled );
349 
350 protected:
360  void putVertex( VERTEX& aTarget, GLfloat aX, GLfloat aY, GLfloat aZ ) const;
361 
363  std::shared_ptr<VERTEX_CONTAINER> m_container;
365  std::shared_ptr<GPU_MANAGER> m_gpu;
366 
371  glm::mat4 m_transform;
373  std::stack<glm::mat4> m_transformStack;
378 
381 
383  unsigned int m_reservedSpace;
384 };
385 
386 } // namespace KIGFX
387 
388 #endif /* VERTEX_MANAGER_H_ */
const glm::mat4 & GetTransformation() const
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:236
void PushMatrix()
Push 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)
Allocate space for vertices, so it will be used with subsequent Vertex() calls.
void BeginDrawing() const
Prepare buffers and items to start drawing.
void Color(GLfloat aRed, GLfloat aGreen, GLfloat aBlue, GLfloat aAlpha)
Change currently used color that will be applied to newly added vertices.
void DrawItem(const VERTEX_ITEM &aItem) const
Draw an item to the buffer.
GLfloat m_shader[SHADER_STRIDE]
Currently used shader and its parameters.
bool Vertex(const VERTEX &aVertex)
Add a vertex with the given coordinates to the currently set item.
void Rotate(GLfloat aAngle, GLfloat aX, GLfloat aY, GLfloat aZ)
Multiply the current matrix by a rotation matrix, so the newly vertices will be rotated by the given ...
double g
Green component.
Definition: color4d.h:372
void putVertex(VERTEX &aTarget, GLfloat aX, GLfloat aY, GLfloat aZ) const
Apply all transformation to the given coordinates and store them at the specified target.
bool m_noTransform
State machine variables True in case there is no need to transform vertices.
double b
Blue component.
Definition: color4d.h:373
glm::mat4 m_transform
Currently used transform matrix.
void FinishItem() const
Clean after adding an item.
double a
Alpha component.
Definition: color4d.h:374
void Unmap()
Unmap vertex buffer.
void ChangeItemDepth(const VERTEX_ITEM &aItem, GLfloat aDepth) const
Change the depth of all vertices owned by an item.
std::stack< glm::mat4 > m_transformStack
Stack of transformation matrices, used for Push/PopMatrix.
VERTEX * GetVertices(const VERTEX_ITEM &aItem) const
Return a pointer to the vertices owned by an item.
void SetShader(SHADER &aShader) const
Set a shader program that is going to be used during rendering.
void Translate(GLfloat aX, GLfloat aY, GLfloat aZ)
Multiply the current matrix by a translation matrix, so newly vertices will be translated by the give...
void Map()
Map vertex buffer.
bool Vertex(const VECTOR2D &aXY, GLfloat aZ)
Add a vertex with the given coordinates to the currently set item.
Provide the access to the OpenGL shaders.
Definition: shader.h:76
void Color(const COLOR4D &aColor)
Changes currently used color that will be applied to newly added vertices.
bool Vertices(const VERTEX aVertices[], unsigned int aSize)
Add one or more vertices to the currently set item.
void PopMatrix()
Pop the current transformation matrix stack.
Common defines and consts used in vertex related classes.
void EndDrawing() const
Finish drawing operations.
void FreeItem(VERTEX_ITEM &aItem) const
Free the memory occupied by the item, so it is no longer stored in the container.
void SetItem(VERTEX_ITEM &aItem) const
Set an item to start its modifications.
const int scale
GLubyte m_color[COLOR_STRIDE]
Currently used color.
void EnableDepthTest(bool aEnabled)
Enable/disable Z buffer depth test.
void Scale(GLfloat aX, GLfloat aY, GLfloat aZ)
Multiply the current matrix by a scaling matrix, so the newly vertices will be scaled by the given fa...
VERTEX_MANAGER(bool aCached)
static constexpr size_t SHADER_STRIDE
Definition: vertex_common.h:81
Class to control vertex container and GPU with possibility of emulating old-style OpenGL 1....
void Shader(GLfloat aShaderType, GLfloat aParam1=0.0f, GLfloat aParam2=0.0f, GLfloat aParam3=0.0f)
Change currently used shader and its parameters that will be applied to newly added vertices.
double r
Red component.
Definition: color4d.h:371
void ChangeItemColor(const VERTEX_ITEM &aItem, const COLOR4D &aColor) const
Change the color of all vertices owned by an item.
std::shared_ptr< GPU_MANAGER > m_gpu
GPU manager for data transfers and drawing operations.
VERTEX * m_reserved
Currently reserved chunk to store vertices.
void Clear() const
Remove all the stored vertices from the container.
unsigned int m_reservedSpace
Currently available reserved space.
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103
static constexpr size_t COLOR_STRIDE
Definition: vertex_common.h:76