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, 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
38#include <confirm.h>
39#include <wx/log.h>
40
41
47static const wxChar traceVertexManager[] = wxT( "KICAD_VERTEX_MANAGER" );
48
49
50using namespace KIGFX;
51
53 m_noTransform( true ),
54 m_transform( 1.0f ),
55 m_reserved( nullptr ),
56 m_reservedSpace( 0 )
57{
60
61 // There is no shader used by default
62 for( unsigned int i = 0; i < SHADER_STRIDE; ++i )
63 m_shader[i] = 0.0f;
64}
65
66
68{
69 m_container->Map();
70}
71
72
74{
75 m_container->Unmap();
76}
77
78
79bool VERTEX_MANAGER::Reserve( unsigned int aSize )
80{
81 if( !aSize )
82 return true;
83
84 // flags to avoid hanging by calling DisplayError too many times:
85 static bool show_err_reserve = true;
86 static bool show_err_alloc = true;
87
88 if( m_reservedSpace != 0 || m_reserved )
89 {
90 if( show_err_reserve )
91 {
92 DisplayError( nullptr, wxT( "VERTEX_MANAGER::Reserve: Did not use all previous vertices allocated" ) );
93 show_err_reserve = false;
94 }
95 }
96
97 m_reserved = m_container->Allocate( aSize );
98
99 if( m_reserved == nullptr )
100 {
101 if( show_err_alloc )
102 {
103 DisplayError( nullptr, wxT( "VERTEX_MANAGER::Reserve: Vertex allocation error" ) );
104 show_err_alloc = false;
105 }
106
107 return false;
108 }
109
110 m_reservedSpace = aSize;
111
112 return true;
113}
114
115
116bool VERTEX_MANAGER::Vertex( GLfloat aX, GLfloat aY, GLfloat aZ )
117{
118 // flag to avoid hanging by calling DisplayError too many times:
119 static bool show_err = true;
120
121 // Obtain the pointer to the vertex in the currently used container
122 VERTEX* newVertex;
123
124 if( m_reservedSpace > 0 )
125 {
126 newVertex = m_reserved++;
128
129 if( m_reservedSpace == 0 )
130 m_reserved = nullptr;
131 }
132 else
133 {
134 newVertex = m_container->Allocate( 1 );
135
136 if( newVertex == nullptr )
137 {
138 if( show_err )
139 {
140 DisplayError( nullptr, wxT( "VERTEX_MANAGER::Vertex: Vertex allocation error" ) );
141 show_err = false;
142 }
143
144 return false;
145 }
146 }
147
148 putVertex( *newVertex, aX, aY, aZ );
149
150 return true;
151}
152
153
154bool VERTEX_MANAGER::Vertices( const VERTEX aVertices[], unsigned int aSize )
155{
156 // flag to avoid hanging by calling DisplayError too many times:
157 static bool show_err = true;
158
159 // Obtain pointer to the vertex in currently used container
160 VERTEX* newVertex = m_container->Allocate( aSize );
161
162 if( newVertex == nullptr )
163 {
164 if( show_err )
165 {
166 DisplayError( nullptr, wxT( "VERTEX_MANAGER::Vertices: Vertex allocation error" ) );
167 show_err = false;
168 }
169
170 return false;
171 }
172
173 // Put vertices in already allocated memory chunk
174 for( unsigned int i = 0; i < aSize; ++i )
175 {
176 putVertex( newVertex[i], aVertices[i].x, aVertices[i].y, aVertices[i].z );
177 }
178
179 return true;
180}
181
182
184{
185 m_container->SetItem( &aItem );
186}
187
188
190{
191 if( m_reservedSpace != 0 || m_reserved )
192 wxLogTrace( traceVertexManager, wxS( "Did not use all previous vertices allocated" ) );
193
194 m_container->FinishItem();
195}
196
197
199{
200 m_container->Delete( &aItem );
201}
202
203
204void VERTEX_MANAGER::ChangeItemColor( const VERTEX_ITEM& aItem, const COLOR4D& aColor ) const
205{
206 unsigned int size = aItem.GetSize();
207 unsigned int offset = aItem.GetOffset();
208
209 VERTEX* vertex = m_container->GetVertices( offset );
210
211 for( unsigned int i = 0; i < size; ++i )
212 {
213 vertex->r = aColor.r * 255.0;
214 vertex->g = aColor.g * 255.0;
215 vertex->b = aColor.b * 255.0;
216 vertex->a = aColor.a * 255.0;
217 vertex++;
218 }
219
220 m_container->SetDirty();
221}
222
223
224void VERTEX_MANAGER::ChangeItemDepth( const VERTEX_ITEM& aItem, GLfloat aDepth ) const
225{
226 unsigned int size = aItem.GetSize();
227 unsigned int offset = aItem.GetOffset();
228
229 VERTEX* vertex = m_container->GetVertices( offset );
230
231 for( unsigned int i = 0; i < size; ++i )
232 {
233 vertex->z = aDepth;
234 vertex++;
235 }
236
237 m_container->SetDirty();
238}
239
240
242{
243 if( aItem.GetSize() == 0 )
244 return nullptr; // The item is not stored in the container
245
246 return m_container->GetVertices( aItem.GetOffset() );
247}
248
249
250void VERTEX_MANAGER::SetShader( SHADER& aShader ) const
251{
252 m_gpu->SetShader( aShader );
253}
254
255
257{
258 m_container->Clear();
259}
260
261
263{
264 m_gpu->BeginDrawing();
265}
266
267
268void VERTEX_MANAGER::DrawItem( const VERTEX_ITEM& aItem ) const
269{
270 m_gpu->DrawIndices( &aItem );
271}
272
273
275{
276 m_gpu->EndDrawing();
277}
278
279
280void VERTEX_MANAGER::putVertex( VERTEX& aTarget, GLfloat aX, GLfloat aY, GLfloat aZ ) const
281{
282 // Modify the vertex according to the currently used transformations
283 if( m_noTransform )
284 {
285 // Simply copy coordinates, when the transform matrix is the identity matrix
286 aTarget.x = aX;
287 aTarget.y = aY;
288 aTarget.z = aZ;
289 }
290 else
291 {
292 // Apply transformations
293 glm::vec4 transVertex( aX, aY, aZ, 1.0f );
294 transVertex = m_transform * transVertex;
295
296 aTarget.x = transVertex.x;
297 aTarget.y = transVertex.y;
298 aTarget.z = transVertex.z;
299 }
300
301 // Apply currently used color
302 aTarget.r = m_color[0];
303 aTarget.g = m_color[1];
304 aTarget.b = m_color[2];
305 aTarget.a = m_color[3];
306
307 // Apply currently used shader
308 for( unsigned int j = 0; j < SHADER_STRIDE; ++j )
309 {
310 aTarget.shader[j] = m_shader[j];
311 }
312}
313
314
316{
317 m_gpu->EnableDepthTest( aEnabled );
318}
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
double r
Red component.
Definition: color4d.h:392
double g
Green component.
Definition: color4d.h:393
double a
Alpha component.
Definition: color4d.h:395
double b
Blue component.
Definition: color4d.h:394
static GPU_MANAGER * MakeManager(VERTEX_CONTAINER *aContainer)
Definition: gpu_manager.cpp:48
Provide the access to the OpenGL shaders.
Definition: shader.h:77
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:68
unsigned int GetSize() const
Return information about number of vertices stored.
Definition: vertex_item.h:58
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:169
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:33
static constexpr size_t SHADER_STRIDE
Definition: vertex_common.h:83
Class to store instances of VERTEX without caching.
GLfloat shader[4]
Definition: vertex_common.h:64
Class to handle an item held in a container.