2012-06-08 67 views
1

我正在处理一个项目,在该项目中我需要从外部设备视频捕获帧并将它们呈现在openSceneGraph节点上。我也在使用GLSL着色器。但我不知道如何在运行时更新纹理。对于其他制服,我们需要进行回调,但是我们是否也需要为glsl和openSceneGraph中的采样器进行回调?在OpenSceneGraph中运行时更新纹理

我的代码看起来像这样。我现在所得到的只是一个黑色的窗口。

osg::ref_ptr<osg::Geometry> pictureQuad = osg::createTexturedQuadGeometry(osg::Vec3(0.0f,0.0f,0.0f), 
    osg::Vec3(_deviceNameToImageFrameMap[deviceName].frame->s(),0.0f,0.0f), osg::Vec3(0.0f,0.0f,_deviceNameToImageFrameMap[deviceName].frame->t()), 
    0.0f, 1.0f,_deviceNameToImageFrameMap[deviceName].frame->s(), _deviceNameToImageFrameMap[deviceName].frame->t()); 

    //creating texture and setting up parameters for video frame 
    osg::ref_ptr<osg::TextureRectangle> myTex= new osg::TextureRectangle(_deviceNameToImageFrameMap[deviceName].frame.get()); 
    myTex->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); 
    myTex->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR); 
    myTex->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE); 
    myTex->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE); 



    _videoSourceNameToNodeMap[sourceName].geode = new osg::Geode(); 
    _videoSourceNameToNodeMap[sourceName].geode->setDataVariance(osg::Object::DYNAMIC); 
    _videoSourceNameToNodeMap[sourceName].geode->addDrawable(pictureQuad.get()); 

    //apply texture to node 
    _videoSourceNameToNodeMap[sourceName].geode->getOrCreateStateSet()->setTextureAttributeAndModes(0, myTex.get(), osg::StateAttribute::ON); 
    _videoSourceNameToNodeMap[sourceName].geode->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF); 
    _videoSourceNameToNodeMap[sourceName].geode->setDataVariance(osg::Object::DYNAMIC); 

    //Set uniform sampler 
    osg::Uniform* srcFrame = new osg::Uniform(osg::Uniform::SAMPLER_2D, "srcFrame"); 
    srcFrame->set(0); 
    //Set Uniform Alpha 
    osg::Uniform* alpha = new osg::Uniform(osg::Uniform::FLOAT, "alpha"); 
    alpha->set(.5f); 
    alpha->setUpdateCallback(new ExampleCallback()); 

    //Enable blending 
    _videoSourceNameToNodeMap[sourceName].geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON); 
    //Adding blend function to node 
    osg::BlendFunc *bf = new osg::BlendFunc(); 
    bf->setFunction(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
    _videoSourceNameToNodeMap[sourceName].geode->getOrCreateStateSet()->setAttributeAndModes(bf); 

    //apply shader to quad 
    _videoSourceNameToNodeMap[sourceName].geode->getOrCreateStateSet()->setAttributeAndModes(program, osg::StateAttribute::ON); 
    //add Uniform to shader 
    _videoSourceNameToNodeMap[sourceName].geode->getOrCreateStateSet()->addUniform(srcFrame); 
    _videoSourceNameToNodeMap[sourceName].geode->getOrCreateStateSet()->addUniform(alpha); 

回答

1

只要打电话image->dirty()并使用该图像所有纹理就会自动

0

更新你并不需要这样做bf->setFunction(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);,因为这是默认的功能。