Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove warning of ImGuiViewer + OSG shadow #1312

Merged
merged 3 commits into from
May 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* Added heightmap support to OSG renderer: [#1293](https://github.com/dartsim/dart/pull/1293)
* Improved voxel grid and point cloud rendering performance: [#1294](https://github.com/dartsim/dart/pull/1294)
* Fixed incorrect alpha value update of InteractiveFrame: [#1297](https://github.com/dartsim/dart/pull/1297)
* Removed warning of ImGuiViewer + OSG shadow: [#1312](https://github.com/dartsim/dart/pull/1312)

* Examples and Tutorials

Expand Down
30 changes: 16 additions & 14 deletions dart/gui/osg/WorldNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@

#include "dart/gui/osg/WorldNode.hpp"
#include "dart/gui/osg/ShapeFrameNode.hpp"
#include "dart/gui/osg/ImGuiViewer.hpp"

#include "dart/simulation/World.hpp"
#include "dart/dynamics/Skeleton.hpp"
Expand Down Expand Up @@ -329,33 +328,36 @@ bool WorldNode::isShadowed() const
}

//==============================================================================
void WorldNode::setShadowTechnique(::osg::ref_ptr<osgShadow::ShadowTechnique> shadowTechnique) {
if(!shadowTechnique) {
void WorldNode::setShadowTechnique(::osg::ref_ptr<osgShadow::ShadowTechnique> shadowTechnique)
{
if(!shadowTechnique)
{
mShadowed = false;
static_cast<osgShadow::ShadowedScene*>(mShadowedGroup.get())->setShadowTechnique(nullptr);
mShadowedGroup->setShadowTechnique(nullptr);
}
else {
ImGuiViewer* viewer = mViewer ? dynamic_cast<ImGuiViewer*>(mViewer) : nullptr;
if(viewer)
dtwarn << "[WorldNode] You are enabling shadows inside an ImGuiViewer. "
<< "The ImGui windows may not render properly.\n";
else
{
mShadowed = true;
static_cast<osgShadow::ShadowedScene*>(mShadowedGroup.get())->setShadowTechnique(shadowTechnique.get());
mShadowedGroup->setShadowTechnique(shadowTechnique);
}
}

//==============================================================================
::osg::ref_ptr<osgShadow::ShadowTechnique> WorldNode::getShadowTechnique() const {
::osg::ref_ptr<osgShadow::ShadowTechnique> WorldNode::getShadowTechnique() const
{
if(!mShadowed)
return nullptr;
return static_cast<osgShadow::ShadowedScene*>(mShadowedGroup.get())->getShadowTechnique();
return mShadowedGroup->getShadowTechnique();
}

//==============================================================================
::osg::ref_ptr<osgShadow::ShadowTechnique> WorldNode::createDefaultShadowTechnique(const Viewer* viewer) {
::osg::ref_ptr<osgShadow::ShadowTechnique> WorldNode::createDefaultShadowTechnique(const Viewer* viewer)
{
assert(viewer);

::osg::ref_ptr<osgShadow::ShadowMap> sm = new osgShadow::ShadowMap;
// increase the resolution of default shadow texture for higher quality
int mapres = std::pow(2, 13);
auto mapres = static_cast<short>(std::pow(2, 13));
sm->setTextureSize(::osg::Vec2s(mapres,mapres));
// we are using Light1 because this is the highest one (on up direction)
sm->setLight(viewer->getLightSource(0));
Expand Down
5 changes: 3 additions & 2 deletions dart/gui/osg/WorldNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ class WorldNode : public ::osg::Group

void refreshShapeFrameNode(dart::dynamics::Frame* frame);

using NodeMap = std::unordered_map<dart::dynamics::Frame*, ShapeFrameNode*>;
using NodeMap = std::unordered_map<
dart::dynamics::Frame*, ::osg::ref_ptr<ShapeFrameNode>>;

/// Map from Frame pointers to FrameNode pointers
NodeMap mFrameToNode;
Expand All @@ -194,7 +195,7 @@ class WorldNode : public ::osg::Group
::osg::ref_ptr<::osg::Group> mNormalGroup;

/// OSG group for shadowed objects
::osg::ref_ptr<::osg::Group> mShadowedGroup;
::osg::ref_ptr<::osgShadow::ShadowedScene> mShadowedGroup;

/// Whether the shadows are enabled
bool mShadowed;
Expand Down
2 changes: 1 addition & 1 deletion examples/atlas_simbicon/AtlasSimbiconEventHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class AtlasSimbiconEventHandler : public osgGA::GUIEventHandler
const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter&) override;

protected:
AtlasSimbiconWorldNode* mNode;
::osg::ref_ptr<AtlasSimbiconWorldNode> mNode;
};

#endif // DART_EXAMPLE_OSG_OSGATLASSIMBICON_ATLASSIMBICONEVENTHANDLER_HPP_
18 changes: 15 additions & 3 deletions examples/atlas_simbicon/AtlasSimbiconWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ AtlasSimbiconWidget::AtlasSimbiconWidget(
void AtlasSimbiconWidget::render()
{
ImGui::SetNextWindowPos(ImVec2(10, 20));
ImGui::SetNextWindowSize(ImVec2(360, 340));
ImGui::SetNextWindowSize(ImVec2(360, 400));
ImGui::SetNextWindowBgAlpha(0.5f);
if (!ImGui::Begin(
"Atlas Control",
Expand Down Expand Up @@ -130,8 +130,20 @@ void AtlasSimbiconWidget::render()

// Headlights
mGuiHeadlights = mViewer->checkHeadlights();
ImGui::Checkbox("Headlights On/Off", &mGuiHeadlights);
mViewer->switchHeadlights(mGuiHeadlights);
if (ImGui::Checkbox("Headlights On/Off", &mGuiHeadlights))
{
mViewer->switchHeadlights(mGuiHeadlights);
}

// Shadow
mShadow = mNode->isShadowed();
if (ImGui::Checkbox("Shadow On/Off", &mShadow))
{
if (mShadow)
mNode->showShadow();
else
mNode->hideShadow();
}
}

if (ImGui::CollapsingHeader(
Expand Down
4 changes: 3 additions & 1 deletion examples/atlas_simbicon/AtlasSimbiconWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,16 @@ class AtlasSimbiconWidget : public dart::gui::osg::ImGuiWidget

::osg::ref_ptr<dart::gui::osg::ImGuiViewer> mViewer;

AtlasSimbiconWorldNode* mNode;
::osg::ref_ptr<AtlasSimbiconWorldNode> mNode;

float mGuiGravityAcc;

float mGravityAcc;

bool mGuiHeadlights;

bool mShadow;

/// Control mode value for GUI
int mGuiControlMode;

Expand Down
22 changes: 22 additions & 0 deletions examples/atlas_simbicon/AtlasSimbiconWorldNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

#include "AtlasSimbiconWorldNode.hpp"

#include <osgShadow/ShadowMap>

//==============================================================================
AtlasSimbiconWorldNode::AtlasSimbiconWorldNode(
const dart::simulation::WorldPtr& world,
Expand Down Expand Up @@ -111,3 +113,23 @@ void AtlasSimbiconWorldNode::switchToNoControl()
{
mController->changeStateMachine("standing", mWorld->getTime());
}

//==============================================================================
void AtlasSimbiconWorldNode::showShadow()
{
auto shadow
= dart::gui::osg::WorldNode::createDefaultShadowTechnique(mViewer);
if (auto sm = dynamic_cast<::osgShadow::ShadowMap*>(shadow.get()))
{
auto mapResolution = static_cast<short>(std::pow(2, 12));
sm->setTextureSize(::osg::Vec2s(mapResolution, mapResolution));
}

setShadowTechnique(shadow);
}

//==============================================================================
void AtlasSimbiconWorldNode::hideShadow()
{
setShadowTechnique(nullptr);
}
3 changes: 3 additions & 0 deletions examples/atlas_simbicon/AtlasSimbiconWorldNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class AtlasSimbiconWorldNode : public dart::gui::osg::RealTimeWorldNode
void switchToShortStrideWalking();
void switchToNoControl();

void showShadow();
void hideShadow();

protected:
std::unique_ptr<Controller> mController;
Eigen::Vector3d mExternalForce;
Expand Down
5 changes: 4 additions & 1 deletion examples/atlas_simbicon/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,12 @@ int main()
= new dart::gui::osg::ImGuiViewer();
viewer->addWorldNode(node);

// Enable shadow
node->showShadow();

// Add control widget for atlas
viewer->getImGuiHandler()->addWidget(
std::make_shared<AtlasSimbiconWidget>(viewer, node.get()));
std::make_shared<AtlasSimbiconWidget>(viewer, node));

// Pass in the custom event handler
viewer->addEventHandler(new AtlasSimbiconEventHandler(node));
Expand Down