KiCad PCB EDA Suite
Loading...
Searching...
No Matches
vrml2_material.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
26#include "vrml2_base.h"
27#include "vrml2_material.h"
29
30
36
37
39{
42 m_Parent = aParent;
43
44 if( nullptr != m_Parent )
45 m_Parent->AddChildNode( this );
46}
47
48
50{
51 wxLogTrace( traceVrmlPlugin, wxT( " * [INFO] Destroying Material node." ) );
52}
53
54
56{
57 // default material values as per VRML2 spec
58 diffuseColor.x = 0.8f;
59 diffuseColor.y = 0.8f;
60 diffuseColor.z = 0.8f;
61
62 emissiveColor.x = 0.0f;
63 emissiveColor.y = 0.0f;
64 emissiveColor.z = 0.0f;
65
67
68 ambientIntensity = 0.2f;
69 shininess = 0.2f;
70 transparency = 0.0f;
71}
72
73
75{
76 // this node is dangling unless it has a parent of type WRL2NODES::WRL2_APPEARANCE
77
78 if( nullptr == m_Parent || m_Parent->GetNodeType() != WRL2NODES::WRL2_APPEARANCE )
79 return true;
80
81 return false;
82}
83
84
86{
87 // this node may not own or reference any other node
88 wxCHECK_MSG( false, false, wxT( "AddRefNode is not applicable." ) );
89}
90
91
93{
94 // this node may not own or reference any other node
95 wxCHECK_MSG( false, false, wxT( "AddChildNode is not applicable." ) );
96}
97
98
99bool WRL2MATERIAL::Read( WRLPROC& proc, WRL2BASE* aTopNode )
100{
101 wxCHECK_MSG( aTopNode, false, wxT( "Invalid top node." ) );
102
103 char tok = proc.Peek();
104
105 if( proc.eof() )
106 {
107 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
108 " * [INFO] bad file format; unexpected eof %s." ),
109 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition() );
110
111 return false;
112 }
113
114 if( '{' != tok )
115 {
116 wxLogTrace( traceVrmlPlugin,
117 wxT( "%s:%s:%d\n"
118 " * [INFO] bad file format; expecting '{' but got '%s' %s." ),
119 __FILE__, __FUNCTION__, __LINE__, tok, proc.GetFilePosition() );
120
121 return false;
122 }
123
124 proc.Pop();
125 std::string glob;
126
127 while( true )
128 {
129 if( proc.Peek() == '}' )
130 {
131 proc.Pop();
132 break;
133 }
134
135 if( !proc.ReadName( glob ) )
136 {
137 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
138 "%s" ),
139 __FILE__, __FUNCTION__, __LINE__ , proc.GetError() );
140
141 return false;
142 }
143
144 // expecting one of:
145 // ambientIntensity
146 // diffuseColor
147 // emissiveColor
148 // shininess
149 // specularColor
150 // transparency
151
152 if( !glob.compare( "specularColor" ) )
153 {
154 if( !proc.ReadSFVec3f( specularColor ) )
155 {
156 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
157 " * [INFO] invalid specularColor set %s\n"
158 " * [INFO] file: '%s'\n"
159 "%s" ),
160 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
161 proc.GetFileName(), proc.GetError() );
162
163 return false;
164 }
165 }
166 else if( !glob.compare( "diffuseColor" ) )
167 {
168 if( !proc.ReadSFVec3f( diffuseColor ) )
169 {
170 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
171 " * [INFO] invalid diffuseColor set %s\n"
172 " * [INFO] file: '%s'\n"
173 "%s" ),
174 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
175 proc.GetFileName(), proc.GetError() );
176
177 return false;
178 }
179 }
180 else if( !glob.compare( "emissiveColor" ) )
181 {
182 if( !proc.ReadSFVec3f( emissiveColor ) )
183 {
184 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
185 " * [INFO] invalid emissiveColor set %s\n"
186 " * [INFO] file: '%s'\n"
187 "%s" ),
188 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
189 proc.GetFileName(), proc.GetError() );
190
191 return false;
192 }
193 }
194 else if( !glob.compare( "shininess" ) )
195 {
196 if( !proc.ReadSFFloat( shininess ) )
197 {
198 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
199 " * [INFO] invalid shininess set %s\n"
200 " * [INFO] file: '%s'\n"
201 "%s" ),
202 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
203 proc.GetFileName(), proc.GetError() );
204
205 return false;
206 }
207 }
208 else if( !glob.compare( "transparency" ) )
209 {
210 if( !proc.ReadSFFloat( transparency ) )
211 {
212 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
213 " * [INFO] invalid transparency set %s\n"
214 " * [INFO] file: '%s'\n"
215 "%s" ),
216 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
217 proc.GetFileName(), proc.GetError() );
218
219 return false;
220 }
221 }
222 else if( !glob.compare( "ambientIntensity" ) )
223 {
224 if( !proc.ReadSFFloat( ambientIntensity ) )
225 {
226 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
227 " * [INFO] invalid ambientIntensity set %s\n"
228 " * [INFO] file: '%s'\n"
229 "%s" ),
230 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
231 proc.GetFileName(), proc.GetError() );
232
233 return false;
234 }
235 }
236 else
237 {
238 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
239 " * [INFO] invalid Material %s.\n"
240 " * [INFO] file: '%s'\n" ),
241 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
242 proc.GetFileName() );
243
244 return false;
245 }
246 } // while( true ) -- reading contents of Material{}
247
248 return true;
249}
250
251
253{
254 S3D::SGTYPES ptype = S3D::GetSGNodeType( aParent );
255
256 wxCHECK_MSG( aParent && ( ptype == S3D::SGTYPE_SHAPE ), nullptr,
257 wxString::Format( wxT( "IndexedFaceSet does not have a Shape parent (parent "
258 "ID: %d)." ), ptype ) );
259
260 wxLogTrace( traceVrmlPlugin,
261 wxT( " * [INFO] Translating IndexedFaceSet with %zu children, %zu references, and"
262 "%zu back pointers." ),
263 m_Children.size(), m_Refs.size(), m_BackPointers.size() );
264
265 if( m_sgNode )
266 {
267 if( nullptr != aParent )
268 {
269 if( nullptr == S3D::GetSGNodeParent( m_sgNode )
270 && !S3D::AddSGNodeChild( aParent, m_sgNode ) )
271 {
272 return nullptr;
273 }
274 else if( aParent != S3D::GetSGNodeParent( m_sgNode )
275 && !S3D::AddSGNodeRef( aParent, m_sgNode ) )
276 {
277 return nullptr;
278 }
279 }
280
281 return m_sgNode;
282 }
283
284 IFSG_APPEARANCE matNode( aParent );
288 float ambr = ambientIntensity * diffuseColor.x;
289 float ambg = ambientIntensity * diffuseColor.y;
290 float ambb = ambientIntensity * diffuseColor.z;
291 matNode.SetAmbient( ambr, ambg, ambb );
292 matNode.SetShininess( shininess );
293 matNode.SetTransparency( transparency );
294 m_sgNode = matNode.GetRawPtr();
295
296 return m_sgNode;
297}
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
The top node of a VRML2 model.
Definition vrml2_base.h:56
virtual ~WRL2MATERIAL()
SGNODE * TranslateToSG(SGNODE *aParent) override
Produce a representation of the data using the intermediate scenegraph structures of the kicad_3dsg l...
WRLVEC3F specularColor
bool isDangling(void) override
Determine whether an object should be moved to a different parent during the VRML to SG* translation.
void setDefaults(void)
bool AddRefNode(WRL2NODE *aNode) override
WRLVEC3F emissiveColor
bool Read(WRLPROC &proc, WRL2BASE *aTopNode) override
WRLVEC3F diffuseColor
float ambientIntensity
bool AddChildNode(WRL2NODE *aNode) override
SGNODE * m_sgNode
Definition vrml2_node.h:174
std::list< WRL2NODE * > m_BackPointers
Definition vrml2_node.h:169
WRL2NODE * m_Parent
Definition vrml2_node.h:165
std::list< WRL2NODE * > m_Children
Definition vrml2_node.h:170
WRL2NODES m_Type
Definition vrml2_node.h:166
std::list< WRL2NODE * > m_Refs
Definition vrml2_node.h:171
void Pop(void)
Definition wrlproc.cpp:2031
bool ReadSFFloat(float &aSFFloat)
Definition wrlproc.cpp:802
char Peek(void)
Definition wrlproc.cpp:2003
std::string GetFileName(void)
Definition wrlproc.cpp:1991
std::string GetError(void)
Definition wrlproc.cpp:1956
bool eof(void)
Definition wrlproc.cpp:1950
bool ReadName(std::string &aName)
Definition wrlproc.cpp:285
std::string GetFilePosition() const
Definition wrlproc.cpp:1978
bool ReadSFVec3f(WRLVEC3F &aSFVec3f)
Definition wrlproc.cpp:1078
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
@ WRL2_MATERIAL
Definition wrltypes.h:149
@ WRL2_APPEARANCE
Definition wrltypes.h:125