DI-Guy Graphics API, Scene Graph with External Loader Example

simple_playback_osg_2_ext.cpp

DI-Guy API Version 12.5.1

This file was automatically generated from simple_playback_osg_2_ext.cpp. Do not edit this file directly; the changes will be lost.
Note: Use of external loaders is being deprecated as of DI-Guy version 11. The format of data used for skinned appearances is much more restricted than that for static appearances. The loading of Collada .dae files using an External Loader often does not provide the data in a format that works for DI-Guy. Use of an External Loader for new development is not recommended!

This example program would load and plays back a DI-Guy Scenario (.dss) file using Open Scene Graph and the OSG geometry loader.

This example exists only as a code example. It currently does not run, as there is no external loader available in OSG that can load compressed DI-Guy geometry files.

Header files and forward declarations
#include <math.h>

#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/TrackballManipulator>
#include <osgDB/ReadFile>

osg::ref_ptr<osgViewer::Viewer> viewer;
osg::observer_ptr<osgViewer::GraphicsWindow> window;

#include <diguy_graphics_api.h>

#include <diguy_module_script_lua.h>

#include <diguyApp.h>
#include <diguyScenario.h>
#include <diguyViewCamera.h>
#include <diguyCharacter.h>
#include <libbdilog.h>

#include "ogl_headers.h"
#include "diguyOsgExtGraphicsLink.h"
#include "diguyOsgExtGraphicsShape.h"
#include "diguyOsgExtGraphicsFile.h"

void view_init_time(const double start);
double view_get_time();
const char* arg_set = "sets/default_stage/default_stage.flt";
const char* arg_dss_filename = "example.dss";

static double time_base = 0.0f;
static double t = 0.0;
static double t_prev = 0.0;

static diguyScenario* scenario = NULL;
main
Description:

Initialize the 3D environment, the DI-Guy Graphics API, and various DI-Guy modules. Once initiailzed, start the main loop of the example.

int
main(int argc, char** argv)
{
    void glut_idle_callback(void);
    void glut_display_callback(void);
    void glut_keyboard_callback(unsigned char key, int x, int y);
    void shutdown();

bdi_log_printf(BDI_LOG_ERROR,
    "\n\n"
    "*********************************************************************\n"
    "This example exists only as a code example.  It currently does not\n"
    "run, as there is no external loader available in OSG that can load\n"
    "compressed DI-Guy geometry files.\n"
    "*********************************************************************\n"
    "\n\n");
exit(EXIT_SUCCESS);

    /*
     *  These calls initialize the BDI logging mechanism.  Log messages of
     *   priority BDI_LOG_DEBUG or higher get recorded into the file
     *   diguy_log.txt.  Log messages of priority BDI_LOG_WARN or higher get
     *   printed on standard out.
     */
    bdi_log_file_enable(BDI_LOG_WARN, "diguy_log.txt", 1);
    bdi_log_stdout_enable(BDI_LOG_WARN);

    /*
     *  Decide which file to play back.
     */
    if (argc > 1)
        arg_dss_filename = argv[1];

    /*
     *  Set up window.
     */
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_ALPHA);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(800, 600);
    glutCreateWindow(argv[0]);

    /*
     *  Create the OSG viewer.
     */
    viewer = new osgViewer::Viewer;
    window = viewer->setUpViewerAsEmbeddedInWindow(100, 100, 800, 600);

    //viewer->setCameraManipulator(new osgGA::TrackballManipulator);
    viewer->addEventHandler(new osgViewer::StatsHandler);
    viewer->realize();

    /*
     *  Create a scene root node.
     */
    osg::ref_ptr<osg::Group> osg_diguy_attach_pt = new osg::Group();

    osg::StateSet* state_set = new osg::StateSet;
    osg_diguy_attach_pt->setStateSet(state_set);

    /*
     *  Set graphics object create functions.  This has to be done before
     *   calling diguy_graphics_initialize().
     *
     *  Because we're using an external loader we override the
     *   diguyGraphicsFile class instead of diguyGraphcisMesh, etc.
     *
     *  The Open Scene Graph .dae loader plugin does not read DI-Guy Collada
     *   files in a way that works with DI-Guy shaders, so skinned
     *   appearances will not be available in this example.
     */
    diguy_graphics_set_link_create_func(diguyOsgExtGraphicsLink_create_func);
    diguy_graphics_set_shape_create_func(diguyOsgExtGraphicsShape_create_func);
    diguy_graphics_set_file_create_func(diguyOsgExtGraphicsFile_create_func);

    diguyOsgExtGraphicsLink::set_diguy_attach_ptr(osg_diguy_attach_pt);

    viewer->setSceneData(osg_diguy_attach_pt);

    /*
     *  Create a light.
     */
    osg::Light* light1 = new osg::Light;
    light1->setLightNum(0);
    light1->setPosition(osg::Vec4(1.0f, 1.0f, 1.0f, 0.0f));
    light1->setAmbient(osg::Vec4(0.7f, 0.7f, 0.7f, 1.0f));
    light1->setDiffuse(osg::Vec4(0.8f, 0.8f, 0.8f, 1.0f));
    light1->setDirection(osg::Vec3(1.0f, 1.0f, -1.0f));

    viewer->setLight(light1);

    /*
     *  Initialize the view to look at the center of the scene graph.
     */
    osg::Matrix view_matrix;
    view_matrix.makeLookAt(osg::Vec3(0.0f, 18.0f, 2.0f),
        osg::Vec3(0.0f, 0.0f, 1.0f),
        osg::Vec3(0.0f, 0.0f, 1.0f));

    viewer->getCamera()->setViewMatrix(view_matrix);

    /*
     *  Load a background.
     */
    diguyApp* app = diguyApp::get_app();
    const char* geometry_dir = app->get_diguy_subdir_path("geometry");
    char set_filename[512];
    sprintf(set_filename, "%s/%s",
        geometry_dir,
        arg_set);

    osg::ref_ptr<osg::Node> background_node = osgDB::readNodeFile(set_filename);
    if (background_node != NULL)
    {
        osg_diguy_attach_pt->addChild(background_node);
    }
    else
    {
        bdi_log_printf(BDI_LOG_WARN,
            "WARNING: Failed to load background set '%s'.\n",
            set_filename);
    }

    /*
     *  Initialize DI-Guy.
     */
    if (diguy_graphics_initialize(NULL))
    {
        bdi_log_printf(BDI_LOG_ERROR, "DI-Guy failed to initialize.\n");
        exit(EXIT_FAILURE);
    }

    /*
     *  Initialize the DI-Guy lua interpreter.
     */
    if (diguy_script_lua_initialize())
    {
        bdi_log_printf(BDI_LOG_WARN, "WARNING: DI-Guy lua interpreter failed to initialize.\n");
    }

    /*
     *  Create a scenario.
     */
    scenario = diguy_create_scenario();
    if (!scenario)
    {
        bdi_log_printf(BDI_LOG_ERROR, "DI-Guy failed to create a scenario.\n");
        exit(EXIT_FAILURE);
    }

    /*
     *  Disable scene objects.
     */
    scenario->set_scene_objects_enabled(0);

    /*
     *  Load the dss file.
     */
    scenario->load(arg_dss_filename);

    /*
     *  Start time now.
     */
    t = 0.0;
    view_init_time(t);

    /*
     *  Start the glut main loop.
     */
    glutIdleFunc(glut_idle_callback);
    glutDisplayFunc(glut_display_callback);
    glutKeyboardFunc(glut_keyboard_callback);
    glutMainLoop();

    return 0;
}
shutdown
Description:

