Ejemplo n.º 1
0
    void convertToColorIdentificationSwitchable(OSG::Node * const root)
    {
        // This is a recursive traversal of the Scene Graph, so as to replace
        // each material by a SwitchMaterial, in wich we put the normal one on one
        // side, and the color identification one in other side.
        OSG::UInt32 children = root->getNChildren();

        if(root->getCore()->getType().isDerivedFrom(OSG::MaterialGroup::getClassType()))
        {
            // Need to turn off material groups, as they would override our
            // geo-specific materials
            // Just record them here.
            
            OSG::MaterialGroup *mg = dynamic_cast<OSG::MaterialGroup *>(root->getCore());

            _mgswitches.push_back(
                MaterialGroupList::value_type(
                    mg, 
                    static_cast<OSG::Material *>(NULL)));
        }
        
        if(root->getCore()->getType().isDerivedFrom(OSG::Geometry::getClassType()))
        {

            OSG::Geometry *geo = dynamic_cast<OSG::Geometry *>(root->getCore());

            // If we get a Geometry, we replace it by a switch, 
            // we add this geometry a SwitchMaterial, with its original material
            // in one case, and a chunkmaterial corresponding to the node ID in the other.
            OSG::Color4ub c = create_new_color();
            OSG::Color4f cf;
            cf.setRGBA(c.getRGBA());
            // We add the associated pair color/node to the map
            //_node_index[c] = root;
            _node_index [c] = root;

            OSG::PolygonChunkRefPtr  pc = OSG::PolygonChunk::create();
            pc->setSmooth(false);

            OSG::MaterialChunkRefPtr mc = OSG::MaterialChunk::create();
            mc->setLit(false);
            mc->setEmission(cf);
            mc->setDiffuse(cf);

            OSG::ChunkMaterialRefPtr cm = OSG::ChunkMaterial::create();
            cm->addChunk(pc);
            cm->addChunk(mc);

            OSG::Material             *mat = geo->getMaterial();
            OSG::SwitchMaterialRefPtr  sw  = OSG::SwitchMaterial::create();

            sw->addMaterial(mat);        // Choice 0
            sw->addMaterial(cm);        // Choice 1

            geo->setMaterial(sw);

            _switches.push_back(sw);
        }
        
        // check all children
        for (OSG::UInt32 i = 0; i < children; i++)
        {
            convertToColorIdentificationSwitchable(root->getChild(i));
        }
    }
void prepareSceneGraph(OSG::Node * const node)
{
    if(!prepared)
    {
        polygonChunk = OSG::PolygonChunk::create();
        prepared = true;
    }

    OSG::NodeCore *core  =node->getCore();
    if(core != NULL)
    {
        OSG::Geometry *geo   = dynamic_cast<OSG::Geometry *>(core);
        if(geo != NULL)
        {
            OSG::Material *mat = geo->getMaterial();
            if(mat != NULL)
            {
                OSG::ChunkMaterial *cmat = 
                    dynamic_cast<OSG::ChunkMaterial *>(mat);
                if(cmat->find(OSG::PolygonChunk::getClassType()) == NULL)
                {
                    cmat->addChunk(polygonChunk);
                }
            }
            // get num positions
            OSG::GeoVectorProperty *positionsPtr=geo->getPositions();
            if(positionsPtr != NULL)
                sum_positions += positionsPtr->size32();
            // get num triangles
            OSG::UInt32 triangle=0;
            OSG::UInt32 line=0;
            OSG::UInt32 point=0;
            calcPrimitiveCount(geo,triangle,line,point);
            sum_triangles += triangle;
            // sum of geometry nodes
            ++sum_geometries;
        }
        else
        {
            OSG::MaterialGroup *matGrp = 
                dynamic_cast<OSG::MaterialGroup *>(core);
            if(matGrp != NULL)
            {
                OSG::Material *mat = matGrp->getMaterial();
                if(mat != NULL)
                {
                    OSG::ChunkMaterial *cmat = 
                        dynamic_cast<OSG::ChunkMaterial *>(mat);
                    if(cmat->find(OSG::PolygonChunk::getClassType()) == NULL)
                    {
                        cmat->addChunk(polygonChunk);
                    }
                }
            }
            else
            {
                OSG::ProxyGroup *proxy = dynamic_cast<OSG::ProxyGroup *>(core);
                if(proxy != NULL)
                {
                    sum_triangles += proxy->getTriangles();
                    sum_positions += proxy->getPositions();
                    sum_geometries += proxy->getGeometries();
                }
            }
        }
    }
    for(OSG::MFUnrecChildNodePtr::const_iterator nI=node->getMFChildren()->begin();
        nI != node->getMFChildren()->end();
        ++nI)
    {
        prepareSceneGraph(*nI);
    }
}