This example shows how to use the Opacity caster.

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

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


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

	// Create the reduction processor. 
	Simplygon::spReductionProcessor sgReductionProcessor = sg->CreateReductionProcessor();

	sgReductionProcessor->SetScene( sgScene );

	Simplygon::spReductionSettings sgReductionSettings = sgReductionProcessor->GetReductionSettings();
	Simplygon::spMappingImageSettings sgMappingImageSettings = sgReductionProcessor->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 );

	// Start the reduction process. 
	sgReductionProcessor->RunProcessing();

	// Setup and run the opacity material casting. 
	Simplygon::spOpacityCaster sgOpacityCaster = sg->CreateOpacityCaster();
	sgOpacityCaster->SetMappingImage( sgReductionProcessor->GetMappingImage() );
	sgOpacityCaster->SetSourceMaterials( sgScene->GetMaterialTable() );
	sgOpacityCaster->SetSourceTextures( sgScene->GetTextureTable() );
	sgOpacityCaster->SetOutputFilePath( "OpacityTexture" );

	Simplygon::spOpacityCasterSettings sgOpacityCasterSettings = sgOpacityCaster->GetOpacityCasterSettings();
	sgOpacityCasterSettings->SetMaterialChannel( "Opacity" );
	sgOpacityCasterSettings->SetOutputImageFileFormat( Simplygon::EImageOutputFormat::PNG );
	sgOpacityCasterSettings->SetOutputPixelFormat( Simplygon::EPixelFormat::R8 );

	sgOpacityCaster->RunProcessing();
	std::string opacityTextureFilePath = sgOpacityCaster->GetOutputFilePath();

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

	Simplygon::spShadingTextureNode sgOpacityTextureShadingNode = sg->CreateShadingTextureNode();
	sgOpacityTextureShadingNode->SetTexCoordLevel( 0 );
	sgOpacityTextureShadingNode->SetTextureName( "Opacity" );

	sgMaterial->AddMaterialChannel( "Opacity" );
	sgMaterial->SetShadingNetwork( "Opacity", sgOpacityTextureShadingNode );

	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( "Output.fbx" );
	if(!sgSceneExporter->RunExport())
		throw std::exception("Failed to save Output.fbx.");

}

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

	OpacityCasting(sg);

	Simplygon::Deinitialize(sg);

	return 0;
}