Hi,

I am trying to use render bins and the stencil buffer, I want to load two 
objects , not render them but use them to create a mask which is applied to a 
third object.

Render bin 1  Load a cylinder set color mask to false, so cylinder isn’t 
rendered

Render bin 2  Load a plane, write a 1 into the stencil buffer where the plane 
passes the depth buffer test. (i.e set pixels in plane that are outside the 
cylinder to 1) set color mask to false, so plane isn’t rendered

Render bin 3 Display a square where the stencil buffer is 1.  set color mask to 
true, so square is rendered.

However it appears even though the cylinder and plane from render bins 1 and 2 
aren’t drawn to the screen an outline of their shape (in the same colour as the 
background) is obscuring the final shape in render bin 3.
Is there some depth testing going on before the color mask is applied?  can 
anybody tell me how I can resolve this.  Any help would be appreciated as I 
have ground to a bit of a halt.
Example code below.


Thank you!

Cheers,
Craig



void createScene(osg::Group* root)
{

        

        //Bin 1
                /*************************************************
                * Render  cylinder
                * 
                **************************************************/
                
                osg::StateSet*ssBin1 = new osg::StateSet();
                ssBin1->setRenderBinDetails(1,"RenderBin");

                osg::ColorMask* colorMask1 = new osg::ColorMask;
                //colorMask1->setMask(true,true,true,true);
                colorMask1->setMask(false,false,false,false);
                ssBin1->setAttributeAndModes(colorMask1, 
osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
                
                osg::ref_ptr<osg::Cylinder> myCylinder = new 
osg::Cylinder(osg::Vec3(0,-1,0),0.1,1.0);
                osg::ref_ptr<osg::ShapeDrawable> myCylinderDrawable = new 
osg::ShapeDrawable(myCylinder);
                osg::Geode* geode1 = new osg::Geode;
                geode1->addDrawable(myCylinderDrawable);
                
                geode1->setStateSet(ssBin1);

                root->addChild(geode1);

        
         //Bin 2
        
                /*****************************************
                * Create mask geometry, sets the bit in the stencil buffer to 1 
where 
                * the mask is rendered (if the mask is inside the cylinder
                * at that point it isn't rendered, so is failing the 
                * depth test and the bit is left as 0
                *
                ******************************************/
                
                osg::Vec3Array *vertices = new osg::Vec3Array;

                vertices->push_back(osg::Vec3(-0.5, -1.0, -0.5));
                vertices->push_back(osg::Vec3(0.5, -1.0, -0.5));
                vertices->push_back(osg::Vec3(0.5, -1.0, 0.5));
                vertices->push_back(osg::Vec3(-0.5, -1.0, 0.5));


                osg::Geometry *geom = new osg::Geometry;
                geom->setVertexArray(vertices);
                geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, 4));

                osg::Geode *geode = new osg::Geode;
                geode->addDrawable(geom);
                root->addChild(geode);

                osg::StateSet*ssBin2 = new osg::StateSet();
                ssBin2->setRenderBinDetails(2,"RenderBin");
                geode->setStateSet(ssBin2);
                                
                
                osg::ColorMask* colorMask2 = new osg::ColorMask;
                //colorMask2->setMask(true,true,true,true);
                colorMask2->setMask(false,false,false,false);
                ssBin2->setAttribute(colorMask2);

                osg::Stencil* rootStencil = new osg::Stencil();
                rootStencil->setWriteMask(true);
                
                rootStencil->setFunction(osg::Stencil::ALWAYS,1,~0u);
                rootStencil->setOperation(osg::Stencil::KEEP, 
osg::Stencil::KEEP, osg::Stencil::REPLACE);
                ssBin2->setAttributeAndModes(rootStencil);
        

                //bin3
                /*********************************************************
                * Create a square but only render the parts of the square
                * where the stencil isn't equal to 1
                *
                ***********************************************************/
        
                osg::StateSet*ssBin3= new osg::StateSet();
                ssBin3->setRenderBinDetails(3,"RenderBin");


                osg::ColorMask* colorMask3 = new osg::ColorMask;
                colorMask3->setMask(true,true,true,true);
                //colorMask3->setMask(false,false,false,false);
                ssBin3->setAttributeAndModes(colorMask3, 
osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);

                
                osg::Stencil* stencil3 = new osg::Stencil();
                stencil3->setFunction(osg::Stencil::NOTEQUAL,1,~0u);
                //stencil3->setFunction(osg::Stencil::EQUAL,1,~0u);
                stencil3->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, 
osg::Stencil::KEEP);
                ssBin3->setAttributeAndModes(stencil3);
                

                /*************************************************
                * Render  the square
                * 
                //**************************************************/
                
                /*******************************
                * Using these coordinates is rendered correctly
                * because the final shape is in front of the 
                * other objects
                *********************************/
                /*osg::Vec3Array* vertices3 = new osg::Vec3Array;
                vertices3->push_back(osg::Vec3(-1.0, -2.0, -1.0));
                vertices3->push_back(osg::Vec3(1.0, -2.0, -1.0));
                vertices3->push_back(osg::Vec3(1.0, -2.0, 1.0));
                vertices3->push_back(osg::Vec3(-1.0, -2.0, 1.0));*/

                /*******************************
                * Using these coordinates is obscured
                * by the cylinder and mask
                *********************************/
                osg::Vec3Array* vertices3 = new osg::Vec3Array;
                vertices3->push_back(osg::Vec3(-1.0, 0.0, -1.0));
                vertices3->push_back(osg::Vec3(1.0, 0.0, -1.0));
                vertices3->push_back(osg::Vec3(1.0, 0.0, 1.0));
                vertices3->push_back(osg::Vec3(-1.0, 0.0, 1.0));

                osg::Geometry* geom3 = new osg::Geometry;
                geom3->setVertexArray(vertices3);
                geom3->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, 4));

                osg::Geode* geode3 = new osg::Geode;
                geode3->addDrawable(geom3);

                geode3->setStateSet(ssBin3);

                //depth->setWriteMask( false );
                root->addChild(geode3);


        


}




int main( int argc, char **argv )
{
    osg::DisplaySettings::instance()->setMinimumNumStencilBits(8);

        osgViewer::Viewer viewer;

        viewer.setUpViewInWindow( 50, 50, 1024, 768 );  

    osg::Group* rootNode = new osg::Group;
                
        
        createScene(rootNode);
        
    
    // add model to the viewer.
    viewer.setSceneData( rootNode );

    return viewer.run();
}

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




Attachments: 
http://forum.openscenegraph.org//files/renderbins4_900.cpp


_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to