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>
33#include <gal/gal.h>
35#include <gal/color4d.h>
36#include <stack>
37#include <memory>
38
39namespace KIGFX
40{
41class SHADER;
42class VERTEX_ITEM;
43class VERTEX_CONTAINER;
44class GPU_MANAGER;
45
51{
52public:
57 VERTEX_MANAGER( bool aCached );
58
63 explicit VERTEX_MANAGER( std::shared_ptr<VERTEX_CONTAINER> aContainer );
64
70 void Abandon();
71
75 void Map();
76
80 void Unmap();
81
88 bool Reserve( unsigned int aSize );
89
100 inline bool Vertex( const VERTEX& aVertex )
101 {
102 return Vertex( aVertex.x, aVertex.y, aVertex.z );
103 }
104
115 bool Vertex( GLfloat aX, GLfloat aY, GLfloat aZ );
116
126 bool Vertex( const VECTOR2D& aXY, GLfloat aZ )
127 {
128 return Vertex( aXY.x, aXY.y, aZ );
129 }
130
143 bool Vertices( const VERTEX aVertices[], unsigned int aSize );
144
150 inline void Color( const COLOR4D& aColor )
151 {
152 m_color[0] = aColor.r * 255.0;
153 m_color[1] = aColor.g * 255.0;
154 m_color[2] = aColor.b * 255.0;
155 m_color[3] = aColor.a * 255.0;
156 }
157
168 inline void Color( GLfloat aRed, GLfloat aGreen, GLfloat aBlue, GLfloat aAlpha )
169 {
170 m_color[0] = aRed * 255.0;
171 m_color[1] = aGreen * 255.0;
172 m_color[2] = aBlue * 255.0;
173 m_color[3] = aAlpha * 255.0;
174 }
175
189 inline void Shader( GLfloat aShaderType, GLfloat aParam1 = 0.0f, GLfloat aParam2 = 0.0f,
190 GLfloat aParam3 = 0.0f )
191 {
192 m_shader[0] = aShaderType;
193 m_shader[1] = aParam1;
194 m_shader[2] = aParam2;
195 m_shader[3] = aParam3;
196 }
197
208 inline void Translate( GLfloat aX, GLfloat aY, GLfloat aZ )
209 {
210 m_transform = glm::translate( m_transform, glm::vec3( aX, aY, aZ ) );
211 }
212
224 inline void Rotate( GLfloat aAngle, GLfloat aX, GLfloat aY, GLfloat aZ )
225 {
226 m_transform = glm::rotate( m_transform, aAngle, glm::vec3( aX, aY, aZ ) );
227 }
228
239 inline void Scale( GLfloat aX, GLfloat aY, GLfloat aZ )
240 {
241 m_transform = glm::scale( m_transform, glm::vec3( aX, aY, aZ ) );
242 }
243
249 inline void PushMatrix()
250 {
252
253 // Every transformation starts with PushMatrix
254 m_noTransform = false;
255 }
256
263 {
264 wxASSERT( !m_transformStack.empty() );
265
267 m_transformStack.pop();
268
269 if( m_transformStack.empty() )
270 {
271 // We return back to the identity matrix, thus no vertex transformation is needed
272 m_noTransform = true;
273 }
274 }
275
283 void SetItem( VERTEX_ITEM& aItem ) const;
284
288 void FinishItem() const;
289
295 void FreeItem( VERTEX_ITEM& aItem ) const;
296
303 void ChangeItemColor( const VERTEX_ITEM& aItem, const COLOR4D& aColor ) const;
304
311 void ChangeItemDepth( const VERTEX_ITEM& aItem, GLfloat aDepth ) const;
312
319 VERTEX* GetVertices( const VERTEX_ITEM& aItem ) const;
320
321 const glm::mat4& GetTransformation() const
322 {
323 return m_transform;
324 }
325
331 void SetShader( SHADER& aShader ) const;
332
336 void Clear() const;
337
341 void BeginDrawing() const;
342
348 void DrawItem( const VERTEX_ITEM& aItem ) const;
349
353 void EndDrawing() const;
354
358 void EnableDepthTest( bool aEnabled );
359
360protected:
370 void putVertex( VERTEX& aTarget, GLfloat aX, GLfloat aY, GLfloat aZ ) const;
371
373 std::shared_ptr<VERTEX_CONTAINER> m_container;
374
376 std::shared_ptr<GPU_MANAGER> m_gpu;
377
381
383 glm::mat4 m_transform;
384
386 std::stack<glm::mat4> m_transformStack;
387
390
393
396
398 unsigned int m_reservedSpace;
399};
400
401} // namespace KIGFX
402
403#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
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 Abandon()
Give up ownership of the container's GL objects without deleting them.
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 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 Color(GLfloat aRed, GLfloat aGreen, GLfloat aBlue, GLfloat aAlpha)
Change currently used color that will be applied to newly added vertices.
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 * 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.
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.
#define GAL_API
Definition gal.h:27
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.