DI-Guy Graphics API, diguyOglGraphicsTexture

diguyOglGraphicsTexture.cpp

DI-Guy API Version 12.5.1

This file was automatically generated from diguyOglGraphicsTexture.cpp. Do not edit this file directly; the changes will be lost.
Introduction
Textures modify the visible appearance of meshes. They work in concert with materials to determine the final appearance of visible geometry.

The class diguyOglGraphicsTexture is an example of a diguyGraphicsTexture subclass for an immediate mode renderer using the DI-Guy Loader. The example subclass shows how to use the accessors of the diguyGraphicsTexture class and which virtual functions should be overridden for an immediate mode renderer such as OpenGL.

Header files and forward declarations
#include "diguyOglGraphicsTexture.h"
#include <libbdilog.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
Constructor
Description:

Initialize data members of the class to a known state.

Most base class data will not be fully initialized until during the Build Stage, so most base class accessors should not be called here.

diguyOglGraphicsTexture::diguyOglGraphicsTexture(void* internal_data)
    :
    diguyGraphicsTexture(internal_data),
    m_texture_object(0)
{
    /*
     *  Nothing else to do after calling base class constructor and
     *   initializing data members.
     */
}
build
Build Stage

Description:

Override of diguyGraphicsTexture::build().

The build() function should create renderer objects that will set the proper texture state in bind() calls made during the Draw Stage.

This implementation creates an OpenGL texture object for setting texture properties of meshes.

