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

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


void RunRemeshingWithMaterialCasting(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 remeshing processor. 
	Simplygon::spRemeshingProcessor sgRemeshingProcessor = sg->CreateRemeshingProcessor();

	sgRemeshingProcessor->SetScene( sgScene );

	Simplygon::spRemeshingSettings sgRemeshingSettings = sgRemeshingProcessor->GetRemeshingSettings();
	Simplygon::spMappingImageSettings sgMappingImageSettings = sgRemeshingProcessor->GetMappingImageSettings();

	// Set on-screen size target for remeshing. 
	sgRemeshingSettings->SetOnScreenSize( 300 );

	// Generates a mapping image which is used after the remeshing to cast new materials to the new 
	// remeshed 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 );

	// Start the remeshing process. 
	sgRemeshingProcessor->RunProcessing();

	// Setup and run the diffuse material casting. 
	Simplygon::spColorCaster sgDiffuseCaster = sg->CreateColorCaster();
	sgDiffuseCaster->SetMappingImage( sgRemeshingProcessor->GetMappingImage() );
	sgDiffuseCaster->SetSourceMaterials( sgScene->GetMaterialTable() );
	sgDiffuseCaster->SetSourceTextures( sgScene->GetTextureTable() );
	sgDiffuseCaster->SetOutputFilePath( "DiffuseTexture" );

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

	sgDiffuseCaster->RunProcessing();
	std::string diffuseTextureFilePath = sgDiffuseCaster->GetOutputFilePath();

	// Setup and run the normals material casting. 
	Simplygon::spNormalCaster sgNormalsCaster = sg->CreateNormalCaster();
	sgNormalsCaster->SetMappingImage( sgRemeshingProcessor->GetMappingImage() );
	sgNormalsCaster->SetSourceMaterials( sgScene->GetMaterialTable() );
	sgNormalsCaster->SetSourceTextures( sgScene->GetTextureTable() );
	sgNormalsCaster->SetOutputFilePath( "NormalsTexture" );

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

	sgNormalsCaster->RunProcessing();
	std::string normalsTextureFilePath = sgNormalsCaster->GetOutputFilePath();

	// Update scene with new casted textures. 
	Simplygon::spMaterialTable sgMaterialTable = sg->CreateMaterialTable();
	Simplygon::spTextureTable sgTextureTable = sg->CreateTextureTable();
	Simplygon::spMaterial sgMaterial = sg->CreateMaterial();
	Simplygon::spTexture sgDiffuseTexture = sg->CreateTexture();
	sgDiffuseTexture->SetName( "Diffuse" );
	sgDiffuseTexture->SetFilePath( diffuseTextureFilePath.c_str() );
	sgTextureTable->AddTexture( sgDiffuseTexture );

	Simplygon::spShadingTextureNode sgDiffuseTextureShadingNode = sg->CreateShadingTextureNode();
	sgDiffuseTextureShadingNode->SetTexCoordLevel( 0 );
	sgDiffuseTextureShadingNode->SetTextureName( "Diffuse" );

	sgMaterial->AddMaterialChannel( "Diffuse" );
	sgMaterial->SetShadingNetwork( "Diffuse", sgDiffuseTextureShadingNode );
	Simplygon::spTexture sgNormalsTexture = sg->CreateTexture();
	sgNormalsTexture->SetName( "Normals" );
	sgNormalsTexture->SetFilePath( normalsTextureFilePath.c_str() );
	sgTextureTable->AddTexture( sgNormalsTexture );

	Simplygon::spShadingTextureNode sgNormalsTextureShadingNode = sg->CreateShadingTextureNode();
	sgNormalsTextureShadingNode->SetTexCoordLevel( 0 );
	sgNormalsTextureShadingNode->SetTextureName( "Normals" );

	sgMaterial->AddMaterialChannel( "Normals" );
	sgMaterial->SetShadingNetwork( "Normals", sgNormalsTextureShadingNode );

	sgMaterialTable->AddMaterial( sgMaterial );

	sgScene->GetTextureTable()->Clear();
	sgScene->GetMaterialTable()->Clear();
	sgScene->GetTextureTable()->Copy(sgTextureTable);
	sgScene->GetMaterialTable()->Copy(sgMaterialTable);

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

}

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

	RunRemeshingWithMaterialCasting(sg);

	Simplygon::Deinitialize(sg);
}

