#include "../Common/Example.h"
#include "BMPheader.h"
void RunExampleCasting( const std::string& readFrom, const std::string& writeTo );
int main( int argc, char* argv[] )
{
try
{
InitExample();
sg->SetGlobalSetting("DefaultTBNType", SG_TANGENTSPACEMETHOD_ORTHONORMAL);
std::string assetPath = GetAssetPath();
RunExampleCasting(assetPath + "MaterialTestMesh/materialTestMesh.obj", "ObjectTextured");
DeinitExample();
}
catch (const std::exception& ex)
{
std::cerr << ex.what() << std::endl;
return -1;
}
return 0;
}
void RunExampleCasting( const std::string& readFrom, const std::string& writeTo )
{
std::string exePath = GetExecutablePath();
std::string outputGeometryFilename = exePath + writeTo + ".obj";
std::string outputDiffuse1Filename = exePath + writeTo + "_diffuse1.png";
std::string outputDiffuse2Filename = exePath + writeTo + "_diffuse2.png";
spWavefrontImporter objReader = sg->CreateWavefrontImporter();
objReader->SetImportFilePath( readFrom.c_str() );
if( !objReader->RunImport() )
throw std::exception("Failed to load input file!");
spScene scene = objReader->GetScene();
spMaterialTable originalMaterials = scene->GetMaterialTable();
spTextureTable originalTextures = scene->GetTextureTable();
spScene lodScene = sg->CreateScene();
lodScene->DeepCopy( scene );
spMaterialTable lodMatTable = lodScene->GetMaterialTable();
lodMatTable->Clear();
spTextureTable lodTexTable = lodScene->GetTextureTable();
lodTexTable->Clear();
spReductionProcessor red = sg->CreateReductionProcessor();
red->SetScene( lodScene );
spRepairSettings repairSettings = red->GetRepairSettings();
repairSettings->SetWeldDist( 0.2f );
repairSettings->SetTjuncDist( 0.2f );
spReductionSettings reductionSettings = red->GetReductionSettings();
reductionSettings->SetTriangleRatio( 1.0f );
spNormalCalculationSettings normalSettings = red->GetNormalCalculationSettings();
normalSettings->SetReplaceNormals( true );
normalSettings->SetHardEdgeAngleInRadians( 3.14159f * 90.f / 180.0f );
spMappingImageSettings mappingSettings = red->GetMappingImageSettings();
mappingSettings->SetGenerateMappingImage( true );
mappingSettings->SetGenerateTexCoords( true );
mappingSettings->SetParameterizerMaxStretch( 0.5f );
mappingSettings->SetInputMaterialCount( originalMaterials->GetMaterialsCount() );
mappingSettings->SetOutputMaterialCount( 2 );
mappingSettings->SetInputOutputMaterialMapping( 0, 0 );
mappingSettings->SetInputOutputMaterialMapping( 1, 0 );
mappingSettings->SetInputOutputMaterialMapping( 2, 1 );
mappingSettings->SetGutterSpace( 0, 4 );
mappingSettings->SetWidth( 0, 512 );
mappingSettings->SetHeight( 0, 512 );
mappingSettings->SetMultisamplingLevel( 0, 2 );
mappingSettings->SetGutterSpace( 1, 4 );
mappingSettings->SetWidth( 1, 1024 );
mappingSettings->SetHeight( 1, 1024 );
mappingSettings->SetMultisamplingLevel( 1, 2 );
red->RunProcessing();
spMappingImage mappingImage0 = red->GetMappingImage( 0 );
spMappingImage mappingImage1 = red->GetMappingImage( 1 );
spMaterial outputMaterial0 = sg->CreateMaterial();
outputMaterial0->SetName( "output_material1" );
lodMatTable->AddMaterial( outputMaterial0 );
spMaterial outputMaterial1 = sg->CreateMaterial();
outputMaterial1->SetName( "output_material2" );
lodMatTable->AddMaterial( outputMaterial1 );
if( true )
{
spColorCaster cast = sg->CreateColorCaster();
cast->SetColorType( SG_MATERIAL_CHANNEL_DIFFUSE );
cast->SetSourceMaterials( originalMaterials );
cast->SetSourceTextures( originalTextures );
cast->SetMappingImage( mappingImage0 );
cast->SetOutputChannels( 3 );
cast->SetOutputChannelBitDepth( 8 );
cast->SetDilation( 10 );
cast->SetOutputFilePath( outputDiffuse1Filename.c_str() );
cast->RunProcessing();
{
spTexture newTex = sg->CreateTexture();
newTex->SetFilePath( outputDiffuse1Filename.c_str() );
newTex->SetName( "mat0Diff" );
lodTexTable->AddTexture( newTex );
spShadingTextureNode texNode = sg->CreateShadingTextureNode();
texNode->SetTextureName( "mat0Diff" );
outputMaterial0->SetShadingNetwork( SG_MATERIAL_CHANNEL_DIFFUSE, texNode );
}
}
if( true )
{
spColorCaster cast = sg->CreateColorCaster();
cast->SetColorType( SG_MATERIAL_CHANNEL_DIFFUSE );
cast->SetSourceMaterials( originalMaterials );
cast->SetSourceTextures( originalTextures );
cast->SetMappingImage( mappingImage1 );
cast->SetOutputChannels( 3 );
cast->SetOutputChannelBitDepth( 8 );
cast->SetDilation( 10 );
cast->SetOutputFilePath( outputDiffuse2Filename.c_str() );
cast->RunProcessing();
{
spTexture newTex = sg->CreateTexture();
newTex->SetFilePath( outputDiffuse2Filename.c_str() );
newTex->SetName( "mat1Diff" );
lodTexTable->AddTexture( newTex );
spShadingTextureNode texNode = sg->CreateShadingTextureNode();
texNode->SetTextureName( "mat1Diff" );
outputMaterial1->SetShadingNetwork( SG_MATERIAL_CHANNEL_DIFFUSE, texNode );
}
}
spWavefrontExporter objExporter = sg->CreateWavefrontExporter();
objExporter->SetExportFilePath( outputGeometryFilename.c_str() );
objExporter->SetScene( lodScene );
objExporter->RunExport();
}