KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Maciej Suminski <[email protected]>
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, see <https://www.gnu.org/licenses/>.
21 */
22
26
27#ifndef VERTEX_MANAGER_H_
28#define VERTEX_MANAGER_H_
29
30#define GLM_FORCE_RADIANS
31#include <glm/gtc/matrix_transform.hpp>
32#include <glm/glm.hpp>
34#include <gal/color4d.h>
35#include <stack>
36#include <memory>
37
38namespace KIGFX
39{
40class SHADER;
41class VERTEX_ITEM;
42class VERTEX_CONTAINER;
43class GPU_MANAGER;
44
50{
51public:
56 VERTEX_MANAGER( bool aCached );
57
63 void Abandon();
64
68 void Map();
69
73 void Unmap();
74
81 bool Reserve( unsigned int aSize );
82
93 inline bool Vertex( const VERTEX& aVertex )
94 {
95 return Vertex( aVertex.x, aVertex.y, aVertex.z );
96 }
97
108 bool Vertex( GLfloat aX, GLfloat aY, GLfloat aZ );
109
119 bool Vertex( const VECTOR2D& aXY, GLfloat aZ )
120 {
121 return Vertex( aXY.x, aXY.y, aZ );
122 }
123
136 bool Vertices( const VERTEX aVertices[], unsigned int aSize );
137
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
161 inline void Color( GLfloat aRed, GLfloat aGreen, GLfloat aBlue, GLfloat aAlpha )
162 {
163 m_color[0] = aRed * 255.0;
164 m_color[1] = aGreen * 255.0;
165 m_color[2] = aBlue * 255.0;
166 m_color[3] = aAlpha * 255.0;
167 }
168
182 inline void Shader( GLfloat aShaderType, GLfloat aParam1 = 0.0f, GLfloat aParam2 = 0.0f,
183 GLfloat aParam3 = 0.0f )
184 {
185 m_shader[0] = aShaderType;
186 m_shader[1] = aParam1;
187 m_shader[2] = aParam2;
188 m_shader[3] = aParam3;
189 }
190
201 inline void Translate( GLfloat aX, GLfloat aY, GLfloat aZ )
202 {
203 m_transform = glm::translate( m_transform, glm::vec3( aX, aY, aZ ) );
204 }
205
217 inline void Rotate( GLfloat aAngle, GLfloat aX, GLfloat aY, GLfloat aZ )
218 {
219 m_transform = glm::rotate( m_transform, aAngle, glm::vec3( aX, aY, aZ ) );
220 }
221
232 inline void Scale( GLfloat aX, GLfloat aY, GLfloat aZ )
233 {
234 m_transform = glm::scale( m_transform, glm::vec3( aX, aY, aZ ) );
235 }
236
242 inline void PushMatrix()
243 {
245
246 // Every transformation starts with PushMatrix
247 m_noTransform = false;
248 }
249
256 {
257 wxASSERT( !m_transformStack.empty() );
258
260 m_transformStack.pop();
261
262 if( m_transformStack.empty() )
263 {
264 // We return back to the identity matrix, thus no vertex transformation is needed
265 m_noTransform = true;
266 }
267 }
268
276 void SetItem( VERTEX_ITEM& aItem ) const;
277
281 void FinishItem() const;
282
288 void FreeItem( VERTEX_ITEM& aItem ) const;
289
296 void ChangeItemColor( const VERTEX_ITEM& aItem, const COLOR4D& aColor ) const;
297
304 void ChangeItemDepth( const VERTEX_ITEM& aItem, GLfloat aDepth ) const;
305
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
329 void Clear() const;
330
334 void BeginDrawing() const;
335
341 void DrawItem( const VERTEX_ITEM& aItem ) const;
342
346 void EndDrawing() const;
347
351 void EnableDepthTest( bool aEnabled );
352
353protected:
363 void putVertex( VERTEX& aTarget, GLfloat aX, GLfloat aY, GLfloat aZ ) const;
364
366 std::shared_ptr<VERTEX_CONTAINER> m_container;
367
369 std::shared_ptr<GPU_MANAGER> m_gpu;
370
374
376 glm::mat4 m_transform;
377
379 std::stack<glm::mat4> m_transformStack;
380
383
386
389
391 unsigned int m_reservedSpace;
392};
393
394} // namespace KIGFX
395
396#endif /* VERTEX_MANAGER_H_ */
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
double r
Red component.
Definition color4d.h:391
double g
Green component.
Definition color4d.h:392
double a
Alpha component.
Definition color4d.h:394
double b
Blue component.
Definition color4d.h:393
Provide the access to the OpenGL shaders.
Definition shader.h:73
void EndDrawing() const
Finish drawing operations.
bool Vertex(const VERTEX &aVertex)
Add a vertex with the given coordinates to the currently set item.
const glm::mat4 & GetTransformation() const
void Map()
Map vertex buffer.
bool m_noTransform
State machine variables True in case there is no need to transform vertices.
void SetItem(VERTEX_ITEM &aItem) const
Set an item to start its modifications.
void Clear() const
Remove all the stored vertices from the container.
void Abandon()
Give up ownership of the container's GL objects without deleting them.
void BeginDrawing() const
Prepare buffers and items to start drawing.
void ChangeItemColor(const VERTEX_ITEM &aItem, const COLOR4D &aColor) const
Change the color of all vertices owned by an item.
void Color(const COLOR4D &aColor)
Change currently used color that will be applied to newly added vertices.
bool Reserve(unsigned int aSize)
Allocate space for vertices, so it will be used with subsequent Vertex() calls.
void FinishItem() const
Clean after adding an item.
void ChangeItemDepth(const VERTEX_ITEM &aItem, GLfloat aDepth) const
Change the depth of all vertices owned by an 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 ...
void FreeItem(VERTEX_ITEM &aItem) const
Free the memory occupied by the item, so it is no longer stored in the container.
void Color(GLfloat aRed, GLfloat aGreen, GLfloat aBlue, GLfloat aAlpha)
Change currently used color that will be applied to newly added vertices.
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.
void EnableDepthTest(bool aEnabled)
Enable/disable Z buffer depth test.
glm::mat4 m_transform
Currently used transform matrix.
void PopMatrix()
Pop the current transformation matrix stack.
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.
std::stack< glm::mat4 > m_transformStack
Stack of transformation matrices, used for Push/PopMatrix.
VERTEX_MANAGER(bool aCached)
unsigned int m_reservedSpace
Currently available reserved space.
VERTEX * GetVertices(const VERTEX_ITEM &aItem) const
Return a pointer to the vertices owned by an item.
VERTEX * m_reserved
Currently reserved chunk to store vertices.
GLubyte m_color[COLOR_STRIDE]
Currently used color.
bool Vertex(const VECTOR2D &aXY, GLfloat aZ)
Add a vertex with the given coordinates to the currently set item.
void PushMatrix()
Push the current transformation matrix stack.
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...
GLfloat m_shader[SHADER_STRIDE]
Currently used shader and its parameters.
void Unmap()
Unmap vertex buffer.
bool Vertices(const VERTEX aVertices[], unsigned int aSize)
Add one or more vertices to the currently set item.
void SetShader(SHADER &aShader) const
Set a shader program that is going to be used during rendering.
std::shared_ptr< GPU_MANAGER > m_gpu
GPU manager for data transfers and drawing operations.
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...
std::shared_ptr< VERTEX_CONTAINER > m_container
Container for vertices, may be cached or noncached.
void DrawItem(const VERTEX_ITEM &aItem) const
Draw an item to the buffer.
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:30
static constexpr size_t COLOR_STRIDE
static constexpr size_t SHADER_STRIDE
VECTOR2< double > VECTOR2D
Definition vector2d.h:707
Common defines and consts used in vertex related classes.