Skip to content

Commit

Permalink
Added support to vsg::Builder for decorating created subgraphs with C…
Browse files Browse the repository at this point in the history
…ullNode
  • Loading branch information
robertosfield committed Sep 27, 2023
1 parent 1a7f6cc commit a797253
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
4 changes: 4 additions & 0 deletions include/vsg/utils/Builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ namespace vsg
vec4 color = {1.0f, 1.0f, 1.0f, 1.0f};
mat4 transform;

/// cullNode flag indicates whether a CullNode should decorate the creted subgraph
bool cullNode = false;

template<typename T>
void set(const t_box<T>& bb)
{
Expand Down Expand Up @@ -122,6 +125,7 @@ namespace vsg
ref_ptr<Data> instancePositions(const GeometryInfo& info, uint32_t& instanceCount);
ref_ptr<Data> instanceColors(const GeometryInfo& info, uint32_t instanceCount);
vec3 y_texcoord(const StateInfo& info) const;
ref_ptr<Node> decorateWithCullNodeIfRequired(const GeometryInfo& info, ref_ptr<Node> node) const;

ref_ptr<ShaderSet> _flatShadedShaderSet;
ref_ptr<ShaderSet> _phongShaderSet;
Expand Down
54 changes: 47 additions & 7 deletions src/vsg/utils/Builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <vsg/io/Logger.h>
#include <vsg/io/read.h>
#include <vsg/nodes/StateGroup.h>
#include <vsg/nodes/CullNode.h>
#include <vsg/nodes/VertexIndexDraw.h>
#include <vsg/state/ColorBlendState.h>
#include <vsg/state/DepthStencilState.h>
Expand Down Expand Up @@ -203,6 +204,45 @@ vec3 Builder::y_texcoord(const StateInfo& info) const
}
}

ref_ptr<Node> Builder::decorateWithCullNodeIfRequired(const GeometryInfo& info, ref_ptr<Node> node) const
{
if (info.cullNode)
{
auto cullNode = vsg::CullNode::create();
cullNode->child = node;

if (info.positions)
{
if (auto v3a = info.positions.cast<vec3Array>())
{
box bound;
for(auto& v : *v3a)
{
bound.add(v);
}
cullNode->bound.center = (bound.min + bound.max) * 0.5f;
cullNode->bound.radius = vsg::length(bound.max - bound.min) * 0.5 + vsg::length(info.dx + info.dy + info.dz);
}
else
{
// unable to compute bound so do not decorate with a CullNode.
return node;
}
}
else
{
cullNode->bound.center = info.position;
cullNode->bound.radius = vsg::length(info.dx + info.dy + info.dz);
}

return cullNode;
}
else
{
return node;
}
}

ref_ptr<Node> Builder::createBox(const GeometryInfo& info, const StateInfo& stateInfo)
{
auto& subgraph = _boxes[info];
Expand Down Expand Up @@ -339,7 +379,7 @@ ref_ptr<Node> Builder::createBox(const GeometryInfo& info, const StateInfo& stat

if (compileTraversal) compileTraversal->compile(scenegraph);

subgraph = scenegraph;
subgraph = decorateWithCullNodeIfRequired(info, scenegraph);
return subgraph;
}

Expand Down Expand Up @@ -568,7 +608,7 @@ ref_ptr<Node> Builder::createCapsule(const GeometryInfo& info, const StateInfo&

if (compileTraversal) compileTraversal->compile(scenegraph);

subgraph = scenegraph;
subgraph = decorateWithCullNodeIfRequired(info, scenegraph);
return subgraph;
}

Expand Down Expand Up @@ -778,7 +818,7 @@ ref_ptr<Node> Builder::createCone(const GeometryInfo& info, const StateInfo& sta

if (compileTraversal) compileTraversal->compile(scenegraph);

subgraph = scenegraph;
subgraph = decorateWithCullNodeIfRequired(info, scenegraph);
return subgraph;
}

Expand Down Expand Up @@ -1025,7 +1065,7 @@ ref_ptr<Node> Builder::createCylinder(const GeometryInfo& info, const StateInfo&

if (compileTraversal) compileTraversal->compile(scenegraph);

subgraph = scenegraph;
subgraph = decorateWithCullNodeIfRequired(info, scenegraph);
return subgraph;
}

Expand Down Expand Up @@ -1128,7 +1168,7 @@ ref_ptr<Node> Builder::createDisk(const GeometryInfo& info, const StateInfo& sta

if (compileTraversal) compileTraversal->compile(scenegraph);

subgraph = scenegraph;
subgraph = decorateWithCullNodeIfRequired(info, scenegraph);
return subgraph;
}

Expand Down Expand Up @@ -1317,7 +1357,7 @@ ref_ptr<Node> Builder::createSphere(const GeometryInfo& info, const StateInfo& s

if (compileTraversal) compileTraversal->compile(scenegraph);

subgraph = scenegraph;
subgraph = decorateWithCullNodeIfRequired(info, scenegraph);
return subgraph;
}

Expand Down Expand Up @@ -1450,6 +1490,6 @@ ref_ptr<Node> Builder::createHeightField(const GeometryInfo& info, const StateIn

if (compileTraversal) compileTraversal->compile(scenegraph);

subgraph = scenegraph;
subgraph = decorateWithCullNodeIfRequired(info, scenegraph);
return subgraph;
}

0 comments on commit a797253

Please sign in to comment.