KiCad PCB EDA Suite
Loading...
Searching...
No Matches
vertex_manager.cpp
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
28
34#include <confirm.h>
35#include <wx/log.h>
36
37
43static const wxChar traceVertexManager[] = wxT( "KICAD_VERTEX_MANAGER" );
44
45
46using namespace KIGFX;
47
49 m_noTransform( true ),
50 m_transform( 1.0f ),
51 m_reserved( nullptr ),
53{
56
57 // There is no shader used by default
58 for( unsigned int i = 0; i < SHADER_STRIDE; ++i )
59 m_shader[i] = 0.0f;
60}
61
62
64{
65 m_container->Map();
66}
67
68
70{
71 m_container->Unmap();
72}
73
74
75bool VERTEX_MANAGER::Reserve( unsigned int aSize )
76{
77 if( !aSize )
78 return true;
79
80 // flags to avoid hanging by calling DisplayError too many times:
81 static bool show_err_reserve = true;
82 static bool show_err_alloc = true;
83
84 if( m_reservedSpace != 0 || m_reserved )
85 {
86 if( show_err_reserve )
87 {
88 DisplayError( nullptr, wxT( "VERTEX_MANAGER::Reserve: Did not use all previous vertices allocated" ) );
89 show_err_reserve = false;
90 }
91 }
92
93 m_reserved = m_container->Allocate( aSize );
94
95 if( m_reserved == nullptr )
96 {
97 if( show_err_alloc )
98 {
99 DisplayError( nullptr, wxT( "VERTEX_MANAGER::Reserve: Vertex allocation error" ) );
100 show_err_alloc = false;
101 }
102
103 return false;
104 }
105
106 m_reservedSpace = aSize;
107
108 return true;
109}
110
111
112bool VERTEX_MANAGER::Vertex( GLfloat aX, GLfloat aY, GLfloat aZ )
113{
114 // flag to avoid hanging by calling DisplayError too many times:
115 static bool show_err = true;
116
117 // Obtain the pointer to the vertex in the currently used container
118 VERTEX* newVertex;
119
120 if( m_reservedSpace > 0 )
121 {
122 newVertex = m_reserved++;
124
125 if( m_reservedSpace == 0 )
126 m_reserved = nullptr;
127 }
128 else
129 {
130 newVertex = m_container->Allocate( 1 );
131
132 if( newVertex == nullptr )
133 {
134 if( show_err )
135 {
136 DisplayError( nullptr, wxT( "VERTEX_MANAGER::Vertex: Vertex allocation error" ) );
137 show_err = false;
138 }
139
140 return false;
141 }
142 }
143
144 putVertex( *newVertex, aX, aY, aZ );
145
146 return true;
147}
148
149
150bool VERTEX_MANAGER::Vertices( const VERTEX aVertices[], unsigned int aSize )
151{
152 // flag to avoid hanging by calling DisplayError too many times:
153 static bool show_err = true;
154
155 // Obtain pointer to the vertex in currently used container
156 VERTEX* newVertex = m_container->Allocate( aSize );
157
158 if( newVertex == nullptr )
159 {
160 if( show_err )
161 {
162 DisplayError( nullptr, wxT( "VERTEX_MANAGER::Vertices: Vertex allocation error" ) );
163 show_err = false;
164 }
165
166 return false;
167 }
168
169 // Put vertices in already allocated memory chunk
170 for( unsigned int i = 0; i < aSize; ++i )
171 {
172 putVertex( newVertex[i], aVertices[i].x, aVertices[i].y, aVertices[i].z );
173 }
174
175 return true;
176}
177
178
180{
181 m_container->SetItem( &aItem );
182}
183
184
186{
187 if( m_reservedSpace != 0 || m_reserved )
188 wxLogTrace( traceVertexManager, wxS( "Did not use all previous vertices allocated" ) );
189
190 m_container->FinishItem();
191}
192
193
195{
196 m_container->Delete( &aItem );
197}
198
199
200void VERTEX_MANAGER::ChangeItemColor( const VERTEX_ITEM& aItem, const COLOR4D& aColor ) const
201{
202 unsigned int size = aItem.GetSize();
203 unsigned int offset = aItem.GetOffset();
204
205 VERTEX* vertex = m_container->GetVertices( offset );
206
207 for( unsigned int i = 0; i < size; ++i )
208 {
209 vertex->r = aColor.r * 255.0;
210 vertex->g = aColor.g * 255.0;
211 vertex->b = aColor.b * 255.0;
212 vertex->a = aColor.a * 255.0;
213 vertex++;
214 }
215
216 m_container->SetDirty();
217}
218
219
220void VERTEX_MANAGER::ChangeItemDepth( const VERTEX_ITEM& aItem, GLfloat aDepth ) const
221{
222 unsigned int size = aItem.GetSize();
223 unsigned int offset = aItem.GetOffset();
224
225 VERTEX* vertex = m_container->GetVertices( offset );
226
227 for( unsigned int i = 0; i < size; ++i )
228 {
229 vertex->z = aDepth;
230 vertex++;
231 }
232
233 m_container->SetDirty();
234}
235
236
238{
239 if( aItem.GetSize() == 0 )
240 return nullptr; // The item is not stored in the container
241
242 return m_container->GetVertices( aItem.GetOffset() );
243}
244
245
246void VERTEX_MANAGER::SetShader( SHADER& aShader ) const
247{
248 m_gpu->SetShader( aShader );
249}
250
251
253{
254 m_container->Clear();
255}
256
257
259{
260 m_gpu->BeginDrawing();
261}
262
263
264void VERTEX_MANAGER::DrawItem( const VERTEX_ITEM& aItem ) const
265{
266 m_gpu->DrawIndices( &aItem );
267}
268
269
271{
272 m_gpu->EndDrawing();
273}
274
275
276void VERTEX_MANAGER::putVertex( VERTEX& aTarget, GLfloat aX, GLfloat aY, GLfloat aZ ) const
277{
278 // Modify the vertex according to the currently used transformations
279 if( m_noTransform )
280 {
281 // Simply copy coordinates, when the transform matrix is the identity matrix
282 aTarget.x = aX;
283 aTarget.y = aY;
284 aTarget.z = aZ;
285 }
286 else
287 {
288 // Apply transformations
289 glm::vec4 transVertex( aX, aY, aZ, 1.0f );
290 transVertex = m_transform * transVertex;
291
292 aTarget.x = transVertex.x;
293 aTarget.y = transVertex.y;
294 aTarget.z = transVertex.z;
295 }
296
297 // Apply currently used color
298 aTarget.r = m_color[0];
299 aTarget.g = m_color[1];
300 aTarget.b = m_color[2];
301 aTarget.a = m_color[3];
302
303 // Apply currently used shader
304 for( unsigned int j = 0; j < SHADER_STRIDE; ++j )
305 {
306 aTarget.shader[j] = m_shader[j];
307 }
308}
309
310
312{
313 m_gpu->EnableDepthTest( aEnabled );
314}
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
static GPU_MANAGER * MakeManager(VERTEX_CONTAINER *aContainer)
Provide the access to the OpenGL shaders.
Definition shader.h:73
static VERTEX_CONTAINER * MakeContainer(bool aCached)
Return a pointer to a new container of an appropriate type.
unsigned int GetOffset() const
Return data offset in the container.
Definition vertex_item.h:64
unsigned int GetSize() const
Return information about number of vertices stored.
Definition vertex_item.h:54
void EndDrawing() const
Finish drawing operations.
bool Vertex(const VERTEX &aVertex)
Add a vertex with the given coordinates to the currently set item.
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.
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 FreeItem(VERTEX_ITEM &aItem) const
Free the memory occupied by the item, so it is no longer stored in the container.
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.
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.
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.
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.
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition confirm.cpp:192
This file is part of the common library.
static const wxChar traceVertexManager[]
Flag to enable VERTEX_MANAGER debugging output.
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:29
static constexpr size_t SHADER_STRIDE
Class to store instances of VERTEX without caching.
GLfloat shader[4]
Class to handle an item held in a container.