2015-12-08 07:31:57 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2017-03-02 22:53:49 +00:00
|
|
|
* Copyright (C) 2015-2017 Cirilo Bernardo <cirilo.bernardo@gmail.com>
|
2020-12-16 18:19:28 +00:00
|
|
|
* Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
|
2015-12-08 07:31:57 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-02-23 06:46:22 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <wx/log.h>
|
|
|
|
|
2015-12-09 07:30:48 +00:00
|
|
|
#include "3d_cache/sg/sg_shape.h"
|
|
|
|
#include "3d_cache/sg/sg_faceset.h"
|
|
|
|
#include "3d_cache/sg/sg_appearance.h"
|
|
|
|
#include "3d_cache/sg/sg_helpers.h"
|
|
|
|
#include "3d_cache/sg/sg_coordindex.h"
|
|
|
|
#include "3d_cache/sg/sg_coords.h"
|
|
|
|
#include "3d_cache/sg/sg_colors.h"
|
|
|
|
#include "3d_cache/sg/sg_normals.h"
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
SGSHAPE::SGSHAPE( SGNODE* aParent ) : SGNODE( aParent )
|
|
|
|
{
|
|
|
|
m_SGtype = S3D::SGTYPE_SHAPE;
|
2020-12-16 18:19:28 +00:00
|
|
|
m_Appearance = nullptr;
|
|
|
|
m_RAppearance = nullptr;
|
|
|
|
m_FaceSet = nullptr;
|
|
|
|
m_RFaceSet = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr != aParent && S3D::SGTYPE_TRANSFORM != aParent->GetNodeType() )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
m_Parent = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [BUG] inappropriate parent to SGSHAPE (type %d)" ),
|
2020-12-16 18:19:28 +00:00
|
|
|
__FILE__, __FUNCTION__, __LINE__, aParent->GetNodeType() );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
2020-12-16 18:19:28 +00:00
|
|
|
else if( nullptr != aParent && S3D::SGTYPE_TRANSFORM == aParent->GetNodeType() )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
m_Parent->AddChildNode( this );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SGSHAPE::~SGSHAPE()
|
|
|
|
{
|
|
|
|
// drop references
|
|
|
|
if( m_RAppearance )
|
|
|
|
{
|
|
|
|
m_RAppearance->delNodeRef( this );
|
2020-12-16 18:19:28 +00:00
|
|
|
m_RAppearance = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( m_RFaceSet )
|
|
|
|
{
|
|
|
|
m_RFaceSet->delNodeRef( this );
|
2020-12-16 18:19:28 +00:00
|
|
|
m_RFaceSet = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// delete objects
|
|
|
|
if( m_Appearance )
|
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
m_Appearance->SetParent( nullptr, false );
|
2015-12-08 07:31:57 +00:00
|
|
|
delete m_Appearance;
|
2020-12-16 18:19:28 +00:00
|
|
|
m_Appearance = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( m_FaceSet )
|
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
m_FaceSet->SetParent( nullptr, false );
|
2015-12-08 07:31:57 +00:00
|
|
|
delete m_FaceSet;
|
2020-12-16 18:19:28 +00:00
|
|
|
m_FaceSet = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SGSHAPE::SetParent( SGNODE* aParent, bool notify )
|
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr != m_Parent )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
if( aParent == m_Parent )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// handle the change in parents
|
|
|
|
if( notify )
|
|
|
|
m_Parent->unlinkChildNode( this );
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
m_Parent = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == aParent )
|
2015-12-08 07:31:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// only a SGTRANSFORM may be parent to a SGSHAPE
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr != aParent && S3D::SGTYPE_TRANSFORM != aParent->GetNodeType() )
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
m_Parent = aParent;
|
|
|
|
|
|
|
|
if( m_Parent )
|
|
|
|
m_Parent->AddChildNode( this );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
SGNODE* SGSHAPE::FindNode( const char* aNodeName, const SGNODE* aCaller )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == aNodeName || 0 == aNodeName[0] )
|
|
|
|
return nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
if( !m_Name.compare( aNodeName ) )
|
|
|
|
return this;
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
SGNODE* tmp = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr != m_Appearance )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
tmp = m_Appearance->FindNode( aNodeName, this );
|
|
|
|
|
|
|
|
if( tmp )
|
|
|
|
{
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr != m_FaceSet )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
tmp = m_FaceSet->FindNode( aNodeName, this );
|
|
|
|
|
|
|
|
if( tmp )
|
|
|
|
{
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// query the parent if appropriate
|
2020-12-16 18:19:28 +00:00
|
|
|
if( aCaller == m_Parent || nullptr == m_Parent )
|
|
|
|
return nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
return m_Parent->FindNode( aNodeName, this );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SGSHAPE::unlinkNode( const SGNODE* aNode, bool isChild )
|
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == aNode )
|
2015-12-08 07:31:57 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if( isChild )
|
|
|
|
{
|
|
|
|
if( aNode == m_Appearance )
|
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
m_Appearance = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( aNode == m_FaceSet )
|
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
m_FaceSet = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( aNode == m_RAppearance )
|
|
|
|
{
|
2016-03-14 01:30:59 +00:00
|
|
|
delNodeRef( this );
|
2020-12-16 18:19:28 +00:00
|
|
|
m_RAppearance = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( aNode == m_RFaceSet )
|
|
|
|
{
|
2016-03-14 01:30:59 +00:00
|
|
|
delNodeRef( this );
|
2020-12-16 18:19:28 +00:00
|
|
|
m_RFaceSet = nullptr;
|
2016-03-14 01:30:59 +00:00
|
|
|
return;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [BUG] unlinkNode() did not find its target" ),
|
2020-12-16 18:19:28 +00:00
|
|
|
__FILE__, __FUNCTION__, __LINE__ );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SGSHAPE::unlinkChildNode( const SGNODE* aNode )
|
|
|
|
{
|
|
|
|
unlinkNode( aNode, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SGSHAPE::unlinkRefNode( const SGNODE* aNode )
|
|
|
|
{
|
|
|
|
unlinkNode( aNode, false );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SGSHAPE::addNode( SGNODE* aNode, bool isChild )
|
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
wxCHECK( aNode, false );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
if( S3D::SGTYPE_APPEARANCE == aNode->GetNodeType() )
|
|
|
|
{
|
|
|
|
if( m_Appearance || m_RAppearance )
|
|
|
|
{
|
2016-01-08 05:23:42 +00:00
|
|
|
if( aNode != m_Appearance && aNode != m_RAppearance )
|
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [BUG] assigning multiple Appearance "
|
|
|
|
"nodes" ),
|
2020-12-16 18:19:28 +00:00
|
|
|
__FILE__, __FUNCTION__, __LINE__ );
|
2016-01-12 06:09:04 +00:00
|
|
|
|
2016-01-08 05:23:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( isChild )
|
|
|
|
{
|
|
|
|
m_Appearance = (SGAPPEARANCE*)aNode;
|
|
|
|
m_Appearance->SetParent( this );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_RAppearance = (SGAPPEARANCE*)aNode;
|
|
|
|
m_RAppearance->addNodeRef( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( S3D::SGTYPE_FACESET == aNode->GetNodeType() )
|
|
|
|
{
|
|
|
|
if( m_FaceSet || m_RFaceSet )
|
|
|
|
{
|
2016-01-08 05:23:42 +00:00
|
|
|
if( aNode != m_FaceSet && aNode != m_RFaceSet )
|
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [BUG] assigning multiple FaceSet nodes" ),
|
2020-12-16 18:19:28 +00:00
|
|
|
__FILE__, __FUNCTION__, __LINE__ );
|
2016-01-12 06:09:04 +00:00
|
|
|
|
2016-01-08 05:23:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( isChild )
|
|
|
|
{
|
|
|
|
m_FaceSet = (SGFACESET*)aNode;
|
|
|
|
m_FaceSet->SetParent( this );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_RFaceSet = (SGFACESET*)aNode;
|
|
|
|
m_RFaceSet->addNodeRef( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [BUG] object %s is not a valid type for this "
|
|
|
|
"object (%d)" ),
|
2020-12-16 18:19:28 +00:00
|
|
|
__FILE__, __FUNCTION__, __LINE__, aNode->GetName(), aNode->GetNodeType() );
|
2016-01-12 06:09:04 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SGSHAPE::AddRefNode( SGNODE* aNode )
|
|
|
|
{
|
|
|
|
return addNode( aNode, false );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SGSHAPE::AddChildNode( SGNODE* aNode )
|
|
|
|
{
|
|
|
|
return addNode( aNode, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SGSHAPE::ReNameNodes( void )
|
|
|
|
{
|
|
|
|
m_written = false;
|
|
|
|
|
|
|
|
// rename this node
|
|
|
|
m_Name.clear();
|
|
|
|
GetName();
|
|
|
|
|
|
|
|
// rename Appearance
|
|
|
|
if( m_Appearance )
|
|
|
|
m_Appearance->ReNameNodes();
|
|
|
|
|
|
|
|
// rename FaceSet
|
|
|
|
if( m_FaceSet )
|
|
|
|
m_FaceSet->ReNameNodes();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-02 22:53:49 +00:00
|
|
|
bool SGSHAPE::WriteVRML( std::ostream& aFile, bool aReuseFlag )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
if( !m_Appearance && !m_RAppearance && !m_FaceSet && !m_RFaceSet )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( aReuseFlag )
|
|
|
|
{
|
|
|
|
if( !m_written )
|
|
|
|
{
|
|
|
|
aFile << "DEF " << GetName() << " Shape {\n";
|
|
|
|
m_written = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
aFile << " USE " << GetName() << "\n";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
aFile << " Shape {\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if( m_Appearance )
|
|
|
|
m_Appearance->WriteVRML( aFile, aReuseFlag );
|
|
|
|
|
|
|
|
if( m_RAppearance )
|
|
|
|
m_RAppearance->WriteVRML( aFile, aReuseFlag );
|
|
|
|
|
|
|
|
if( m_FaceSet )
|
|
|
|
m_FaceSet->WriteVRML( aFile, aReuseFlag );
|
|
|
|
|
|
|
|
if( m_RFaceSet )
|
|
|
|
m_RFaceSet->WriteVRML( aFile, aReuseFlag );
|
|
|
|
|
|
|
|
aFile << "}\n";
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-02 22:53:49 +00:00
|
|
|
bool SGSHAPE::WriteCache( std::ostream& aFile, SGNODE* parentNode )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == parentNode )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
wxCHECK( m_Parent, false );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
SGNODE* np = m_Parent;
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
while( nullptr != np->GetParent() )
|
2015-12-08 07:31:57 +00:00
|
|
|
np = np->GetParent();
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
if( np->WriteCache( aFile, nullptr ) )
|
2016-03-14 01:30:59 +00:00
|
|
|
{
|
|
|
|
m_written = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
wxCHECK( parentNode == m_Parent, false );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
if( !aFile.good() )
|
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [BUG] bad stream" ),
|
|
|
|
__FILE__, __FUNCTION__, __LINE__ );
|
2016-01-12 06:09:04 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-14 01:30:59 +00:00
|
|
|
// check if any references are unwritten and swap parents if so
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr != m_RAppearance && !m_RAppearance->isWritten() )
|
2016-03-14 01:30:59 +00:00
|
|
|
m_RAppearance->SwapParent(this);
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr != m_RFaceSet && !m_RFaceSet->isWritten() )
|
2016-03-14 01:30:59 +00:00
|
|
|
m_RFaceSet->SwapParent( this );
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
aFile << "[" << GetName() << "]";
|
|
|
|
#define NITEMS 4
|
|
|
|
bool items[NITEMS];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for( i = 0; i < NITEMS; ++i )
|
|
|
|
items[i] = 0;
|
|
|
|
|
|
|
|
i = 0;
|
2020-12-16 18:19:28 +00:00
|
|
|
|
|
|
|
if( nullptr != m_Appearance )
|
2015-12-08 07:31:57 +00:00
|
|
|
items[i] = true;
|
|
|
|
|
|
|
|
++i;
|
2020-12-16 18:19:28 +00:00
|
|
|
|
|
|
|
if( nullptr != m_RAppearance )
|
2015-12-08 07:31:57 +00:00
|
|
|
items[i] = true;
|
|
|
|
|
|
|
|
++i;
|
2020-12-16 18:19:28 +00:00
|
|
|
|
|
|
|
if( nullptr != m_FaceSet )
|
2015-12-08 07:31:57 +00:00
|
|
|
items[i] = true;
|
|
|
|
|
|
|
|
++i;
|
2020-12-16 18:19:28 +00:00
|
|
|
|
|
|
|
if( nullptr != m_RFaceSet )
|
2015-12-08 07:31:57 +00:00
|
|
|
items[i] = true;
|
|
|
|
|
2016-06-01 12:22:05 +00:00
|
|
|
for( int jj = 0; jj < NITEMS; ++jj )
|
|
|
|
aFile.write( (char*)&items[jj], sizeof(bool) );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
if( items[0] )
|
|
|
|
m_Appearance->WriteCache( aFile, this );
|
|
|
|
|
|
|
|
if( items[1] )
|
|
|
|
aFile << "[" << m_RAppearance->GetName() << "]";
|
|
|
|
|
|
|
|
if( items[2] )
|
|
|
|
m_FaceSet->WriteCache( aFile, this );
|
|
|
|
|
|
|
|
if( items[3] )
|
|
|
|
aFile << "[" << m_RFaceSet->GetName() << "]";
|
|
|
|
|
|
|
|
if( aFile.fail() )
|
|
|
|
return false;
|
|
|
|
|
2016-03-14 01:30:59 +00:00
|
|
|
m_written = true;
|
2015-12-08 07:31:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-02 22:53:49 +00:00
|
|
|
bool SGSHAPE::ReadCache( std::istream& aFile, SGNODE* parentNode )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
wxCHECK( m_Appearance == nullptr && m_RAppearance == nullptr && m_FaceSet == nullptr &&
|
|
|
|
m_RFaceSet == nullptr, false );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
#define NITEMS 4
|
|
|
|
bool items[NITEMS];
|
|
|
|
|
|
|
|
for( int i = 0; i < NITEMS; ++i )
|
|
|
|
aFile.read( (char*)&items[i], sizeof(bool) );
|
|
|
|
|
|
|
|
if( ( items[0] && items[1] ) || ( items[2] && items[3] ) )
|
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [INFO] corrupt data; multiple item definitions "
|
|
|
|
"at position %ul" ),
|
|
|
|
__FILE__, __FUNCTION__, __LINE__,
|
|
|
|
static_cast<unsigned long>( aFile.tellg() ) );
|
2016-01-12 06:09:04 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string name;
|
|
|
|
|
|
|
|
if( items[0] )
|
|
|
|
{
|
|
|
|
if( S3D::SGTYPE_APPEARANCE != S3D::ReadTag( aFile, name ) )
|
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [INFO] corrupt data; bad child appearance "
|
|
|
|
"tag at position %ul" ),
|
2020-12-16 18:19:28 +00:00
|
|
|
__FILE__, __FUNCTION__, __LINE__,
|
|
|
|
static_cast<unsigned long>( aFile.tellg() ) );
|
2016-01-12 06:09:04 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Appearance = new SGAPPEARANCE( this );
|
|
|
|
m_Appearance->SetName( name.c_str() );
|
|
|
|
|
|
|
|
if( !m_Appearance->ReadCache( aFile, this ) )
|
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [INFO] corrupt data while reading appearance "
|
|
|
|
"'%s'" ),
|
2020-12-16 18:19:28 +00:00
|
|
|
__FILE__, __FUNCTION__, __LINE__, name );
|
2016-01-12 06:09:04 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( items[1] )
|
|
|
|
{
|
|
|
|
if( S3D::SGTYPE_APPEARANCE != S3D::ReadTag( aFile, name ) )
|
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [INFO] corrupt data; bad ref appearance tag "
|
|
|
|
"at position %ul" ),
|
2020-12-16 18:19:28 +00:00
|
|
|
__FILE__, __FUNCTION__, __LINE__,
|
|
|
|
static_cast<unsigned long>( aFile.tellg() ) );
|
2016-01-12 06:09:04 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SGNODE* np = FindNode( name.c_str(), this );
|
|
|
|
|
|
|
|
if( !np )
|
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [INFO] corrupt data: cannot find ref "
|
|
|
|
"appearance '%s'" ),
|
|
|
|
__FILE__, __FUNCTION__, __LINE__,
|
|
|
|
name );
|
2016-01-12 06:09:04 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( S3D::SGTYPE_APPEARANCE != np->GetNodeType() )
|
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [INFO] corrupt data: type is not "
|
|
|
|
"SGAPPEARANCE '%s'" ),
|
|
|
|
__FILE__, __FUNCTION__, __LINE__,
|
|
|
|
name );
|
2016-01-12 06:09:04 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_RAppearance = (SGAPPEARANCE*)np;
|
|
|
|
m_RAppearance->addNodeRef( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( items[2] )
|
|
|
|
{
|
|
|
|
if( S3D::SGTYPE_FACESET != S3D::ReadTag( aFile, name ) )
|
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [INFO] corrupt data; bad child face set tag "
|
|
|
|
"at position %ul" ),
|
2020-12-16 18:19:28 +00:00
|
|
|
__FILE__, __FUNCTION__, __LINE__,
|
|
|
|
static_cast<unsigned long>( aFile.tellg() ) );
|
2016-01-12 06:09:04 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_FaceSet = new SGFACESET( this );
|
|
|
|
m_FaceSet->SetName( name.c_str() );
|
|
|
|
|
|
|
|
if( !m_FaceSet->ReadCache( aFile, this ) )
|
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [INFO] corrupt data while reading face set "
|
|
|
|
"'%s'" ),
|
2020-12-16 18:19:28 +00:00
|
|
|
__FILE__, __FUNCTION__, __LINE__, name );
|
2016-01-12 06:09:04 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( items[3] )
|
|
|
|
{
|
|
|
|
if( S3D::SGTYPE_FACESET != S3D::ReadTag( aFile, name ) )
|
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [INFO] corrupt data; bad ref face set tag at "
|
|
|
|
"position %ul" ),
|
2020-12-16 18:19:28 +00:00
|
|
|
__FILE__, __FUNCTION__, __LINE__,
|
|
|
|
static_cast<unsigned long>( aFile.tellg() ) );
|
2016-01-12 06:09:04 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SGNODE* np = FindNode( name.c_str(), this );
|
|
|
|
|
|
|
|
if( !np )
|
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [INFO] corrupt data: cannot find ref face "
|
|
|
|
"set '%s'" ),
|
|
|
|
__FILE__, __FUNCTION__, __LINE__,
|
|
|
|
name );
|
2016-01-12 06:09:04 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( S3D::SGTYPE_FACESET != np->GetNodeType() )
|
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [INFO] corrupt data: type is not SGFACESET "
|
|
|
|
"'%s'" ),
|
|
|
|
__FILE__, __FUNCTION__, __LINE__,
|
|
|
|
name );
|
2016-01-12 06:09:04 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_RFaceSet = (SGFACESET*)np;
|
|
|
|
m_RFaceSet->addNodeRef( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( aFile.fail() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
bool SGSHAPE::Prepare( const glm::dmat4* aTransform, S3D::MATLIST& materials,
|
|
|
|
std::vector< SMESH >& meshes )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
SMESH m;
|
|
|
|
S3D::INIT_SMESH( m );
|
|
|
|
|
|
|
|
SGAPPEARANCE* pa = m_Appearance;
|
|
|
|
SGFACESET* pf = m_FaceSet;
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == pa )
|
2015-12-08 07:31:57 +00:00
|
|
|
pa = m_RAppearance;
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == pf )
|
2015-12-08 07:31:57 +00:00
|
|
|
pf = m_RFaceSet;
|
|
|
|
|
|
|
|
// no face sets = nothing to render, which is valid though pointless
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == pf )
|
2015-12-08 07:31:57 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if( !pf->validate() )
|
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [INFO] bad model; inconsistent data" ),
|
2020-12-16 18:19:28 +00:00
|
|
|
__FILE__, __FUNCTION__, __LINE__ );
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == pa )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
m.m_MaterialIdx = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
if( !S3D::GetMatIndex( materials, pa, idx ) )
|
|
|
|
{
|
|
|
|
m.m_MaterialIdx = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m.m_MaterialIdx = idx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SGCOLORS* pc = pf->m_Colors;
|
|
|
|
SGCOORDS* pv = pf->m_Coords;
|
|
|
|
SGCOORDINDEX* vidx = pf->m_CoordIndices;
|
|
|
|
SGNORMALS* pn = pf->m_Normals;
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == pc )
|
2015-12-08 07:31:57 +00:00
|
|
|
pc = pf->m_RColors;
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == pv )
|
2015-12-08 07:31:57 +00:00
|
|
|
pv = pf->m_RCoords;
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == pn )
|
2015-12-08 07:31:57 +00:00
|
|
|
pn = pf->m_RNormals;
|
|
|
|
|
|
|
|
// set the vertex points and indices
|
|
|
|
size_t nCoords = 0;
|
2020-12-16 18:19:28 +00:00
|
|
|
SGPOINT* pCoords = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
pv->GetCoordsList( nCoords, pCoords );
|
|
|
|
|
2016-01-06 04:34:14 +00:00
|
|
|
size_t nColors = 0;
|
2020-12-16 18:19:28 +00:00
|
|
|
SGCOLOR* pColors = nullptr;
|
2016-01-06 04:34:14 +00:00
|
|
|
|
|
|
|
if( pc )
|
|
|
|
{
|
|
|
|
// check the vertex colors
|
|
|
|
pc->GetColorList( nColors, pColors );
|
|
|
|
|
|
|
|
if( nColors < nCoords )
|
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [INFO] bad model; not enough colors per "
|
|
|
|
"vertex (%ul vs %ul)" ),
|
2020-12-16 18:19:28 +00:00
|
|
|
__FILE__, __FUNCTION__, __LINE__, static_cast<unsigned long>( nColors ),
|
|
|
|
static_cast<unsigned long>( nCoords ) );
|
|
|
|
|
2016-01-06 04:34:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
// set the vertex indices
|
|
|
|
size_t nvidx = 0;
|
2020-12-16 18:19:28 +00:00
|
|
|
int* lv = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
vidx->GetIndices( nvidx, lv );
|
|
|
|
|
|
|
|
// note: reduce the vertex set to include only the referenced vertices
|
|
|
|
std::vector< int > vertices; // store the list of temp vertex indices
|
|
|
|
std::map< int, unsigned int > indexmap; // map temp vertex to true vertex
|
|
|
|
std::map< int, unsigned int >::iterator mit;
|
|
|
|
|
|
|
|
for( unsigned int i = 0; i < nvidx; ++i )
|
|
|
|
{
|
|
|
|
mit = indexmap.find( lv[i] );
|
|
|
|
|
|
|
|
if( mit == indexmap.end() )
|
|
|
|
{
|
|
|
|
indexmap.insert( std::pair< int, unsigned int >( lv[i], vertices.size() ) );
|
|
|
|
vertices.push_back( lv[i] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( vertices.size() < 3 )
|
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [INFO] bad model; not enough vertices" ),
|
2020-12-16 18:19:28 +00:00
|
|
|
__FILE__, __FUNCTION__, __LINE__ );
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-06 04:34:14 +00:00
|
|
|
// construct the final vertex/color list
|
2020-12-16 18:19:28 +00:00
|
|
|
SFVEC3F* lColors = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
SFVEC3F* lCoords = new SFVEC3F[ vertices.size() ];
|
2016-03-10 22:37:39 +00:00
|
|
|
int ti;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2016-01-06 04:34:14 +00:00
|
|
|
if( pc )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2016-01-06 05:15:54 +00:00
|
|
|
lColors = new SFVEC3F[vertices.size()];
|
2016-01-06 04:34:14 +00:00
|
|
|
m.m_Color = lColors;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( pc )
|
|
|
|
{
|
|
|
|
for( size_t i = 0; i < vertices.size(); ++i )
|
|
|
|
{
|
|
|
|
ti = vertices[i];
|
|
|
|
glm::dvec4 pt( pCoords[ti].x, pCoords[ti].y, pCoords[ti].z, 1.0 );
|
|
|
|
pt = (*aTransform) * pt;
|
|
|
|
pColors[ti].GetColor( lColors[i].x, lColors[i].y, lColors[i].z );
|
|
|
|
lCoords[i] = SFVEC3F( pt.x, pt.y, pt.z );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( size_t i = 0; i < vertices.size(); ++i )
|
|
|
|
{
|
|
|
|
ti = vertices[i];
|
|
|
|
glm::dvec4 pt( pCoords[ti].x, pCoords[ti].y, pCoords[ti].z, 1.0 );
|
|
|
|
pt = (*aTransform) * pt;
|
|
|
|
lCoords[i] = SFVEC3F( pt.x, pt.y, pt.z );
|
|
|
|
}
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m.m_VertexSize = (unsigned int) vertices.size();
|
|
|
|
m.m_Positions = lCoords;
|
|
|
|
unsigned int* lvidx = new unsigned int[ nvidx ];
|
|
|
|
|
|
|
|
for( unsigned int i = 0; i < nvidx; ++i )
|
|
|
|
{
|
|
|
|
mit = indexmap.find( lv[i] );
|
|
|
|
lvidx[i] = mit->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
m.m_FaceIdxSize = (unsigned int )nvidx;
|
|
|
|
m.m_FaceIdx = lvidx;
|
|
|
|
|
|
|
|
// set the per-vertex normals
|
|
|
|
size_t nNorms = 0;
|
2020-12-16 18:19:28 +00:00
|
|
|
SGVECTOR* pNorms = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
double x, y, z;
|
|
|
|
|
|
|
|
pn->GetNormalList( nNorms, pNorms );
|
|
|
|
SFVEC3F* lNorms = new SFVEC3F[ vertices.size() ];
|
|
|
|
|
|
|
|
for( size_t i = 0; i < vertices.size(); ++i )
|
|
|
|
{
|
|
|
|
ti = vertices[i];
|
|
|
|
pNorms[ti].GetVector( x, y, z );
|
|
|
|
glm::dvec4 pt( x, y, z, 0.0 );
|
|
|
|
pt = (*aTransform) * pt;
|
|
|
|
|
|
|
|
lNorms[i] = SFVEC3F( pt.x, pt.y, pt.z );
|
|
|
|
}
|
|
|
|
|
|
|
|
m.m_Normals = lNorms;
|
|
|
|
meshes.push_back( m );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|