Skip to content

Commit

Permalink
Merge pull request #279 from vsg-dev/Shadows
Browse files Browse the repository at this point in the history
Added better controls to vsgshadow example
  • Loading branch information
robertosfield authored Nov 29, 2023
2 parents 1697264 + 96b5b48 commit cc96a51
Showing 1 changed file with 68 additions and 28 deletions.
96 changes: 68 additions & 28 deletions examples/nodes/vsgshadow/vsgshadow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,73 @@

#include <iostream>

vsg::ref_ptr<vsg::Node> createTestScene(vsg::ref_ptr<vsg::Options> options, vsg::Path textureFile = {}, bool requiresBase = true, bool insertCullNode = true)
struct ModelSettings
{
vsg::ref_ptr<vsg::Options> options;
vsg::Path textureFile;
double lodScreenRatio = 0.02;
bool insertBaseGeometry = true;
bool insertCullNode = false;
bool insertLODNode = false;
};

vsg::ref_ptr<vsg::Node> decorateIfRequired(vsg::ref_ptr<vsg::Node> node, const ModelSettings& settings)
{
if (settings.insertLODNode)
{
if (auto cullNode = node.cast<vsg::CullNode>())
{
// replace cullNode with LOD
auto lod = vsg::LOD::create();
lod->bound = cullNode->bound;
lod->addChild(vsg::LOD::Child{settings.lodScreenRatio, cullNode->child});
return lod;
}
else
{
auto bb = vsg::visit<vsg::ComputeBounds>(node).bounds;
auto lod = vsg::LOD::create();
lod->bound = vsg::sphere((bb.min + bb.max) * 0.5, vsg::length(bb.max - bb.min));
lod->addChild(vsg::LOD::Child{settings.lodScreenRatio, node});
return lod;
}
}

return node;
};


