2013-03-27 18:34:23 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Tuomas Vaherkoski <tuomasvaherkoski@gmail.com>
|
|
|
|
* Copyright (C) 1992-2013 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file x3dmodelparser.cpp
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <fctsys.h>
|
|
|
|
#include <macros.h>
|
|
|
|
#include <info3d_visu.h>
|
|
|
|
#include <wx/xml/xml.h>
|
|
|
|
#include <wx/tokenzr.h>
|
|
|
|
#include <wx/string.h>
|
|
|
|
#include <map>
|
|
|
|
#include <queue>
|
|
|
|
#include <vector>
|
|
|
|
|
2014-01-08 01:34:04 +00:00
|
|
|
#include <3d_struct.h>
|
|
|
|
#include <modelparsers.h>
|
|
|
|
#include <xnode.h>
|
|
|
|
|
2013-03-27 18:34:23 +00:00
|
|
|
|
2013-03-28 16:51:22 +00:00
|
|
|
X3D_MODEL_PARSER::X3D_MODEL_PARSER( S3D_MASTER* aMaster ) :
|
|
|
|
S3D_MODEL_PARSER( aMaster )
|
2014-08-16 18:01:00 +00:00
|
|
|
{
|
|
|
|
}
|
2013-03-27 18:34:23 +00:00
|
|
|
|
2013-03-28 16:51:22 +00:00
|
|
|
|
2013-03-27 18:34:23 +00:00
|
|
|
X3D_MODEL_PARSER::~X3D_MODEL_PARSER()
|
2014-08-16 18:01:00 +00:00
|
|
|
{
|
|
|
|
}
|
2013-03-27 18:34:23 +00:00
|
|
|
|
2013-03-28 16:51:22 +00:00
|
|
|
|
2014-08-21 11:59:57 +00:00
|
|
|
void X3D_MODEL_PARSER::Load( const wxString& aFilename, double aVrmlunits_to_3Dunits )
|
2013-03-27 18:34:23 +00:00
|
|
|
{
|
2013-03-28 16:51:22 +00:00
|
|
|
wxXmlDocument doc;
|
|
|
|
|
|
|
|
if( !doc.Load( aFilename ) )
|
2013-03-27 18:34:23 +00:00
|
|
|
{
|
2014-01-08 01:34:04 +00:00
|
|
|
wxLogError( wxT( "Error while parsing file '%s'" ), GetChars( aFilename ) );
|
2013-03-27 18:34:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( doc.GetRoot()->GetName() != wxT( "X3D" ) )
|
|
|
|
{
|
2014-01-08 01:34:04 +00:00
|
|
|
wxLogError( wxT( "Filetype is not X3D '%s'" ), GetChars( aFilename ) );
|
2013-03-27 18:34:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2014-08-21 11:59:57 +00:00
|
|
|
float vrmlunits_to_3Dunits = aVrmlunits_to_3Dunits;
|
2014-07-31 07:01:30 +00:00
|
|
|
glScalef( vrmlunits_to_3Dunits, vrmlunits_to_3Dunits, vrmlunits_to_3Dunits );
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2014-08-02 07:46:47 +00:00
|
|
|
glm::vec3 matScale( GetMaster()->m_MatScale.x, GetMaster()->m_MatScale.y,
|
2014-08-16 18:01:00 +00:00
|
|
|
GetMaster()->m_MatScale.z );
|
2014-08-02 07:46:47 +00:00
|
|
|
glm::vec3 matRot( GetMaster()->m_MatRotation.x, GetMaster()->m_MatRotation.y,
|
2014-08-16 18:01:00 +00:00
|
|
|
GetMaster()->m_MatRotation.z );
|
2014-08-02 07:46:47 +00:00
|
|
|
glm::vec3 matPos( GetMaster()->m_MatPosition.x, GetMaster()->m_MatPosition.y,
|
2014-08-16 18:01:00 +00:00
|
|
|
GetMaster()->m_MatPosition.z );
|
2014-08-02 07:46:47 +00:00
|
|
|
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2014-08-16 18:01:00 +00:00
|
|
|
#define SCALE_3D_CONV ( (IU_PER_MILS * 1000.0f) / UNITS3D_TO_UNITSPCB )
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2014-08-02 07:46:47 +00:00
|
|
|
|
2014-07-31 07:01:30 +00:00
|
|
|
glTranslatef( matPos.x * SCALE_3D_CONV, matPos.y * SCALE_3D_CONV, matPos.z * SCALE_3D_CONV );
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2014-08-16 18:01:00 +00:00
|
|
|
glRotatef( -matRot.z, 0.0f, 0.0f, 1.0f );
|
|
|
|
glRotatef( -matRot.y, 0.0f, 1.0f, 0.0f );
|
|
|
|
glRotatef( -matRot.x, 1.0f, 0.0f, 0.0f );
|
2014-08-02 07:46:47 +00:00
|
|
|
|
2014-07-30 09:01:25 +00:00
|
|
|
glScalef( matScale.x, matScale.y, matScale.z );
|
|
|
|
|
|
|
|
// Switch the locale to standard C (needed to print floating point numbers)
|
|
|
|
LOCALE_IO toggle;
|
|
|
|
|
|
|
|
childs.clear();
|
|
|
|
|
2013-03-27 18:34:23 +00:00
|
|
|
// Shapes are inside of Transform nodes
|
2013-03-28 16:51:22 +00:00
|
|
|
// Transform node contains information about
|
2013-03-27 18:34:23 +00:00
|
|
|
// transition, scale and rotation of the shape
|
|
|
|
NODE_LIST transforms;
|
|
|
|
GetChildsByName( doc.GetRoot(), wxT( "Transform" ), transforms );
|
2013-03-28 16:51:22 +00:00
|
|
|
|
2013-03-27 18:34:23 +00:00
|
|
|
for( NODE_LIST::iterator node_it = transforms.begin();
|
2014-08-16 18:01:00 +00:00
|
|
|
node_it != transforms.end();
|
|
|
|
node_it++ )
|
2013-03-27 18:34:23 +00:00
|
|
|
{
|
2014-07-30 09:01:25 +00:00
|
|
|
m_model = new S3D_MESH();
|
|
|
|
childs.push_back( m_model );
|
|
|
|
|
2013-03-27 18:34:23 +00:00
|
|
|
readTransform( *node_it );
|
|
|
|
}
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2014-08-16 18:01:00 +00:00
|
|
|
// DBG( printf( "chils size:%lu\n", childs.size() ) );
|
2014-07-30 09:01:25 +00:00
|
|
|
|
|
|
|
if( GetMaster()->IsOpenGlAllowed() )
|
|
|
|
{
|
|
|
|
for( unsigned int idx = 0; idx < childs.size(); idx++ )
|
|
|
|
{
|
|
|
|
childs[idx]->openGL_RenderAllChilds();
|
|
|
|
}
|
|
|
|
}
|
2013-03-27 18:34:23 +00:00
|
|
|
}
|
|
|
|
|
2013-03-28 16:51:22 +00:00
|
|
|
|
2014-07-30 09:01:25 +00:00
|
|
|
wxString X3D_MODEL_PARSER::VRML2_representation()
|
2013-03-27 18:34:23 +00:00
|
|
|
{
|
|
|
|
wxString output;
|
2013-03-28 16:51:22 +00:00
|
|
|
|
|
|
|
for( unsigned i = 0; i < vrml_points.size(); i++ )
|
2013-03-27 18:34:23 +00:00
|
|
|
{
|
2014-01-08 01:34:04 +00:00
|
|
|
output +=
|
|
|
|
wxT( "Shape {\n"
|
|
|
|
" appearance Appearance {\n"
|
|
|
|
" material Material {\n" ) +
|
|
|
|
vrml_materials[i] +
|
|
|
|
wxT( " }\n"
|
|
|
|
" }\n"
|
|
|
|
" geometry IndexedFaceSet {\n"
|
|
|
|
" solid TRUE\n"
|
|
|
|
" coord Coordinate {\n"
|
2014-08-16 18:01:00 +00:00
|
|
|
" point [\n" ) +
|
2014-01-08 01:34:04 +00:00
|
|
|
vrml_points[i] +
|
|
|
|
wxT( " ]\n"
|
|
|
|
" }\n"
|
|
|
|
" coordIndex [\n" ) +
|
|
|
|
vrml_coord_indexes[i] +
|
|
|
|
wxT( " ]\n"
|
|
|
|
" }\n"
|
|
|
|
"},\n" );
|
2013-03-27 18:34:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2013-03-28 16:51:22 +00:00
|
|
|
|
|
|
|
void X3D_MODEL_PARSER::GetChildsByName( wxXmlNode* aParent,
|
2014-08-16 18:01:00 +00:00
|
|
|
const wxString aName,
|
|
|
|
std::vector<wxXmlNode*>& aResult )
|
2013-03-27 18:34:23 +00:00
|
|
|
{
|
|
|
|
// Breadth-first search (BFS)
|
2014-08-16 18:01:00 +00:00
|
|
|
std::queue<wxXmlNode*> found;
|
2014-01-08 01:34:04 +00:00
|
|
|
|
2013-03-27 18:34:23 +00:00
|
|
|
found.push( aParent );
|
2013-03-28 16:51:22 +00:00
|
|
|
|
|
|
|
while( !found.empty() )
|
2013-03-27 18:34:23 +00:00
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
wxXmlNode* elem = found.front();
|
2013-03-28 16:51:22 +00:00
|
|
|
|
2014-08-16 18:01:00 +00:00
|
|
|
for( wxXmlNode* child = elem->GetChildren();
|
2013-03-28 16:51:22 +00:00
|
|
|
child != NULL;
|
2013-03-27 18:34:23 +00:00
|
|
|
child = child->GetNext() )
|
|
|
|
{
|
2013-03-28 16:51:22 +00:00
|
|
|
if( child->GetName() == aName )
|
2013-03-27 18:34:23 +00:00
|
|
|
{
|
|
|
|
aResult.push_back( child );
|
|
|
|
}
|
2013-03-28 16:51:22 +00:00
|
|
|
|
2013-03-27 18:34:23 +00:00
|
|
|
found.push( child );
|
|
|
|
}
|
2013-03-28 16:51:22 +00:00
|
|
|
|
2013-03-27 18:34:23 +00:00
|
|
|
found.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 16:51:22 +00:00
|
|
|
|
|
|
|
void X3D_MODEL_PARSER::GetNodeProperties( wxXmlNode* aNode, PROPERTY_MAP& aProps )
|
|
|
|
{
|
2014-01-08 01:34:04 +00:00
|
|
|
wxXmlAttribute* prop;
|
2013-03-28 16:51:22 +00:00
|
|
|
|
|
|
|
for( prop = aNode->GetAttributes();
|
|
|
|
prop != NULL;
|
2013-03-27 18:34:23 +00:00
|
|
|
prop = prop->GetNext() )
|
|
|
|
{
|
|
|
|
aProps[ prop->GetName() ] = prop->GetValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-16 18:01:00 +00:00
|
|
|
|
2013-03-27 18:34:23 +00:00
|
|
|
/* Private ----- */
|
|
|
|
|
|
|
|
void X3D_MODEL_PARSER::readTransform( wxXmlNode* aTransformNode )
|
|
|
|
{
|
|
|
|
NODE_LIST childnodes;
|
2014-08-16 18:01:00 +00:00
|
|
|
|
2013-03-27 18:34:23 +00:00
|
|
|
GetChildsByName( aTransformNode, wxT( "Material" ), childnodes );
|
|
|
|
|
|
|
|
for( NODE_LIST::iterator node = childnodes.begin();
|
|
|
|
node != childnodes.end();
|
2013-03-28 16:51:22 +00:00
|
|
|
node++ )
|
2013-03-27 18:34:23 +00:00
|
|
|
{
|
|
|
|
readMaterial( *node );
|
|
|
|
}
|
2013-03-28 16:51:22 +00:00
|
|
|
|
2013-03-27 18:34:23 +00:00
|
|
|
childnodes.clear();
|
|
|
|
|
|
|
|
PROPERTY_MAP properties;
|
2014-01-08 01:34:04 +00:00
|
|
|
|
2013-03-27 18:34:23 +00:00
|
|
|
GetNodeProperties( aTransformNode, properties );
|
2014-08-16 18:01:00 +00:00
|
|
|
GetChildsByName( aTransformNode, wxT( "IndexedFaceSet" ), childnodes );
|
2013-03-28 16:51:22 +00:00
|
|
|
|
2013-03-27 18:34:23 +00:00
|
|
|
for( NODE_LIST::iterator node = childnodes.begin();
|
|
|
|
node != childnodes.end();
|
2013-03-28 16:51:22 +00:00
|
|
|
node++ )
|
2013-03-27 18:34:23 +00:00
|
|
|
{
|
|
|
|
readIndexedFaceSet( *node, properties );
|
|
|
|
}
|
2013-03-28 16:51:22 +00:00
|
|
|
|
2013-03-27 18:34:23 +00:00
|
|
|
childnodes.clear();
|
|
|
|
}
|
|
|
|
|
2013-03-28 16:51:22 +00:00
|
|
|
|
2013-03-27 18:34:23 +00:00
|
|
|
void X3D_MODEL_PARSER::readMaterial( wxXmlNode* aMatNode )
|
|
|
|
{
|
2014-07-30 09:01:25 +00:00
|
|
|
glm::vec3 color;
|
|
|
|
|
2013-03-27 18:34:23 +00:00
|
|
|
PROPERTY_MAP properties;
|
2014-08-16 18:01:00 +00:00
|
|
|
|
2013-03-27 18:34:23 +00:00
|
|
|
GetNodeProperties( aMatNode, properties );
|
|
|
|
|
|
|
|
// DEFine new Material named as value of DEF
|
2013-03-28 16:51:22 +00:00
|
|
|
if( properties.find( wxT( "DEF" ) ) != properties.end() )
|
|
|
|
{
|
2013-03-27 18:34:23 +00:00
|
|
|
double amb, shine, transp;
|
|
|
|
|
|
|
|
S3D_MATERIAL* material = new S3D_MATERIAL( GetMaster(), properties[ wxT( "DEF" ) ] );
|
|
|
|
GetMaster()->Insert( material );
|
|
|
|
|
2014-07-30 09:01:25 +00:00
|
|
|
m_model->m_Materials = material;
|
|
|
|
|
2014-08-16 18:01:00 +00:00
|
|
|
if( !parseDoubleTriplet( properties[ wxT( "diffuseColor" ) ], color ) )
|
2013-03-28 16:51:22 +00:00
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
// DBG( printf( "diffuseColor parsing error" ) );
|
2014-08-02 07:46:47 +00:00
|
|
|
}
|
|
|
|
else
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2014-08-02 07:46:47 +00:00
|
|
|
m_model->m_Materials->m_DiffuseColor.push_back( color );
|
2013-03-27 18:34:23 +00:00
|
|
|
}
|
2013-03-28 16:51:22 +00:00
|
|
|
|
2014-08-02 07:46:47 +00:00
|
|
|
if( !parseDoubleTriplet( properties[ wxT( "specularColor" ) ], color ) )
|
2013-03-27 18:34:23 +00:00
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
// DBG( printf( "specularColor parsing error" ) );
|
2014-08-02 07:46:47 +00:00
|
|
|
}
|
|
|
|
else
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
|
|
|
m_model->m_Materials->m_SpecularColor.push_back( color );
|
2013-03-27 18:34:23 +00:00
|
|
|
}
|
|
|
|
|
2014-08-02 07:46:47 +00:00
|
|
|
if( !parseDoubleTriplet( properties[ wxT( "emissiveColor" ) ], color ) )
|
2013-03-28 16:51:22 +00:00
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
// DBG( printf( "emissiveColor parsing error" ) );
|
2014-08-02 07:46:47 +00:00
|
|
|
}
|
|
|
|
else
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
|
|
|
m_model->m_Materials->m_EmissiveColor.push_back( color );
|
2013-03-27 18:34:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wxStringTokenizer values;
|
|
|
|
values.SetString( properties[ wxT( "ambientIntensity" ) ] );
|
2013-03-28 16:51:22 +00:00
|
|
|
|
|
|
|
if( values.GetNextToken().ToDouble( &amb ) )
|
2013-03-27 18:34:23 +00:00
|
|
|
{
|
2014-07-30 09:01:25 +00:00
|
|
|
m_model->m_Materials->m_AmbientColor.push_back( glm::vec3( amb, amb, amb ) );
|
2013-03-28 16:51:22 +00:00
|
|
|
}
|
|
|
|
else
|
2013-03-27 18:34:23 +00:00
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
// DBG( printf( "ambienterror" ) );
|
2013-03-27 18:34:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
values.SetString( properties[ wxT( "shininess" ) ] );
|
2013-03-28 16:51:22 +00:00
|
|
|
|
|
|
|
if( values.GetNextToken().ToDouble( &shine ) )
|
2013-03-27 18:34:23 +00:00
|
|
|
{
|
2014-07-30 09:01:25 +00:00
|
|
|
// VRML value is normalized and openGL expects a value 0 - 128
|
2014-08-19 14:51:15 +00:00
|
|
|
if( shine > 1.0 )
|
|
|
|
{
|
|
|
|
shine = 1.0;
|
|
|
|
} else if( shine < 0.0 )
|
|
|
|
{
|
|
|
|
shine = 0.0;
|
|
|
|
}
|
2014-07-30 09:01:25 +00:00
|
|
|
shine = shine * 128.0f;
|
|
|
|
m_model->m_Materials->m_Shininess.push_back( shine );
|
2013-03-28 16:51:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
// DBG( printf( "shininess error" ) );
|
2013-03-27 18:34:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
values.SetString( properties[ wxT( "transparency" ) ] );
|
2013-03-28 16:51:22 +00:00
|
|
|
|
|
|
|
if( values.GetNextToken().ToDouble( &transp ) )
|
2013-03-27 18:34:23 +00:00
|
|
|
{
|
2014-07-30 09:01:25 +00:00
|
|
|
m_model->m_Materials->m_Transparency.push_back( transp );
|
2013-03-28 16:51:22 +00:00
|
|
|
}
|
2013-03-27 18:34:23 +00:00
|
|
|
else
|
2013-03-28 16:51:22 +00:00
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
// DBG( printf( "trans error" ) );
|
2013-03-27 18:34:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// VRML
|
|
|
|
wxString vrml_material;
|
2014-08-16 18:01:00 +00:00
|
|
|
PROPERTY_MAP::const_iterator p = ++properties.begin(); // skip DEF
|
2013-03-28 16:51:22 +00:00
|
|
|
|
2014-01-08 01:34:04 +00:00
|
|
|
for( ; p != properties.end(); p++ )
|
2013-03-28 16:51:22 +00:00
|
|
|
{
|
|
|
|
vrml_material.Append( p->first + wxT( " " ) + p->second + wxT( "\n" ) );
|
2013-03-27 18:34:23 +00:00
|
|
|
}
|
|
|
|
|
2013-03-28 16:51:22 +00:00
|
|
|
vrml_materials.push_back( vrml_material );
|
2013-03-27 18:34:23 +00:00
|
|
|
}
|
|
|
|
// USE existing material named by value of USE
|
2013-03-28 16:51:22 +00:00
|
|
|
else if( properties.find( wxT( "USE" ) ) != properties.end() )
|
|
|
|
{
|
2013-03-27 18:34:23 +00:00
|
|
|
S3D_MATERIAL* material = NULL;
|
|
|
|
wxString mat_name = properties[ wxT( "USE" ) ];
|
|
|
|
|
|
|
|
for( material = GetMaster()->m_Materials; material; material = material->Next() )
|
|
|
|
{
|
|
|
|
if( material->m_Name == mat_name )
|
|
|
|
{
|
|
|
|
wxString vrml_material;
|
2014-01-08 01:34:04 +00:00
|
|
|
|
2013-03-28 16:51:22 +00:00
|
|
|
vrml_material.Append( wxString::Format( wxT( "specularColor %f %f %f\n" ),
|
2014-08-16 18:01:00 +00:00
|
|
|
material->m_SpecularColor[0].x,
|
|
|
|
material->m_SpecularColor[0].y,
|
|
|
|
material->m_SpecularColor[0].z ) );
|
2014-08-02 07:46:47 +00:00
|
|
|
|
2013-03-28 16:51:22 +00:00
|
|
|
vrml_material.Append( wxString::Format( wxT( "diffuseColor %f %f %f\n" ),
|
2014-08-16 18:01:00 +00:00
|
|
|
material->m_DiffuseColor[0].x,
|
|
|
|
material->m_DiffuseColor[0].y,
|
|
|
|
material->m_DiffuseColor[0].z ) );
|
2014-08-02 07:46:47 +00:00
|
|
|
|
2013-03-28 16:51:22 +00:00
|
|
|
vrml_material.Append( wxString::Format( wxT( "emissiveColor %f %f %f\n" ),
|
2014-08-16 18:01:00 +00:00
|
|
|
material->m_EmissiveColor[0].x,
|
|
|
|
material->m_EmissiveColor[0].y,
|
|
|
|
material->m_EmissiveColor[0].z ) );
|
2013-03-27 18:34:23 +00:00
|
|
|
|
2014-08-16 18:01:00 +00:00
|
|
|
vrml_material.Append( wxString::Format( wxT( "ambientIntensity %f\n" ),
|
|
|
|
material->m_AmbientColor[0].x ) );
|
2013-03-27 18:34:23 +00:00
|
|
|
|
2014-08-16 18:01:00 +00:00
|
|
|
vrml_material.Append( wxString::Format( wxT( "shininess %f\n" ),
|
|
|
|
material->m_Shininess[0] ) );
|
2013-03-27 18:34:23 +00:00
|
|
|
|
2014-08-16 18:01:00 +00:00
|
|
|
vrml_material.Append( wxString::Format( wxT( "transparency %f\n" ),
|
|
|
|
material->m_Transparency[0] ) );
|
2013-03-27 18:34:23 +00:00
|
|
|
|
2013-03-28 16:51:22 +00:00
|
|
|
vrml_materials.push_back( vrml_material );
|
2013-03-27 18:34:23 +00:00
|
|
|
|
2014-07-30 09:01:25 +00:00
|
|
|
m_model->m_Materials = material;
|
|
|
|
|
2013-03-27 18:34:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-16 18:01:00 +00:00
|
|
|
// DBG( printf( "ReadMaterial error: material not found\n" ) );
|
2013-03-27 18:34:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 16:51:22 +00:00
|
|
|
|
|
|
|
bool X3D_MODEL_PARSER::parseDoubleTriplet( const wxString& aData,
|
2014-08-16 18:01:00 +00:00
|
|
|
S3D_VERTEX& aResult )
|
2013-03-27 18:34:23 +00:00
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
wxStringTokenizer tokens( aData );
|
2013-03-28 16:51:22 +00:00
|
|
|
|
2014-11-15 13:43:23 +00:00
|
|
|
double x = 0;
|
|
|
|
double y = 0;
|
|
|
|
double z = 0;
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2014-08-16 18:01:00 +00:00
|
|
|
bool ret = tokens.GetNextToken().ToDouble( &x )
|
|
|
|
&& tokens.GetNextToken().ToDouble( &y )
|
|
|
|
&& tokens.GetNextToken().ToDouble( &z );
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2014-08-16 18:01:00 +00:00
|
|
|
aResult.x = x;
|
|
|
|
aResult.y = y;
|
|
|
|
aResult.z = z;
|
2014-07-30 09:01:25 +00:00
|
|
|
|
|
|
|
return ret;
|
2013-03-27 18:34:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-28 16:51:22 +00:00
|
|
|
void X3D_MODEL_PARSER::rotate( S3D_VERTEX& aV,
|
2014-08-16 18:01:00 +00:00
|
|
|
S3D_VERTEX& aU,
|
|
|
|
double angle )
|
2013-03-27 18:34:23 +00:00
|
|
|
{
|
|
|
|
S3D_VERTEX rotated;
|
2014-08-16 18:01:00 +00:00
|
|
|
double C = cos( angle );
|
|
|
|
double S = sin( angle );
|
|
|
|
double t = 1.0 - C;
|
2013-03-27 18:34:23 +00:00
|
|
|
|
2013-03-28 16:51:22 +00:00
|
|
|
rotated.x = ( t * aU.x * aU.x + C ) * aV.x +
|
|
|
|
( t * aU.x * aU.y - S * aU.z ) * aV.y +
|
2013-03-27 18:34:23 +00:00
|
|
|
( t * aU.x * aU.z + S * aU.y ) * aV.z;
|
|
|
|
|
2013-03-28 16:51:22 +00:00
|
|
|
rotated.y = ( t * aU.x * aU.y + S * aU.z ) * aV.x +
|
|
|
|
( t * aU.y * aU.y + C ) * aV.y +
|
2013-03-27 18:34:23 +00:00
|
|
|
( t * aU.y * aU.z - S * aU.x ) * aV.z;
|
|
|
|
|
2013-03-28 16:51:22 +00:00
|
|
|
rotated.z = ( t * aU.x * aU.z - S * aU.y ) * aV.x +
|
|
|
|
( t * aU.y * aU.z + S * aU.x ) * aV.y +
|
2013-03-27 18:34:23 +00:00
|
|
|
( t * aU.z * aU.z + C) * aV.z;
|
|
|
|
|
2014-08-16 18:01:00 +00:00
|
|
|
aV.x = rotated.x;
|
|
|
|
aV.y = rotated.y;
|
|
|
|
aV.z = rotated.z;
|
2013-03-27 18:34:23 +00:00
|
|
|
}
|
|
|
|
|
2013-03-28 16:51:22 +00:00
|
|
|
|
2013-03-27 18:34:23 +00:00
|
|
|
/* Steps:
|
|
|
|
* 1. Read transform data
|
2013-03-28 16:51:22 +00:00
|
|
|
* 2. Read vertex triplets
|
2013-03-27 18:34:23 +00:00
|
|
|
* 3. Read coordinate indexes
|
|
|
|
* 4. Apply geometry to Master object
|
|
|
|
*/
|
2013-03-28 16:51:22 +00:00
|
|
|
void X3D_MODEL_PARSER::readIndexedFaceSet( wxXmlNode* aFaceNode,
|
2014-08-16 18:01:00 +00:00
|
|
|
PROPERTY_MAP& aTransformProps )
|
2013-03-27 18:34:23 +00:00
|
|
|
{
|
|
|
|
/* Step 1: Read transform data
|
|
|
|
* --------------------------- */
|
2013-03-28 16:51:22 +00:00
|
|
|
|
2013-03-27 18:34:23 +00:00
|
|
|
S3D_VERTEX translation;
|
2014-08-16 18:01:00 +00:00
|
|
|
|
2013-03-28 16:51:22 +00:00
|
|
|
parseDoubleTriplet( aTransformProps[ wxT( "translation" ) ], translation );
|
2013-03-27 18:34:23 +00:00
|
|
|
|
|
|
|
S3D_VERTEX scale;
|
|
|
|
parseDoubleTriplet( aTransformProps[ wxT( "scale" ) ], scale );
|
|
|
|
|
|
|
|
S3D_VERTEX rotation;
|
|
|
|
double angle = 0.0;
|
2014-08-16 18:01:00 +00:00
|
|
|
wxStringTokenizer tokens( aTransformProps[ wxT( "rotation" ) ] );
|
2013-03-28 16:51:22 +00:00
|
|
|
|
2014-08-16 18:01:00 +00:00
|
|
|
double x, y, z;
|
|
|
|
|
|
|
|
if( !( tokens.GetNextToken().ToDouble( &x )
|
|
|
|
&& tokens.GetNextToken().ToDouble( &y )
|
|
|
|
&& tokens.GetNextToken().ToDouble( &z )
|
|
|
|
&& tokens.GetNextToken().ToDouble( &angle ) ) )
|
2013-03-27 18:34:23 +00:00
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
// DBG( printf( "rotation read error" ) );
|
2013-03-27 18:34:23 +00:00
|
|
|
}
|
2014-07-30 09:01:25 +00:00
|
|
|
else
|
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
rotation.x = x;
|
|
|
|
rotation.y = y;
|
|
|
|
rotation.z = z;
|
2014-07-30 09:01:25 +00:00
|
|
|
}
|
2013-03-27 18:34:23 +00:00
|
|
|
|
|
|
|
/* Step 2: Read all coordinate points
|
|
|
|
* ---------------------------- */
|
2014-08-16 18:01:00 +00:00
|
|
|
std::vector<double> points;
|
2013-03-27 18:34:23 +00:00
|
|
|
NODE_LIST coordinates;
|
2014-08-16 18:01:00 +00:00
|
|
|
GetChildsByName( aFaceNode, wxT( "Coordinate" ), coordinates );
|
2013-03-27 18:34:23 +00:00
|
|
|
|
|
|
|
PROPERTY_MAP coordinate_properties;
|
|
|
|
// IndexedFaceSet has one Coordinate child node
|
|
|
|
GetNodeProperties( coordinates[0], coordinate_properties );
|
|
|
|
|
|
|
|
// Save points to vector as doubles
|
2014-08-16 18:01:00 +00:00
|
|
|
wxStringTokenizer point_tokens( coordinate_properties[ wxT( "point" ) ] );
|
2013-03-27 18:34:23 +00:00
|
|
|
double point = 0.0;
|
2013-03-28 16:51:22 +00:00
|
|
|
|
|
|
|
while( point_tokens.HasMoreTokens() )
|
2013-03-27 18:34:23 +00:00
|
|
|
{
|
|
|
|
if( point_tokens.GetNextToken().ToDouble( &point ) )
|
|
|
|
{
|
|
|
|
points.push_back( point );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxLogError( wxT( "Error converting to double" ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 16:51:22 +00:00
|
|
|
if( points.size() % 3 != 0 )
|
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
// DBG( printf( "Number of points is incorrect" ) );
|
2013-03-27 18:34:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create 3D vertex from 3 points and
|
2013-03-28 16:51:22 +00:00
|
|
|
* apply transforms in order of SCALE, ROTATION, TRANSLATION
|
2013-03-27 18:34:23 +00:00
|
|
|
*/
|
|
|
|
wxString vrml_pointlist;
|
2014-08-16 18:01:00 +00:00
|
|
|
std::vector<S3D_VERTEX> triplets;
|
2013-03-28 16:51:22 +00:00
|
|
|
|
2013-03-27 18:34:23 +00:00
|
|
|
for( unsigned id = 0; id < points.size() / 3; id++ )
|
|
|
|
{
|
|
|
|
int triplet_indx = id * 3;
|
2013-03-28 16:51:22 +00:00
|
|
|
S3D_VERTEX point( points[ triplet_indx ],
|
2014-08-16 18:01:00 +00:00
|
|
|
points[ triplet_indx + 1 ],
|
|
|
|
points[ triplet_indx + 2 ] );
|
2013-03-27 18:34:23 +00:00
|
|
|
|
|
|
|
point.x *= scale.x;
|
|
|
|
point.y *= scale.y;
|
|
|
|
point.z *= scale.z;
|
|
|
|
|
2013-03-28 16:51:22 +00:00
|
|
|
rotate( point, rotation, angle );
|
2013-03-27 18:34:23 +00:00
|
|
|
|
|
|
|
point.x += translation.x;
|
|
|
|
point.y += translation.y;
|
|
|
|
point.z += translation.z;
|
|
|
|
|
2014-07-30 09:01:25 +00:00
|
|
|
m_model->m_Point.push_back( point );
|
2013-03-27 18:34:23 +00:00
|
|
|
|
|
|
|
// VRML
|
2013-03-28 16:51:22 +00:00
|
|
|
vrml_pointlist.Append( wxString::Format( wxT( "%f %f %f\n" ), point.x, point.y, point.z ) );
|
2013-03-27 18:34:23 +00:00
|
|
|
}
|
2013-03-28 16:51:22 +00:00
|
|
|
|
|
|
|
vrml_points.push_back( vrml_pointlist );
|
2013-03-27 18:34:23 +00:00
|
|
|
|
2014-07-30 09:01:25 +00:00
|
|
|
|
|
|
|
/* Step 3: Read all color points
|
|
|
|
* ---------------------------- */
|
2014-08-16 18:01:00 +00:00
|
|
|
std::vector<double> color_points;
|
2014-07-30 09:01:25 +00:00
|
|
|
NODE_LIST color;
|
2014-08-16 18:01:00 +00:00
|
|
|
GetChildsByName( aFaceNode, wxT( "Color" ), color );
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2014-08-02 07:46:47 +00:00
|
|
|
// Some models lack color information, need to handle this safely
|
|
|
|
if( !color.empty() )
|
|
|
|
{
|
|
|
|
PROPERTY_MAP color_properties;
|
|
|
|
// IndexedFaceSet has one Coordinate child node
|
|
|
|
GetNodeProperties( color[0], color_properties );
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2014-08-02 07:46:47 +00:00
|
|
|
// Save points to vector as doubles
|
2014-08-16 18:01:00 +00:00
|
|
|
wxStringTokenizer colorpoint_tokens( color_properties[ wxT( "color" ) ] );
|
2014-08-02 07:46:47 +00:00
|
|
|
double color_point = 0.0;
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2014-08-02 07:46:47 +00:00
|
|
|
while( colorpoint_tokens.HasMoreTokens() )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2014-08-02 07:46:47 +00:00
|
|
|
if( colorpoint_tokens.GetNextToken().ToDouble( &color_point ) )
|
|
|
|
{
|
|
|
|
color_points.push_back( color_point );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxLogError( wxT( "Error converting to double" ) );
|
|
|
|
}
|
2014-07-30 09:01:25 +00:00
|
|
|
}
|
2014-08-02 07:46:47 +00:00
|
|
|
|
|
|
|
if( color_points.size() % 3 != 0 )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2014-08-16 18:01:00 +00:00
|
|
|
// DBG( printf( "Number of points is incorrect" ) );
|
2014-08-02 07:46:47 +00:00
|
|
|
return;
|
2014-07-30 09:01:25 +00:00
|
|
|
}
|
|
|
|
|
2014-08-02 07:46:47 +00:00
|
|
|
/* Create 3D face color from 3 color points
|
|
|
|
*/
|
|
|
|
m_model->m_Materials->m_DiffuseColor.clear();
|
2014-08-16 18:01:00 +00:00
|
|
|
|
2014-08-02 07:46:47 +00:00
|
|
|
for( unsigned id = 0; id < color_points.size() / 3; id++ )
|
|
|
|
{
|
|
|
|
m_model->m_MaterialIndex.push_back( id );
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2014-08-02 07:46:47 +00:00
|
|
|
int color_triplet_indx = id * 3;
|
|
|
|
glm::vec3 colorface( color_points[ color_triplet_indx + 0 ],
|
2014-08-16 18:01:00 +00:00
|
|
|
color_points[ color_triplet_indx + 1 ],
|
|
|
|
color_points[ color_triplet_indx + 2 ] );
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2014-08-02 07:46:47 +00:00
|
|
|
m_model->m_Materials->m_DiffuseColor.push_back( colorface );
|
|
|
|
}
|
2014-07-30 09:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-27 18:34:23 +00:00
|
|
|
/* -- Read coordinate indexes -- */
|
|
|
|
PROPERTY_MAP faceset_properties;
|
|
|
|
GetNodeProperties( aFaceNode, faceset_properties );
|
|
|
|
|
|
|
|
wxString coordIndex_str = faceset_properties[ wxT( "coordIndex" ) ];
|
|
|
|
wxStringTokenizer index_tokens( coordIndex_str );
|
|
|
|
|
|
|
|
wxString vrml_coord_indx_list;
|
2013-03-28 16:51:22 +00:00
|
|
|
|
2014-07-30 09:01:25 +00:00
|
|
|
std::vector<int> coord_list;
|
|
|
|
coord_list.clear();
|
|
|
|
|
2013-03-28 16:51:22 +00:00
|
|
|
while( index_tokens.HasMoreTokens() )
|
2013-03-27 18:34:23 +00:00
|
|
|
{
|
|
|
|
long index = 0;
|
2014-01-08 01:34:04 +00:00
|
|
|
|
2013-03-28 16:51:22 +00:00
|
|
|
index_tokens.GetNextToken().ToLong( &index );
|
2013-03-27 18:34:23 +00:00
|
|
|
|
|
|
|
// -1 marks the end of polygon
|
|
|
|
if( index < 0 )
|
|
|
|
{
|
|
|
|
/* Step 4: Apply geometry to Master object
|
|
|
|
* --------------------------------------- */
|
2014-07-30 09:01:25 +00:00
|
|
|
m_model->m_CoordIndex.push_back( coord_list );
|
2013-03-27 18:34:23 +00:00
|
|
|
|
2014-07-30 09:01:25 +00:00
|
|
|
coord_list.clear();
|
2013-03-28 16:51:22 +00:00
|
|
|
vrml_coord_indx_list.Append( wxT( "-1\n" ) );
|
2013-03-27 18:34:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-30 09:01:25 +00:00
|
|
|
coord_list.push_back( index );
|
2014-08-19 14:51:15 +00:00
|
|
|
vrml_coord_indx_list.Append( wxString::Format( wxT( "%ld " ), index ) );
|
2013-03-27 18:34:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 16:51:22 +00:00
|
|
|
vrml_coord_indexes.push_back( vrml_coord_indx_list );
|
|
|
|
}
|