osg::Geomtery多图元重叠闪烁问题

来源:互联网 发布:淘宝卖家改地址 编辑:程序博客网 时间:2024/06/05 01:59

void DrawGeom()

{

if(!_geom.valid())

    {
        _geom = new osg::Geometry;
        _geom->setDataVariance(osg::Object::DYNAMIC);\
        _geom->getOrCreateStateSet()->setMode(GL_BLEND,osg::StateAttribute::ON);
        _geom->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
        osg::ref_ptr<osg::CullFace> cullface = new osg::CullFace(osg::CullFace::BACK);
             _geom->getOrCreateStateSet()->setAttribute(cullface.get());
     
                osg::PolygonOffset* polyoffset = new osg::PolygonOffset;
                polyoffset->setFactor(-100.0f);
                polyoffset->setUnits(-10.0f);
                osg::PolygonMode* polymode = new osg::PolygonMode;
                polymode->setMode(osg::PolygonMode::FRONT,osg::PolygonMode::FILL);
                _geom->getOrCreateStateSet()->setAttributeAndModes(polyoffset,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
                _geom->getOrCreateStateSet()->setAttributeAndModes(polymode,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
        //定义颜色数组
        osg::ref_ptr<osg::Vec4Array> c = new osg::Vec4Array;
        _geom->setColorArray(c.get());
        _geom->setColorBinding(osg::Geometry::BIND_OVERALL);
        c->push_back(_LineColor);
        //定义法线
        osg::ref_ptr<osg::Vec3Array> n = new osg::Vec3Array;
        _geom->setNormalArray(n.get());
        _geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
        n->push_back(osg::Vec3(0.f, -1.f, 0.f));
        _pointArray = new osg::Vec3Array;
        _geom->setVertexArray(_pointArray.get());
        osg::ref_ptr<osg::Geode> geode = new osg::Geode;
        geode->addDrawable(_geom.get());
        //geode->setUpdateCallback(new FadeCallback(_LineColor));
        geode->getOrCreateStateSet()->setAttributeAndModes(new osg::BlendFunc);
        geode->getOrCreateStateSet()->setMode(GL_BLEND,osg::StateAttribute::ON);
        geode->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
        _shapeGroup->addChild(geode.get());
        primitiveSetCount = 0L;
    }
    _pointArray->push_back(wp1*mat);
    _pointArray->push_back(wp2*mat);
    _pointArray->push_back(wp3*mat);
    _pointArray->push_back(wp4*mat);
    _geom->addPrimitiveSet(
                new osg::DrawArrays(osg::PrimitiveSet::QUADS, primitiveSetCount*4, 4));
    primitiveSetCount++;

}

这样还是会有重叠的效果  试过很多方法 还是不行  没有好的opengl基础 真是空中楼阁  碰到点难得就不会 完全没有思路 跟着网上的东西走 心累 初学osg 记录自己的心路历程