Exemplo n.º 1
0
void X3D_MODEL_PARSER::readMaterial( wxXmlNode* aMatNode )
{
    glm::vec3 color;

    PROPERTY_MAP properties;

    GetNodeProperties( aMatNode, properties );

    // DEFine new Material named as value of DEF
    if( properties.find( wxT( "DEF" ) ) != properties.end() )
    {
        double amb, shine, transp;

        S3D_MATERIAL* material = new S3D_MATERIAL( GetMaster(), properties[ wxT( "DEF" ) ] );
        GetMaster()->Insert( material );

        m_model->m_Materials = material;

        if( !parseDoubleTriplet( properties[ wxT( "diffuseColor" ) ], color ) )
        {
            // DBG( printf( "diffuseColor parsing error" ) );
        }
        else
        {
            m_model->m_Materials->m_DiffuseColor.push_back( color );
        }

        if( !parseDoubleTriplet( properties[ wxT( "specularColor" ) ], color ) )
        {
            // DBG( printf( "specularColor parsing error" ) );
        }
        else
        {
            m_model->m_Materials->m_SpecularColor.push_back( color );
        }

        if( !parseDoubleTriplet( properties[ wxT( "emissiveColor" ) ], color ) )
        {
            // DBG( printf( "emissiveColor parsing error" ) );
        }
        else
        {
            m_model->m_Materials->m_EmissiveColor.push_back( color );
        }

        wxStringTokenizer values;
        values.SetString( properties[ wxT( "ambientIntensity" ) ] );

        if( values.GetNextToken().ToDouble( &amb ) )
        {
            m_model->m_Materials->m_AmbientColor.push_back( glm::vec3( amb, amb, amb ) );
        }
        else
        {
            // DBG( printf( "ambienterror" ) );
        }

        values.SetString( properties[ wxT( "shininess" ) ]  );

        if( values.GetNextToken().ToDouble( &shine ) )
        {
            // VRML value is normalized and openGL expects a value 0 - 128
            if( shine > 1.0 )
            {
                shine = 1.0;
            } else if( shine < 0.0 )
            {
                shine = 0.0;
            }
            shine = shine * 128.0f;
            m_model->m_Materials->m_Shininess.push_back( shine );
        }
        else
        {
            // DBG( printf( "shininess error" ) );
        }

        values.SetString( properties[ wxT( "transparency" ) ] );

        if( values.GetNextToken().ToDouble( &transp ) )
        {
            m_model->m_Materials->m_Transparency.push_back( transp );
        }
        else
        {
            // DBG( printf( "trans error" ) );
        }

        // VRML
        wxString vrml_material;
        PROPERTY_MAP::const_iterator p = ++properties.begin();    // skip DEF

        for( ; p != properties.end(); p++ )
        {
            vrml_material.Append( p->first + wxT( " " ) + p->second + wxT( "\n" ) );
        }

        vrml_materials.push_back( vrml_material );
    }
    // USE existing material named by value of USE
    else if( properties.find( wxT( "USE" ) ) != properties.end() )
    {
        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;

                vrml_material.Append( wxString::Format( wxT( "specularColor %f %f %f\n" ),
                                material->m_SpecularColor[0].x,
                                material->m_SpecularColor[0].y,
                                material->m_SpecularColor[0].z ) );

                vrml_material.Append( wxString::Format( wxT( "diffuseColor %f %f %f\n" ),
                                material->m_DiffuseColor[0].x,
                                material->m_DiffuseColor[0].y,
                                material->m_DiffuseColor[0].z ) );

                vrml_material.Append( wxString::Format( wxT( "emissiveColor %f %f %f\n" ),
                                material->m_EmissiveColor[0].x,
                                material->m_EmissiveColor[0].y,
                                material->m_EmissiveColor[0].z ) );

                vrml_material.Append( wxString::Format( wxT( "ambientIntensity %f\n" ),
                                material->m_AmbientColor[0].x ) );

                vrml_material.Append( wxString::Format( wxT( "shininess %f\n" ),
                                material->m_Shininess[0] ) );

                vrml_material.Append( wxString::Format( wxT( "transparency %f\n" ),
                                material->m_Transparency[0] ) );

                vrml_materials.push_back( vrml_material );

                m_model->m_Materials = material;

                return;
            }
        }

        // DBG( printf( "ReadMaterial error: material not found\n" ) );
    }
}
Exemplo n.º 2
0
int VRML_MODEL_PARSER::readMaterial( FILE* file, int* LineNum )
{
    char          line[512], * text, * command;
    wxString      mat_name;
    S3D_MATERIAL* material = NULL;

    command  = strtok( NULL, sep_chars );
    text     = strtok( NULL, sep_chars );
    mat_name = FROM_UTF8( text );

    if( stricmp( command, "USE" ) == 0 )
    {
        for( material = GetMaster()->m_Materials; material; material = material->Next() )
        {
            if( material->m_Name == mat_name )
            {
                material->SetMaterial();
                return 1;
            }
        }

        DBG( printf( "ReadMaterial error: material not found\n" ) );
        return 0;
    }

    if( stricmp( command, "DEF" ) == 0 || stricmp( command, "Material") == 0)
    {
        material = new S3D_MATERIAL( GetMaster(), mat_name );

        GetMaster()->Insert( material );

        while( GetLine( file, line, LineNum, 512 ) )
        {
            text = strtok( line, sep_chars );

            if( text == NULL )
                continue;

            if( text[0] == '}' )
            {
                material->SetMaterial();
                return 0;
            }

            if( stricmp( text, "diffuseColor" ) == 0 )
            {
                text = strtok( NULL, sep_chars );
                material->m_DiffuseColor.x = atof( text );
                text = strtok( NULL, sep_chars );
                material->m_DiffuseColor.y = atof( text );
                text = strtok( NULL, sep_chars );
                material->m_DiffuseColor.z = atof( text );
            }
            else if( stricmp( text, "emissiveColor" ) == 0 )
            {
                text = strtok( NULL, sep_chars );
                material->m_EmissiveColor.x = atof( text );
                text = strtok( NULL, sep_chars );
                material->m_EmissiveColor.y = atof( text );
                text = strtok( NULL, sep_chars );
                material->m_EmissiveColor.z = atof( text );
            }
            else if( strnicmp( text, "specularColor", 13 ) == 0 )
            {
                text = strtok( NULL, sep_chars );
                material->m_SpecularColor.x = atof( text );
                text = strtok( NULL, sep_chars );
                material->m_SpecularColor.y = atof( text );
                text = strtok( NULL, sep_chars );
                material->m_SpecularColor.z = atof( text );
            }
            else if( strnicmp( text, "ambientIntensity", 16 ) == 0 )
            {
                text = strtok( NULL, sep_chars );
                material->m_AmbientIntensity = atof( text );
            }
            else if( strnicmp( text, "transparency", 12 ) == 0 )
            {
                text = strtok( NULL, sep_chars );
                material->m_Transparency = atof( text );
            }
            else if( strnicmp( text, "shininess", 9 ) == 0 )
            {
                text = strtok( NULL, sep_chars );
                material->m_Shininess = atof( text );
            }
        }
    }

    return -1;
}