[osg-users] Yet another transparency issue (lazy state updating?)

Morné Pistorius mpistorius.osg at googlemail.com
Fri Sep 28 03:33:58 PDT 2007


Hi guys,

Continuing in the same vein as the recent discussion, I would also
like to add something to the problem.

I am trying to create 2D tracks that fade in the distance by drawing
lines with decreasing alpha (see screenshot).  Unfortunately, no
matter what I try the lines just would not blend with the background
image.  What is strange though, is that it works sometimes, without me
having changed anything.  I think it is a depth sorting problem, since
I can change the camera as I wish, and it doesn't make a difference.
It either works all the time or not at all.

If anyone has any ideas, I would really appreciate them.  I'm stumped.

This is my creation code:

osg::Geode* VOSG2DTracks::CreateTracks()
{
  osg::Geode* geode = new osg::Geode;

  // set up the Geometry. Initially created with no vertexes
  osg::Geometry* geom = new osg::Geometry;

  osg::Vec4Array* color = new osg::Vec4Array;

  osg::Vec3Array* coords = new osg::Vec3Array;

  geom->setVertexArray(coords);

  osg::Vec3Array* norms = new osg::Vec3Array(1);
  (*norms)[0] = osg::Vec3( 0.0, 0.0, 1.0 );

  geom->setNormalArray(norms);
  geom->setNormalBinding(osg::Geometry::BIND_OVERALL);

  geom->setColorArray( color );
  geom->setColorBinding( osg::Geometry::BIND_PER_VERTEX );

  geom->addPrimitiveSet(new osg::DrawArrays( osg::PrimitiveSet::LINES,
0, coords->size() ) );
  geom->setUseDisplayList( false );
  geom->setDataVariance( osg::Object::DYNAMIC );

  osg::StateSet* stateset = geode->getOrCreateStateSet();
  stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
  stateset->setMode(GL_BLEND,osg::StateAttribute::ON );
  stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
  stateset->setRenderBinDetails( 101, "RenderBin",
osg::StateSet::INHERIT_RENDERBIN_DETAILS );

  geode->addDrawable( geom );

  return geode;
}


And I update the tracks using this in the update traversal

void VOSG2DTracks::UpdateTracks()
{
  osg::Geode* geode = dynamic_cast< osg::Geode* > ( getChild( 0 ) );
  osg::Geometry* geom = geode->getDrawable( 0 )->asGeometry();

  osg::Vec3Array* coords = dynamic_cast< osg::Vec3Array* > (
geom->getVertexArray() );
  coords->clear();

  osg::Vec4Array* color = dynamic_cast< osg::Vec4Array* > (
geom->getColorArray() );
  color->clear();

  for ( int i = 0; i < m_pActiveTracksOnly->size(); ++i )
  {
    V2DTrack & Track = (*m_pActiveTracksOnly)[i];
    for ( int n = 0; n < Track.m_XYPos.size()-1; ++n )
    {
      coords->push_back( osg::Vec3( Track.m_XYPos[n][0],
Track.m_XYPos[n][1], 2 ) );
      coords->push_back( osg::Vec3( Track.m_XYPos[n+1][0],
Track.m_XYPos[n+1][1], 2 ) );

      // make track change from green to yellow and fade out in distance
      double d1 = 1 - ( double( MaxLenght ) - double( n )   ) /
double( MaxLenght );
      double d2 = 1 - ( double( MaxLenght ) - double( n+1 ) ) /
double( MaxLenght );
//       color->push_back( osg::Vec4( 1-d1, 1, d1, d1 ) );
//       color->push_back( osg::Vec4( 1-d2, 1, d2, d2 ) );
      color->push_back( osg::Vec4( 1-d1, 1, d1, 0.2 ) );  // test transparency
      color->push_back( osg::Vec4( 1-d2, 1, d2, 0.2 ) );  // test transparency
    }
  }

  osg::DrawArrays * da = dynamic_cast< osg::DrawArrays*> (
geom->getPrimitiveSet(0) );
  da->setCount( coords->size() );
}


As always, thanks!
Regards,

Morne Pistorius
-------------- next part --------------
A non-text attachment was scrubbed...
Name: tracks.jpg
Type: image/jpeg
Size: 69161 bytes
Desc: not available
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20070928/33581d42/attachment-0002.jpg>


More information about the osg-users mailing list