Deinitialize all of DI-Guy and exit the program.

void
shutdown()
{
    glutIdleFunc(NULL);

    diguy_destroy_scenario(scenario);

    diguy_script_lua_deinitialize();

    diguy_deinitialize();

    exit(EXIT_SUCCESS);
}
glut_idle_callback
Description:

Called by glut periodically, when not responding to other events. Advance simulation time and update DI-Guy.

void
glut_idle_callback(void)
{
    t_prev = t;
    t = view_get_time();

    // debug timing uses fixed step dt
    //t += 0.025;

    if (scenario)
    {
        if (t >= scenario->get_tout())
            shutdown();

        scenario->update(t);
    }

    glutPostRedisplay();
}
glut_display_callback
Description:

Called by glut when it's time to re-render.

void
glut_display_callback(void)
{
    glPushMatrix(); {

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        scenario->draw();

    } glPopMatrix();

    if (viewer.valid())
        viewer->frame();

    glutSwapBuffers();
}
glut_keyboard_callback
Description:

Called by glut on a key press. Handle the key press.

void
glut_keyboard_callback(unsigned char key, int /*x*/, int /*y*/)
{
    switch (key)
    {
        case 'q':
        case 27:
            shutdown();
            break;
        default:
            if (window.valid())
            {
                window->getEventQueue()->keyPress((osgGA::GUIEventAdapter::KeySymbol) key);
                window->getEventQueue()->keyRelease((osgGA::GUIEventAdapter::KeySymbol) key);
            }
            break;
    }
}
view_init_time
Description:

Initialize time clock. Subsequent calls to view_get_time() will return times relative to the current time.

static double time_base_double;
struct timeval time_base_struct;

void
view_init_time(const double start)
{

#if BDI_OS_TYPE_win32

    time_base_double = (timeGetTime() / 1000.0) - start;

#else

    struct timezone time_zone_struct;

    gettimeofday(&time_base_struct, &time_zone_struct);
    time_base_double = start;

#endif

}
view_get_time
Description:

Return current time relative to base time computed in view_init_time().

double
view_get_time()
{

#if BDI_OS_TYPE_win32

    double time_now = (double) (timeGetTime() / 1000.0);
    return time_now - time_base_double;

#else

    struct timeval now;
    struct timezone time_zone_struct;

    gettimeofday(&now, &time_zone_struct);

    return (time_base_double
        + (double)(now.tv_sec - time_base_struct.tv_sec)
        + 0.000001 * (double)(now.tv_usec - time_base_struct.tv_usec));

#endif

}



Copyright (C) 1992-2012 Boston Dynamics

ALL RIGHTS RESERVED.

These coded instructions, statements, and computer programs contain unpublished proprietary information of Boston Dynamics and are protected by Copyright Laws of the United States. They may not be used, duplicated, or disclosed in any form, in whole or in part, without the prior written consent from Boston Dynamics.

RESTRICTED RIGHTS LEGEND

Use, duplication, or disclosure by the government is subject to restrictions as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights in Technical Data and Computer Sofware clause at DFARS 252.227-7013 and/or in similar or successor clauses in the FAR, or the DOD or NASA FAR Supplement, or to subparagraphs (c)(1) and (c)(2) of the Commercial Computer Software--Restricted Rights at 48 CFR 52.227-19, as applicable. Unpublished-rights reserved under the Copyright Laws of the United States.

Contractor/Manufacturer is:

Boston Dynamics/78 Fourth Avenue/Waltham MA 02451.