GLTF: reading a gltf file from a memory block

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

GLTF: reading a gltf file from a memory block

Post by mootools » Thu Oct 07, 2021 12:32 pm

This is now possible to read and write GLTF from/to memory block.
This feature is useful to avoid read/write operation on the disk when you already have the gltf file loaded in your memory.

This can be done through the COMMON_IMPORT_FROM_MEMORY_DATA option as shown below:

Code: Select all

// Where data is you memory pointer on the GLTF data (unsigned char *data)
// Where memDataSize is the size of the block
CSceneImportOptions impoptions;
impoptions.GetParserOptions().SetBinary(COMMON_IMPORT_FROM_MEMORY_DATA, data, memDataSize); // Cf. note above
C3DIo file(L"datablock.glb", FILE_PARSER_LOADING);
C3DScene *newscene = file.Read(&impoptions);
XASSERT(newscene);

Note that the filename does not really matter but it is needed to know if the memory block is a glb or a gltf file.
This is done by the SDK using the provided extension.

This is also possible to write a scene to GLTF file to a memory block as shown here:

Code: Select all

CSceneExportOptions expoptions;
expoptions.GetParserOptions().SetBool(COMMON_EXPORT_TO_MEMORY, true);
expoptions.GetParserOptions().SetBool(GLTF_EXPORT_OPTION_EMBEDDED_MEDIA, true); // You can embed media with some restriction

C3DIo file(_T("MemoryScene.glb"), FILE_PARSER_SAVING);
bool result = file.Save(scene, &expoptions); // &options is optional and can be removed if you don't need JT
bool result = file.Save(scene, &expoptions); // &options is optional and can be removed if you don't need JT
if (result)
{
	unsigned int memDataSize = expoptions.GetParserOptions().GetDataSize(COMMON_EXPORT_MEMORY_DATA);
	unsigned char *data = (unsigned char *)xAllocateArray(unsigned char, memDataSize);

	expoptions.GetParserOptions().GetBinary(COMMON_EXPORT_MEMORY_DATA, data, memDataSize); // Get you memory block from the exporter

As above, the extension is used to know if you want to output to memory a gltf or glb memory block.
You can also embed media in the output block if the texture map is a JPEG, PNG, BMP or GIF file.

Note that read / write to memory block can be done using the update mode.
In this case the pipeline is the following:

  • Get gltf memory block, read it and retrieve the C3DScene pointer. Simply add:

    Code: Select all

    impoptions.flags = SCENE_IMPORT_UPDATE_MODE;
  • Optimize the C3DScene and get the optimize scene

  • Write back to gltf using the update feature to keep as most information as possible from the input gltf memory block. Simply add:

    Code: Select all

    expoptions.flags |= SCENE_EXPORT_TRY_UPDATE_FILE;

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest