|
我需要动态的改变某个节点的材质,(就是一个碰撞检测,当检测到碰撞时,改变节点的材质)
需要把这些都放在preframeupdate()中进行才不会有错吗?
还有动态改变时,每一次更新都需要重新new一个material,
osg::Material *mat=new osg::Material();
//是否相交
if(m_isintersects)
{
//红色材质
mat->setDiffuse(osg::Material::Face::FRONT,osg::Vec4(0.8,0.1,0.1,1));
mat->setAmbient(osg::Material::Face::FRONT,osg::Vec4(0.8,0.1,0.1,1));
mat->setSpecular(osg::Material::Face::FRONT,osg::Vec4(0.8,0.8,0.8,1));
mat->setShininess(osg::Material::Face::FRONT,16);
if(m_plane)
m_plane->getOrCreateStateSet()->setAttribute(mat,osg::StateAttribute::OVERRIDE);
}
else
{
//绿色材质
mat->setDiffuse(osg::Material::Face::FRONT,osg::Vec4(0.1,0.8,0.1,1));
mat->setAmbient(osg::Material::Face::FRONT,osg::Vec4(0.1,0.8,0.1,1));
mat->setSpecular(osg::Material::Face::FRONT,osg::Vec4(0.8,0.8,0.8,1));
mat->setShininess(osg::Material::Face::FRONT,16);
if(m_plane)
m_plane->getOrCreateStateSet()->setAttribute(mat,osg::StateAttribute::OVERRIDE);
}
如果定义两个redmat,greenmat,初始化的时候就定义好了,按照上述赋值时,会报错
//是否相交
if(m_isintersects)
{
//红色材质
if(m_plane)
m_plane->getOrCreateStateSet()->setAttribute(redmat,osg::StateAttribute::OVERRIDE);
}
else
{
//绿色材质
if(m_plane)
m_plane->getOrCreateStateSet()->setAttribute(greenmat,osg::StateAttribute::OVERRIDE);
} |
|