GR/GR_BoundingBox.C

/*
 * Copyright (c) 2013
 *      Side Effects Software Inc.  All rights reserved.
 *
 * Redistribution and use of Houdini Development Kit samples in source and
 * binary forms, with or without modification, are permitted provided that the
 * following conditions are met:
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. The name of Side Effects Software may not be used to endorse or
 *    promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY SIDE EFFECTS SOFTWARE `AS IS' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
 * NO EVENT SHALL SIDE EFFECTS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *----------------------------------------------------------------------------
 */

#include <UT/UT_DSOVersion.h>

#include <RE/RE_Render.h>

#include <GEO/GEO_Primitive.h>

#include <GU/GU_Detail.h>
#include <GU/GU_PrimGroupClosure.h>

#include <GR/GR_Detail.h>
#include <GR/GR_RenderHook.h>
#include <GR/GR_RenderTable.h>
#include <GR/GR_DisplayOption.h>

namespace HDK_Sample {
class GR_BoundingBox : public GR_RenderHook
{
public:
    GR_BoundingBox() {}
    virtual ~GR_BoundingBox() {}

    int          getWireMask(GU_Detail * /*gdp*/,
                             const GR_DisplayOption *dopt) const
    {
        // If hull display is on, we don't draw anything.
        if (dopt->drawPrimHulls())
            return 0;
        else
            return GEOPRIMALL;
    }

    virtual void renderWire(GU_Detail *gdp,
                        RE_Render &ren,
                        const GR_AttribOffset &ptinfo,
                        const GR_DisplayOption *dopt,
                        float lod,
                        const GU_PrimGroupClosure *hidden_geometry);

    int          getShadedMask(GU_Detail * /*gdp*/,
                             const GR_DisplayOption *dopt) const
    {
        // If hull display is on, we don't draw anything.
        if (dopt->drawPrimHulls())
            return 0;
        else
            return GEOPRIMALL;
    }

    virtual void renderShaded(GU_Detail *gdp,
                        RE_Render &ren,
                        const GR_AttribOffset &ptinfo,
                        const GR_DisplayOption *dopt,
                        float lod,
                        const GU_PrimGroupClosure *hidden_geometry);

    virtual const char *getName() const { return "GR_BoundingBox"; }
};
} // End HDK_Sample namespace
using namespace HDK_Sample;

void
GR_BoundingBox::renderWire(GU_Detail *gdp,
                    RE_Render &ren,
                    const GR_AttribOffset & /*ptinfo*/,
                    const GR_DisplayOption *dopt,
                    float /*lod*/,
                    const GU_PrimGroupClosure *hidden_geometry)
{
    if (!dopt->drawPrimHulls())
        return;
    
    UT_BoundingBox              bbox;

    // Find the bounding box of the visible geometry.
    gdp->getBBox(&bbox, 0, hidden_geometry);

    // And render that bounding box as wires.
    ren.beginLine();

    ren.vertex3DW(bbox(0, 0), bbox(1, 0), bbox(2, 0));
    ren.vertex3DW(bbox(0, 1), bbox(1, 0), bbox(2, 0));
    ren.vertex3DW(bbox(0, 1), bbox(1, 1), bbox(2, 0));
    ren.vertex3DW(bbox(0, 0), bbox(1, 1), bbox(2, 0));
    ren.vertex3DW(bbox(0, 0), bbox(1, 0), bbox(2, 0));
    ren.vertex3DW(bbox(0, 0), bbox(1, 0), bbox(2, 1));
    ren.vertex3DW(bbox(0, 0), bbox(1, 1), bbox(2, 1));
    ren.vertex3DW(bbox(0, 1), bbox(1, 1), bbox(2, 1));
    ren.vertex3DW(bbox(0, 1), bbox(1, 0), bbox(2, 1));
    ren.vertex3DW(bbox(0, 1), bbox(1, 0), bbox(2, 0));

    ren.endLine();

    // And last three lines.
    ren.beginLine();

    ren.vertex3DW(bbox(0, 0), bbox(1, 1), bbox(2, 0));
    ren.vertex3DW(bbox(0, 0), bbox(1, 1), bbox(2, 1));

    ren.endLine();

    ren.beginLine();

    ren.vertex3DW(bbox(0, 1), bbox(1, 1), bbox(2, 0));
    ren.vertex3DW(bbox(0, 1), bbox(1, 1), bbox(2, 1));

    ren.endLine();
    
    ren.beginLine();

    ren.vertex3DW(bbox(0, 0), bbox(1, 0), bbox(2, 1));
    ren.vertex3DW(bbox(0, 1), bbox(1, 0), bbox(2, 1));

    ren.endLine();
}

