DI-Guy Graphics API, diguyOsgExtGraphicsShape

diguyOsgExtGraphicsShape.cpp

DI-Guy API Version 12.5.1

This file was automatically generated from diguyOsgExtGraphicsShape.cpp. Do not edit this file directly; the changes will be lost.
Introduction
Shapes are the high-level containers of visible geometry. They are attached to links, and contain zero or more mesh objects.

The class diguyOsgExtGraphicsShape is an example of a diguyGraphicsShape subclass for a retained renderer using an external loader. The example subclass shows how to use the accessors of the diguyGraphicsShape class and which virtual functions should be overridden for an immediate mode renderer such as Open Scene Graph.

Header files and forward declarations
#include "diguyOsgExtGraphicsShape.h"
#include "diguyOsgExtGraphicsLink.h"

#include <libbdilog.h>

#include <diguyScenario.h>
#include <diguyCharacter.h>

/*
 *  Top-level static variable that determines whether LOD calculations are
 *   based strictly on distance of object from camera, or on size of object,
 *   in pixels, in frame.
 */
bool diguyOsgExtGraphicsShape::s_use_pixel_lods = false;
Constructor
Description:

Initialize data members of the class to a known state.

Base class data will not be fully initialized until the Build Stage is done, so base class accessors should not be called here.

diguyOsgExtGraphicsShape::diguyOsgExtGraphicsShape(void* internal_data)
    :
    diguyGraphicsShape(internal_data),
    m_switch_node(NULL),
    m_lod_node(NULL),
    m_manual_lod_switch_node(NULL)
{
    /*
     *  Nothing else to do after calling base class constructor and
     *   initializing data members.
     */
}
build
Build Stage

Description:

Override of diguyGraphicsShape::build().

The build() function should look up the shape's parts from previously loaded geometry files, and create the necessary objects that will apply offset data of this shape from its parent link during the Draw Stage.

This implementation creates the nodes necessary to switch the shape geometry on and off, apply lod calculations, and look up and attach nodes found in a DI-Guy geometry file.

