static Action::ResultE modifyMaterial(Node * const node)
{   
    MaterialGroup *mg = dynamic_cast<MaterialGroup *>(node->getCore());
    
    if(mg == NULL)
        return Action::Continue; 
    
    ChunkMaterial *cmat = dynamic_cast<ChunkMaterial *>(mg->getMaterial());
    
    if(cmat == NULL)
        return Action::Continue; 
    
    TextureObjChunk *texc = 
        dynamic_cast<TextureObjChunk *>(
            cmat->find(TextureObjChunk::getClassType()));
    
    if(texc == NULL)
        return Action::Continue;
    
    MaterialChunk *matc = 
        dynamic_cast<MaterialChunk *>(
            cmat->find(MaterialChunk::getClassType()));

    TextureEnvChunkUnrecPtr texe = 
        dynamic_cast<TextureEnvChunk *>(
            cmat->find(TextureEnvChunk::getClassType()));
    
    if(texe == NULL)
    {
        texe = TextureEnvChunk::create();
        cmat->addChunk(texe);
    }

    if(matc == NULL)
    {
        // no material chunk so we use the replace mode.
        texe->setEnvMode(GL_REPLACE);
        return Action::Continue;
    }
    
    if(matc != NULL)
    {
        Image *img = texc->getImage();

        texe->setEnvMode(GL_MODULATE);

        if(img != NULL && img->getBpp() > 2)
        {
            // for color textures the texture replaces only the diffuse part.
            matc->setDiffuse(Color4f(1.0f, 1.0f, 1.0f, 1.0f));
        }
        
        
        // check for textures with alpha
        if(!matc->isTransparent()         && 
            img                   != NULL &&
            img->getBpp()         ==    4   )
        {
            BlendChunkUnrecPtr blendc = 
                dynamic_cast<BlendChunk *>(
                    cmat->find(BlendChunk::getClassType()));

            if(blendc == NULL)
            {
                blendc = OSG::BlendChunk::create();

                blendc->setSrcFactor (GL_SRC_ALPHA);
                blendc->setDestFactor(GL_ONE_MINUS_SRC_ALPHA);
    
                cmat->addChunk(blendc);
            }
        }
    }

    return Action::Continue;
}
示例#2
0
void MergeGraphOp::processGroups(Node * const node)
{
    MFUnrecChildNodePtr::const_iterator mfit = node->getMFChildren()->begin();
    MFUnrecChildNodePtr::const_iterator mfen = node->getMFChildren()->end  ();
    std::vector<Node *> toAdd;
    std::vector<Node *> toSub;
    
    for ( ; mfit != mfen; ++mfit )
    {
        bool special=isInExcludeList(*mfit);
        bool leaf=isLeaf(*mfit);
        
        if (isGroup(*mfit))
        {
            if (!leaf && !special)
            {
                MFUnrecChildNodePtr::const_iterator it2 = 
                    (*mfit)->getMFChildren()->begin();
                MFUnrecChildNodePtr::const_iterator en2 = 
                    (*mfit)->getMFChildren()->end  ();
                
                for ( ; it2 != en2; ++it2 )
                {
                    toAdd.push_back(*it2);
                }                
            }
            
            if (!special)
            {
                toSub.push_back(*mfit);
                continue;
            }
            
            if (leaf && special)
            {
                //what to do?
            }
            if (!leaf && special)
            {
                //what to do?
            }
            continue;
        }
        else if ((*mfit)->getCore()->getType().isDerivedFrom( 
                     MaterialGroup::getClassType() ))
        {
            MaterialGroup *mg = 
                dynamic_cast<MaterialGroup *>((*mfit)->getCore());
            
            MFUnrecChildNodePtr::const_iterator it2 = 
                (*mfit)->getMFChildren()->begin();
            MFUnrecChildNodePtr::const_iterator en2 = 
                (*mfit)->getMFChildren()->end  ();
            
            bool empty=true;
            
            for ( ; it2 != en2; ++it2 )
            {
                if (!isInExcludeList(*it2))
                {
                    //check if geometry
                    if ((*it2)->getCore()->getType().isDerivedFrom(
                            Geometry::getClassType()))
                    {
                        if(!isLeaf(*it2))
                        {
                            //hmm...bad tree...
                            empty=false;
                        }
                        else
                        {                                
                            //it is a leaf geometry, so apply the transformation
                            Geometry *geo = 
                                dynamic_cast<Geometry *>((*it2)->getCore());

                            geo->setMaterial(mg->getMaterial());

                            toAdd.push_back(*it2);                            
                        }
                    } 
                    else 
                    {
                        empty=false;
                    }
                } 
                else 
                {
                    empty=false;                
                }
            }
            
            if (empty) 
                toSub.push_back(*mfit);
        }
    }
    
    std::vector<Node *>::const_iterator vit = toAdd.begin();
    std::vector<Node *>::const_iterator ven = toAdd.end  ();
    
    for ( ; vit != ven; ++vit )
    {
        node->addChild(*vit);
    }
    
    vit = toSub.begin();
    ven = toSub.end  ();
    
    for ( ; vit != ven; ++vit )
    {
        node->subChild(*vit);
    }
}
void SortLastWindow::collectDrawables(Node * const   node,
                                      DrawableListT &drawables)
{
    Material *mat  = NULL;
    NodeCore *core = node->getCore();

    if(core != NULL)
    {
        // handle material groups
        MaterialGroup *matGrp = dynamic_cast<MaterialGroup *>(core);

        if(matGrp != NULL)
        {
            mat = matGrp->getMaterial();

            // ignore transparent material groups
            if(mat != NULL && mat->isTransparent())
                return;
        }

        // handle geometries
        Geometry *geo = dynamic_cast<Geometry *>(core);

        if(geo != NULL)
        {
            mat = geo->getMaterial();
            // ignore transparent materials

            if(mat == NULL || mat->isTransparent() == false)
            {
                DrawableInfo drawableInfo;

                drawableInfo.node = node;

                // get transformed volume
                node->updateVolume();

                BoxVolume volume;
                node->getWorldVolume(volume);

                // get min,max
                volume.getBounds(drawableInfo.bMin, drawableInfo.bMax);

                // num of indices
                drawableInfo.load = 0;

                GeoIntegralProperty *indicesPtr = 
                    geo->getIndex(Geometry::PositionsIndex);

                if(indicesPtr != NULL)
                    drawableInfo.load = indicesPtr->size();

                // put to list
                drawables.push_back(drawableInfo);
            }
        }

        // handle poxy groups
        ProxyGroup *proxy = dynamic_cast<ProxyGroup *>(core);

        if(proxy != NULL)
        {
            DrawableInfo drawableInfo;

            drawableInfo.node = node;

            // get transformed volume
            node->updateVolume();

            BoxVolume volume;
            node->getWorldVolume(volume);

            // get min,max
            volume.getBounds(drawableInfo.bMin, drawableInfo.bMax);

            // num of indices
            drawableInfo.load = proxy->getIndices();

            // put to list
            drawables.push_back(drawableInfo);
        }
    }
    
    MFUnrecChildNodePtr::const_iterator nI;

    for(  nI  = node->getMFChildren()->begin();
          nI != node->getMFChildren()->end();
        ++nI)
    {
        collectDrawables(*nI, drawables);
    }
}