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

Osg shadows and lighting #976

Closed
wants to merge 5 commits into from
Closed
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
2 changes: 1 addition & 1 deletion dart/gui/osg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ endif()
if(DART_BUILD_GUI_OSG)

find_package(OpenSceneGraph 3.0 QUIET
COMPONENTS osg osgViewer osgManipulator osgGA osgDB)
COMPONENTS osg osgViewer osgManipulator osgGA osgDB osgShadow)

# It seems that OPENSCENEGRAPH_FOUND will inadvertently get set to true when
# OpenThreads is found, even if OpenSceneGraph is not installed. This is quite
Expand Down
84 changes: 84 additions & 0 deletions dart/gui/osg/ShadowedWorldNode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2011-2017, The DART development contributors
* All rights reserved.
*
* The list of contributors can be found at:
* https://github.com/dartsim/dart/blob/master/LICENSE
*
* This file is provided under the following "BSD-style" License:
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "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 THE COPYRIGHT HOLDER OR
* CONTRIBUTORS 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 <osgShadow/ShadowedScene>
#include <osgShadow/ShadowMap>

#include "dart/gui/osg/ShadowedWorldNode.hpp"

namespace dart {
namespace gui {
namespace osg {

//==============================================================================
ShadowedWorldNode::ShadowedWorldNode(std::shared_ptr<dart::simulation::World> _world)
: WorldNode(_world) {}

//==============================================================================
void ShadowedWorldNode::setupViewer()
{
constexpr int ReceivesShadowTraversalMask = 0x2;
constexpr int CastsShadowTraversalMask = 0x1;

// Setup shadows
// Create the ShadowedScene
::osg::ref_ptr<osgShadow::ShadowedScene> shadowedScene = new osgShadow::ShadowedScene;
shadowedScene->setReceivesShadowTraversalMask(ReceivesShadowTraversalMask);
shadowedScene->setCastsShadowTraversalMask(CastsShadowTraversalMask);
// Use the ShadowMap technique
::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);
sm->setTextureSize(::osg::Vec2s(mapres,mapres));

// set the technique
shadowedScene->setShadowTechnique(sm.get());

// add the Viewer's root object to the shadowed scene
shadowedScene->addChild(mViewer->getRootGroup().get());

// save the shadowed scene
mShadowedScene = shadowedScene;
// get camera's home position
// it is resetted when changing the SceneData, which can lead to undesired behavior
::osg::Vec3d eye, center, up;
mViewer->getCameraManipulator()->getHomePosition(eye, center, up);
// replace the viewer's scene data with the shadowed scene
mViewer->setSceneData(mShadowedScene.get());
// reset the home position
mViewer->getCameraManipulator()->setHomePosition(eye, center, up);
mViewer->home();
}

} // namespace osg
} // namespace gui
} // namespace dart
65 changes: 65 additions & 0 deletions dart/gui/osg/ShadowedWorldNode.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2011-2017, The DART development contributors
* All rights reserved.
*
* The list of contributors can be found at:
* https://github.com/dartsim/dart/blob/master/LICENSE
*
* This file is provided under the following "BSD-style" License:
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "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 THE COPYRIGHT HOLDER OR
* CONTRIBUTORS 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.
*/

#ifndef DART_GUI_OSG_SHADOWEDWORLDNODE_HPP_
#define DART_GUI_OSG_SHADOWEDWORLDNODE_HPP_

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

namespace dart {
namespace gui {
namespace osg {

/// ShadowedWorldNode class encapsulates a World to be displayed in OpenSceneGraph with shadows
class ShadowedWorldNode : public WorldNode
{
public:

/// Default constructor
explicit ShadowedWorldNode(std::shared_ptr<dart::simulation::World> _world = nullptr);

protected:
/// Called when this world gets added to an dart::gui::osg::Viewer. Override
/// this function to customize the way your WorldNode starts up in an
/// dart::gui::osg::Viewer. Default behavior enables shadows.
void setupViewer();

/// Osg ShadowedScene for handling shadows
::osg::ref_ptr<::osg::Group> mShadowedScene;

};

} // namespace osg
} // namespace gui
} // namespace dart

#endif // DART_GUI_OSG_SHADOWEDWORLDNODE_HPP_
35 changes: 20 additions & 15 deletions dart/gui/osg/Viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,48 +316,50 @@ void Viewer::switchHeadlights(bool _on)

