DI-Guy Graphics API, diguyOglGraphicsShape

diguyOglGraphicsShape.cpp

DI-Guy API Version 12.5.1

This file was automatically generated from diguyOglGraphicsShape.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 diguyOglGraphicsShape is an example of a diguyGraphicsShape subclass for an immediate mode renderer using the DI-Guy 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 OpenGL.

Header files and forward declarations
#include "diguyOglGraphicsShape.h"
#include "ogl_headers.h"

#include <stdlib.h>
#include <diguyApp.h>
#include <diguyGraphicsShaderProgram.h>
#include <diguyGraphicsTexture.h>

GLuint diguyOglGraphicsShape::sm_default_variation_texture_object = 0;
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.

diguyOglGraphicsShape::diguyOglGraphicsShape(void* internal_data)
    :
    diguyGraphicsShape(internal_data),
    m_display_list(0),
    m_unique_texture_object(0)
{
    /*
     *  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 create the necessary objects that will apply offset data of this shape from its parent link during the Draw Stage.

This implementation checks to see if this shape has any offsets from its link. If so, it defines a display list that will apply the offsets each time the shape is drawn. Shape offsets are always static, so they can safely be encoded in a display list. Note that no visible geometry is built. This will be done when the mesh objects attached to this shape have their build() functions called.

No display list is created if this shape has no offsets.

void
diguyOglGraphicsShape::build()
{
    if (get_has_offset_translation() ||
        get_has_offset_rotation() ||
        get_has_offset_scale())
    {
        m_display_list = glGenLists(1);
        if (m_display_list == 0)
            return;

        /*
         *  Compile the transformations into m_display_list.
         */
        glNewList(m_display_list, GL_COMPILE);
        {
            if (get_has_offset_translation())
            {
                float offset_translation[3];
                get_offset_translation(&offset_translation[0],
                    &offset_translation[1],
                    &offset_translation[2]);

                if ((offset_translation[0] != 0.0f) ||
                    (offset_translation[1] != 0.0f) ||
                    (offset_translation[2] != 0.0f))
                {
                    glTranslatef(offset_translation[0],
                    	offset_translation[1],
                    	offset_translation[2]);
                }
            }

            if (get_has_offset_rotation())
            {
                float offset_rotation [3];
                get_offset_rotation (&offset_rotation [0],
                    &offset_rotation [1],
                    &offset_rotation [2]);

                if (offset_rotation [0] != 0.0f)
                    glRotatef(offset_rotation [0], 0.0, 0.0, 1.0);
                if (offset_rotation [1] != 0.0f)
                    glRotatef(offset_rotation [1], 1.0, 0.0, 0.0);
                if (offset_rotation [2] != 0.0f)
                    glRotatef(offset_rotation [2], 0.0, 1.0, 0.0);
            }

            if (get_has_offset_scale())
            {
                float offset_scale[3];
                get_offset_scale(&offset_scale[0],
                    &offset_scale[1],
                    &offset_scale[2]);

                if ((offset_scale[0] != 1.0f) ||
                    (offset_scale[1] != 1.0f) ||
                    (offset_scale[2] != 1.0f))
                {
                    glScalef(offset_scale[0],
                    	offset_scale[1],
                    	offset_scale[2]);
                }
            }
        }
        glEndList();
    }
}
unbuild
Unbuild Stage

Description:

Override of diguyGraphicsShape::unbuild().

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

This implementation destroys the display list created in the build() function.

void
diguyOglGraphicsShape::unbuild()
{
    if (m_display_list != 0)
        glDeleteLists(m_display_list, 1);

    m_display_list = 0;

    if(m_unique_texture_object){
        glDeleteTextures(1, &m_unique_texture_object);
    }
    m_unique_texture_object = 0;
}
draw
Draw Stage

Description:

Override of diguyGraphicsShape::draw().

The draw() function needs to apply offsets created in the Build Stage so that subsequent drawing of child meshes will be correctly placed.

This implementation applies the display list created in the Build Stage. This will change the OpenGL matrix stack to include this shape's offsets, which will change the position and orientation of meshes attached to this shape.

For this immediate mode example all of the visible geometry is in mesh objects attached to this shape, so the lod argument isn't needed.

void
diguyOglGraphicsShape::draw(int lod)
{
    if (m_display_list != 0)
        glCallList(m_display_list);

    /*
     *  If this is a render pass for finding intersections, look up and apply
     *   the unique color of this shape.
     */
    if (diguyApp::get_app()->get_render_pass_flags() & DIGUY_RENDER_PASS_INTERSECTION)
    {
        unsigned char r, g, b;
        get_unique_color(&r, &g, &b);
        glColor3ub(r, g, b);
    }
    /*
     *  If this shader support the variation texture system bind either the degenerate texture
     *  or the unique one.
     */
    diguyGraphicsShaderProgram * shader = diguyApp::get_app()->get_current_bound_shader_program();
    if(shader && shader->get_textures_used_bitmask() & DIGUY_TEXTURE_MAP_MASK_COMPONENTS)
    {
        if (glActiveTexture){
            glActiveTexture(GL_TEXTURE0 + DIGUY_TEXTURE_MAP_TYPE_COMPONENT_RAMP);
        }

        if(m_unique_texture_object)
        {
            glBindTexture(GL_TEXTURE_2D, m_unique_texture_object);
        }
        else{
            if(sm_default_variation_texture_object == 0){
                create_default_variation_texture();
            }
            glBindTexture(GL_TEXTURE_2D, sm_default_variation_texture_object);
    
        }
    }
}
void 
diguyOglGraphicsShape::update_variation_texture(int num_pixels, unsigned char * pixel_data)
{
    /*
     *  Override from the base class. Send a unique texture to the graphics card.
     */
    if(m_unique_texture_object == 0){
        glGenTextures(1, &m_unique_texture_object);
    }
    glBindTexture(GL_TEXTURE_2D, m_unique_texture_object);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, num_pixels, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixel_data);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glGenerateMipmapEXT(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, 0);
}
void 
diguyOglGraphicsShape::create_default_variation_texture()
{
    /*
     *  If there's no variation texture and we're using a variation based shader 
     *  create one as a place holder.
     */
    if(sm_default_variation_texture_object == 0){
        glGenTextures(1, &sm_default_variation_texture_object );
    }
    glBindTexture(GL_TEXTURE_2D, sm_default_variation_texture_object );
    unsigned char buf[] = {128, 128, 128, 255};
    glTexImage2D(GL_TEXTURE_2D, 0, 4, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glGenerateMipmapEXT(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, 0);
}
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*
diguyOglGraphicsShape_create_func(void* internal_data)
{
    return new diguyOglGraphicsShape(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.