void
diguyOsgExtGraphicsShape::build()
{
    int lod;

    diguyGraphicsLink* diguy_link = get_link();
    diguyOsgExtGraphicsLink* osg_link = (diguyOsgExtGraphicsLink *) diguy_link;

    osg::MatrixTransform* link_dof_node = osg_link->get_dof_node();

    if (!link_dof_node)
        return;

    /*
     *  By default we'll attach meshes to the link's dof node.  If this
     *   shape is switched, however (e.g. for switchable hands), we'll create
     *   a swtich node and attach them to the switch.
     */
    osg::Group* lod_attach_node = link_dof_node;

    /*
     *  See if we should create a switch.
     */
    if (get_switch_var_name() != NULL)
    {
        m_switch_node = new osg::Switch();
        m_switch_node->setNewChildDefaultValue(true);
        link_dof_node->addChild(m_switch_node);

        lod_attach_node = m_switch_node;
    }

    /*
     *  Determine whether automatic graphics LOD switching is enabled.
     *   This will determine which kind of node we create for LOD switching.
     */
    diguyCharacter* character = get_character();
    diguyScenario* scenario = character ? character->get_scenario() : NULL;
    int automatic_graphics_lod_switching = scenario ? scenario->get_automatic_graphics_lod_switching() : 1;

    /*
     *  If automatic switching is enabled, create an osg::LOD() node.
     */
    if (automatic_graphics_lod_switching)
    {
        /*
         *  Determine the lod_ranges.
         */
        float* lod_ranges = get_lod_ranges();

        /*
         *  Create an lod node.
         */
        m_lod_node = new osg::LOD();
        m_lod_node->setCenter(osg::Vec3f(0.0f, 0.0f, 0.0f));

        if (s_use_pixel_lods)
            m_lod_node->setRangeMode(osg::LOD::PIXEL_SIZE_ON_SCREEN);
        else
            m_lod_node->setRangeMode(osg::LOD::DISTANCE_FROM_EYE_POINT);

        lod_attach_node->addChild(m_lod_node);

        /*
         *  Attach shape parts.
         */
        for (lod=1; lod<=get_num_lods(); lod++)
        {
            void* diguy_part = get_diguy_graphics_file_part(lod);
            if (diguy_part)
            {
                osg::Node* osg_part = (osg::Node*) diguy_part;

                if (s_use_pixel_lods)
                {
                    /*
                     *  Because DI-Guy LOD ranges must increase at each index,
                     *   but unlike with distances the earlier numbers represent
                     *   low LODs, we have to reverse the order of lod ranges.
                     *
                     *  LOD 1: lod_ranges[6] to lod_ranges[7]
                     *  LOD 2: lod_ranges[5] to lod_ranges[6]
                     *  etc.
                     */
                    m_lod_node->addChild(osg_part, lod_ranges[7 - lod], lod_ranges[8 - lod]);
                }
                else
                {
                    /*
                     *  LOD 1: lod_ranges[0] to lod_ranges[1]
                     *  LOD 2: lod_ranges[1] to lod_ranges[2]
                     *  etc.
                     */
                    m_lod_node->addChild(osg_part, lod_ranges[lod-1], lod_ranges[lod]);
                }
            }
        }
    }

    /*
     *  Else automatic LOD switching is not enabled.  Create a switch node
     *   for manual LOD switching.
     */
    else
    {
        m_manual_lod_switch_node = new osg::Switch();
        m_manual_lod_switch_node->setNewChildDefaultValue(false);

        lod_attach_node->addChild(m_manual_lod_switch_node);

        /*
         *  Create a geode for each lod.
         *  Attach mesh drawables to geodes.
         */
        for (lod=1; lod<=get_num_lods(); lod++)
        {
            void* diguy_part = get_diguy_graphics_file_part(lod);

            if (diguy_part)
            {
                osg::Node* osg_part = (osg::Node*) diguy_part;
                m_manual_lod_switch_node->addChild(osg_part);
                m_manual_lod_switch_node->setSingleChildOn(get_graphics_lod() - 1);
            }
        }
    }

///	/*
///	 *  Create a switch node.
///	 */
///	m_switch_node = new osg::Switch();
///	m_switch_node->setNewChildDefaultValue(true);
///	link_dof_node->addChild(m_switch_node);
///
///	/*
///	 *  Determine the lod_ranges.
///	 */
///	float* lod_ranges = get_lod_ranges();
///
///	/*
///	 *  Create an lod node.
///	 */
///	m_lod_node = new osg::LOD();
///	m_lod_node->setCenter(osg::Vec3f(0.0f, 0.0f, 0.0f));
///
///	m_switch_node->addChild(m_lod_node);
///
///	/*
///	 *  Attach shape parts.
///	 */
///	for (lod=1; lod<=get_num_lods(); lod++)
///	{
///		void* diguy_part = get_diguy_graphics_file_part(lod);
///		if (diguy_part)
///		{
///			osg::Node* osg_part = (osg::Node*) diguy_part;
///			m_lod_node->addChild(osg_part, lod_ranges[lod-1], lod_ranges[lod]);
///		}
///	}
}
unbuild
Unbuild Stage

Description:

Override of diguyGraphicsShape::unbuild().

The unbuild() function should destroy the objects created in the build() function.

This implementation detaches and dereferences the nodes created and attached in the build() function.