if(_on)
{
// Turn on lights
if(getLight())
{
getLight()->setAmbient(::osg::Vec4(0.1,0.1,0.1,1.0));
getLight()->setDiffuse(::osg::Vec4(0.8,0.8,0.8,1.0));
getLight()->setAmbient(::osg::Vec4(0.1,0.1,0.1,1.0)); // small ambient lighting is always desirable
getLight()->setDiffuse(::osg::Vec4(1.0,1.0,1.0,1.0));
getLight()->setSpecular(::osg::Vec4(1.0,1.0,1.0,1.0));
}

if(mLight1)
{
mLight1->setAmbient(::osg::Vec4(0.0,0.0,0.0,1.0));
mLight1->setDiffuse(::osg::Vec4(0.0,0.0,0.0,1.0));
mLight1->setSpecular(::osg::Vec4(0.0,0.0,0.0,1.0));
mLight1->setDiffuse(::osg::Vec4(1.0,1.0,1.0,1.0));
mLight1->setSpecular(::osg::Vec4(1.0,1.0,1.0,1.0));
}

if(mLight2)
{
mLight2->setAmbient(::osg::Vec4(0.0,0.0,0.0,1.0));
mLight2->setDiffuse(::osg::Vec4(0.0,0.0,0.0,1.0));
mLight2->setSpecular(::osg::Vec4(0.0,0.0,0.0,1.0));
mLight1->setDiffuse(::osg::Vec4(1.0,1.0,1.0,1.0));
mLight1->setSpecular(::osg::Vec4(1.0,1.0,1.0,1.0));
}
}
else
{
// Turn off lights
if(getLight())
{
getLight()->setAmbient(::osg::Vec4(0.1,0.1,0.1,1.0));
getLight()->setAmbient(::osg::Vec4(0.1,0.1,0.1,1.0)); // small ambient lighting is always desirable
getLight()->setDiffuse(::osg::Vec4(0.0,0.0,0.0,1.0));
getLight()->setSpecular(::osg::Vec4(0.0,0.0,0.0,1.0));
}

if(mLight1)
{
mLight1->setAmbient(::osg::Vec4(0.0,0.0,0.0,1.0));
mLight1->setDiffuse(::osg::Vec4(0.7,0.7,0.7,1.0));
mLight1->setSpecular(::osg::Vec4(0.9,0.9,0.9,1.0));
mLight1->setDiffuse(::osg::Vec4(0.0,0.0,0.0,1.0));
mLight1->setSpecular(::osg::Vec4(0.0,0.0,0.0,1.0));
}

if(mLight2)
{
mLight2->setAmbient(::osg::Vec4(0.0,0.0,0.0,1.0));
mLight2->setDiffuse(::osg::Vec4(0.3,0.3,0.3,1.0));
mLight2->setSpecular(::osg::Vec4(0.4,0.4,0.4,1.0));
mLight2->setDiffuse(::osg::Vec4(0.0,0.0,0.0,1.0));
mLight2->setSpecular(::osg::Vec4(0.0,0.0,0.0,1.0));
}
}
}
Expand Down Expand Up @@ -508,10 +510,13 @@ void Viewer::setUpwardsDirection(const ::osg::Vec3& _up)
mOver = ::osg::Vec3(0,0,1)^_up;
mOver.normalize();

::osg::Vec3 p1 = mUpwards+mOver;
mLight1->setPosition(::osg::Vec4(p1[0], p1[1], p1[2], 0.0));
::osg::Vec3 p2 = mUpwards-mOver;
mLight2->setPosition(::osg::Vec4(p2[0], p2[1], p2[2], 0.0));
::osg::Vec3 p1 = mUpwards*2.+mOver;
mLight1->setPosition(::osg::Vec4(p1[0], p1[1], p1[2], 1.0)); // 1.0 at the end means point or spot light
// default is point light since the spot cutoff is set at 180 degrees by default
mLight1->setQuadraticAttenuation(1.); // enable quadratic attenuation for more realistic lighting
::osg::Vec3 p2 = mUpwards*2.-mOver;
mLight2->setPosition(::osg::Vec4(p2[0], p2[1], p2[2], 1.0)); // 1.0 at the end means point or spot light
mLight2->setQuadraticAttenuation(1.); // enable quadratic attenuation for more realistic lighting
}

//==============================================================================
Expand Down