void
diguyOglGraphicsTexture::build()
{
    const char* fname = get_filename();
    unsigned char* texture_map_data = get_texture_map_data();

    /*
     *  If there is texture data, create a mip-mapped texture object.
     */
    if (texture_map_data)
    {
        int width = get_width();
        int height = get_height();
        int num_components = get_num_components();

        int pixel_format_as_int = get_gl_format();
        if (pixel_format_as_int == -1)
        {
            bdi_log_printf(BDI_LOG_WARN, "WARNING: Failed to obtain legal "
                "pixel format for texture '%s'.\n",
                get_name());
            goto MAKE_NULL_TEXTURE;
        }

        GLenum pixel_format = (GLenum) pixel_format_as_int;

        /*
         *  Choose reasonable mag and min filters.
         */
        GLenum texture_mag_filter = GL_LINEAR;
        GLenum texture_min_filter = GL_LINEAR_MIPMAP_LINEAR;

        glGenTextures(1, &m_texture_object);
        glBindTexture(GL_TEXTURE_2D, m_texture_object);

        glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

        if (get_clamp_s())
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
        else
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);

        if (get_clamp_t())
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
        else
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, texture_mag_filter);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, texture_min_filter);
        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

        /*
         *  Get the texture data into OpenGL.  The calls we use depend
         *   on whether the texture data is compressed or not.
         */
        diguyGraphicsTextureCompressionType compression_type = get_texture_compression_type();

        //printf("%s %s\n", get_filename(), get_gl_format_as_string());

        if (compression_type == DIGUY_GRAPHICS_TEXTURE_COMPRESSION_TYPE_NONE)
        {
            /*
             *  Currently uncompressed textures are assumed to not have
             *   included mipmaps.  This doesn't take into account .dds
             *   files that aren't compressed, which DI-Guy doesn't yet
             *   support.
             *
             *  If we've set the min filter to one of the "MIPMAP" options
             *   (e.g., GL_LINEAR_MIPMAP_LINEAR) we should have OpenGL create
             *   mipmaps.  The filters GL_NEAREST and GL_LINEAR don't require
             *   mipmaps.
             *
             *  http://www.opengl.org/wiki/Common_Mistakes says never use
             *   gluBuild2DMipmaps.
             */
            //gluBuild2DMipmaps(GL_TEXTURE_2D,
            //	num_components,
            //	width,
            //	height,
            //	pixel_format,
            //	GL_UNSIGNED_BYTE,
            //	texture_map_data);

            int color_format = -1;
            switch (num_components)
            {
                case 1: color_format = GL_LUMINANCE; break;
                case 2: color_format = GL_LUMINANCE_ALPHA; break;
                case 3: color_format = GL_RGB; break;
                case 4: color_format = GL_RGBA; break;
            }

            glTexImage2D(GL_TEXTURE_2D,
                0,
                pixel_format,
                width,
                height,
                0,
                color_format,
                GL_UNSIGNED_BYTE,
                texture_map_data);

            if ((texture_min_filter == GL_NEAREST_MIPMAP_NEAREST) ||
                (texture_min_filter == GL_NEAREST_MIPMAP_LINEAR)  ||
                (texture_min_filter == GL_LINEAR_MIPMAP_NEAREST)  ||
                (texture_min_filter == GL_LINEAR_MIPMAP_LINEAR))
            {
                glGenerateMipmap(GL_TEXTURE_2D);
            }
        }
        else
        {
            /*
             *  Compressed textures may already include mipmaps.  If this
             *   is the case, we need to manually tell OpenGL the mipmap
             *   offsets.
             */
            int num_included_mipmaps = get_num_included_mipmaps();

            /*
             *  There's always at least 1 included mipmap: the base image.
             */
            if (num_included_mipmaps == 0)
                num_included_mipmaps = 1;

            /*
             *  Get mipmap offsets.
             *
             *  "Mipmap 0" is really the base-level image.  Each successive
             *   mipmap should decrease in width and height by a factor of 2.
             */
            int mipmap_width = width;
            int mipmap_height = height;

            assert(glCompressedTexImage2D);

            int k;
            for (k=0; k<num_included_mipmaps; k++)
            {
                if (mipmap_width  == 0) mipmap_width  = 1;
                if (mipmap_height == 0) mipmap_height = 1;

                glCompressedTexImage2D(GL_TEXTURE_2D,
                    k,
                    pixel_format,
                    mipmap_width,
                    mipmap_height,
                    0,
                    get_mipmap_data_size(k),
                    texture_map_data + get_mipmap_data_offset(k));

                mipmap_width  >>= 1;
                mipmap_height >>= 1;
            }

            /*
             *  If there are missing mipmaps, the following keeps GL working
             *   right.  Otherwise the mipmap levels that are missing would
             *   show up as white.
             */
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, num_included_mipmaps - 1);
        }
    }

    /*
     *  Else create a "null" texture object.
     */
    else
    {
MAKE_NULL_TEXTURE:

      glGenTextures(1, &m_texture_object);
      glBindTexture(GL_TEXTURE_2D, m_texture_object);
      unsigned char buf[] = {255,255,255,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);
      glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

    }

    glBindTexture(GL_TEXTURE_2D, 0);

    /*
     *  Free memory allocated by the loader.  Now that the texture data is
     *   in an OpenGL texture object, the raw data loaded from the texture
     *   file can be freed.
     */
    free_loader_memory();
}
unbuild
Unbuild Stage

Description:

Override of diguyGraphicsTexture::unbuild().

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

This implementation deletes the OpenGL texture object created in build().

void
diguyOglGraphicsTexture::unbuild()
{
    if (m_texture_object != 0)
        glDeleteTextures(1, &m_texture_object);
}
bind_now
Draw Stage

Description:

Override of virtual function of diguyGraphicsTexture::bind_now(), called during Draw Stage.

The bind_now() function should do whatever is necessary to change the current state of the renderer to use this texture.

The texture object created in build() is bound, changing OpenGL texture settings.

void
diguyOglGraphicsTexture::bind_now(diguyTextureMapType texture_type)
{
    if (diguyOglGraphicsTexture::get_last_bound_texture(texture_type) == this)
        return;

    if (m_texture_object == 0)
    {
        return;
    }
    glActiveTexture(GL_TEXTURE0 + texture_type);
    glBindTexture(GL_TEXTURE_2D, m_texture_object);

}
Create function definition
Create and return an object of a subclass of diguyGraphicsTexture. This function will be called whenever DI-Guy requires a texture object.

diguyGraphicsTexture*
diguyOglGraphicsTexture_create_func(void* internal_data)
{
    return new diguyOglGraphicsTexture(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.