PackedGeometryExample.cpp

<< Click to Display Table of Contents >>

Navigation:  Simplygon 7.1 examples >

PackedGeometryExample.cpp

///////////////////////////////////////////////////////////////////////////
//
//  System:    Simplygon
//  File:      PackedGeometryExample.cpp
//  Language:  C++
//
//  Copyright (c) 2015 Donya Labs AB. All rights reserved.
//
///////////////////////////////////////////////////////////////////////////
//
//  #Description#
//
//  This example shows how to use the IPackedGeometryData
//  object to be able to import/export geometries that have per-vertex
//  data where Simplygon use per-corner data. Please
//  note that the number of vertices in packed geometries most of the
//  time is a lot higher than in a standard geometry, as any difference
//  in any per-corner field results in split vertices.
//  The example also shows how to use the PrintInfo() on some objects
//  to print the contents of a data object.
//  The example loads a wavefront .obj file into a standard IGeometryData
//  object, then creates an IPackedGeometryData object copy, and prints
//  the contents to an output file.
//
///////////////////////////////////////////////////////////////////////////
#include "../Common/Example.h"
void RunExample();
int main( int argc , char* argv[] )
    {
    // Initiate
    InitExample();
    // Run the example code
    RunExample();
    // Deinitialize the SDK
    DeinitExample();
    return 0;
    }
void RunExample()
    {
    std::string assetPath = GetAssetPath();
    // Import geometry data
    //
    spWavefrontImporter is = sg->CreateWavefrontImporter();
    is->SetExtractGroups(false); // We only want one geometry, no need to extract groups
    is->SetImportFilePath( (assetPath + "cube.obj").c_str() );
    if( !is->RunImport() )
        {
        ExitWithError("Could not open input test file");
        }
    spScene scene = is->GetScene();
    spSceneMesh sceneMesh = SafeCast<ISceneMesh>( scene->GetRootNode()->GetChild(0) );
    // Convert the geometry to a packed geometry data object
    // where all corner fields are converted into vertex fields.
    // Please note that the packed geometry will have more vertices
    // than the original geometry, because non-identical
    // per-corner data will cause the vertex to be split.
    //
    spGeometryData geom = sceneMesh->GetGeometry();
    // Remove unused fields
    //
    geom->RemoveMaterialIds();
    geom->RemoveUserTriangleField("SmoothingGroupIds");
    // Make packed copy
    //
    spPackedGeometryData packedGeom = geom->NewPackedCopy();
    // Print the contents of the standard and packed geometries to output.txt
    //
    std::string outputPath = GetExecutablePath() + "output.txt";
    freopen( outputPath.c_str() , "w" , stdout );
    printf("#####################################################################\n");
    printf("Contents of standard geometry:\n");
    printf("#####################################################################\n");
    geom->PrintInfo();
    printf("\n");
    printf("#####################################################################\n");
    printf("Contents of packed geometry:\n");
    printf("#####################################################################\n");
    packedGeom->PrintInfo();
    printf("\n");
    freopen( "CON" , "w" , stdout );
    // done
    }