简体   繁体   English

如何在 Openscenegraph 几何节点中禁用状态属性?

[英]How to disable State attribute in Openscenegraph geometry node?

I tried adding a fragment shader program to OSG::Geometry node as below.我尝试将片段着色器程序添加到OSG::Geometry节点,如下所示。

    osg::ref_ptr<osg::Geometry> node = new osg::Geometry();
    osg::ref_ptr<osg::Program> m_program= new osg::Program;
    osg::ref_ptr<osg::Shader> fragShader = new osg::Shader(osg::Shader::FRAGMENT);
    //TODO Add LOG if shader failed to load
    if (!fragShader->loadShaderSourceFromFile("Shaders/Sel.frag"))
        return;
    m_program->addShader(fragShader);
    osg::StateSet* state = node->getOrCreateStateSet();
    state->setAttributeAndModes(m_program, osg::StateAttribute::ON);

At some point, I removed the program using the below method在某些时候,我使用以下方法删除了该程序

state->removeAttribute(m_program);

After removing the attribute, the next immediate render frame loop frame() throws an exception as below.删除该属性后,下一个立即渲染帧循环frame()会引发如下异常。

在此处输入图片说明

I tried to debug the openscenegraph and found the map which is causing the issue.我尝试调试 openscenegraph 并找到导致问题的地图。

Header file: State头文件:状态

Method name :方法名称:

inline void State::applyAttributeList(AttributeMap& attributeMap,const StateSet::AttributeList& attributeList)

The variable which caused the exception.导致异常的变量。

attributeMap

if I am not removing the program state attribute, it is working fine.如果我没有删除程序状态属性,它工作正常。 Only removing the attribute, causing the issue.仅删除属性,导致问题。

This is most likely due to the fact that you're updating the stateset while the rendering thread(s) is still using it to finish the dispatch of the previous frame.这很可能是因为您正在更新状态集,而渲染线程仍在使用它来完成前一帧的调度。 To make sure this is the cause of the crash, you can try to run the application with OSG SingleThreaded scheme (either set it via code on the viewer or set the OSG_THREADING env var).为了确保这是崩溃的原因,您可以尝试使用 OSG SingleThreaded方案运行应用程序(通过查看器上的代码设置它或设置 OSG_THREADING 环境变量)。
If that is the cause and you want to use the one of the other threading schemes, you can set the stateset's data variance to DYNAMIC - this will ensure that the rendering thread is done with your stateset before the next update callback is called for the new frame.如果这是原因并且您想使用其他线程方案之一,则可以将 stateset 的数据差异设置为DYNAMIC - 这将确保在为新调用下一次更新回调之前,渲染线程已使用您的 stateset 完成框架。
This topic has been discussed many times in the osg-users mailing list, you may check the archives for further info.这个话题已经在 osg-users 邮件列表中讨论了很多次,你可以查看档案以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM