KiCad PCB EDA Suite
Loading...
Searching...
No Matches
x3d_appearance.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) 2016 Cirilo Bernardo <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21
22#include <iostream>
23#include <sstream>
24#include <wx/log.h>
25#include <wx/xml/xml.h>
26#include "x3d_ops.h"
27#include "x3d_appearance.h"
29
30
36
37
39{
41 init();
42
43 if( nullptr != aParent )
44 {
45 X3DNODES ptype = aParent->GetNodeType();
46
47 if( X3D_SHAPE == ptype )
48 m_Parent = aParent;
49 }
50
51 if( nullptr != m_Parent )
52 m_Parent->AddChildNode( this );
53}
54
55
57{
58 wxLogTrace( traceVrmlPlugin, wxT( " * [INFO] Destroying Appearance" ) );
59
60 if( !m_MatName.empty() && m_Dict )
61 m_Dict->DelName( m_MatName, this );
62}
63
64
66{
67 // default material values as per VRML2 spec
68 diffuseColor.x = 0.8f;
69 diffuseColor.y = 0.8f;
70 diffuseColor.z = 0.8f;
71
72 emissiveColor.x = 0.0f;
73 emissiveColor.y = 0.0f;
74 emissiveColor.z = 0.0f;
75
77
78 ambientIntensity = 0.2f;
79 shininess = 0.2f;
80 transparency = 0.0f;
81
82 return;
83}
84
85
86void X3DAPP::readFields( wxXmlNode* aNode )
87{
88 // DEF
89 // diffuseColor
90 // emissiveColor
91 // specularColor
92 // ambientIntensity
93 // shininess
94 // transparency
95
96 wxXmlAttribute* prop;
97
98 for( prop = aNode->GetAttributes(); prop != nullptr; prop = prop->GetNext() )
99 {
100 const wxString& pname = prop->GetName();
101
102 if( pname == wxT( "DEF" ) )
103 {
104 m_MatName = prop->GetValue();
105 m_Dict->AddName( m_MatName, this );
106 }
107 else if( pname == wxT( "USE" ) )
108 {
109 X3DNODE* np = m_Dict->FindName( prop->GetValue() );
110
111 if( nullptr != np && np->GetNodeType() == X3D_APPEARANCE )
112 {
113 X3DAPP* ap = (X3DAPP*) np;
118 shininess = ap->shininess;
120 }
121 }
122 else if( pname == wxT( "diffuseColor" ) )
123 {
124 X3D::ParseSFVec3( prop->GetValue(), diffuseColor );
125 }
126 else if( pname == wxT( "emissiveColor" ) )
127 {
128 X3D::ParseSFVec3( prop->GetValue(), emissiveColor );
129 }
130 else if( pname == wxT( "specularColor" ) )
131 {
132 X3D::ParseSFVec3( prop->GetValue(), specularColor );
133 }
134 else if( pname == wxT( "ambientIntensity" ) )
135 {
136 X3D::ParseSFFloat( prop->GetValue(), ambientIntensity );
137 }
138 else if( pname == wxT( "shininess" ) )
139 {
140 X3D::ParseSFFloat( prop->GetValue(), shininess );
141 }
142 else if( pname == wxT( "transparency" ) )
143 {
144 X3D::ParseSFFloat( prop->GetValue(), transparency );
145 }
146 }
147}
148
149
150bool X3DAPP::Read( wxXmlNode* aNode, X3DNODE* aTopNode, X3D_DICT& aDict )
151{
152 if( nullptr == aTopNode || nullptr == aNode )
153 return false;
154
155 m_Dict = &aDict;
156 wxXmlAttribute* prop;
157
158 for( prop = aNode->GetAttributes(); prop != nullptr; prop = prop->GetNext() )
159 {
160 const wxString& pname = prop->GetName();
161
162 if( pname == wxT( "DEF" ) )
163 {
164 m_Name = prop->GetValue();
165 m_Dict->AddName( m_Name, this );
166 }
167 }
168
169 wxXmlNode* pmat = nullptr;
170
171 for( wxXmlNode* child = aNode->GetChildren(); child != nullptr; child = child->GetNext() )
172 {
173 if( child->GetName() == wxT( "Material" ) )
174 pmat = child;
175
176 }
177
178 if( nullptr == pmat )
179 return false;
180
181 readFields( pmat );
182
183 if( !SetParent( aTopNode ) )
184 return false;
185
186 return true;
187}
188
189
190bool X3DAPP::SetParent( X3DNODE* aParent, bool doUnlink )
191{
192 if( aParent == m_Parent )
193 return true;
194
195 if( nullptr != aParent )
196 {
197 X3DNODES nt = aParent->GetNodeType();
198
199 if( nt != X3D_SHAPE )
200 return false;
201 }
202
203 if( nullptr != m_Parent && doUnlink )
204 m_Parent->unlinkChildNode( this );
205
206 m_Parent = aParent;
207
208 if( nullptr != m_Parent )
209 m_Parent->AddChildNode( this );
210
211 return true;
212}
213
214
216{
217 return false;
218}
219
220
222{
223 return false;
224}
225
226
228{
229 S3D::SGTYPES ptype = S3D::GetSGNodeType( aParent );
230
231 if( nullptr != aParent && ptype != S3D::SGTYPE_SHAPE )
232 {
233 wxLogTrace( traceVrmlPlugin,
234 wxT( " * [BUG] Appearance does not have a Shape parent (parent ID: %d)" ),
235 ptype );
236
237 return nullptr;
238 }
239
240 wxLogTrace( traceVrmlPlugin,
241 wxT( " * [INFO] Translating Appearance node with %zu children, %zu"
242 "references, and %zu back pointers." ),
243 m_Children.size(), m_Refs.size(), m_BackPointers.size() );
244
245 if( m_sgNode )
246 {
247 if( nullptr != aParent )
248 {
249 if( nullptr == S3D::GetSGNodeParent( m_sgNode )
250 && !S3D::AddSGNodeChild( aParent, m_sgNode ) )
251 {
252 return nullptr;
253 }
254 else if( aParent != S3D::GetSGNodeParent( m_sgNode )
255 && !S3D::AddSGNodeRef( aParent, m_sgNode ) )
256 {
257 return nullptr;
258 }
259 }
260
261 return m_sgNode;
262 }
263
264 IFSG_APPEARANCE matNode( aParent );
268 float ambr = ambientIntensity * diffuseColor.x;
269 float ambg = ambientIntensity * diffuseColor.y;
270 float ambb = ambientIntensity * diffuseColor.z;
271 matNode.SetAmbient( ambr, ambg, ambb );
272 matNode.SetShininess( shininess );
273 matNode.SetTransparency( transparency );
274 m_sgNode = matNode.GetRawPtr();
275
276 return m_sgNode;
277}
bool SetDiffuse(float aRVal, float aGVal, float aBVal)
bool SetEmissive(float aRVal, float aGVal, float aBVal)
bool SetSpecular(float aRVal, float aGVal, float aBVal)
bool SetShininess(float aShininess) noexcept
bool SetAmbient(float aRVal, float aGVal, float aBVal)
bool SetTransparency(float aTransparency) noexcept
SGNODE * GetRawPtr(void) noexcept
Return the raw internal SGNODE pointer.
Definition ifsg_node.cpp:61
The base class of all Scene Graph nodes.
Definition sg_node.h:71
float ambientIntensity
float transparency
float shininess
WRLVEC3F specularColor
void readFields(wxXmlNode *aNode)
wxString m_MatName
bool SetParent(X3DNODE *aParent, bool doUnlink=true) override
Set the parent X3DNODE of this object.
WRLVEC3F diffuseColor
WRLVEC3F emissiveColor
void init()
bool AddRefNode(X3DNODE *aNode) override
bool Read(wxXmlNode *aNode, X3DNODE *aTopNode, X3D_DICT &aDict) override
virtual ~X3DAPP()
bool AddChildNode(X3DNODE *aNode) override
SGNODE * TranslateToSG(SGNODE *aParent) override
Produce a representation of the data using the intermediate scenegraph structures of the kicad_3dsg l...
X3DNODES m_Type
Definition x3d_base.h:156
std::list< X3DNODE * > m_Children
Definition x3d_base.h:160
SGNODE * m_sgNode
Definition x3d_base.h:165
std::list< X3DNODE * > m_BackPointers
Definition x3d_base.h:159
X3DNODE * m_Parent
Definition x3d_base.h:155
std::list< X3DNODE * > m_Refs
Definition x3d_base.h:161
X3DNODES GetNodeType(void) const
Return the type of this node instance.
Definition x3d_base.cpp:180
wxString m_Name
Definition x3d_base.h:164
X3D_DICT * m_Dict
Definition x3d_base.h:157
const wxChar *const traceVrmlPlugin
Flag to enable VRML plugin trace output.
Definition vrml.cpp:59
collects header files for all SG* wrappers and the API
SGLIB_API S3D::SGTYPES GetSGNodeType(SGNODE *aNode)
Definition ifsg_api.cpp:481
SGLIB_API SGNODE * GetSGNodeParent(SGNODE *aNode)
Definition ifsg_api.cpp:490
SGTYPES
Definition sg_types.h:32
@ SGTYPE_SHAPE
Definition sg_types.h:41
SGLIB_API bool AddSGNodeChild(SGNODE *aParent, SGNODE *aChild)
Definition ifsg_api.cpp:508
SGLIB_API bool AddSGNodeRef(SGNODE *aParent, SGNODE *aChild)
Definition ifsg_api.cpp:499
bool ParseSFVec3(const wxString &aSource, WRLVEC3F &aResult)
Definition x3d_ops.cpp:260
bool ParseSFFloat(const wxString &aSource, float &aResult)
Definition x3d_ops.cpp:248
X3DNODES
Definition x3d_base.h:56
@ X3D_APPEARANCE
Definition x3d_base.h:60
@ X3D_SHAPE
Definition x3d_base.h:59