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

simple_playback_osg_2.cpp

DI-Guy API Version 12.5.1

This file was automatically generated from simple_playback_osg_2.cpp. Do not edit this file directly; the changes will be lost.
This example program loads and plays back a DI-Guy Scenario (.dss) file using Open Scene Graph and the DI-Guy geometry loader.

This example makes use of the following 3rdparty software to create a 3D graphics environment:

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

#ifdef BDI_OS_TYPE_win32
#include <windows.h> // for timeGetTime()
#include <time.h>
#else
#include <sys/time.h>
#include <unistd.h>
#endif

#include <GL/glut.h>

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

#include <osg/Program>
#include <osg/Shader>
#include <osg/Uniform>

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 "diguyOsgGraphicsLink.h"
#include "diguyOsgGraphicsShape.h"
#include "diguyOsgGraphicsMesh.h"
#include "diguyOsgGraphicsState.h"
#include "diguyOsgGraphicsMaterial.h"
#include "diguyOsgGraphicsTexture.h"
#include "diguyOsgGraphicsShaderProgram.h"
#include "diguyOsgGraphicsShaderInstance.h"
const char* arg_set = "sets/default_stage11/geometry/flt/default_stage11.flt";
const char* arg_dss_filename = "example_seg_and_sk.dss";

static double t = 0.0;
static double t_prev = 0.0;
#define AT_T(t_now) ((t_prev < t_now) && (t >= t_now))

static diguyScenario* scenario = NULL;
static diguyCharacter* character1 = NULL;
static diguyCharacter* character2 = NULL;

static osg::ref_ptr<osg::Group> osg_diguy_attach_pt = NULL;

void view_init_time(const double start);
double view_get_time();
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();

    /*
     *  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_DEBUG, "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->addEventHandler(new osgViewer::StatsHandler);
    viewer->realize();

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

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

    viewer->setSceneData(osg_diguy_attach_pt);

    diguyOsgGraphicsLink::set_diguy_attach_ptr(osg_diguy_attach_pt);
    diguyOsgGraphicsLink::set_viewer(viewer);

    /*
     *  Set graphics object create functions.  This has to be done before
     *   calling diguy_graphics_initialize().
     */
    diguy_graphics_set_link_create_func(diguyOsgGraphicsLink_create_func);
    diguy_graphics_set_shape_create_func(diguyOsgGraphicsShape_create_func);
    diguy_graphics_set_mesh_create_func(diguyOsgGraphicsMesh_create_func);
    diguy_graphics_set_state_create_func(diguyOsgGraphicsState_create_func);
    diguy_graphics_set_material_create_func(diguyOsgGraphicsMaterial_create_func);
    diguy_graphics_set_texture_create_func(diguyOsgGraphicsTexture_create_func);
    diguy_graphics_set_shader_program_create_func(diguyOsgGraphicsShaderProgram_create_func);
    diguy_graphics_set_shader_instance_create_func(diguyOsgGraphicsShaderInstance_create_func);

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

    /*
     *  Add it and turn it on
     */
    osg::LightSource* lightsource = new osg::LightSource();
    lightsource->setLight(light1);
    lightsource->setStateSetModes(*state_set, osg::StateAttribute::ON);
    osg_diguy_attach_pt->addChild(lightsource);

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

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

    /*
     *  Uncomment to load a background.  Use the built-in OpenFlight loader
     *   for this.
     */
    //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.
     */
    bdiGraphicsInitGraphicsAPI init;
    diguy_graphics_get_defaults(&init);

    init.use_array_format_for_vertex_buffer_data = 0;
    init.default_vertex_type = DIGUY_GRAPHICS_INDEX_TYPE_USHORT;
    init.transpose_shader_matrices = 1;
    init.use_cached_optimized_geometry = 1;
    init.use_cached_compressed_textures = 1;

    /*
     *  OSG can't handle sRGB textures of any sort.
     */
    init.use_sRGB_corrected_textures = 0;

    if (diguy_graphics_initialize(&init))
    {
        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 scenario-specified scene objects.
     */
    scenario->set_scene_objects_enabled(0);

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

    /*
     *  Save the camera position for use by the bump map shader.
     */
    scenario->get_scenario_camera()->set_position(eye_point.x(), eye_point.y(), eye_point.z());

    /*
     *  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();

    window = NULL;
    viewer = NULL;

    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;

    // uncomment to test dynamic lights
    //viewer->getLight()->setPosition(osg::Vec4(cos(t), sin(t), 1.0f, 0.0f));
    if (scenario)
    {
        if (character2 &&
            (!strcmp(character2->get_character_type_string(), "soldier_07")))
        {
            if (AT_T(1.0f))
                character2->set_desired_action("walk");

            if (AT_T(4.0f))
                character2->set_desired_action("walk_low");

            if (AT_T(7.0f))
            {
                character2->set_desired_action("prone_aim");
                character2->aim_at_angle_local(0.0f, 0.0f, 10000.0f);
            }

            if (AT_T(12.0f))
                character2->fire_weapon_n_times(4, 1.0f, "m16");
        }

        if (character1 &&
            (!strcmp(character1->get_character_type_string(), "mped_07")))
        {
            if (AT_T(1.0f))
                character1->set_desired_action("walk");

            if (AT_T(7.0f))
                character1->set_desired_action("angry_yell");
        }

        scenario->update(t);
    }

    glutPostRedisplay();
}
glut_display_callback
Description:

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

void
glut_display_callback(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    /*
     *  Don't need to draw() the scenario; drawing is handled internally
     *   by OSG.
     */
    //scenario->draw();

    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.