Standalone .mi File Renderer

The following code implements a miniature version of the mental ray standalone executable:

#include <stdio.h>
#include <shader.h>
#include <geoshader.h>
#include <mirelay.h>

static void memerror(void)  { mi_fatal("out of memory"); }
static void imgerror(miImg_file * const ifp)
                            { mi_fatal("file error, exiting."); }

int main(
    int             argc,
    char            *argv[])
{
    int             nthreads;
    miBoolean       resume = miFALSE;

    if (!mi_raylib_attach_process())
            return(1);
    mi_mem_error_handler(memerror);
    mi_img_err_handler(imgerror);
    mi_mem_init();
    mi_ntlib_init();
    nthreads = mi_raylib_license_get(mi_msg_no_of_cpus());
    mi_raylib_init(miFALSE, nthreads, miFALSE);
#ifdef _WIN32
    mi_link_set_module_handle(GetModuleHandle("libray.dll"));
#endif
    mi_mi_parse_rayrc(0, miFALSE);
    while (mi_mi_parse(argv[1], resume, 0, 0, 0, getc, miFALSE, 0)) {
            miTag root, caminst, cam, opt;
            miInh_func inh;
            mi_api_render_params(&root, &caminst, &cam, &opt, &inh);
            resume = mi_rc_run(miRENDER_DEFAULT, 0, 0, root, caminst,
                                                    cam, opt, inh);
            mi_api_render_release();
            if (!resume)
                    break;
    }
    mi_raylib_license_release();
    mi_raylib_exit();
    mi_raylib_detach_process();
    return(0);
}

The initialization and shutdown parts have already been discussed in this chapter. Four new functions were added:

mi_mi_parse_rayrc
This function reads the ray3rc startup file or, if it was not found, the rayrc startup file, which may contain link, registry, $include statements, and other .mi scene language statements that customize the local environment. Neither the include path nor the verbosity-override flag are used.
mi_mi_parse
This function implements the parser frame loop. It will parse the .mi scene file argv[1] and stop after each end of a frame definition. (Obviously there should be error checking here to make sure that argv[1] is defined.) The first time it is called with a resume flag set to miFALSE, which makes it open the scene file, and all later calls set resume to miTRUE to tell mi_mi_parse to continue parsing where it left off. The include path, filename override, file type override, and verbosity override arguments are not used. The function to read characters from the scene file is getc, and there is no frame callback.
mi_api_render_params
This function picks up the root group, camera instance, camera, and options tags and the global inheritance or traversal function from the scene.
mi_rc_run
This function performs preprocessing, tessellation, rendering, postprocessing, and all other operations necessary for rendering an image, and saves it to disk if the scene file contains the appropriate statements.
Copyright © 1986, 2015 NVIDIA ARC GmbH. All rights reserved.