[osg-users] RTT With Background Alpha

Dan West osgforum at tevs.eu
Fri Apr 8 12:54:16 PDT 2011


Hi,

  I'm trying to create an overlay texture rendered using render to texture (RTT).

  I've been playing around with the osgprerender example, but can't get the effect I'm trying for.

  I am trying to use the RTT texture as an overlay over an existing texture, so (starting at the stock osgprerender example) I duplicated the setTextCoordArray line to apply the texture coordinates to both texture units 0 and 1. I changed the RTT texture application to use texture unit 1, and created a texture and applied that to texture unit 0. (see below)


Code:

        ...

        // pass the created vertex array to the points geometry object.
        polyGeom->setVertexArray(vertices);

        polyGeom->setTexCoordArray(0,texcoords);
        polyGeom->setTexCoordArray(1,texcoords);

        osg::Vec4Array* colors = new osg::Vec4Array;
        colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
        polyGeom->setColorArray(colors);
        polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL);

        polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUAD_STRIP,0,vertices->size()));

        // new we need to add the texture to the Drawable, we do so by creating a 
        // StateSet to contain the Texture StateAttribute.
        osg::StateSet* stateset = new osg::StateSet;

        stateset->setTextureAttributeAndModes(1, texture, osg::StateAttribute::ON);

        polyGeom->setStateSet(stateset);

        polyGeom->setUpdateCallback(new MyGeometryCallback(origin,xAxis,yAxis,zAxis,1.0,1.0/width,0.2f));

        osg::Geode* geode = new osg::Geode();
        geode->addDrawable(polyGeom);
        
        {
           osg::Texture2D* flagTex = new osg::Texture2D();
           flagTex->setInternalFormat( GL_RGBA );
           flagTex->setWrap( osg::Texture::WRAP_R, osg::Texture::CLAMP_TO_BORDER );
           flagTex->setWrap( osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_BORDER );
           flagTex->setWrap( osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_BORDER );
           flagTex->setFilter( osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR );
           flagTex->setFilter( osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR );
           flagTex->setDataVariance( osg::Object::DYNAMIC ); // protect from being optimized away as static state.
           flagTex->setImage( osgDB::readImageFile("Images/american-flag.png") );
           polyGeom->getOrCreateStateSet()->setTextureAttributeAndModes( 0, flagTex );
        }

        parent->addChild(geode);

    }




  This is sort of what I'm going for (only the RRT overlay won't be a spinning 3d model), however everything behind the model is colored with the RTT camera's background color. (see screenshot below)

(screenshot omitted due to lack of posts)

  At this point, I'd like to simply set the background color to have some alpha ( 0.0f ), and be done with this issue, but I have been unable to disable the background and make it completely transparent.

  Any OSG guru's out there know how to do what I'm trying to do?

Thanks!
~ talyn

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








More information about the osg-users mailing list