MaterialReplacerExample.cpp

<< Click to Display Table of Contents >>

Navigation:  Simplygon 7.1 examples >

MaterialReplacerExample.cpp

///////////////////////////////////////////////////////////////////////////
//
//  System:    Simplygon
//  File:      MaterialReplacerExample.cpp
//  Language:  C++
//
//  Copyright (c) 2015 Donya Labs AB. All rights reserved.
//
//  This is private property, and it is illegal to copy or distribute in
//  any form, without written authorization by the copyright owner(s).
//
///////////////////////////////////////////////////////////////////////////
//
//  #Description#
// 
//  In this example, a set percentage of materials are replaced or removed
//  determined by the least visual cost.
//
//  Low visual cost can be because the objects are hidden or objects with
//  similar looking materials get their materials replaced by the other.
// 
// 
///////////////////////////////////////////////////////////////////////////
#include "../Common/Example.h"
void RunExampleMaterialReplacer(const std::string& readFrom, const std::string& writeTo);
int main( int argc , char* argv[] )
    {
    InitExample();
   
    // Set global variable. Using Orthonormal method for calculating tangentspace.
    sg->SetGlobalSetting( "DefaultTBNType" , SG_TANGENTSPACEMETHOD_ORTHONORMAL );
   
    std::string assetPath = GetAssetPath();
    // Run the example code
    RunExampleMaterialReplacer( assetPath + "MaterialReplacerAssets/simplygon_logo_shelf.obj", "simplygon_logo_shelf_out" );
       
    DeinitExample();
    return 0;
    }
void RunExampleMaterialReplacer( const std::string& readFrom, const std::string& writeTo )
    {
    std::string output_geometry_filename = GetExecutablePath() + writeTo + ".obj";
    // Load from file
    spWavefrontImporter objReader = sg->CreateWavefrontImporter();
    objReader->SetImportFilePath( readFrom.c_str() );
    objReader->SetExtractGroups(false);
    if( !objReader->RunImport() )
        return;
    // Get the scene
    spScene scene = objReader->GetScene();
    spMaterialTable matTable = scene->GetMaterialTable();
    spMaterialReplacer matRep = sg->CreateMaterialReplacer();
    //Fetch the only geometry
    spSelectionSet selectionSet = scene->GetSelectionSetTable()->GetSelectionSet(scene->SelectNodes("ISceneMesh"));
    spSceneMesh sceneMesh = Cast<ISceneMesh>(scene->GetNodeByGUID(selectionSet->GetItem(0)));
    spGeometryData geom = sceneMesh->GetGeometry();
    matRep->SetMaterials(matTable);
    matRep->SetGeometry(geom);
    // Set to only keep 50 % of the materials in the scene.
    // Materials will either be replaced with other materials, or the
    // meshes that use a material will be removed depending on what
    // gives the minimal visual difference.
    matRep->SetMaterialRatio( 0.5 );
    // Make sure all hidden materials are removed.
    matRep->SetReplaceHiddenMaterials( true );
   
    unsigned int original_material_count = matTable->GetItemsCount();
    matRep->RunMaterialReplacement();
    unsigned int reduced_material_count = matTable->GetItemsCount();
    // Store to file
    spWavefrontExporter objexp = sg->CreateWavefrontExporter();
   
    objexp->SetExportFilePath( output_geometry_filename.c_str() );
    objexp->SetScene(scene);
    objexp->RunExport();
    }