void
diguyOsgExtGraphicsShape::unbuild()
{
    diguyGraphicsLink* diguy_link = get_link();
    diguyOsgExtGraphicsLink* osg_link = (diguyOsgExtGraphicsLink *) diguy_link;

    osg::MatrixTransform* link_dof_node = osg_link->get_dof_node();

    if (!link_dof_node)
        return;

    osg::Group* lod_detach_node = link_dof_node;

    /*
     *  Remove all of the group nodes we created and added in the build()
     *   call; this should reduce the ref count of the added nodes by 1, to
     *   1.  Then set the pointers to NULL; this should reduce the ref count
     *   to 0, resulting in the group nodes being deleted.
     *
     *  Setting the m_switch_node ref_ptr to NULL dereferences the Switch
     *   object it points to, possibly deleting it.  Ditto for m_lod_node and
     *   m_manual_lod_switch_node.
     */
    if (m_switch_node != NULL)
        lod_detach_node = m_switch_node;

    if (m_lod_node != NULL)
    {
        lod_detach_node->removeChild(m_lod_node);
        m_lod_node = NULL;
    }
    else if (m_manual_lod_switch_node != NULL)
    {
        lod_detach_node->removeChild(m_manual_lod_switch_node);
        m_manual_lod_switch_node = NULL;
    }

    if (m_switch_node != NULL)
    {
        link_dof_node->removeChild(m_switch_node);
        m_switch_node = NULL;
    }

///	diguyGraphicsLink* diguy_link = get_link();
///	diguyOsgExtGraphicsLink* osg_link = (diguyOsgExtGraphicsLink *) diguy_link;
///
///	osg::MatrixTransform* link_dof_node = osg_link->get_dof_node();
///
///	if (!link_dof_node)
///		return;
///
///	/*
///	 *  Detach shape parts.
///	 */
///	int lod;
///	for (lod=1; lod<=get_num_lods(); lod++)
///	{
///		void* diguy_part = get_diguy_graphics_file_part(lod);
///		if (diguy_part)
///		{
///			osg::Node* osg_part = (osg::Node*) diguy_part;
///			m_lod_node->removeChild(osg_part);
///		}
///	}
///
///	/*
///	 *  Dereference lod node.
///	 */
///	m_switch_node->removeChild(m_lod_node);
///	m_lod_node = NULL;
///
///	/*
///	 *  Dereference switch.
///	 */
///	link_dof_node->removeChild(m_switch_node);
///	m_switch_node = NULL;
}
show
Update Stage

Description:

Override of diguyGraphicsShape::show().

The show() function should take the necessary steps to make the shape's visible geometry visible.

This implementation turns all children of the shape's switch node on.

void
diguyOsgExtGraphicsShape::show()
{
    if (m_switch_node)
        m_switch_node->setAllChildrenOn();
}
hide
Update Stage

Description:

Override of diguyGraphicsShape::hide().

The hide() function should take the necessary steps to make the shape's visible geometry hidden.

This implementation turns all children of the shape's switch node off.

void
diguyOsgExtGraphicsShape::hide()
{
    if (m_switch_node)
        m_switch_node->setAllChildrenOff();
}
set_graphics_lod_ranges
Description:

Override of diguyGraphicsShape::set_graphics_lod_ranges(), called as needed.

The set_graphics_lod_ranges() function should update the ranges of lod objects created in the build() function with the passed ranges.

In this implementation the ranges of the osg::LOD created in build() are updated. If automatic LOD switching is enabled, the LOD node will not exist and this function will have no effect.

void
diguyOsgExtGraphicsShape::set_graphics_lod_ranges(float* lod_ranges)
{
    if (m_lod_node != NULL)
    {
        int lod;
        for (lod=1; lod<=get_num_lods(); lod++)
        {
            m_lod_node->setRange(lod-1, lod_ranges[lod-1], lod_ranges[lod]);
        }
    }
}
set_graphics_lod
Description:

Override of diguyGraphicsShape::set_graphics_lod(), called as needed.

The set_graphics_lod() function should select and show the object representing the passed LOD.

This implementation manually sets the osg::Switch created in build() to show the correct geode. If automatic LOD switching is enabled, the switch node will not exist and this function will have no effect.

void
diguyOsgExtGraphicsShape::set_graphics_lod(int lod)
{
    if (m_manual_lod_switch_node != NULL)
    {
        m_manual_lod_switch_node->setSingleChildOn(lod-1);
    }
}
Create function definition
Create and return an object of a subclass of diguyGraphicsShape. This function will be called whenever DI-Guy requires a shape object.

diguyGraphicsShape*
diguyOsgExtGraphicsShape_create_func(void* internal_data)
{
    return new diguyOsgExtGraphicsShape(internal_data);
}



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.