2014-07-30 09:01:25 +00:00
/*
* This program source code file is part of KiCad , a free EDA CAD application .
*
* Copyright ( C ) 2014 Mario Luzeiro < mrluzeiro @ gmail . com >
2015-01-18 11:49:32 +00:00
* Copyright ( C ) 1992 - 2015 KiCad Developers , see AUTHORS . txt for contributors .
2014-07-30 09:01:25 +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
*/
/**
* @ file vrml_v1_modelparser . cpp
*/
# include <fctsys.h>
# include <vector>
# include <macros.h>
# include <kicad_string.h>
# include <info3d_visu.h>
# include "3d_struct.h"
# include "modelparsers.h"
# include "vrml_aux.h"
2015-03-13 19:27:25 +00:00
# define BUFLINE_SIZE 32
2015-03-05 19:46:38 +00:00
2015-03-13 19:27:25 +00:00
/**
* Trace mask used to enable or disable the trace output of the VRML V1 parser code .
* The debug output can be turned on by setting the WXTRACE environment variable to
* " KI_TRACE_VRML_V1_PARSER " . See the wxWidgets documentation on wxLogTrace for
* more information .
*/
static const wxChar * traceVrmlV1Parser = wxT ( " KI_TRACE_VRML_V1_PARSER " ) ;
VRML1_MODEL_PARSER : : VRML1_MODEL_PARSER ( S3D_MODEL_PARSER * aModelParser )
2014-07-30 09:01:25 +00:00
{
2015-03-13 19:27:25 +00:00
m_ModelParser = aModelParser ;
m_Master = m_ModelParser - > GetMaster ( ) ;
2014-07-30 09:01:25 +00:00
m_model = NULL ;
2014-08-16 18:01:00 +00:00
m_file = NULL ;
2015-02-28 20:50:35 +00:00
m_normalPerVertex = true ;
colorPerVertex = true ;
2014-07-30 09:01:25 +00:00
}
VRML1_MODEL_PARSER : : ~ VRML1_MODEL_PARSER ( )
{
}
2015-03-13 19:27:25 +00:00
bool VRML1_MODEL_PARSER : : Load ( const wxString & aFilename )
2014-07-30 09:01:25 +00:00
{
2015-03-05 19:46:38 +00:00
char text [ BUFLINE_SIZE ] ;
2014-07-30 09:01:25 +00:00
2015-03-13 19:27:25 +00:00
wxLogTrace ( traceVrmlV1Parser , wxT ( " Loading: %s " ) , GetChars ( aFilename ) ) ;
2014-07-30 09:01:25 +00:00
m_file = wxFopen ( aFilename , wxT ( " rt " ) ) ;
if ( m_file = = NULL )
2015-03-13 19:27:25 +00:00
return false ;
2014-07-30 09:01:25 +00:00
2015-03-13 19:27:25 +00:00
// Switch the locale to standard C (needed to print floating point numbers)
LOCALE_IO toggle ;
2014-08-09 18:18:04 +00:00
2015-03-13 19:27:25 +00:00
m_ModelParser - > childs . clear ( ) ;
2014-07-30 09:01:25 +00:00
2015-02-20 00:21:34 +00:00
while ( GetNextTag ( m_file , text , sizeof ( text ) ) )
2014-07-30 09:01:25 +00:00
{
2015-01-18 11:49:32 +00:00
if ( ( * text = = ' } ' ) | | ( * text = = ' ] ' ) )
2014-07-30 09:01:25 +00:00
{
continue ;
}
if ( strcmp ( text , " Separator " ) = = 0 )
{
m_model = new S3D_MESH ( ) ;
2015-03-13 19:27:25 +00:00
m_ModelParser - > childs . push_back ( m_model ) ;
2014-07-30 09:01:25 +00:00
read_separator ( ) ;
}
}
fclose ( m_file ) ;
2015-03-13 19:27:25 +00:00
return true ;
2014-07-30 09:01:25 +00:00
}
2014-08-16 18:01:00 +00:00
2014-07-30 09:01:25 +00:00
int VRML1_MODEL_PARSER : : read_separator ( )
{
2015-03-05 19:46:38 +00:00
char text [ BUFLINE_SIZE ] ;
2014-07-30 09:01:25 +00:00
2014-08-16 18:01:00 +00:00
// DBG( printf( "Separator\n" ) );
2014-07-30 09:01:25 +00:00
2015-02-20 00:21:34 +00:00
while ( GetNextTag ( m_file , text , sizeof ( text ) ) )
2014-07-30 09:01:25 +00:00
{
if ( strcmp ( text , " Material " ) = = 0 )
{
2014-08-16 18:01:00 +00:00
readMaterial ( ) ;
2014-08-09 18:18:04 +00:00
}
else if ( strcmp ( text , " Coordinate3 " ) = = 0 )
2014-07-30 09:01:25 +00:00
{
readCoordinate3 ( ) ;
2014-08-09 18:18:04 +00:00
}
else if ( strcmp ( text , " IndexedFaceSet " ) = = 0 )
2014-07-30 09:01:25 +00:00
{
readIndexedFaceSet ( ) ;
2014-08-09 18:18:04 +00:00
}
else if ( strcmp ( text , " Separator " ) = = 0 )
2014-07-30 09:01:25 +00:00
{
2014-08-16 18:01:00 +00:00
S3D_MESH * parent = m_model ;
2014-07-30 09:01:25 +00:00
2014-08-16 18:01:00 +00:00
S3D_MESH * new_mesh_model = new S3D_MESH ( ) ;
2014-07-30 09:01:25 +00:00
m_model - > childs . push_back ( new_mesh_model ) ;
m_model = new_mesh_model ;
// recursive
read_separator ( ) ;
m_model = parent ;
2014-08-09 18:18:04 +00:00
}
2014-08-16 18:01:00 +00:00
else if ( ( * text ! = ' } ' ) )
2014-07-30 09:01:25 +00:00
{
2014-08-16 18:01:00 +00:00
// DBG( printf( "read_NotImplemented %s\n", text ) );
read_NotImplemented ( m_file , ' } ' ) ;
2014-08-09 18:18:04 +00:00
}
else
2014-07-30 09:01:25 +00:00
break ;
}
return 0 ;
}
int VRML1_MODEL_PARSER : : readMaterial ( )
{
2015-03-05 19:46:38 +00:00
char text [ BUFLINE_SIZE ] ;
2014-07-30 09:01:25 +00:00
S3D_MATERIAL * material = NULL ;
2014-08-16 18:01:00 +00:00
// DBG( printf( " readMaterial\n" ) );
2014-07-30 09:01:25 +00:00
2014-08-16 18:01:00 +00:00
wxString mat_name ;
2014-07-30 09:01:25 +00:00
2015-03-13 19:27:25 +00:00
material = new S3D_MATERIAL ( m_Master , mat_name ) ;
2014-07-30 09:01:25 +00:00
2015-03-13 19:27:25 +00:00
m_Master - > Insert ( material ) ;
2014-07-30 09:01:25 +00:00
m_model - > m_Materials = material ;
2015-02-20 00:21:34 +00:00
while ( GetNextTag ( m_file , text , sizeof ( text ) ) )
2014-07-30 09:01:25 +00:00
{
2015-01-18 11:49:32 +00:00
if ( * text = = ' ] ' )
2014-07-30 09:01:25 +00:00
{
continue ;
}
2014-11-15 13:43:23 +00:00
if ( * text = = ' } ' )
2014-07-30 09:01:25 +00:00
{
return 0 ;
}
if ( strcmp ( text , " ambientColor " ) = = 0 )
{
readMaterial_ambientColor ( ) ;
2014-08-09 18:18:04 +00:00
}
else if ( strcmp ( text , " diffuseColor " ) = = 0 )
2014-07-30 09:01:25 +00:00
{
2014-08-16 18:01:00 +00:00
readMaterial_diffuseColor ( ) ;
2014-08-09 18:18:04 +00:00
}
else if ( strcmp ( text , " emissiveColor " ) = = 0 )
2014-07-30 09:01:25 +00:00
{
2014-08-16 18:01:00 +00:00
readMaterial_emissiveColor ( ) ;
2014-08-09 18:18:04 +00:00
}
else if ( strcmp ( text , " specularColor " ) = = 0 )
2014-07-30 09:01:25 +00:00
{
2014-08-16 18:01:00 +00:00
readMaterial_specularColor ( ) ;
2014-08-09 18:18:04 +00:00
}
else if ( strcmp ( text , " shininess " ) = = 0 )
2014-07-30 09:01:25 +00:00
{
2014-08-16 18:01:00 +00:00
readMaterial_shininess ( ) ;
2014-08-09 18:18:04 +00:00
}
else if ( strcmp ( text , " transparency " ) = = 0 )
2014-07-30 09:01:25 +00:00
{
2014-08-16 18:01:00 +00:00
readMaterial_transparency ( ) ;
2014-07-30 09:01:25 +00:00
}
}
2015-03-13 19:27:25 +00:00
wxLogTrace ( traceVrmlV1Parser , wxT ( " readMaterial failed " ) ) ;
2014-07-30 09:01:25 +00:00
return - 1 ;
}
2014-08-16 18:01:00 +00:00
int VRML1_MODEL_PARSER : : readCoordinate3 ( )
2014-07-30 09:01:25 +00:00
{
2015-03-05 19:46:38 +00:00
char text [ BUFLINE_SIZE ] ;
2014-07-30 09:01:25 +00:00
2014-08-16 18:01:00 +00:00
// DBG( printf( " readCoordinate3\n" ) );
2014-07-30 09:01:25 +00:00
2015-02-20 00:21:34 +00:00
while ( GetNextTag ( m_file , text , sizeof ( text ) ) )
2014-07-30 09:01:25 +00:00
{
2015-01-18 11:49:32 +00:00
if ( * text = = ' ] ' )
2014-07-30 09:01:25 +00:00
{
continue ;
}
2014-11-15 13:43:23 +00:00
if ( * text = = ' } ' )
2014-07-30 09:01:25 +00:00
{
return 0 ;
}
if ( strcmp ( text , " point " ) = = 0 )
{
2014-08-16 18:01:00 +00:00
readCoordinate3_point ( ) ;
2014-07-30 09:01:25 +00:00
}
}
2015-03-13 19:27:25 +00:00
wxLogTrace ( traceVrmlV1Parser , wxT ( " readCoordinate3 failed " ) ) ;
2014-07-30 09:01:25 +00:00
return - 1 ;
}
2014-08-16 18:01:00 +00:00
int VRML1_MODEL_PARSER : : readIndexedFaceSet ( )
2014-07-30 09:01:25 +00:00
{
2015-03-05 19:46:38 +00:00
char text [ BUFLINE_SIZE ] ;
2014-07-30 09:01:25 +00:00
2014-08-16 18:01:00 +00:00
// DBG( printf( " readIndexedFaceSet\n" ) );
2014-07-30 09:01:25 +00:00
2015-02-20 00:21:34 +00:00
while ( GetNextTag ( m_file , text , sizeof ( text ) ) )
2014-07-30 09:01:25 +00:00
{
2015-01-18 11:49:32 +00:00
if ( * text = = ' ] ' )
2014-07-30 09:01:25 +00:00
{
continue ;
}
2014-11-15 13:43:23 +00:00
if ( * text = = ' } ' )
2014-07-30 09:01:25 +00:00
{
return 0 ;
}
if ( strcmp ( text , " coordIndex " ) = = 0 )
{
2014-08-16 18:01:00 +00:00
readIndexedFaceSet_coordIndex ( ) ;
2014-08-09 18:18:04 +00:00
}
else if ( strcmp ( text , " materialIndex " ) = = 0 )
2014-07-30 09:01:25 +00:00
{
2014-08-16 18:01:00 +00:00
readIndexedFaceSet_materialIndex ( ) ;
2014-07-30 09:01:25 +00:00
}
}
2015-03-13 19:27:25 +00:00
wxLogTrace ( traceVrmlV1Parser , wxT ( " readIndexedFaceSet failed " ) ) ;
2014-07-30 09:01:25 +00:00
return - 1 ;
}
2014-08-16 18:01:00 +00:00
int VRML1_MODEL_PARSER : : readMaterial_ambientColor ( )
2014-07-30 09:01:25 +00:00
{
2014-08-16 18:01:00 +00:00
// DBG( printf( " readMaterial_ambientColor\n" ) );
2014-07-30 09:01:25 +00:00
2014-08-16 18:01:00 +00:00
return parseVertexList ( m_file , m_model - > m_Materials - > m_AmbientColor ) ;
2014-07-30 09:01:25 +00:00
}
2014-08-16 18:01:00 +00:00
int VRML1_MODEL_PARSER : : readMaterial_diffuseColor ( )
2014-07-30 09:01:25 +00:00
{
2014-08-16 18:01:00 +00:00
// DBG( printf( " readMaterial_diffuseColor\n" ) );
2014-08-09 18:18:04 +00:00
2014-08-16 18:01:00 +00:00
return parseVertexList ( m_file , m_model - > m_Materials - > m_DiffuseColor ) ;
2014-07-30 09:01:25 +00:00
}
2014-08-16 18:01:00 +00:00
int VRML1_MODEL_PARSER : : readMaterial_emissiveColor ( )
2014-07-30 09:01:25 +00:00
{
2014-08-16 18:01:00 +00:00
// DBG( printf( " readMaterial_emissiveColor\n" ) );
2014-07-30 09:01:25 +00:00
2014-08-16 18:01:00 +00:00
int ret = parseVertexList ( m_file , m_model - > m_Materials - > m_EmissiveColor ) ;
2014-07-30 09:01:25 +00:00
2015-03-13 19:27:25 +00:00
if ( m_Master - > m_use_modelfile_emissiveColor = = false )
2014-07-30 09:01:25 +00:00
{
m_model - > m_Materials - > m_EmissiveColor . clear ( ) ;
}
return ret ;
}
int VRML1_MODEL_PARSER : : readMaterial_specularColor ( )
{
2014-08-16 18:01:00 +00:00
// DBG( printf( " readMaterial_specularColor\n" ) );
2014-07-30 09:01:25 +00:00
int ret = parseVertexList ( m_file , m_model - > m_Materials - > m_SpecularColor ) ;
2015-03-13 19:27:25 +00:00
if ( m_Master - > m_use_modelfile_specularColor = = false )
2014-07-30 09:01:25 +00:00
{
m_model - > m_Materials - > m_SpecularColor . clear ( ) ;
}
return ret ;
}
2014-08-16 18:01:00 +00:00
int VRML1_MODEL_PARSER : : readMaterial_shininess ( )
2014-07-30 09:01:25 +00:00
{
2014-08-16 18:01:00 +00:00
// DBG( printf( " readMaterial_shininess\n" ) );
2014-07-30 09:01:25 +00:00
m_model - > m_Materials - > m_Shininess . clear ( ) ;
float shininess_value ;
2014-08-09 18:18:04 +00:00
2014-07-30 09:01:25 +00:00
while ( fscanf ( m_file , " %f, " , & shininess_value ) )
{
// VRML value is normalized and openGL expects a value 0 - 128
shininess_value = shininess_value * 128.0f ;
m_model - > m_Materials - > m_Shininess . push_back ( shininess_value ) ;
}
2015-03-13 19:27:25 +00:00
if ( m_Master - > m_use_modelfile_shininess = = false )
2014-07-30 09:01:25 +00:00
{
m_model - > m_Materials - > m_Shininess . clear ( ) ;
}
2014-08-16 18:01:00 +00:00
// DBG( printf( " m_Shininess.size: %ld\n", m_model->m_Materials->m_Shininess.size() ) );
2014-07-30 09:01:25 +00:00
return 0 ;
}
int VRML1_MODEL_PARSER : : readMaterial_transparency ( )
{
2014-08-16 18:01:00 +00:00
// DBG( printf( " readMaterial_transparency\n" ) );
2014-07-30 09:01:25 +00:00
m_model - > m_Materials - > m_Transparency . clear ( ) ;
float tmp ;
2014-08-09 18:18:04 +00:00
2014-08-16 18:01:00 +00:00
while ( fscanf ( m_file , " %f, " , & tmp ) )
2014-07-30 09:01:25 +00:00
{
m_model - > m_Materials - > m_Transparency . push_back ( tmp ) ;
}
2015-03-13 19:27:25 +00:00
if ( m_Master - > m_use_modelfile_transparency = = false )
2014-07-30 09:01:25 +00:00
{
m_model - > m_Materials - > m_Transparency . clear ( ) ;
}
2014-08-09 18:18:04 +00:00
2014-08-16 18:01:00 +00:00
// DBG( printf( " m_Transparency.size: %ld\n", m_model->m_Materials->m_Transparency.size() ) );
2014-07-30 09:01:25 +00:00
return 0 ;
}
int VRML1_MODEL_PARSER : : readCoordinate3_point ( )
{
2014-08-16 18:01:00 +00:00
// DBG( printf( " readCoordinate3_point\n" ) );
2014-08-09 18:18:04 +00:00
2014-07-30 09:01:25 +00:00
if ( parseVertexList ( m_file , m_model - > m_Point ) = = 0 )
{
return 0 ;
}
2015-03-13 19:27:25 +00:00
wxLogTrace ( traceVrmlV1Parser , wxT ( " readCoordinate3_point failed " ) ) ;
2014-07-30 09:01:25 +00:00
return - 1 ;
}
int VRML1_MODEL_PARSER : : readIndexedFaceSet_coordIndex ( )
{
2014-08-16 18:01:00 +00:00
// DBG( printf( " readIndexedFaceSet_coordIndex\n" ) );
2014-07-30 09:01:25 +00:00
m_model - > m_CoordIndex . clear ( ) ;
glm : : ivec3 coord ;
2014-08-16 18:01:00 +00:00
int dummy ; // should be -1
2014-07-30 09:01:25 +00:00
while ( fscanf ( m_file , " %d,%d,%d,%d, " , & coord [ 0 ] , & coord [ 1 ] , & coord [ 2 ] , & dummy ) )
{
std : : vector < int > coord_list ;
coord_list . resize ( 3 ) ;
coord_list [ 0 ] = coord [ 0 ] ;
coord_list [ 1 ] = coord [ 1 ] ;
coord_list [ 2 ] = coord [ 2 ] ;
2014-08-16 18:01:00 +00:00
if ( ( coord [ 0 ] = = coord [ 1 ] )
| | ( coord [ 0 ] = = coord [ 2 ] )
| | ( coord [ 2 ] = = coord [ 1 ] ) )
2014-07-30 09:01:25 +00:00
{
2015-03-13 19:27:25 +00:00
wxLogTrace ( traceVrmlV1Parser , wxT ( " invalid coordIndex at index %u (%d, %d, %d, %d) " ) , ( unsigned int ) m_model - > m_CoordIndex . size ( ) + 1 , coord [ 0 ] , coord [ 1 ] , coord [ 2 ] , dummy ) ;
2014-07-30 09:01:25 +00:00
}
2014-08-16 18:01:00 +00:00
if ( dummy ! = - 1 )
2014-07-30 09:01:25 +00:00
{
2015-03-13 19:27:25 +00:00
wxLogTrace ( traceVrmlV1Parser , wxT ( " Error at index %u, -1 Expected, got %d " ) , ( unsigned int ) m_model - > m_CoordIndex . size ( ) + 1 , dummy ) ;
2014-07-30 09:01:25 +00:00
}
2014-08-16 18:01:00 +00:00
2014-07-30 09:01:25 +00:00
m_model - > m_CoordIndex . push_back ( coord_list ) ;
}
2014-08-16 18:01:00 +00:00
// DBG( printf( " m_CoordIndex.size: %ld\n", m_model->m_CoordIndex.size() ) );
2014-07-30 09:01:25 +00:00
return 0 ;
}
int VRML1_MODEL_PARSER : : readIndexedFaceSet_materialIndex ( )
{
2014-08-16 18:01:00 +00:00
// DBG( printf( " readIndexedFaceSet_materialIndex\n" ) );
2014-07-30 09:01:25 +00:00
m_model - > m_MaterialIndex . clear ( ) ;
int index ;
2014-08-16 18:01:00 +00:00
2014-07-30 09:01:25 +00:00
while ( fscanf ( m_file , " %d, " , & index ) )
{
m_model - > m_MaterialIndex . push_back ( index ) ;
}
2014-08-16 18:01:00 +00:00
// DBG( printf( " m_MaterialIndex.size: %ld\n", m_model->m_MaterialIndex.size() ) );
2014-07-30 09:01:25 +00:00
return 0 ;
}