2013-07-22 08:41:12 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2016-05-02 13:56:10 +00:00
|
|
|
* Copyright (C) 2013-2016 CERN
|
2021-02-16 22:25:27 +00:00
|
|
|
* Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
2013-07-22 08:41:12 +00:00
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file vertex_manager.cpp
|
|
|
|
* @brief Class to control vertex container and GPU with possibility of emulating old-style OpenGL
|
|
|
|
* 1.0 state machine using modern OpenGL methods.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gal/opengl/vertex_manager.h>
|
|
|
|
#include <gal/opengl/cached_container.h>
|
|
|
|
#include <gal/opengl/noncached_container.h>
|
|
|
|
#include <gal/opengl/gpu_manager.h>
|
|
|
|
#include <gal/opengl/vertex_item.h>
|
2013-11-11 09:48:49 +00:00
|
|
|
#include <confirm.h>
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
using namespace KIGFX;
|
2013-07-22 08:41:12 +00:00
|
|
|
|
|
|
|
VERTEX_MANAGER::VERTEX_MANAGER( bool aCached ) :
|
2021-02-16 22:25:27 +00:00
|
|
|
m_noTransform( true ),
|
|
|
|
m_transform( 1.0f ),
|
2021-02-22 13:15:09 +00:00
|
|
|
m_reserved( nullptr ),
|
2021-02-16 22:25:27 +00:00
|
|
|
m_reservedSpace( 0 )
|
2013-07-22 08:41:12 +00:00
|
|
|
{
|
|
|
|
m_container.reset( VERTEX_CONTAINER::MakeContainer( aCached ) );
|
|
|
|
m_gpu.reset( GPU_MANAGER::MakeManager( m_container.get() ) );
|
|
|
|
|
|
|
|
// There is no shader used by default
|
2017-09-09 18:40:00 +00:00
|
|
|
for( unsigned int i = 0; i < SHADER_STRIDE; ++i )
|
2013-07-22 08:41:12 +00:00
|
|
|
m_shader[i] = 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-02 14:12:16 +00:00
|
|
|
void VERTEX_MANAGER::Map()
|
|
|
|
{
|
|
|
|
m_container->Map();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VERTEX_MANAGER::Unmap()
|
|
|
|
{
|
|
|
|
m_container->Unmap();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-02 13:56:11 +00:00
|
|
|
bool VERTEX_MANAGER::Reserve( unsigned int aSize )
|
2016-05-02 13:56:10 +00:00
|
|
|
{
|
2021-02-22 13:15:09 +00:00
|
|
|
assert( m_reservedSpace == 0 && m_reserved == nullptr );
|
2016-05-02 13:56:10 +00:00
|
|
|
|
|
|
|
// flag to avoid hanging by calling DisplayError too many times:
|
|
|
|
static bool show_err = true;
|
|
|
|
|
|
|
|
m_reserved = m_container->Allocate( aSize );
|
|
|
|
|
2021-02-22 13:15:09 +00:00
|
|
|
if( m_reserved == nullptr )
|
2016-05-02 13:56:10 +00:00
|
|
|
{
|
|
|
|
if( show_err )
|
|
|
|
{
|
2021-02-22 13:15:09 +00:00
|
|
|
DisplayError( nullptr, wxT( "VERTEX_MANAGER::Reserve: Vertex allocation error" ) );
|
2016-05-02 13:56:10 +00:00
|
|
|
show_err = false;
|
|
|
|
}
|
|
|
|
|
2016-05-02 13:56:11 +00:00
|
|
|
return false;
|
2016-05-02 13:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_reservedSpace = aSize;
|
2016-05-02 13:56:11 +00:00
|
|
|
|
|
|
|
return true;
|
2016-05-02 13:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-02 13:56:11 +00:00
|
|
|
bool VERTEX_MANAGER::Vertex( GLfloat aX, GLfloat aY, GLfloat aZ )
|
2013-07-22 08:41:12 +00:00
|
|
|
{
|
2015-11-04 12:27:42 +00:00
|
|
|
// flag to avoid hanging by calling DisplayError too many times:
|
|
|
|
static bool show_err = true;
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
// Obtain the pointer to the vertex in the currently used container
|
2016-05-02 13:56:10 +00:00
|
|
|
VERTEX* newVertex;
|
|
|
|
|
|
|
|
if( m_reservedSpace > 0 )
|
|
|
|
{
|
|
|
|
newVertex = m_reserved++;
|
|
|
|
--m_reservedSpace;
|
|
|
|
|
|
|
|
if( m_reservedSpace == 0 )
|
2021-02-22 13:15:09 +00:00
|
|
|
m_reserved = nullptr;
|
2016-05-02 13:56:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newVertex = m_container->Allocate( 1 );
|
2013-07-30 16:29:54 +00:00
|
|
|
|
2022-09-14 16:07:47 +00:00
|
|
|
if( newVertex == nullptr )
|
2015-11-04 12:27:42 +00:00
|
|
|
{
|
2022-09-14 16:07:47 +00:00
|
|
|
if( show_err )
|
|
|
|
{
|
|
|
|
DisplayError( nullptr, wxT( "VERTEX_MANAGER::Vertex: Vertex allocation error" ) );
|
|
|
|
show_err = false;
|
|
|
|
}
|
2015-11-04 12:27:42 +00:00
|
|
|
|
2022-09-14 16:07:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-07-22 08:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
putVertex( *newVertex, aX, aY, aZ );
|
2016-05-02 13:56:11 +00:00
|
|
|
|
|
|
|
return true;
|
2013-07-22 08:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-02 13:56:11 +00:00
|
|
|
bool VERTEX_MANAGER::Vertices( const VERTEX aVertices[], unsigned int aSize )
|
2013-07-22 08:41:12 +00:00
|
|
|
{
|
2015-11-04 12:27:42 +00:00
|
|
|
// flag to avoid hanging by calling DisplayError too many times:
|
|
|
|
static bool show_err = true;
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
// Obtain pointer to the vertex in currently used container
|
|
|
|
VERTEX* newVertex = m_container->Allocate( aSize );
|
2013-07-30 16:29:54 +00:00
|
|
|
|
2021-02-22 13:15:09 +00:00
|
|
|
if( newVertex == nullptr )
|
2013-07-22 08:41:12 +00:00
|
|
|
{
|
2015-11-04 12:27:42 +00:00
|
|
|
if( show_err )
|
|
|
|
{
|
2021-02-22 13:15:09 +00:00
|
|
|
DisplayError( nullptr, wxT( "VERTEX_MANAGER::Vertices: Vertex allocation error" ) );
|
2015-11-04 12:27:42 +00:00
|
|
|
show_err = false;
|
|
|
|
}
|
|
|
|
|
2016-05-02 13:56:11 +00:00
|
|
|
return false;
|
2013-07-22 08:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put vertices in already allocated memory chunk
|
|
|
|
for( unsigned int i = 0; i < aSize; ++i )
|
|
|
|
{
|
|
|
|
putVertex( newVertex[i], aVertices[i].x, aVertices[i].y, aVertices[i].z );
|
|
|
|
}
|
2016-05-02 13:56:11 +00:00
|
|
|
|
|
|
|
return true;
|
2013-07-22 08:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VERTEX_MANAGER::SetItem( VERTEX_ITEM& aItem ) const
|
|
|
|
{
|
|
|
|
m_container->SetItem( &aItem );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-12 07:44:57 +00:00
|
|
|
void VERTEX_MANAGER::FinishItem() const
|
|
|
|
{
|
|
|
|
m_container->FinishItem();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
void VERTEX_MANAGER::FreeItem( VERTEX_ITEM& aItem ) const
|
|
|
|
{
|
2013-09-12 07:44:57 +00:00
|
|
|
m_container->Delete( &aItem );
|
2013-07-22 08:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VERTEX_MANAGER::ChangeItemColor( const VERTEX_ITEM& aItem, const COLOR4D& aColor ) const
|
|
|
|
{
|
2021-02-16 22:25:27 +00:00
|
|
|
unsigned int size = aItem.GetSize();
|
2013-07-22 08:41:12 +00:00
|
|
|
unsigned int offset = aItem.GetOffset();
|
|
|
|
|
|
|
|
VERTEX* vertex = m_container->GetVertices( offset );
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
for( unsigned int i = 0; i < size; ++i )
|
|
|
|
{
|
2013-07-25 16:04:15 +00:00
|
|
|
vertex->r = aColor.r * 255.0;
|
|
|
|
vertex->g = aColor.g * 255.0;
|
|
|
|
vertex->b = aColor.b * 255.0;
|
|
|
|
vertex->a = aColor.a * 255.0;
|
2013-07-22 08:41:12 +00:00
|
|
|
vertex++;
|
|
|
|
}
|
2013-07-25 16:04:15 +00:00
|
|
|
|
|
|
|
m_container->SetDirty();
|
2013-07-22 08:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VERTEX_MANAGER::ChangeItemDepth( const VERTEX_ITEM& aItem, GLfloat aDepth ) const
|
|
|
|
{
|
2021-02-16 22:25:27 +00:00
|
|
|
unsigned int size = aItem.GetSize();
|
2013-07-22 08:41:12 +00:00
|
|
|
unsigned int offset = aItem.GetOffset();
|
|
|
|
|
|
|
|
VERTEX* vertex = m_container->GetVertices( offset );
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
for( unsigned int i = 0; i < size; ++i )
|
|
|
|
{
|
|
|
|
vertex->z = aDepth;
|
|
|
|
vertex++;
|
|
|
|
}
|
2013-07-25 16:04:15 +00:00
|
|
|
|
|
|
|
m_container->SetDirty();
|
2013-07-22 08:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VERTEX* VERTEX_MANAGER::GetVertices( const VERTEX_ITEM& aItem ) const
|
|
|
|
{
|
|
|
|
if( aItem.GetSize() == 0 )
|
2021-02-22 13:15:09 +00:00
|
|
|
return nullptr; // The item is not stored in the container
|
2013-07-22 08:41:12 +00:00
|
|
|
|
|
|
|
return m_container->GetVertices( aItem.GetOffset() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VERTEX_MANAGER::SetShader( SHADER& aShader ) const
|
|
|
|
{
|
|
|
|
m_gpu->SetShader( aShader );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VERTEX_MANAGER::Clear() const
|
|
|
|
{
|
|
|
|
m_container->Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VERTEX_MANAGER::BeginDrawing() const
|
|
|
|
{
|
|
|
|
m_gpu->BeginDrawing();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VERTEX_MANAGER::DrawItem( const VERTEX_ITEM& aItem ) const
|
|
|
|
{
|
2021-11-28 21:32:46 +00:00
|
|
|
m_gpu->DrawIndices( &aItem );
|
2013-07-22 08:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VERTEX_MANAGER::EndDrawing() const
|
|
|
|
{
|
|
|
|
m_gpu->EndDrawing();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VERTEX_MANAGER::putVertex( VERTEX& aTarget, GLfloat aX, GLfloat aY, GLfloat aZ ) const
|
|
|
|
{
|
|
|
|
// Modify the vertex according to the currently used transformations
|
|
|
|
if( m_noTransform )
|
|
|
|
{
|
|
|
|
// Simply copy coordinates, when the transform matrix is the identity matrix
|
|
|
|
aTarget.x = aX;
|
|
|
|
aTarget.y = aY;
|
|
|
|
aTarget.z = aZ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Apply transformations
|
|
|
|
glm::vec4 transVertex( aX, aY, aZ, 1.0f );
|
|
|
|
transVertex = m_transform * transVertex;
|
|
|
|
|
|
|
|
aTarget.x = transVertex.x;
|
|
|
|
aTarget.y = transVertex.y;
|
|
|
|
aTarget.z = transVertex.z;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply currently used color
|
|
|
|
aTarget.r = m_color[0];
|
|
|
|
aTarget.g = m_color[1];
|
|
|
|
aTarget.b = m_color[2];
|
|
|
|
aTarget.a = m_color[3];
|
|
|
|
|
|
|
|
// Apply currently used shader
|
2017-09-09 18:40:00 +00:00
|
|
|
for( unsigned int j = 0; j < SHADER_STRIDE; ++j )
|
2013-07-22 08:41:12 +00:00
|
|
|
{
|
|
|
|
aTarget.shader[j] = m_shader[j];
|
|
|
|
}
|
|
|
|
}
|
2018-10-14 22:09:59 +00:00
|
|
|
|
|
|
|
void VERTEX_MANAGER::EnableDepthTest( bool aEnabled )
|
|
|
|
{
|
|
|
|
m_gpu->EnableDepthTest( aEnabled );
|
|
|
|
}
|