首页 文章

在OpenSceneGraph中运行时更新纹理

提问于
浏览
2

我正在开发一个项目,我需要从外部设备视频捕获帧并在openSceneGraph节点上渲染它们 . 我也在使用GLSL着色器 . 但我不知道如何在运行时更新纹理 . 对于其他制服,我们需要进行回调,但是我们还需要在glsl和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 );

2 回答

  • 0

    只需调用 image->dirty() ,使用该图像的所有纹理都会自动更新

  • 1

    您不需要这样做 bf->setFunction(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); ,因为这是默认功能 .

相关问题