User Tools

Site Tools


Loading SRT Files

SRT files can be loaded by the CCore class either by passing the SDK a filename or memory block. The memory block version expects a pointer to a block that’s essentially a binary SRT file read into a buffer. The Core library includes the utility function, LoadFileIntoBuffer(), that will read an SRT file and return a memory buffer that will work with LoadTree().

Note: When passing a memory block into the SDK, you must specify ownership of the block. To minimize the number of heap allocations that occur on load, the SDK can simply use offsets into the provided memory block as geometry references. The tradeoff using this approach is that the memory block cannot be deleted until after CCore::DeleteGeometry() is called. Alternatively, you can instruct the SDK to make an internal copy of the buffer after which its management will be handled automatically, freeing the application from restrictions on its own copy of the buffer. Traditionally, users will choose to manage the buffer directly.

The following example demonstrates how to read an SRT file and retrieve some of its data. Once the load is complete, all of the SRT data is immediately available.

using namespace SpeedTree;
 
///////////////////////////////////////////////////////////////////////  
//  Loading an SRT file
 
CCore* LoadAnSrtFile(const char* pFilename)
{
    CCore* pTree = NULL;
 
    // load file into buffer using static utility function
    st_uint32 uiBufferSize = 0;
    st_byte* pBuffer = CCore::LoadFileIntoBuffer(pFilename, uiBufferSize);
 
    // if the load succeeded, pass the buffer into the Core LoadTree() function
    if (pBuffer)
    {
        pTree = new CCore;
 
        const bool c_bRequestSdkInternalCopy = true;
        const bool c_bIsGrassModel = false;
        const float c_fGeometryScalar = 1.0f;
        if (pTree->LoadTree(pBuffer, 
                            uiBufferSize, 
                            c_bRequestSdkInternalCopy, 
                            c_bIsGrassModel, 
                            c_fGeometryScalar))
        {
            // NOTE: there is an overloaded LoadTree() function that takes
            // a filename directly, bypassing the need for LoadFileIntoBuffer()
            printf("successfully loaded: [%s]\n", pFilename);
        }
        else
        {
            // error will include reason for failure
            Error("Failed to load [%s]: %s\n", pFilename, CCore::GetError( ));
            st_delete(pTree);
        }
 
        // we requested for the SDK to make a copy of the buffer, so
        // let's delete the app-side copy
        st_delete_array(pBuffer);
    }
    else
        // error string will include filename & reason for failure
        Error("%s\n", CCore::GetError( ));
 
    return pTree;
}

Note the error handling code. If an error occurs within the Core library, it will try to provide some explanation for the failure via the CCore::GetError() function.

More on the SDK's error system.

Note: Once you've scanned or copied the geometry and materials, it can be deleted using CCore::DeleteGeometry(), leaving the rest of the CCore object intact. DeleteGeometry() will free about 85-90% of SRT file size from the heap.