HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
packedprim/packedprim.C
/*
* Copyright (c) 2024
* 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 <GU/GU_Detail.h>
#include <UT/UT_Main.h>
#include <stdio.h>
/// @file
/// This example has simple examples to show how to create various types of
/// packed primitives.
static void
setCenter(GEO_Primitive &prim, const UT_Vector3 &P)
{
prim.getDetail().setPos3(prim.getPointOffset(0), P);
}
static void
packedGeo(GU_Detail &dest)
{
GU_Detail *geo_to_pack = new GU_Detail;
geo_to_pack->cube(-1, 1, -1, 1, -1, 1); // Cube translated up a bit
gdh.allocateAndSet(geo_to_pack, true); // Pass gdp to handle
// The same GU_DetailHandle can be used to create multiple primitives.
// Each packed primitive shares the underlying GU_DetailHandle.
float x = -3;
for (int i = 0; i < 3; ++i, x += 3)
{
// Pack the geometry into a new packed primitive
setCenter(*pack, UT_Vector3(x, 3, 0));
}
}
static void
packedDisk(GU_Detail &dest)
{
"defgeo.bgeo", // Relative to geometry path
false,
1.0);
// Set the viewport display to be the bounding box of the geometry
setCenter(*pack, UT_Vector3(0, 1, 0));
}
static void
packedDiskSequence(GU_Detail &dest)
{
static const char* names[] = {
"defgeo.bgeo",
"axis.bgeo",
"shaderbox.bgeo",
"deflight.bgeo",
"defnull.bgeo",
};
static UT_StringArray filenames(names);
filenames,
0,
setCenter(*pack, UT_Vector3(0, -1, 0));
}
static void
packedFragments(GU_Detail &dest)
{
// Packed fragments partition a single piece of geometry into "fragments"
// based on an attribute value.
static const char* names[] = {
"part1",
"part2",
"part3",
};
static UT_StringArray partNames(names);
GU_Detail *geo_to_pack = new GU_Detail;
geo_to_pack->cube(-1, 1, -1, 1, -1, 1); // Cube translated up a bit
GA_Attribute *name_atr = geo_to_pack->addStringTuple(
"name",
1);
int part = 0;
for (GA_Iterator it(geo_to_pack->getPrimitiveRange()); !it.atEnd(); ++it)
{
name.set(*it, partNames[part % partNames.size()]);
++part;
}
gdh.allocateAndSet(geo_to_pack, true); // Pass gdp to handle
float x = -3;
for (auto &&part_name : partNames)
{
dest,
gdh,
name_atr,
part_name);
setCenter(*pack, UT_Vector3(x, -3, 0));
x += 3;
}
}
int
theMain(int argc, char *argv[])
{
GU_Detail::loadIODSOs(); // Ensure all plugins are loaded
const char *filename = "packed.bgeo";
if (argc > 1)
filename = argv[1];
GU_Detail full;
packedGeo(full);
packedDisk(full);
packedDiskSequence(full);
packedFragments(full);
fprintf(stderr, "Saving to: %s\n", filename);
if (!full.save(filename, nullptr))
fprintf(stderr, "Error saving to %s\n", filename);
return 0;
}