ParameterizerExample.cpp

<< Click to Display Table of Contents >>

Navigation:  Simplygon 7.1 examples >

ParameterizerExample.cpp

///////////////////////////////////////////////////////////////////////////
//
//  System:    Simplygon
//  File:      ParameterizerExample.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#
//
//  This example shows how to generate unique texture coordinates for a
//  mesh using the spParameterizer.
//
//  To generate texture coordinates and baking the original materials to
//  the new UVs, see MaterialCastingExample.
//
///////////////////////////////////////////////////////////////////////////
#include "../Common/Example.h"
void RunExample(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 );
    //sg->SetGlobalSetting( "DefaultTBNType" , SG_TANGENTSPACEMETHOD_3DSMAX );
    std::string assetPath = GetAssetPath();
    // Run the example code
    RunExample(assetPath + "object.obj", "object_with_UVs.obj");
    DeinitExample();
    return 0;
    }
void RunExample(const std::string& readFrom, const std::string& writeTo)
    {
    //Import the wavefront OBJ file
    spWavefrontImporter importer = sg->CreateWavefrontImporter();
    importer->SetImportFilePath( readFrom.c_str() );
    if( !importer->RunImport() )
        ExitWithError("Couldn't load Wavefront object file.");
    //The scene will contain the read geometry, and the .obj reader will have put
    //all geometries in mesh nodes that are direct children of the root. Hence,
    //we can easily iterate through them.
    spScene scene = importer->GetScene();
    //Create a parameterizer object
    spParameterizer param = sg->CreateParameterizer();
   
    //Set the properties for the parameterizer
    param->SetMaxStretch(0.25f);
    param->SetTextureWidth(512);
    param->SetTextureHeight(512);
    // Parameterize all the objects in the file.
    for( uint geomId = 0; geomId < scene->GetRootNode()->GetChildCount(); ++geomId )
        {
        //Cast the node to mesh node, and fetch geometry pointer from it
        spGeometryData geom = SimplygonSDK::SafeCast<ISceneMesh>( scene->GetRootNode()->GetChild(geomId) )->GetGeometry();
       
        //If the mesh does not have UVs, create them
        if( geom->GetTexCoords(0) == nullptr )
            geom->AddTexCoords(0);
        //Parameterize the selected geometry and input the generated texture coordinates
        //into the first (index 0) UV field.
        if( !param->Parameterize( geom, geom->GetTexCoords(0) ) )
            std::cout << "Couldn't parameterize geometry." << std::endl;
        }
    //Export the mesh with the new UVs
    spWavefrontExporter exporter = sg->CreateWavefrontExporter();
    exporter->SetExportFilePath( writeTo.c_str() );
    exporter->SetScene( scene );
    exporter->RunExport();
    }