HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GUI/GUI_PolygonNormalShade.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.
*/
/*
* COMMENTS:
* Example of a filter hook.
* Replaces Cd in a GT polygon mesh with false N color, mapped from
* normalized [-1,1] to [0,1].
*/
#include "GUI_PolygonNormalShade.h"
using namespace HDK_Sample;
const char *NCD_OPTION = "visNasCd";
// install the hook.
{
// the priority only matters if multiple hooks are assigned to the same
// primitive type. If this is the case, the hook with the highest priority
// (largest priority value) is processed first.
const int priority = 0;
// register the actual hook
dm_table->registerGTHook(new GUI_PolygonNormalShade(),
// because we hook into the refinement loop, when the option is
// changed a refine must be performed. By default a custom option
// does not trigger a refine.
NCD_OPTION, "Normal Color Override",
}
GUI_PolygonNormalShade::GUI_PolygonNormalShade()
: GUI_PrimitiveHook("Normal color override", GUI_RENDER_MASK_ALL)
{
}
GUI_PolygonNormalShade::~GUI_PolygonNormalShade()
{
}
GUI_PolygonNormalShade::filterPrimitive(const GT_PrimitiveHandle &gt_prm,
const GEO_Primitive *geo_prm,
const GR_RenderInfo *info,
GR_PrimAcceptResult &processed)
{
if(!info->getDisplayOption()->getUserOptionState(NCD_OPTION))
{
// we're interested in this prim, but are not doing anything with it
// at this time. Mark it as processed but don't return a new prim.
processed = GR_PROCESSED;
return ph;
}
// As this was registered for a GT prim type, a valid GT prim should be
// passed to filterPrimitive(), not a GEO primitive.
UT_ASSERT(geo_prm == NULL);
UT_ASSERT(gt_prm);
if(!gt_prm)
{
processed = GR_NOT_PROCESSED;
return ph;
}
// Fetch the point normals (N) from the polygon mesh.
bool point_normals = false;
// Check to see if vertex normals are present.
if(gt_prm->getVertexAttributes())
nh = gt_prm->getVertexAttributes()->get("N");
// If no vertex normals, check point normals.
if(!nh)
{
if(gt_prm->getPointAttributes())
nh = gt_prm->getPointAttributes()->get("N");
// If no point normals, generate smooth point normals from P and the
// connectivity.
if(!nh)
{
auto mesh=UTverify_cast<const GT_PrimPolygonMesh *>(gt_prm.get());
if(mesh)
nh = mesh->createPointNormals();
}
point_normals = true;
}
// Normals found: generate Cd from them.
if(nh)
{
// Create a new data array for Cd (vec3 in FP16 format) and map the
// normalized normal values from [-1,1] to [0,1].
auto *cd =
new GT_Real16Array(nh->entries(), 3, GT_TYPE_COLOR);
for(int i=0; i<nh->entries(); i++)
{
nh->import(i, col.data(), 3);
col.normalize();
cd->getData(i)[0]= col.x() *0.5 + 0.5;
cd->getData(i)[1]= col.y() *0.5 + 0.5;
cd->getData(i)[2]= col.z() *0.5 + 0.5;
}
// Copy the input mesh, replacing either its vertex or point attribute
// list.
auto mesh=UTverify_cast<const GT_PrimPolygonMesh *>(gt_prm.get());
if(point_normals)
{
// Add Cd to the point attribute list, replacing it if Cd already
// exists.
gt_prm->getPointAttributes()->addAttribute("Cd", cdh, true);
ph = new GT_PrimPolygonMesh(*mesh,
ah,
mesh->getVertexAttributes(),
mesh->getUniformAttributes(),
mesh->getDetailAttributes());
}
else
{
// Add Cd to the vertex attribute list, replacing it if Cd already
// exists.
gt_prm->getVertexAttributes()->addAttribute("Cd", cdh, true);
ph = new GT_PrimPolygonMesh(*mesh,
mesh->getPointAttributes(),
ah,
mesh->getUniformAttributes(),
mesh->getDetailAttributes());
}
processed = GR_PROCESSED;
}
// return a new polygon mesh with modified Cd attribute.
return ph;
}