void
GR_BoundingBox::renderShaded(GU_Detail *gdp,
                    RE_Render &ren,
                    const GR_AttribOffset & /*ptinfo*/,
                    const GR_DisplayOption *dopt,
                    float /*lod*/,
                    const GU_PrimGroupClosure *hidden_geometry)
{
    // Only proceed if hull drawing is on.
    if (!dopt->drawPrimHulls())
        return;
    
    UT_BoundingBox              bbox;
    float                       nml[3];
    float                       cd[3];

    // Find the bounding box of the visible geometry.
    gdp->getBBox(&bbox, 0, hidden_geometry);

    // We want to override the colour with the wire colour.
    dopt->wireColor().getRGB(&cd[0], &cd[1], &cd[2]);

    // In order to have colour to show up, we need a diffuse material.
    ren.pushMaterialDiffuse();

    // Draw the cube:

    nml[0] = 0.0f;
    nml[1] = 0.0f;
    nml[2] = -1.0f;

    ren.beginPolygon(0);
        ren.n3DW(nml);
        ren.c3DW(cd);
        ren.vertex3DW(bbox(0, 0), bbox(1, 0), bbox(2, 0));
        ren.vertex3DW(bbox(0, 0), bbox(1, 1), bbox(2, 0));
        ren.vertex3DW(bbox(0, 1), bbox(1, 1), bbox(2, 0));
        ren.vertex3DW(bbox(0, 1), bbox(1, 0), bbox(2, 0));
    ren.endPolygon();

    nml[0] = 0.0f;
    nml[1] = 0.0f;
    nml[2] = 1.0f;

    ren.beginPolygon(0);
        ren.n3DW(nml);
        ren.c3DW(cd);
        ren.vertex3DW(bbox(0, 0), bbox(1, 0), bbox(2, 1));
        ren.vertex3DW(bbox(0, 1), bbox(1, 0), bbox(2, 1));
        ren.vertex3DW(bbox(0, 1), bbox(1, 1), bbox(2, 1));
        ren.vertex3DW(bbox(0, 0), bbox(1, 1), bbox(2, 1));
    ren.endPolygon();

    nml[0] = 0.0f;
    nml[1] = -1.0f;
    nml[2] = 0.0f;

    ren.beginPolygon(0);
        ren.n3DW(nml);
        ren.c3DW(cd);
        ren.vertex3DW(bbox(0, 0), bbox(1, 0), bbox(2, 0));
        ren.vertex3DW(bbox(0, 1), bbox(1, 0), bbox(2, 0));
        ren.vertex3DW(bbox(0, 1), bbox(1, 0), bbox(2, 1));
        ren.vertex3DW(bbox(0, 0), bbox(1, 0), bbox(2, 1));
    ren.endPolygon();

    nml[0] = 0.0f;
    nml[1] = 1.0f;
    nml[2] = 0.0f;

    ren.beginPolygon(0);
        ren.n3DW(nml);
        ren.c3DW(cd);
        ren.vertex3DW(bbox(0, 0), bbox(1, 1), bbox(2, 0));
        ren.vertex3DW(bbox(0, 0), bbox(1, 1), bbox(2, 1));
        ren.vertex3DW(bbox(0, 1), bbox(1, 1), bbox(2, 1));
        ren.vertex3DW(bbox(0, 1), bbox(1, 1), bbox(2, 0));
    ren.endPolygon();

    nml[0] = -1.0f;
    nml[1] = 0.0f;
    nml[2] = 0.0f;

    ren.beginPolygon(0);
        ren.n3DW(nml);
        ren.c3DW(cd);
        ren.vertex3DW(bbox(0, 0), bbox(1, 0), bbox(2, 0));
        ren.vertex3DW(bbox(0, 0), bbox(1, 0), bbox(2, 1));
        ren.vertex3DW(bbox(0, 0), bbox(1, 1), bbox(2, 1));
        ren.vertex3DW(bbox(0, 0), bbox(1, 1), bbox(2, 0));
    ren.endPolygon();

    nml[0] = 1.0f;
    nml[1] = 0.0f;
    nml[2] = 0.0f;

    ren.beginPolygon(0);
        ren.n3DW(nml);
        ren.c3DW(cd);
        ren.vertex3DW(bbox(0, 1), bbox(1, 0), bbox(2, 0));
        ren.vertex3DW(bbox(0, 1), bbox(1, 1), bbox(2, 0));
        ren.vertex3DW(bbox(0, 1), bbox(1, 1), bbox(2, 1));
        ren.vertex3DW(bbox(0, 1), bbox(1, 0), bbox(2, 1));
    ren.endPolygon();

    ren.popMaterialDiffuse();
}

void
newRenderHook(GR_RenderTable *table)
{
    GR_BoundingBox *hook = new GR_BoundingBox;
    
    table->addHook(hook, GR_RENDER_HOOK_VERSION);
}

Generated on Mon Jan 28 00:26:18 2013 for HDK by  doxygen 1.5.9