2016-02-05 04:32:52 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 Cirilo Bernardo <cirilo.bernardo@gmail.com>
|
2022-01-12 23:55:18 +00:00
|
|
|
* Copyright (C) 2021-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2016-02-05 04:32:52 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
2016-02-23 00:20:34 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <wx/log.h>
|
2016-02-05 04:32:52 +00:00
|
|
|
#include <wx/xml/xml.h>
|
|
|
|
#include "x3d_ops.h"
|
|
|
|
#include "x3d_appearance.h"
|
2016-02-05 05:08:30 +00:00
|
|
|
#include "plugins/3dapi/ifsg_all.h"
|
2016-02-05 04:32:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
X3DAPP::X3DAPP() : X3DNODE()
|
|
|
|
{
|
|
|
|
m_Type = X3D_APPEARANCE;
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
X3DAPP::X3DAPP( X3DNODE* aParent ) : X3DNODE()
|
|
|
|
{
|
|
|
|
m_Type = X3D_APPEARANCE;
|
|
|
|
init();
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr != aParent )
|
2016-02-05 04:32:52 +00:00
|
|
|
{
|
|
|
|
X3DNODES ptype = aParent->GetNodeType();
|
|
|
|
|
|
|
|
if( X3D_SHAPE == ptype )
|
|
|
|
m_Parent = aParent;
|
|
|
|
}
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr != m_Parent )
|
2016-02-05 04:32:52 +00:00
|
|
|
m_Parent->AddChildNode( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
X3DAPP::~X3DAPP()
|
|
|
|
{
|
2022-02-05 13:25:43 +00:00
|
|
|
wxLogTrace( traceVrmlPlugin, wxT( " * [INFO] Destroying Appearance" ) );
|
2016-02-05 04:32:52 +00:00
|
|
|
|
|
|
|
if( !m_MatName.empty() && m_Dict )
|
|
|
|
m_Dict->DelName( m_MatName, this );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void X3DAPP::init()
|
|
|
|
{
|
|
|
|
// default material values as per VRML2 spec
|
2019-12-12 21:51:21 +00:00
|
|
|
diffuseColor.x = 0.8f;
|
|
|
|
diffuseColor.y = 0.8f;
|
|
|
|
diffuseColor.z = 0.8f;
|
2016-02-05 04:32:52 +00:00
|
|
|
|
2019-12-12 21:51:21 +00:00
|
|
|
emissiveColor.x = 0.0f;
|
|
|
|
emissiveColor.y = 0.0f;
|
|
|
|
emissiveColor.z = 0.0f;
|
2016-02-05 04:32:52 +00:00
|
|
|
|
|
|
|
specularColor = emissiveColor;
|
|
|
|
|
2019-12-12 21:51:21 +00:00
|
|
|
ambientIntensity = 0.2f;
|
|
|
|
shininess = 0.2f;
|
|
|
|
transparency = 0.0f;
|
2016-02-05 04:32:52 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void X3DAPP::readFields( wxXmlNode* aNode )
|
|
|
|
{
|
|
|
|
// DEF
|
|
|
|
// diffuseColor
|
|
|
|
// emissiveColor
|
|
|
|
// specularColor
|
|
|
|
// ambientIntensity
|
|
|
|
// shininess
|
|
|
|
// transparency
|
|
|
|
|
|
|
|
wxXmlAttribute* prop;
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
for( prop = aNode->GetAttributes(); prop != nullptr; prop = prop->GetNext() )
|
2016-02-05 04:32:52 +00:00
|
|
|
{
|
2017-09-22 17:11:11 +00:00
|
|
|
const wxString& pname = prop->GetName();
|
2016-02-05 04:32:52 +00:00
|
|
|
|
2022-02-05 13:25:43 +00:00
|
|
|
if( pname == wxT( "DEF" ) )
|
2016-02-05 04:32:52 +00:00
|
|
|
{
|
|
|
|
m_MatName = prop->GetValue();
|
|
|
|
m_Dict->AddName( m_MatName, this );
|
|
|
|
}
|
2022-02-05 13:25:43 +00:00
|
|
|
else if( pname == wxT( "USE" ) )
|
2016-02-05 04:32:52 +00:00
|
|
|
{
|
|
|
|
X3DNODE* np = m_Dict->FindName( prop->GetValue() );
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr != np && np->GetNodeType() == X3D_APPEARANCE )
|
2016-02-05 04:32:52 +00:00
|
|
|
{
|
|
|
|
X3DAPP* ap = (X3DAPP*) np;
|
|
|
|
diffuseColor = ap->diffuseColor;
|
|
|
|
emissiveColor = ap->emissiveColor;
|
|
|
|
specularColor = ap->specularColor;
|
|
|
|
ambientIntensity = ap->ambientIntensity;
|
|
|
|
shininess = ap->shininess;
|
|
|
|
transparency = ap->transparency;
|
|
|
|
}
|
|
|
|
}
|
2022-02-05 13:25:43 +00:00
|
|
|
else if( pname == wxT( "diffuseColor" ) )
|
2021-07-18 14:06:48 +00:00
|
|
|
{
|
2016-02-05 04:32:52 +00:00
|
|
|
X3D::ParseSFVec3( prop->GetValue(), diffuseColor );
|
2021-07-18 14:06:48 +00:00
|
|
|
}
|
2022-02-05 13:25:43 +00:00
|
|
|
else if( pname == wxT( "emissiveColor" ) )
|
2021-07-18 14:06:48 +00:00
|
|
|
{
|
2016-02-05 04:32:52 +00:00
|
|
|
X3D::ParseSFVec3( prop->GetValue(), emissiveColor );
|
2021-07-18 14:06:48 +00:00
|
|
|
}
|
2022-02-05 13:25:43 +00:00
|
|
|
else if( pname == wxT( "specularColor" ) )
|
2021-07-18 14:06:48 +00:00
|
|
|
{
|
2016-02-05 04:32:52 +00:00
|
|
|
X3D::ParseSFVec3( prop->GetValue(), specularColor );
|
2021-07-18 14:06:48 +00:00
|
|
|
}
|
2022-02-05 13:25:43 +00:00
|
|
|
else if( pname == wxT( "ambientIntensity" ) )
|
2021-07-18 14:06:48 +00:00
|
|
|
{
|
2016-02-05 04:32:52 +00:00
|
|
|
X3D::ParseSFFloat( prop->GetValue(), ambientIntensity );
|
2021-07-18 14:06:48 +00:00
|
|
|
}
|
2022-02-05 13:25:43 +00:00
|
|
|
else if( pname == wxT( "shininess" ) )
|
2021-07-18 14:06:48 +00:00
|
|
|
{
|
2016-02-05 04:32:52 +00:00
|
|
|
X3D::ParseSFFloat( prop->GetValue(), shininess );
|
2021-07-18 14:06:48 +00:00
|
|
|
}
|
2022-02-05 13:25:43 +00:00
|
|
|
else if( pname == wxT( "transparency" ) )
|
2021-07-18 14:06:48 +00:00
|
|
|
{
|
2016-02-05 04:32:52 +00:00
|
|
|
X3D::ParseSFFloat( prop->GetValue(), transparency );
|
2021-07-18 14:06:48 +00:00
|
|
|
}
|
2016-02-05 04:32:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool X3DAPP::Read( wxXmlNode* aNode, X3DNODE* aTopNode, X3D_DICT& aDict )
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr == aTopNode || nullptr == aNode )
|
2016-02-05 04:32:52 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
m_Dict = &aDict;
|
|
|
|
wxXmlAttribute* prop;
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
for( prop = aNode->GetAttributes(); prop != nullptr; prop = prop->GetNext() )
|
2016-02-05 04:32:52 +00:00
|
|
|
{
|
2017-09-22 17:11:11 +00:00
|
|
|
const wxString& pname = prop->GetName();
|
2016-02-05 04:32:52 +00:00
|
|
|
|
2022-02-05 13:25:43 +00:00
|
|
|
if( pname == wxT( "DEF" ) )
|
2016-02-05 04:32:52 +00:00
|
|
|
{
|
|
|
|
m_Name = prop->GetValue();
|
|
|
|
m_Dict->AddName( m_Name, this );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
wxXmlNode* pmat = nullptr;
|
2016-02-05 04:32:52 +00:00
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
for( wxXmlNode* child = aNode->GetChildren(); child != nullptr; child = child->GetNext() )
|
2016-02-05 04:32:52 +00:00
|
|
|
{
|
2022-02-05 13:25:43 +00:00
|
|
|
if( child->GetName() == wxT( "Material" ) )
|
2016-02-05 04:32:52 +00:00
|
|
|
pmat = child;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr == pmat )
|
2016-02-05 04:32:52 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
readFields( pmat );
|
|
|
|
|
|
|
|
if( !SetParent( aTopNode ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool X3DAPP::SetParent( X3DNODE* aParent, bool doUnlink )
|
|
|
|
{
|
|
|
|
if( aParent == m_Parent )
|
|
|
|
return true;
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr != aParent )
|
2016-02-05 04:32:52 +00:00
|
|
|
{
|
|
|
|
X3DNODES nt = aParent->GetNodeType();
|
|
|
|
|
|
|
|
if( nt != X3D_SHAPE )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr != m_Parent && doUnlink )
|
2016-02-05 04:32:52 +00:00
|
|
|
m_Parent->unlinkChildNode( this );
|
|
|
|
|
|
|
|
m_Parent = aParent;
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr != m_Parent )
|
2016-02-05 04:32:52 +00:00
|
|
|
m_Parent->AddChildNode( this );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
bool X3DAPP::AddChildNode( X3DNODE* aNode )
|
2016-02-05 04:32:52 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool X3DAPP::AddRefNode( X3DNODE* aNode )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SGNODE* X3DAPP::TranslateToSG( SGNODE* aParent )
|
|
|
|
{
|
2016-02-05 05:08:30 +00:00
|
|
|
S3D::SGTYPES ptype = S3D::GetSGNodeType( aParent );
|
|
|
|
|
2022-01-12 23:55:18 +00:00
|
|
|
if( nullptr != aParent && ptype != S3D::SGTYPE_SHAPE )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceVrmlPlugin,
|
|
|
|
wxT( " * [BUG] Appearance does not have a Shape parent (parent ID: %d)" ),
|
|
|
|
ptype );
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-02-05 05:08:30 +00:00
|
|
|
|
2021-07-24 17:27:35 +00:00
|
|
|
wxLogTrace( traceVrmlPlugin,
|
2021-07-28 01:06:24 +00:00
|
|
|
wxT( " * [INFO] Translating Appearance node with %zu children, %zu"
|
|
|
|
"references, and %zu back pointers." ),
|
2021-07-24 17:27:35 +00:00
|
|
|
m_Children.size(), m_Refs.size(), m_BackPointers.size() );
|
2016-02-05 05:08:30 +00:00
|
|
|
|
|
|
|
if( m_sgNode )
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr != aParent )
|
2016-02-05 05:08:30 +00:00
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr == S3D::GetSGNodeParent( m_sgNode )
|
2016-02-05 05:08:30 +00:00
|
|
|
&& !S3D::AddSGNodeChild( aParent, m_sgNode ) )
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
return nullptr;
|
2016-02-05 05:08:30 +00:00
|
|
|
}
|
|
|
|
else if( aParent != S3D::GetSGNodeParent( m_sgNode )
|
|
|
|
&& !S3D::AddSGNodeRef( aParent, m_sgNode ) )
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
return nullptr;
|
2016-02-05 05:08:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_sgNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
IFSG_APPEARANCE matNode( aParent );
|
|
|
|
matNode.SetEmissive( emissiveColor.x, emissiveColor.y, emissiveColor.z );
|
|
|
|
matNode.SetSpecular( specularColor.x, specularColor.y, specularColor.z );
|
|
|
|
matNode.SetDiffuse( diffuseColor.x, diffuseColor.y, diffuseColor.z );
|
2016-02-06 21:41:27 +00:00
|
|
|
float ambr = ambientIntensity * diffuseColor.x;
|
|
|
|
float ambg = ambientIntensity * diffuseColor.y;
|
|
|
|
float ambb = ambientIntensity * diffuseColor.z;
|
|
|
|
matNode.SetAmbient( ambr, ambg, ambb );
|
2016-02-05 05:08:30 +00:00
|
|
|
matNode.SetShininess( shininess );
|
|
|
|
matNode.SetTransparency( transparency );
|
|
|
|
m_sgNode = matNode.GetRawPtr();
|
|
|
|
|
|
|
|
return m_sgNode;
|
2016-02-05 04:32:52 +00:00
|
|
|
}
|