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
61 void Map();
62
66 void Unmap();
67
74 bool Reserve( unsigned int aSize );
75
86 inline bool Vertex( const VERTEX& aVertex )
87 {
88 return Vertex( aVertex.x, aVertex.y, aVertex.z );
89 }
90
101 bool Vertex( GLfloat aX, GLfloat aY, GLfloat aZ );
102
112 bool Vertex( const VECTOR2D& aXY, GLfloat aZ )
113 {
114 return Vertex( aXY.x, aXY.y, aZ );
115 }
116
129 bool Vertices( const VERTEX aVertices[], unsigned int aSize );
130
136 inline void Color( const COLOR4D& aColor )
137 {
138 m_color[0] = aColor.r * 255.0;
139 m_color[1] = aColor.g * 255.0;
140 m_color[2] = aColor.b * 255.0;
141 m_color[3] = aColor.a * 255.0;
142 }
143
154 inline void Color( GLfloat aRed, GLfloat aGreen, GLfloat aBlue, GLfloat aAlpha )
155 {
156 m_color[0] = aRed * 255.0;
157 m_color[1] = aGreen * 255.0;
158 m_color[2] = aBlue * 255.0;
159 m_color[3] = aAlpha * 255.0;
160 }
161
175 inline void Shader( GLfloat aShaderType, GLfloat aParam1 = 0.0f, GLfloat aParam2 = 0.0f,
176 GLfloat aParam3 = 0.0f )
177 {
178 m_shader[0] = aShaderType;
179 m_shader[1] = aParam1;
180 m_shader[2] = aParam2;
181 m_shader[3] = aParam3;
182 }
183
194 inline void Translate( GLfloat aX, GLfloat aY, GLfloat aZ )
195 {
196 m_transform = glm::translate( m_transform, glm::vec3( aX, aY, aZ ) );
197 }
198
210 inline void Rotate( GLfloat aAngle, GLfloat aX, GLfloat aY, GLfloat aZ )
211 {
212 m_transform = glm::rotate( m_transform, aAngle, glm::vec3( aX, aY, aZ ) );
213 }
214
225 inline void Scale( GLfloat aX, GLfloat aY, GLfloat aZ )
226 {
227 m_transform = glm::scale( m_transform, glm::vec3( aX, aY, aZ ) );
228 }
229
235 inline void PushMatrix()
236 {
238
239 // Every transformation starts with PushMatrix
240 m_noTransform = false;
241 }
242
249 {
250 wxASSERT( !m_transformStack.empty() );
251
253 m_transformStack.pop();
254
255 if( m_transformStack.empty() )
256 {
257 // We return back to the identity matrix, thus no vertex transformation is needed
258 m_noTransform = true;
259 }
260 }
261
269 void SetItem( VERTEX_ITEM& aItem ) const;
270
274 void FinishItem() const;
275
281 void FreeItem( VERTEX_ITEM& aItem ) const;
282
289 void ChangeItemColor( const VERTEX_ITEM& aItem, const COLOR4D& aColor ) const;
290
297 void ChangeItemDepth( const VERTEX_ITEM& aItem, GLfloat aDepth ) const;
298
305 VERTEX* GetVertices( const VERTEX_ITEM& aItem ) const;
306
307 const glm::mat4& GetTransformation() const
308 {
309 return m_transform;
310 }
311
317 void SetShader( SHADER& aShader ) const;
318
322 void Clear() const;
323
327 void BeginDrawing() const;
328
334 void DrawItem( const VERTEX_ITEM& aItem ) const;
335
339 void EndDrawing() const;
340
344 void EnableDepthTest( bool aEnabled );
345
346protected:
356 void putVertex( VERTEX& aTarget, GLfloat aX, GLfloat aY, GLfloat aZ ) const;
357
359 std::shared_ptr<VERTEX_CONTAINER> m_container;
360
362 std::shared_ptr<GPU_MANAGER> m_gpu;
363
367
369 glm::mat4 m_transform;
370
372 std::stack<glm::mat4> m_transformStack;
373
376
379
382
384 unsigned int m_reservedSpace;
385};
386
387} // namespace KIGFX
388
389#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:389
double g
Green component.
Definition color4d.h:390
double a
Alpha component.
Definition color4d.h:392
double b
Blue component.
Definition color4d.h:391
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 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:29
static constexpr size_t COLOR_STRIDE
static constexpr size_t SHADER_STRIDE
VECTOR2< double > VECTOR2D
Definition vector2d.h:682
Common defines and consts used in vertex related classes.