bool GraphicSceneClass::RChangeTexture(std::string ObjectName, std::string
filename,unsigned int UnitNumber)
{
    //Находим объект
    osg::ref_ptr<FindNamedNodeVisitor> fnnv = new
FindNamedNodeVisitor(ObjectName);
    root->accept(*fnnv.get());
    //Если такой объект есть, ищем текстуру
    if (!fnnv->_foundNodes.empty())
    {
        if (fnnv->_foundNodes.size()>1) std::cout << "Warning: Duplicate
names - " << ObjectName << std::endl;
        osg::ref_ptr<FindTextureVisitor> zzz = new
FindTextureVisitor(UnitNumber);
        fnnv->_foundNodes.front().get()->accept (*zzz.get());
        //Если текстура есть
        if (zzz->texture.get())
        {
            osg::ref_ptr<osg::Image> image = osgDB::readImageFile(filename);

            //Отправляем текстуру в видеопамять)
            if (image.get())
            {
                zzz->texture->setImage(image.get());
                // dirty the image (increments the modified count) so that
any textures
                // using the image can be informed that they need to update.
                image->dirty();
            }
            else
            {
                std::cout << "NOT find image file for texture";
                //delete zzz;
                //delete fnnv;
                return false;
            }
        }
        else
        {
            std::cout << "texture NOT find";
        }
        //Чистим мусор
        //delete zzz;
    }
    else
    {
        std::cout << "Find objects in Root - FALSE! " << ObjectName <<
std::endl;
        //delete fnnv;
        return false;
    }
    //Чистим мусор
    //delete fnnv;
    return true;
}

class FindTextureVisitor : public osg::NodeVisitor
{
public:
    FindTextureVisitor(unsigned int channel=0):
        osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
        {
            Channel = channel;
            setTraversalMask(0xffffffff);
            setNodeMaskOverride(0xffffffff);
            Sucessful=false;
        }

    virtual void apply(osg::Node& node);

    virtual void apply(osg::MatrixTransform& node);

    virtual void apply(osg::Geode& geode);

    virtual void apply(osg::Group& group);

    unsigned int Channel;
    osg::ref_ptr<osg::Texture2D> texture;
    osg::ref_ptr<osg::Image> image;
    osg::ref_ptr<osg::Material> material;
    osg::ref_ptr<osg::StateSet> stateset;
    osg::ref_ptr<osg::Drawable> drawable;
private:
    bool Sucessful;

};
void FindTextureVisitor::apply(osg::Node& node)
{
    traverse(node);
}

void FindTextureVisitor::apply(osg::Geode& geode)
{
    for(unsigned int i=0;i<geode.getNumDrawables();i++) //++i
    {
        if (Sucessful==true) return;
        //std::cout << "+";
        if (geode.getDrawable(i)->getStateSet())
        {
            drawable = geode.getDrawable(i);
            //текстура
            stateset = geode.getDrawable(i)->getStateSet();
//getOrCreateStateSet();
            texture =
dynamic_cast<osg::Texture2D*>(geode.getDrawable(i)->getStateSet()->getTextureAttribute(Channel,osg::StateAttribute::TEXTURE));
            if (texture.get())
            {
                image = texture->getImage();
                Sucessful=true;
            }
            else
            {
                image=NULL;
            }
            //размер  и т.д.
            //цвет
            //material->setDiffuse(osg::Material::FRONT_AND_BACK, color);
            //stateset->setAttributeAndModes(material,
osg::StateAttribute::ON);

            material =
dynamic_cast<osg::Material*>(geode.getDrawable(i)->getStateSet()->getAttribute(osg::StateAttribute::MATERIAL));
            if (material.get())
            {
                //std::cout << "@";
            }
        }
    }
}

void FindTextureVisitor::apply(osg::Group& group)
{
    traverse(group);
}

void FindTextureVisitor::apply(osg::MatrixTransform& node)
{
      apply((osg::Node&)node);
}



2009/2/23 yang zhiyuan <[email protected]>

> Hi:
>    If I have a node with four texure in it, what should I do if I want to
> change one texture of these 4 dynamically?(the new texure's size my be
> different)
>
>
> zhi yuan
>
> _______________________________________________
> osg-users mailing list
> [email protected]
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
>


-- 
Гаммер Максим Дмитриевич
Начальник отдела ИТ НИИ ЭОР
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to