Example #1
0
    bool GLTFMesh::writeAllBuffers(std::ofstream& verticesOutputStream, std::ofstream& indicesOutputStream)
    {
        typedef map<std::string , shared_ptr<GLTF::GLTFBuffer> > IDToBufferDef;
        IDToBufferDef IDToBuffer;
        
        shared_ptr <AccessorVector> allAccessors = this->accessors();
        
        shared_ptr <GLTFBufferView> dummyBuffer(new GLTFBufferView());
        
        PrimitiveVector primitives = this->getPrimitives();
        unsigned int primitivesCount =  (unsigned int)primitives.size();
        for (unsigned int i = 0 ; i < primitivesCount ; i++) {            
            shared_ptr<GLTF::GLTFPrimitive> primitive = primitives[i];            
            shared_ptr <GLTF::GLTFIndices> uniqueIndices = primitive->getUniqueIndices();
            
            /*
                Convert the indices to unsigned short and write the blob 
             */
            unsigned int indicesCount = (unsigned int)uniqueIndices->getCount();
            
            shared_ptr <GLTFBufferView> indicesBufferView = uniqueIndices->getBufferView();
            unsigned char* uniqueIndicesBufferPtr = (unsigned char*)indicesBufferView->getBuffer()->getData();
            uniqueIndicesBufferPtr += indicesBufferView->getByteOffset();
            
            unsigned int* uniqueIndicesBuffer = (unsigned int*) uniqueIndicesBufferPtr;
            if (indicesCount <= 0) {
                // FIXME: report error
            } else {
                size_t indicesLength = sizeof(unsigned short) * indicesCount;
                unsigned short* ushortIndices = (unsigned short*)calloc(indicesLength, 1);
                for (unsigned int idx = 0 ; idx < indicesCount ; idx++) {
                    ushortIndices[idx] = (unsigned short)uniqueIndicesBuffer[idx];
                }
                    
                uniqueIndices->setByteOffset(static_cast<size_t>(indicesOutputStream.tellp()));
                indicesOutputStream.write((const char*)ushortIndices, indicesLength);
                
                //now that we wrote to the stream we can release the buffer.
                uniqueIndices->setBufferView(dummyBuffer);
                
                free(ushortIndices);
            }
        }
        
        for (unsigned int j = 0 ; j < allAccessors->size() ; j++) {
            shared_ptr <GLTFAccessor> accessor = (*allAccessors)[j];
            shared_ptr <GLTFBufferView> bufferView = accessor->getBufferView();
            shared_ptr <GLTFBuffer> buffer = bufferView->getBuffer();
            
            if (!bufferView.get()) {
                // FIXME: report error
                return false;
            }
                        
            if (!IDToBuffer[bufferView->getBuffer()->getID().c_str()].get()) {
                // FIXME: this should be internal to accessor when a Data buffer is set
                // for this, add a type to buffers , and check this type in setBuffer , then call computeMinMax
                accessor->computeMinMax();
                
                accessor->setByteOffset(static_cast<size_t>(verticesOutputStream.tellp()));
                verticesOutputStream.write((const char*)(static_pointer_cast <GLTFBuffer> (buffer)->getData()), buffer->getByteLength());

                //now that we wrote to the stream we can release the buffer.
                accessor->setBufferView(dummyBuffer);
                
                IDToBuffer[bufferView->getBuffer()->getID().c_str()] = buffer;
            } 
        }
                
        return true;
    }
Example #2
0
bool const JSONMesh::writeAllBuffers(std::ofstream& fileOutputStream)
{
    typedef map<std::string , shared_ptr<JSONExport::JSONBuffer> > IDToBufferDef;
    IDToBufferDef IDToBuffer;

    vector <shared_ptr< JSONExport::JSONAccessor> > allAccessors = this->remappedAccessors();

    shared_ptr <JSONBuffer> dummyBuffer(new JSONBuffer(0));

    std::vector< shared_ptr<JSONExport::JSONPrimitive> > primitives = this->getPrimitives();
    unsigned int primitivesCount =  (unsigned int)primitives.size();
    for (unsigned int i = 0 ; i < primitivesCount ; i++) {
        shared_ptr<JSONExport::JSONPrimitive> primitive = primitives[i];
        shared_ptr <JSONExport::JSONIndices> uniqueIndices = primitive->getUniqueIndices();

        /*
         Convert the indices to unsigned short and write the blob
         */

        unsigned int indicesCount = (unsigned int)uniqueIndices->getCount();
        unsigned int* uniqueIndicesBuffer = (unsigned int*) static_pointer_cast <JSONDataBuffer> (uniqueIndices->getBuffer())->getData();
        if (indicesCount <= 0) {
            // FIXME: report error
        } else {
            size_t indicesLength = sizeof(unsigned short) * indicesCount;
            unsigned short* ushortIndices = (unsigned short*)malloc(indicesLength);
            for (unsigned int idx = 0 ; idx < indicesCount ; idx++) {
                ushortIndices[idx] = (unsigned short)uniqueIndicesBuffer[idx];
            }

            uniqueIndices->setByteOffset(static_cast<size_t>(fileOutputStream.tellp()));
            fileOutputStream.write((const char*)ushortIndices, indicesLength);

            //now that we wrote to the stream we can release the buffer.
            uniqueIndices->setBuffer(dummyBuffer);

            free(ushortIndices);
        }
    }

    for (unsigned int j = 0 ; j < allAccessors.size() ; j++) {
        shared_ptr <JSONExport::JSONAccessor> accessor = allAccessors[j];
        shared_ptr <JSONExport::JSONBuffer> buffer = accessor->getBuffer();

        if (!buffer.get()) {
            // FIXME: report error
            return false;
        }

        if (!IDToBuffer[buffer->getID().c_str()].get()) {
            // FIXME: this should be internal to accessor when a Data buffer is set
            // for this, add a type to buffers , and check this type in setBuffer , then call computeMinMax
            accessor->computeMinMax();

            accessor->setByteOffset(static_cast<size_t>(fileOutputStream.tellp()));
            fileOutputStream.write((const char*)(static_pointer_cast <JSONDataBuffer> (buffer)->getData()), buffer->getByteSize());

            //now that we wrote to the stream we can release the buffer.
            accessor->setBuffer(dummyBuffer);

            IDToBuffer[buffer->getID()] = buffer;
        }
    }

    return true;
}