vsg::ref_ptr<vsg::Node> createTestScene(const ModelSettings& settings)
{
auto builder = vsg::Builder::create();
builder->options = options;
builder->options = settings.options;

auto scene = vsg::Group::create();

vsg::GeometryInfo geomInfo;
vsg::StateInfo stateInfo;
geomInfo.cullNode = settings.insertCullNode;

geomInfo.cullNode = insertCullNode;
if (textureFile) stateInfo.image = vsg::read_cast<vsg::Data>(textureFile, options);
vsg::StateInfo stateInfo;
if (settings.textureFile) stateInfo.image = vsg::read_cast<vsg::Data>(settings.textureFile, settings.options);

geomInfo.color.set(1.0f, 1.0f, 0.5f, 1.0f);
scene->addChild(builder->createBox(geomInfo, stateInfo));
scene->addChild(decorateIfRequired(builder->createBox(geomInfo, stateInfo), settings));

geomInfo.color.set(1.0f, 0.5f, 1.0f, 1.0f);
geomInfo.position += geomInfo.dx * 1.5f;
scene->addChild(builder->createSphere(geomInfo, stateInfo));
scene->addChild(decorateIfRequired(builder->createSphere(geomInfo, stateInfo), settings));

geomInfo.color.set(0.0f, 1.0f, 1.0f, 1.0f);
geomInfo.position += geomInfo.dx * 1.5f;
scene->addChild(builder->createCylinder(geomInfo, stateInfo));
scene->addChild(decorateIfRequired(builder->createCylinder(geomInfo, stateInfo), settings));

geomInfo.color.set(0.5f, 1.0f, 0.5f, 1.0f);
geomInfo.position += geomInfo.dx * 1.5f;
scene->addChild(builder->createCapsule(geomInfo, stateInfo));
scene->addChild(decorateIfRequired(builder->createCapsule(geomInfo, stateInfo), settings));


auto bounds = vsg::visit<vsg::ComputeBounds>(scene).bounds;
if (requiresBase)
if (settings.insertBaseGeometry)
{
double diameter = vsg::length(bounds.max - bounds.min);
geomInfo.position.set((bounds.min.x + bounds.max.x)*0.5, (bounds.min.y + bounds.max.y)*0.5, bounds.min.z);
Expand All @@ -53,19 +89,19 @@ vsg::ref_ptr<vsg::Node> createTestScene(vsg::ref_ptr<vsg::Options> options, vsg:
return scene;
}

vsg::ref_ptr<vsg::Node> createLargeTestScene(vsg::ref_ptr<vsg::Options> options, vsg::Path textureFile = {}, bool requiresBase = true, bool insertCullNode = true)
vsg::ref_ptr<vsg::Node> createLargeTestScene(const ModelSettings& settings)
{
auto builder = vsg::Builder::create();
builder->options = options;
builder->options = settings.options;

auto scene = vsg::Group::create();

vsg::GeometryInfo geomInfo;
vsg::StateInfo stateInfo;
geomInfo.cullNode = settings.insertCullNode;

geomInfo.cullNode = insertCullNode;
vsg::StateInfo stateInfo;
if (settings.textureFile) stateInfo.image = vsg::read_cast<vsg::Data>(settings.textureFile, settings.options);

if (textureFile) stateInfo.image = vsg::read_cast<vsg::Data>(textureFile, options);
vsg::box bounds(vsg::vec3(0.0f, 0.0f, 0.0f), vsg::vec3(1000.0f, 1000.0f, 20.0f));

uint32_t numBoxes = 400;
Expand All @@ -92,28 +128,28 @@ vsg::ref_ptr<vsg::Node> createLargeTestScene(vsg::ref_ptr<vsg::Options> options,
for(uint32_t bi = 0; bi < numBoxes; ++bi)
{
assignRandomGeometryInfo();
auto model = builder->createBox(geomInfo, stateInfo);
vsg::info("BOX geomInfo.position = ", geomInfo.position, ", ", model);
auto model = decorateIfRequired(builder->createBox(geomInfo, stateInfo), settings);
// vsg::info("BOX geomInfo.position = ", geomInfo.position, ", ", model);
scene->addChild(model);
}

for(uint32_t bi = 0; bi < numSpheres; ++bi)
{
assignRandomGeometryInfo();
auto model = builder->createSphere(geomInfo, stateInfo);
vsg::info("Sphere geomInfo.position = ", geomInfo.position, ", ", model);
auto model = decorateIfRequired(builder->createSphere(geomInfo, stateInfo), settings);
// vsg::info("Sphere geomInfo.position = ", geomInfo.position, ", ", model);
scene->addChild(model);
}

for(uint32_t bi = 0; bi < numCapsules; ++bi)
{
assignRandomGeometryInfo();
auto model = builder->createCapsule(geomInfo, stateInfo);
vsg::info("Capsule geomInfo.position = ", geomInfo.position, ", ", model);
auto model = decorateIfRequired(builder->createCapsule(geomInfo, stateInfo), settings);
// vsg::info("Capsule geomInfo.position = ", geomInfo.position, ", ", model);
scene->addChild(model);
}

if (requiresBase)
if (settings.insertBaseGeometry)
{
double diameter = vsg::length(bounds.max - bounds.min);
geomInfo.position.set((bounds.min.x + bounds.max.x)*0.5, (bounds.min.y + bounds.max.y)*0.5, bounds.min.z);
Expand Down Expand Up @@ -152,6 +188,7 @@ int main(int argc, char** argv)
vsg::CommandLine arguments(&argc, argv);
windowTraits->debugLayer = arguments.read({"--debug", "-d"});
windowTraits->apiDumpLayer = arguments.read({"--api", "-a"});
windowTraits->synchronizationLayer = arguments.read("--sync");

arguments.read("--screen", windowTraits->screenNum);
arguments.read("--display", windowTraits->display);
Expand Down Expand Up @@ -188,7 +225,11 @@ int main(int argc, char** argv)
deviceFeatures->get().depthClamp = VK_TRUE;
}

vsg::Path textureFile = arguments.value(vsg::Path{}, {"-i", "--image"});
ModelSettings settings;
settings.options = options;
settings.textureFile = arguments.value(vsg::Path{}, {"-i", "--image"});
settings.insertCullNode = arguments.read("--cull");
settings.insertLODNode = arguments.read("--lod");

auto numShadowMapsPerLight = arguments.value<uint32_t>(1, "--sm");
auto numLights = arguments.value<uint32_t>(1, "-n");
Expand Down Expand Up @@ -282,7 +323,6 @@ int main(int argc, char** argv)
auto pathFilename = arguments.value<vsg::Path>("", "-p");
auto outputFilename = arguments.value<vsg::Path>("", "-o");

bool requiresBase = true;
vsg::ref_ptr<vsg::Node> earthModel;
vsg::ref_ptr<vsg::EllipsoidModel> ellipsoidModel;
if (auto earthFilename = arguments.value<vsg::Path>("", "--earth"))
Expand All @@ -291,11 +331,11 @@ int main(int argc, char** argv)
if (earthModel)
{
ellipsoidModel = earthModel->getRefObject<vsg::EllipsoidModel>("EllipsoidModel");
requiresBase = false;
settings.insertBaseGeometry = false;
}
}

auto insertCullNode = arguments.read("--cull");

auto inherit = arguments.read("--inherit");
auto direction = arguments.value(vsg::dvec3(0.0, 0.0, -1.0), "--direction");
auto location = arguments.value<vsg::dvec3>({0.0, 0.0, 0.0}, "--location");
Expand All @@ -320,7 +360,7 @@ int main(int argc, char** argv)
vsg::ref_ptr<vsg::Node> scene;
if (arguments.read("--large"))
{
scene = createLargeTestScene(options, textureFile, requiresBase, insertCullNode);
scene = createLargeTestScene(settings);
}
else if (argc>1)
{
Expand All @@ -336,7 +376,7 @@ int main(int argc, char** argv)
}
else
{
scene = createTestScene(options, textureFile, requiresBase, insertCullNode);
scene = createTestScene(settings);
}

if (stateGroup)
Expand Down

0 comments on commit cc96a51

Please sign in to comment.