KiCad PCB EDA Suite
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 <cirilo.bernardo@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 
25 #include <iostream>
26 #include <sstream>
27 #include <wx/log.h>
28 #include <wx/xml/xml.h>
29 #include "x3d_ops.h"
30 #include "x3d_appearance.h"
31 #include "plugins/3dapi/ifsg_all.h"
32 
33 
35 {
37  init();
38 
39  return;
40 }
41 
42 
44 {
46  init();
47 
48  if( NULL != aParent )
49  {
50  X3DNODES ptype = aParent->GetNodeType();
51 
52  if( X3D_SHAPE == ptype )
53  m_Parent = aParent;
54  }
55 
56  if( NULL != m_Parent )
57  m_Parent->AddChildNode( this );
58 
59  return;
60 }
61 
62 
64 {
65  #if defined( DEBUG_X3D ) && ( DEBUG_X3D > 2 )
66  wxLogTrace( MASK_VRML, " * [INFO] Destroying Appearance\n" );
67  #endif
68 
69  if( !m_MatName.empty() && m_Dict )
70  m_Dict->DelName( m_MatName, this );
71 
72  return;
73 }
74 
75 
77 {
78  // default material values as per VRML2 spec
79  diffuseColor.x = 0.8f;
80  diffuseColor.y = 0.8f;
81  diffuseColor.z = 0.8f;
82 
83  emissiveColor.x = 0.0f;
84  emissiveColor.y = 0.0f;
85  emissiveColor.z = 0.0f;
86 
88 
89  ambientIntensity = 0.2f;
90  shininess = 0.2f;
91  transparency = 0.0f;
92 
93  return;
94 }
95 
96 
97 void X3DAPP::readFields( wxXmlNode* aNode )
98 {
99  // DEF
100  // diffuseColor
101  // emissiveColor
102  // specularColor
103  // ambientIntensity
104  // shininess
105  // transparency
106 
107  wxXmlAttribute* prop;
108 
109  for( prop = aNode->GetAttributes();
110  prop != NULL;
111  prop = prop->GetNext() )
112  {
113  const wxString& pname = prop->GetName();
114 
115  if( pname == "DEF" )
116  {
117  m_MatName = prop->GetValue();
118  m_Dict->AddName( m_MatName, this );
119  }
120  else if( pname == "USE" )
121  {
122  X3DNODE* np = m_Dict->FindName( prop->GetValue() );
123 
124  if( NULL != np && np->GetNodeType() == X3D_APPEARANCE )
125  {
126  X3DAPP* ap = (X3DAPP*) np;
131  shininess = ap->shininess;
133  }
134  }
135  else if( pname == "diffuseColor" )
136  X3D::ParseSFVec3( prop->GetValue(), diffuseColor );
137  else if( pname == "emissiveColor" )
138  X3D::ParseSFVec3( prop->GetValue(), emissiveColor );
139  else if( pname == "specularColor" )
140  X3D::ParseSFVec3( prop->GetValue(), specularColor );
141  else if( pname == "ambientIntensity" )
142  X3D::ParseSFFloat( prop->GetValue(), ambientIntensity );
143  else if( pname == "shininess" )
144  X3D::ParseSFFloat( prop->GetValue(), shininess );
145  else if( pname == "transparency" )
146  X3D::ParseSFFloat( prop->GetValue(), transparency );
147 
148  }
149 
150  return;
151 }
152 
153 
154 bool X3DAPP::Read( wxXmlNode* aNode, X3DNODE* aTopNode, X3D_DICT& aDict )
155 {
156  if( NULL == aTopNode || NULL == aNode )
157  return false;
158 
159  m_Dict = &aDict;
160  wxXmlAttribute* prop;
161 
162  for( prop = aNode->GetAttributes();
163  prop != NULL;
164  prop = prop->GetNext() )
165  {
166  const wxString& pname = prop->GetName();
167 
168  if( pname == "DEF" )
169  {
170  m_Name = prop->GetValue();
171  m_Dict->AddName( m_Name, this );
172  }
173  }
174 
175  wxXmlNode* pmat = NULL;
176 
177  for( wxXmlNode* child = aNode->GetChildren();
178  child != NULL;
179  child = child->GetNext() )
180  {
181  if( child->GetName() == "Material" )
182  pmat = child;
183 
184  }
185 
186  if( NULL == pmat )
187  return false;
188 
189  readFields( pmat );
190 
191  if( !SetParent( aTopNode ) )
192  return false;
193 
194  return true;
195 }
196 
197 
198 bool X3DAPP::SetParent( X3DNODE* aParent, bool doUnlink )
199 {
200  if( aParent == m_Parent )
201  return true;
202 
203  if( NULL != aParent )
204  {
205  X3DNODES nt = aParent->GetNodeType();
206 
207  if( nt != X3D_SHAPE )
208  return false;
209  }
210 
211  if( NULL != m_Parent && doUnlink )
212  m_Parent->unlinkChildNode( this );
213 
214  m_Parent = aParent;
215 
216  if( NULL != m_Parent )
217  m_Parent->AddChildNode( this );
218 
219  return true;
220 }
221 
222 
224 {
225  return false;
226 }
227 
228 
230 {
231  return false;
232 }
233 
234 
236 {
237  S3D::SGTYPES ptype = S3D::GetSGNodeType( aParent );
238 
239  if( NULL != aParent && ptype != S3D::SGTYPE_SHAPE )
240  {
241  #ifdef DEBUG_X3D
242  std::ostringstream ostr;
243  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
244  ostr << " * [BUG] Appearance does not have a Shape parent (parent ID: ";
245  ostr << ptype << ")";
246  wxLogTrace( MASK_VRML, "%s\n", ostr.str().c_str() );
247  #endif
248 
249  return NULL;
250  }
251 
252  #if defined( DEBUG_X3D ) && ( DEBUG_X3D > 2 )
253  do {
254  std::ostringstream ostr;
255  ostr << " * [INFO] Translating Appearance with " << m_Children.size();
256  ostr << " children, " << m_Refs.size() << " references and ";
257  ostr << m_BackPointers.size() << " backpointers";
258  wxLogTrace( MASK_VRML, "%s\n", ostr.str().c_str() );
259  } while( 0 );
260  #endif
261 
262  if( m_sgNode )
263  {
264  if( NULL != aParent )
265  {
267  && !S3D::AddSGNodeChild( aParent, m_sgNode ) )
268  {
269  return NULL;
270  }
271  else if( aParent != S3D::GetSGNodeParent( m_sgNode )
272  && !S3D::AddSGNodeRef( aParent, m_sgNode ) )
273  {
274  return NULL;
275  }
276  }
277 
278  return m_sgNode;
279  }
280 
281  IFSG_APPEARANCE matNode( aParent );
285  float ambr = ambientIntensity * diffuseColor.x;
286  float ambg = ambientIntensity * diffuseColor.y;
287  float ambb = ambientIntensity * diffuseColor.z;
288  matNode.SetAmbient( ambr, ambg, ambb );
289  matNode.SetShininess( shininess );
290  matNode.SetTransparency( transparency );
291  m_sgNode = matNode.GetRawPtr();
292 
293  return m_sgNode;
294 }
void init()
bool AddChildNode(X3DNODE *aNode) override
float ambientIntensity
std::list< X3DNODE * > m_BackPointers
Definition: x3d_base.h:82
bool SetTransparency(float aTransparency) noexcept
#define MASK_VRML
Definition: wrltypes.h:37
X3DNODE * FindName(const wxString &aName)
Definition: x3d_base.cpp:67
bool SetDiffuse(float aRVal, float aGVal, float aBVal)
X3DNODES m_Type
Definition: x3d_base.h:79
bool DelName(const wxString &aName, X3DNODE *aNode)
Definition: x3d_base.cpp:50
wxString m_Name
Definition: x3d_base.h:87
SGLIB_API SGNODE * GetSGNodeParent(SGNODE *aNode)
Definition: ifsg_api.cpp:636
SGNODE represents the base class of all Scene Graph nodes.
Definition: sg_node.h:76
collects header files for all SG* wrappers and the API
WRLVEC3F specularColor
SGNODE * GetRawPtr(void) noexcept
Function GetRawPtr() returns the raw internal SGNODE pointer.
Definition: ifsg_node.cpp:66
SGLIB_API bool AddSGNodeRef(SGNODE *aParent, SGNODE *aChild)
Definition: ifsg_api.cpp:645
std::list< X3DNODE * > m_Children
Definition: x3d_base.h:83
wxString m_MatName
X3DNODE represents the base class of all X3D nodes.
Definition: x3d_base.h:75
float transparency
WRLVEC3F diffuseColor
bool SetParent(X3DNODE *aParent, bool doUnlink=true) override
Function SetParent sets the parent X3DNODE of this object.
#define NULL
bool SetAmbient(float aRVal, float aGVal, float aBVal)
X3DAPP.
bool AddRefNode(X3DNODE *aNode) override
SGLIB_API bool AddSGNodeChild(SGNODE *aParent, SGNODE *aChild)
Definition: ifsg_api.cpp:654
bool ParseSFFloat(const wxString &aSource, float &aResult)
Definition: x3d_ops.cpp:261
SGNODE * TranslateToSG(SGNODE *aParent) override
Function TranslateToSG produces a representation of the data using the intermediate scenegraph struct...
virtual bool AddChildNode(X3DNODE *aNode)=0
bool SetShininess(float aShininess) noexcept
X3DNODES GetNodeType(void) const
Function GetNodeType returns the type of this node instance.
Definition: x3d_base.cpp:186
virtual void unlinkChildNode(const X3DNODE *aNode)
Function unlinkChild removes references to an owned child; it is invoked by the child upon destructio...
Definition: x3d_base.cpp:101
bool SetEmissive(float aRVal, float aGVal, float aBVal)
bool AddName(const wxString &aName, X3DNODE *aNode)
Definition: x3d_base.cpp:34
virtual ~X3DAPP()
SGTYPES
Definition: sg_types.h:34
SGNODE * m_sgNode
Definition: x3d_base.h:88
bool SetSpecular(float aRVal, float aGVal, float aBVal)
SGLIB_API S3D::SGTYPES GetSGNodeType(SGNODE *aNode)
Definition: ifsg_api.cpp:627
X3DNODE * m_Parent
Definition: x3d_base.h:78
bool Read(wxXmlNode *aNode, X3DNODE *aTopNode, X3D_DICT &aDict) override
bool ParseSFVec3(const wxString &aSource, WRLVEC3F &aResult)
Definition: x3d_ops.cpp:273
void readFields(wxXmlNode *aNode)
X3DNODES
Definition: x3d_base.h:59
X3D_DICT * m_Dict
Definition: x3d_base.h:80
float shininess
WRLVEC3F emissiveColor
std::list< X3DNODE * > m_Refs
Definition: x3d_base.h:84