Optimize a file and update it keeping as many information as possible

Question and answer about Polygon Cruncher SDK
Post Reply
mootools
Site Admin
Posts: 282
Joined: Thu Jul 05, 2007 11:06 am
Contact:

Optimize a file and update it keeping as many information as possible

Post by mootools » Thu Apr 13, 2017 3:44 pm

Polygon Cruncher SDK allows to open a file, optimizes its contents then save the results using the original file as template.
Not all the file format allows this options, but you can use it with FBX, Collada, LXO, GLTF or Cinema4D for example.

The update mecanism is done using the UPDATE mecanism available in the I/O.
There is few things to follow in order to activate UPDATE mode.

  • Read the scene file notifying we would like to update later. By default the update capabilities is turned off as it required more memory.
    To turn on UPDATE, you must provide SCENE_IMPORT_UPDATE_MODE when importing the scene.

    Code: Select all

    	CSceneImportOptions importOptions;
    	importOptions.flags = SCENE_IMPORT_UPDATE_MODE; // This is the better way to keep as many information from your file
    	CXString filename = _T("myFBXFile.fbx");
    	C3DIo file(filename, FILE_PARSER_LOADING);
    	C3DScene *scene = file.Read(&importOptions);
    	if (!scene)
    		return 1;
    
  • Process the scene file in the way you want. Only geometric and channels info will be updated in the output file.

  • Save to same format using the UPDATE mode once again, to make the output taking care of the input file.
    To turn on UPDATE, you must provide SCENE_EXPORT_TRY_UPDATE_FILE when saving the scene.
    The try will be a success if both input and output format are similar and if the format support update.

    Code: Select all

    	CXString outputfile = _T("myOutputFBX.fbx");
    	CSceneExportOptions exportOptions;
    	exportOptions.flags = SCENE_EXPORT_TRY_UPDATE_FILE; // This will create a new file using the information contained in the original file
    
    C3DIo outputFile(outputfile, FILE_PARSER_SAVING);
    if (!outputFile.Save(sceneToSave, &exportOptions))
    {
    	_tprintf(FormatString(_T("%s was not saved. An error occured\n"), outputfile));
    }
    

The following sample opens a file, optimize it (dynamically) to 50% then update the original content.

Code: Select all

#define CRUNCHERSDK_DLL_USE
#define MOOTOOLS_STANDARD_WINDOWS_HEADERS
#define USE_MOOTOOLS_NAMESPACE

#include "common_cpp_type.h"
#include "ApplicationTools.h"
#include "3DType.h"
#include "3DScene.h"
#include "SceneOptimizer.h"
#include "MultiresolutionObject.h"
#include "3DExtObject.h"
#include "3DSceneNode.h"
#include "Io3dmgr.h"

int Process()
{
	CSceneImportOptions importOptions;
	importOptions.flags = SCENE_IMPORT_UPDATE_MODE; // This is the better way to keep as many information from your file
	CXString filename = _T("myFBXFile.fbx");
	C3DIo file(filename, FILE_PARSER_LOADING);
	C3DScene *scene = file.Read(&importOptions);
	if (!scene)
		return 1;

// Very important !
// Convert scene and use C3DExtObject which are required for optimizing
C3DPolygonCruncherObjectCreator cruncherCreator(DEFAULT_OPTIMIZATION_OBJECT);
scene->ConvertToType(NULL, &cruncherCreator);

// Create the optimizer and attach scene
CSceneOptimizer *optimizer = xNew(CSceneOptimizer);
optimizer->SetFlag(SCENEOPTIMIZER_TRACK_CHANGES, true);
optimizer->SetFlag(SCENEOPTIMIZER_PROGRESSIVE_RATIO, true); // Better results when several meshes
optimizer->SetScene(*scene, true);

#define DEFAULT_OPTIMIZATION_MODE OPTIMIZE_PROTECT_BORDER|OPTIMIZE_VERIFY_PAIRS_MASK|OPTIMIZE_KEEP_MATERIAL_FRONTIER|OPTIMIZE_PROTECT_MATERIAL_FRONTIER|OPTIMIZE_KEEP_UV|OPTIMIZE_PROTECT_UV

// Define some optimization settings
optimizer->LockInitialization();
optimizer->SetOptimizeMode(DEFAULT_OPTIMIZATION_MODE, true);
optimizer->UnlockInitialization();

// Compute the dynamic mesh
bool cancel;
optimizer->Optimize(0.0f, cancel);

// Get the dynamic mesh
if (optimizer->GetScene(OPTIMIZED_MULTIRESOLUTION_SCENE) == NULL) // Compute the multiresolution scene
{
	xDelete(optimizer);
	return 1;
}

optimizer->SetMultiresolutionRatio(0.5f, 0, OPTIMIZE_TO_POINT|OPTIMIZE_TO_RATIO);

C3DScene *sceneToSave = optimizer->GetScene(OPTIMIZED_CLEANED_MULTIRESOLUTION_SCENE);

CXString outputfile = _T("myOutputFBX.fbx");
CSceneExportOptions exportOptions;
exportOptions.flags = SCENE_EXPORT_TRY_UPDATE_FILE; // This will create a new file using the information contained in the original file

C3DIo outputFile(outputfile, FILE_PARSER_SAVING);
if (!outputFile.Save(sceneToSave, &exportOptions))
{
	_tprintf(FormatString(_T("%s was not saved. An error occured\n"), outputfile));
}
else
	_tprintf(FormatString(_T("%s was saved (using default options"), outputfile));

// optimizer deletes the scene
xDelete(optimizer);
}

#ifdef __WINDOWS__
int wmain()
#elif defined(__APPLE__) || defined(__LINUX__)
int main()
#endif
{
	InitMootoolsApplication(MOOTOOLS_LICENSE_KEY); // Init mootools application with default settings

int result = Process();

CloseMootoolsApplication(); // Close mootools application properly

return result;
}

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests