[osg-users] Transparency & Invisibility

342 views
Skip to first unread message

Mathieu Scorpionis

unread,
Nov 23, 2010, 4:45:58 AM11/23/10
to osg-...@lists.openscenegraph.org
Hello OSG Community

I am currently playing with opacity/transparency capabilities of geodes in a model.
In that way, i use:

material = (osg::Material *) Geode->getStateSet()->getAttribute(osg::StateAttribute::MATERIAL);
Geode->getStateSet()->setMode( GL_BLEND, osg::StateAttribute::ON );
material->setTransparency(osg::Material::FRONT, 1.-opacity);
Geode->getStateSet()->setAttributeAndModes(material, osg::StateAttribute::OVERRIDE);

It works, the geode becomes transparent.

But is there a way to see objects behind this geode ?
I mean : if i use Geode->setNodeMask(0x000000); the geode is invisible and I can see what is behind, but not with the transparency effect.

Please, do you have an idea how managing the code if i want to see what is behind a transparent object ?


Cheers,
Mathieu

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=33972#33972

_______________________________________________
osg-users mailing list
osg-...@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Juan Hernando

unread,
Nov 23, 2010, 5:11:25 AM11/23/10
to osg-...@lists.openscenegraph.org
Hi Mathieu

> I am currently playing with opacity/transparency capabilities of geodes in a model.
> In that way, i use:
>
> material = (osg::Material *) Geode->getStateSet()->getAttribute(osg::StateAttribute::MATERIAL);
> Geode->getStateSet()->setMode( GL_BLEND, osg::StateAttribute::ON );
> material->setTransparency(osg::Material::FRONT, 1.-opacity);
> Geode->getStateSet()->setAttributeAndModes(material, osg::StateAttribute::OVERRIDE);
>
> It works, the geode becomes transparent.
>
> But is there a way to see objects behind this geode ?

Most probably, your problem is that the transparent object is rendered
first. Then, everything that is behind doesn't pass the z-test and it's
culled away. In order to "see" trhough transparent geometry you have to
render it after all opaque objects have been rendered. Adding:
Geode->getStateSet()->setRenderBinDetails(1, "transparent");
you will force that geode to be rendered in a different render bin which
will be processed after the default render bin.
If you have more than one transparent object you also have to make sure
that they are rendered back to front. I think that's done with:
Geode->getStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
but I'm not fully sure.

I'd also add:
osg::BlendFunc *fuct = new osg::BlendFunc();
func->setFunction(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Geode->getStateSet()->setAttributeAndModes(func);
I don't know it that's the default blending function but I guess that's
the effect you are looking for. Setting it explicitly can't be harmful.

> I mean : if i use Geode->setNodeMask(0x000000); the geode is invisible and I can see what is behind, but not with the transparency effect.

Setting such a node mask makes all scenegraph traversals ignore that
node, that's why it becomes invisible.

> Please, do you have an idea how managing the code if i want to see what is behind a transparent object ?

Keep in mind that the code above only works correctly for
non-intersecting convex objects without back faces and without
visibility cycles (so actually it seldom works). In any other case you
may see artifacts depending on the view point. If you need reliable
transparency you will have to implement a much more sophisticated
technique, such as depth peeling, bucket depth peeling or this one
https://graphics.stanford.edu/wikis/cs448s-10/FrontPage?action=AttachFile&do=get&target=CS448s-10-11-oit.pdf
if your graphics card supports Shader Model 5.

Regards,
Juan

Mathieu Scorpionis

unread,
Nov 23, 2010, 5:23:06 AM11/23/10
to osg-...@lists.openscenegraph.org
Hi Juan

Thank you very much for the precise answer.
I try this !

Cheers,
Mathieu

------------------
Read this topic online here:

http://forum.openscenegraph.org/viewtopic.php?p=33974#33974

Jean-Sébastien Guay

unread,
Nov 23, 2010, 9:11:58 AM11/23/10
to osg-...@lists.openscenegraph.org
Hello Mathieu,

> It works, the geode becomes transparent.
>
> But is there a way to see objects behind this geode ?

Juan gave you a good answer, but the short version is add:

Geode->getStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);

Just to be clear, the canonical way of enabling transparency after
setting the alpha in a material or a texture or a geometry color (with
GL_COLOR_MATERIAL) is:

Geode->getStateSet()->setMode( GL_BLEND, osg::StateAttribute::ON );

Geode->getStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);

Juan spoke about adding an osg::BlendFunc, this is only necessary if you
want to override the default blending function, which is set to what he
said by default (it will give the results you likely want).

And his info about when this type of transparency works is spot on, you
will get good results if two objects are disjoint and manifold,
otherwise you will get artifacts but we generally ignore those kinds of
artifacts for the sake of real-time performance.

Hope this helps,

J-S
--
______________________________________________________
Jean-Sebastien Guay jean-seba...@cm-labs.com
http://www.cm-labs.com/
http://whitestar02.webhop.org/

Reply all
Reply to author
Forward
0 new messages