// Copyright (c) Microsoft Corporation. 
// Licensed under the MIT license. 

#include <string>
#include <stdlib.h>
#include <filesystem>
#include <future>
#include "SimplygonLoader.h"


void RunReductionWithMaterialCasting(Simplygon::ISimplygon* sg)
{
	Simplygon::spSceneImporter sgSceneImporter = sg->CreateSceneImporter();
	sgSceneImporter->SetImportFilePath( "../Assets/RiggedSimplygonMan/RiggedSimplygonMan.fbx" );
	if(!sgSceneImporter->RunImport())
		throw std::exception("Failed to load RiggedSimplygonMan/RiggedSimplygonMan.fbx.");
	Simplygon::spScene sgScene = sgSceneImporter->GetScene();

	// Create the reduction pipeline. 
	Simplygon::spReductionPipeline sgReductionPipeline = sg->CreateReductionPipeline();

	Simplygon::spReductionSettings sgReductionSettings = sgReductionPipeline->GetReductionSettings();
	Simplygon::spMappingImageSettings sgMappingImageSettings = sgReductionPipeline->GetMappingImageSettings();

	// Set reduction target to triangle ratio with a ratio of 50%. 
	sgReductionSettings->SetReductionTargets( Simplygon::EStopCondition::All, true, false, false, false );
	sgReductionSettings->SetReductionTargetTriangleRatio( 0.5f );

	// Generates a mapping image which is used after the reduction to cast new materials to the new 
	// reduced object. 
	sgMappingImageSettings->SetGenerateMappingImage( true );
	sgMappingImageSettings->SetApplyNewMaterialIds( true );
	sgMappingImageSettings->SetGenerateTangents( true );
	sgMappingImageSettings->SetUseFullRetexturing( true );

	Simplygon::spMappingImageOutputMaterialSettings sgOutputMaterialSettings = sgMappingImageSettings->GetOutputMaterialSettings(0);

	// Setting the size of the output material for the mapping image. This will be the output size of the 
	// textures when we do material casting in a later stage. 
	sgOutputMaterialSettings->SetTextureWidth( 2048 );
	sgOutputMaterialSettings->SetTextureHeight( 2048 );

	// Add diffuse material caster to pipeline. 
	Simplygon::spColorCaster sgDiffuseCaster = sg->CreateColorCaster();

	Simplygon::spColorCasterSettings sgDiffuseCasterSettings = sgDiffuseCaster->GetColorCasterSettings();
	sgDiffuseCasterSettings->SetMaterialChannel( "Diffuse" );
	sgDiffuseCasterSettings->SetOutputImageFileFormat( Simplygon::EImageOutputFormat::PNG );

	sgReductionPipeline->AddMaterialCaster( sgDiffuseCaster, 0 );

	// Add normals material caster to pipeline. 
	Simplygon::spNormalCaster sgNormalsCaster = sg->CreateNormalCaster();

	Simplygon::spNormalCasterSettings sgNormalsCasterSettings = sgNormalsCaster->GetNormalCasterSettings();
	sgNormalsCasterSettings->SetMaterialChannel( "Normals" );
	sgNormalsCasterSettings->SetGenerateTangentSpaceNormals( true );
	sgNormalsCasterSettings->SetOutputImageFileFormat( Simplygon::EImageOutputFormat::PNG );

	sgReductionPipeline->AddMaterialCaster( sgNormalsCaster, 0 );

	// Start the reduction pipeline. 
	sgReductionPipeline->RunScene(sgScene, Simplygon::EPipelineRunMode::RunInThisProcess);

	// Get the processed scene. 
	Simplygon::spScene sgProcessedScene = sgReductionPipeline->GetProcessedScene();

	Simplygon::spSceneExporter  sgSceneExporter = sg->CreateSceneExporter();
	sgSceneExporter->SetScene(sgProcessedScene);
	sgSceneExporter->SetExportFilePath( "ReductionOutput.fbx" );
	if(!sgSceneExporter->RunExport())
		throw std::exception("Failed to save ReductionOutput.fbx.");

}

void main()
{
	Simplygon::ISimplygon* sg = NULL;
	Simplygon::EErrorCodes initval = Simplygon::Initialize( &sg );
	if( initval != Simplygon::EErrorCodes::NoError )
	{
		return;
	}

	RunReductionWithMaterialCasting(sg);

	Simplygon::Deinitialize(sg);
}

