Skip to content

Commit

Permalink
Fix depth testing for transparent objects (#1643)
Browse files Browse the repository at this point in the history
  • Loading branch information
jslee02 authored Jan 5, 2022
1 parent c5ed203 commit 4041649
Show file tree
Hide file tree
Showing 17 changed files with 483 additions and 69 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
* Fixed grouping of constraints: [#1624](https://github.com/dartsim/dart/pull/1624), [#1628](https://github.com/dartsim/dart/pull/1628)
* Fixed issue with removing skeletons without shapes: [#1625](https://github.com/dartsim/dart/pull/1625)

* GUI

* Fixed depth testing for transparent objects: [#1643](https://github.com/dartsim/dart/pull/1643)

### [DART 6.12.1 (2021-11-04)](https://github.com/dartsim/dart/milestone/71?closed=1)

* Build
Expand Down
59 changes: 49 additions & 10 deletions dart/gui/osg/GridVisual.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@

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

#include <osg/Depth>

#include "dart/dynamics/BodyNode.hpp"
#include "dart/dynamics/SimpleFrame.hpp"
#include "dart/dynamics/Skeleton.hpp"
#include "dart/dynamics/SphereShape.hpp"
#include "dart/gui/osg/Utils.hpp"
#include "dart/math/Helpers.hpp"

namespace dart {
Expand Down Expand Up @@ -443,11 +446,55 @@ void GridVisual::refresh()
mMinorLineGeom->getOrCreateStateSet()->setAttributeAndModes(
mMinorLineWidth);
mMinorLineGeom->setPrimitiveSet(0, mMinorLineFaces);
if (mMinorLineColor->at(0).a() < 1 - getAlphaThreshold<float>())
{
mMinorLineGeom->getOrCreateStateSet()->setMode(
GL_BLEND, ::osg::StateAttribute::ON);
mMinorLineGeom->getOrCreateStateSet()->setRenderingHint(
::osg::StateSet::TRANSPARENT_BIN);
::osg::ref_ptr<::osg::Depth> depth = new ::osg::Depth;
depth->setWriteMask(false);
mMinorLineGeom->getOrCreateStateSet()->setAttributeAndModes(
depth, ::osg::StateAttribute::ON);
}
else
{
mMinorLineGeom->getOrCreateStateSet()->setMode(
GL_BLEND, ::osg::StateAttribute::OFF);
mMinorLineGeom->getOrCreateStateSet()->setRenderingHint(
::osg::StateSet::OPAQUE_BIN);
::osg::ref_ptr<::osg::Depth> depth = new ::osg::Depth;
depth->setWriteMask(true);
mMinorLineGeom->getOrCreateStateSet()->setAttributeAndModes(
depth, ::osg::StateAttribute::ON);
}

mMajorLineGeom->setVertexArray(mMajorLineVertices);
mMajorLineGeom->getOrCreateStateSet()->setAttributeAndModes(
mMajorLineWidth);
mMajorLineGeom->setPrimitiveSet(0, mMajorLineFaces);
if (mMajorLineColor->at(0).a() < 1 - getAlphaThreshold<float>())
{
mMajorLineGeom->getOrCreateStateSet()->setMode(
GL_BLEND, ::osg::StateAttribute::ON);
mMajorLineGeom->getOrCreateStateSet()->setRenderingHint(
::osg::StateSet::TRANSPARENT_BIN);
::osg::ref_ptr<::osg::Depth> depth = new ::osg::Depth;
depth->setWriteMask(false);
mMajorLineGeom->getOrCreateStateSet()->setAttributeAndModes(
depth, ::osg::StateAttribute::ON);
}
else
{
mMajorLineGeom->getOrCreateStateSet()->setMode(
GL_BLEND, ::osg::StateAttribute::OFF);
mMajorLineGeom->getOrCreateStateSet()->setRenderingHint(
::osg::StateSet::OPAQUE_BIN);
::osg::ref_ptr<::osg::Depth> depth = new ::osg::Depth;
depth->setWriteMask(true);
mMajorLineGeom->getOrCreateStateSet()->setAttributeAndModes(
depth, ::osg::StateAttribute::ON);
}

static const ::osg::Vec4 xAxisLineColor(0.9f, 0.1f, 0.1f, 1.0f);
static const ::osg::Vec4 yAxisLineColor(0.1f, 0.9f, 0.1f, 1.0f);
Expand Down Expand Up @@ -516,9 +563,9 @@ void GridVisual::initialize()
mAxisLineGeom->setVertexArray(mAxisLineVertices);
mAxisLineGeom->setDataVariance(::osg::Object::STATIC);
mAxisLineGeom->getOrCreateStateSet()->setMode(
GL_BLEND, ::osg::StateAttribute::ON);
GL_BLEND, ::osg::StateAttribute::OFF);
mAxisLineGeom->getOrCreateStateSet()->setRenderingHint(
::osg::StateSet::TRANSPARENT_BIN);
::osg::StateSet::OPAQUE_BIN);

// Set grid color
static const ::osg::Vec4 majorLineColor(0.4f, 0.4f, 0.4f, 1.0f);
Expand All @@ -538,20 +585,12 @@ void GridVisual::initialize()
mMajorLineColor->at(0) = majorLineColor;
mMajorLineGeom->setColorArray(mMajorLineColor);
mMajorLineGeom->setColorBinding(::osg::Geometry::BIND_OVERALL);
mMajorLineGeom->getOrCreateStateSet()->setMode(
GL_BLEND, ::osg::StateAttribute::ON);
mMajorLineGeom->getOrCreateStateSet()->setRenderingHint(
::osg::StateSet::TRANSPARENT_BIN);

mMinorLineColor = new ::osg::Vec4Array;
mMinorLineColor->resize(1);
mMinorLineColor->at(0) = minorLineColor;
mMinorLineGeom->setColorArray(mMinorLineColor);
mMinorLineGeom->setColorBinding(::osg::Geometry::BIND_OVERALL);
mMinorLineGeom->getOrCreateStateSet()->setMode(
GL_BLEND, ::osg::StateAttribute::ON);
mMinorLineGeom->getOrCreateStateSet()->setRenderingHint(
::osg::StateSet::TRANSPARENT_BIN);

mMinorLineFaces = new ::osg::DrawElementsUInt(::osg::PrimitiveSet::LINES, 0);
mMinorLineGeom->addPrimitiveSet(mMinorLineFaces);
Expand Down
18 changes: 18 additions & 0 deletions dart/gui/osg/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@
#include <Eigen/Geometry>
#include <osg/Matrix>

//==============================================================================
template <typename T = double>
constexpr T getAlphaThreshold()
{
if constexpr (std::is_same_v<T, float>)
{
return 1e-6;
}
else if constexpr (std::is_same_v<T, double>)
{
return 1e-9;
}
else
{
return 1e-9;
}
}

//==============================================================================
template <typename Scalar>
::osg::Matrix eigToOsgMatrix(
Expand Down
26 changes: 23 additions & 3 deletions dart/gui/osg/render/BoxShapeNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "dart/gui/osg/render/BoxShapeNode.hpp"

#include <osg/CullFace>
#include <osg/Depth>
#include <osg/Geode>
#include <osg/ShapeDrawable>

Expand Down Expand Up @@ -128,8 +129,6 @@ BoxShapeGeode::BoxShapeGeode(
mBoxShape(shape),
mDrawable(nullptr)
{
getOrCreateStateSet()->setMode(GL_BLEND, ::osg::StateAttribute::ON);
getOrCreateStateSet()->setRenderingHint(::osg::StateSet::TRANSPARENT_BIN);
getOrCreateStateSet()->setAttributeAndModes(
new ::osg::CullFace(::osg::CullFace::BACK));
extractData();
Expand Down Expand Up @@ -191,7 +190,28 @@ void BoxShapeDrawable::refresh(bool firstTime)
if (mBoxShape->checkDataVariance(dart::dynamics::Shape::DYNAMIC_COLOR)
|| firstTime)
{
setColor(eigToOsgVec4d(mVisualAspect->getRGBA()));
// Set color
const ::osg::Vec4d color = eigToOsgVec4d(mVisualAspect->getRGBA());
setColor(color);

// Set alpha specific properties
::osg::StateSet* ss = getOrCreateStateSet();
if (std::abs(color.a()) > 1 - getAlphaThreshold())
{
ss->setMode(GL_BLEND, ::osg::StateAttribute::OFF);
ss->setRenderingHint(::osg::StateSet::OPAQUE_BIN);
::osg::ref_ptr<::osg::Depth> depth = new ::osg::Depth;
depth->setWriteMask(true);
ss->setAttributeAndModes(depth, ::osg::StateAttribute::ON);
}
else
{
ss->setMode(GL_BLEND, ::osg::StateAttribute::ON);
ss->setRenderingHint(::osg::StateSet::TRANSPARENT_BIN);
::osg::ref_ptr<::osg::Depth> depth = new ::osg::Depth;
depth->setWriteMask(false);
ss->setAttributeAndModes(depth, ::osg::StateAttribute::ON);
}
}
}

Expand Down
26 changes: 23 additions & 3 deletions dart/gui/osg/render/CapsuleShapeNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "dart/gui/osg/render/CapsuleShapeNode.hpp"

#include <osg/CullFace>
#include <osg/Depth>
#include <osg/Geode>
#include <osg/ShapeDrawable>

Expand Down Expand Up @@ -135,8 +136,6 @@ CapsuleShapeGeode::CapsuleShapeGeode(
mCapsuleShape(shape),
mDrawable(nullptr)
{
getOrCreateStateSet()->setMode(GL_BLEND, ::osg::StateAttribute::ON);
getOrCreateStateSet()->setRenderingHint(::osg::StateSet::TRANSPARENT_BIN);
getOrCreateStateSet()->setAttributeAndModes(
new ::osg::CullFace(::osg::CullFace::BACK));
extractData();
Expand Down Expand Up @@ -201,7 +200,28 @@ void CapsuleShapeDrawable::refresh(bool firstTime)
if (mCapsuleShape->checkDataVariance(dart::dynamics::Shape::DYNAMIC_COLOR)
|| firstTime)
{
setColor(eigToOsgVec4d(mVisualAspect->getRGBA()));
// Set color
const ::osg::Vec4d color = eigToOsgVec4d(mVisualAspect->getRGBA());
setColor(color);

// Set alpha specific properties
::osg::StateSet* ss = getOrCreateStateSet();
if (std::abs(color.a()) > 1 - getAlphaThreshold())
{
ss->setMode(GL_BLEND, ::osg::StateAttribute::OFF);
ss->setRenderingHint(::osg::StateSet::OPAQUE_BIN);
::osg::ref_ptr<::osg::Depth> depth = new ::osg::Depth;
depth->setWriteMask(true);
ss->setAttributeAndModes(depth, ::osg::StateAttribute::ON);
}
else
{
ss->setMode(GL_BLEND, ::osg::StateAttribute::ON);
ss->setRenderingHint(::osg::StateSet::TRANSPARENT_BIN);
::osg::ref_ptr<::osg::Depth> depth = new ::osg::Depth;
depth->setWriteMask(false);
ss->setAttributeAndModes(depth, ::osg::StateAttribute::ON);
}
}
}

Expand Down
26 changes: 23 additions & 3 deletions dart/gui/osg/render/ConeShapeNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "dart/gui/osg/render/ConeShapeNode.hpp"

#include <osg/CullFace>
#include <osg/Depth>
#include <osg/Geode>
#include <osg/ShapeDrawable>

Expand Down Expand Up @@ -134,8 +135,6 @@ ConeShapeGeode::ConeShapeGeode(
mConeShape(shape),
mDrawable(nullptr)
{
getOrCreateStateSet()->setMode(GL_BLEND, ::osg::StateAttribute::ON);
getOrCreateStateSet()->setRenderingHint(::osg::StateSet::TRANSPARENT_BIN);
getOrCreateStateSet()->setAttributeAndModes(
new ::osg::CullFace(::osg::CullFace::BACK));
extractData();
Expand Down Expand Up @@ -200,7 +199,28 @@ void ConeShapeDrawable::refresh(bool firstTime)
if (mConeShape->checkDataVariance(dart::dynamics::Shape::DYNAMIC_COLOR)
|| firstTime)
{
setColor(eigToOsgVec4d(mVisualAspect->getRGBA()));
// Set color
const ::osg::Vec4d color = eigToOsgVec4d(mVisualAspect->getRGBA());
setColor(color);

// Set alpha specific properties
::osg::StateSet* ss = getOrCreateStateSet();
if (std::abs(color.a()) > 1 - getAlphaThreshold())
{
ss->setMode(GL_BLEND, ::osg::StateAttribute::OFF);
ss->setRenderingHint(::osg::StateSet::OPAQUE_BIN);
::osg::ref_ptr<::osg::Depth> depth = new ::osg::Depth;
depth->setWriteMask(true);
ss->setAttributeAndModes(depth, ::osg::StateAttribute::ON);
}
else
{
ss->setMode(GL_BLEND, ::osg::StateAttribute::ON);
ss->setRenderingHint(::osg::StateSet::TRANSPARENT_BIN);
::osg::ref_ptr<::osg::Depth> depth = new ::osg::Depth;
depth->setWriteMask(false);
ss->setAttributeAndModes(depth, ::osg::StateAttribute::ON);
}
}
}

Expand Down
26 changes: 23 additions & 3 deletions dart/gui/osg/render/CylinderShapeNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "dart/gui/osg/render/CylinderShapeNode.hpp"

#include <osg/CullFace>
#include <osg/Depth>
#include <osg/Geode>
#include <osg/ShapeDrawable>

Expand Down Expand Up @@ -135,8 +136,6 @@ CylinderShapeGeode::CylinderShapeGeode(
mCylinderShape(shape),
mDrawable(nullptr)
{
getOrCreateStateSet()->setMode(GL_BLEND, ::osg::StateAttribute::ON);
getOrCreateStateSet()->setRenderingHint(::osg::StateSet::TRANSPARENT_BIN);
getOrCreateStateSet()->setAttributeAndModes(
new ::osg::CullFace(::osg::CullFace::BACK));
extractData();
Expand Down Expand Up @@ -202,7 +201,28 @@ void CylinderShapeDrawable::refresh(bool firstTime)
if (mCylinderShape->checkDataVariance(dart::dynamics::Shape::DYNAMIC_COLOR)
|| firstTime)
{
setColor(eigToOsgVec4d(mVisualAspect->getRGBA()));
// Set color
const ::osg::Vec4d color = eigToOsgVec4d(mVisualAspect->getRGBA());
setColor(color);

// Set alpha specific properties
::osg::StateSet* ss = getOrCreateStateSet();
if (std::abs(color.a()) > 1 - getAlphaThreshold())
{
ss->setMode(GL_BLEND, ::osg::StateAttribute::OFF);
ss->setRenderingHint(::osg::StateSet::OPAQUE_BIN);
::osg::ref_ptr<::osg::Depth> depth = new ::osg::Depth;
depth->setWriteMask(true);
ss->setAttributeAndModes(depth, ::osg::StateAttribute::ON);
}
else
{
ss->setMode(GL_BLEND, ::osg::StateAttribute::ON);
ss->setRenderingHint(::osg::StateSet::TRANSPARENT_BIN);
::osg::ref_ptr<::osg::Depth> depth = new ::osg::Depth;
depth->setWriteMask(false);
ss->setAttributeAndModes(depth, ::osg::StateAttribute::ON);
}
}
}

Expand Down
26 changes: 23 additions & 3 deletions dart/gui/osg/render/EllipsoidShapeNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "dart/gui/osg/render/EllipsoidShapeNode.hpp"

#include <osg/CullFace>
#include <osg/Depth>
#include <osg/Geode>
#include <osg/Light>
#include <osg/Material>
Expand Down Expand Up @@ -159,8 +160,6 @@ EllipsoidShapeGeode::EllipsoidShapeGeode(
mEllipsoidShape(shape),
mDrawable(nullptr)
{
getOrCreateStateSet()->setMode(GL_BLEND, ::osg::StateAttribute::ON);
getOrCreateStateSet()->setRenderingHint(::osg::StateSet::TRANSPARENT_BIN);
getOrCreateStateSet()->setAttributeAndModes(
new ::osg::CullFace(::osg::CullFace::BACK));
extractData();
Expand Down Expand Up @@ -227,7 +226,28 @@ void EllipsoidShapeDrawable::refresh(bool firstTime)
if (mEllipsoidShape->checkDataVariance(dart::dynamics::Shape::DYNAMIC_COLOR)
|| firstTime)
{
setColor(eigToOsgVec4d(mVisualAspect->getRGBA()));
// Set color
const ::osg::Vec4d color = eigToOsgVec4d(mVisualAspect->getRGBA());
setColor(color);

// Set alpha specific properties
::osg::StateSet* ss = getOrCreateStateSet();
if (std::abs(color.a()) > 1 - getAlphaThreshold())
{
ss->setMode(GL_BLEND, ::osg::StateAttribute::OFF);
ss->setRenderingHint(::osg::StateSet::OPAQUE_BIN);
::osg::ref_ptr<::osg::Depth> depth = new ::osg::Depth;
depth->setWriteMask(true);
ss->setAttributeAndModes(depth, ::osg::StateAttribute::ON);
}
else
{
ss->setMode(GL_BLEND, ::osg::StateAttribute::ON);
ss->setRenderingHint(::osg::StateSet::TRANSPARENT_BIN);
::osg::ref_ptr<::osg::Depth> depth = new ::osg::Depth;
depth->setWriteMask(false);
ss->setAttributeAndModes(depth, ::osg::StateAttribute::ON);
}
}
}

Expand Down
Loading

0 comments on commit 4041649

Please sign in to comment.