0
$\begingroup$

I am currently writing a program to parse the .gltf file using the jansson C library for reading JSON files. I want to show how that library is effective in reading .gltf files so that apps can use this.

However, I am not sure how this will work. Depending on what it will be use (i.e. a game engine), I am not certain as to how it does.

int load_gltf(filename)
{
    CFILE *fp, *fpb;
    fp = cfopen(filename, "rb", CFILE_NORMAL, CF_TYPE_MODELS, false);
    if (!fp) {
        return 0;
    }

    int size = cfilelength(fp);
    std::string gltf_data;
    gltf_data.resize((size_t) size);
    cfread(&gltf_data[0], 1, size, fp);

    cfclose(fp);

    json_t* gltf_data_root = json_loads(gltf_data.c_str(), 0, nullptr);
    if (!gltf_data_root) {
        return 0;
    }
    if(!json_is_array(gltf_data_root)) {
        json_decref(gltf_data_root);
        return 0;
    }
    for(i = 0; i < json_array_size(gltf_data_root); i++) {
        json_t *gltf_data, *gltf_scenes, *gltf_scenes_nodes, *gltf_nodes, *gltf_nodes_mesh, *gltf_meshes, *gltf_buffers, *gltf_bufferViews, *gltf_accessors, *gltf_asset;
        gltf_data = json_array_get(gltf_data_root, i);
        if(!json_is_object(gltf_data)) {
            json_decref(gltf_data_root);
            return 0;
        }
        gltf_scenes = json_object_get(gltf_data, "scenes");
        if(!json_is_object(gltf_scenes)) {
            json_decref(gltf_data_root);
            return 0;
        }
        gltf_scenes_nodes = json_object_get(gltf_scenes, "nodes");
        if(!json_is_object(gltf_scenes_nodes)) {
            json_decref(gltf_data_root);
            return 0;
        }
        gltf_nodes = json_object_get(gltf_data, "nodes");
        if(!json_is_object(gltf_nodes)) {
            json_decref(gltf_data_root);
            return 0;
        }
        gltf_nodes_mesh = json_object_get(gltf_nodes, "mesh");
        if(!json_is_object(gltf_nodes_mesh)) {
            json_decref(gltf_data_root);
            return 0;
        }
        gltf_meshes = json_object_get(gltf_data, "meshes");
        if(!json_is_object(gltf_meshes)) {
            json_decref(gltf_data_root);
            return 0;
        }
        gltf_buffers = json_object_get(gltf_data, "buffers");
        if(!json_is_object(gltf_buffers)) {
            json_decref(gltf_data_root);
            return 0;
        }
        gltf_bufferViews = json_object_get(gltf_data, "bufferViews");
        if(!json_is_object(gltf_bufferViews)) {
            json_decref(gltf_data_root);
            return 0;
        }
        gltf_accessors = json_object_get(gltf_data, "accessors");
        if(!json_is_object(gltf_accessors)) {
            json_decref(gltf_data_root);
            return 0;
        }
        gltf_asset = json_object_get(gltf_data, "asset");
        if(!json_is_object(gltf_asset)) {
            json_decref(gltf_data_root);
            return 0;
        }
    }
    json_decref(gltf_data_root);
    return 1;
}
$\endgroup$
1
  • 1
    $\begingroup$ What is the current behaviour of this code ? It's time-consuming to extract this snippet and put it in a runnable project to test it. If you can let us know what currently works with it and what doesn't that would be helpful. $\endgroup$
    – PaulHK
    Feb 24 at 4:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.