查看: 1899|回复: 3

composite view 的问题

[复制链接]

该用户从未签到

发表于 2009-10-7 17:54:33 | 显示全部楼层 |阅读模式
在我电脑上运行一个多视图代码,每次运行到 compositeview.addview(view)时候就出错了,说访问地址出错。

/* OpenSceneGraph example, osgwindows.
*
*  Permission is hereby granted, free of charge, to any person obtaining a copy
*  of this software and associated documentation files (the "Software"), to deal
*  in the Software without restriction, including without limitation the rights
*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*  copies of the Software, and to permit persons to whom the Software is
*  furnished to do so, subject to the following conditions:
*
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
*  THE SOFTWARE.
*/
#include <osgDB/ReadFile>
#include <osgUtil/Optimizer>
#include <osgViewer/Viewer>
#include <osgViewer/CompositeViewer>

#include <iostream>
int main( int argc, char **argv )
{
    // use an ArgumentParser object to manage the program arguments.
    osg::ArgumentParser arguments(&argc,argv);
    // read the scene from the list of file specified commandline args.
    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
    // if not loaded assume no arguments passed in, try use default mode instead.
    if (!loadedModel) loadedModel = osgDB::readNodeFile("cow.osg");
   
    // if no model has been successfully loaded report failure.
    if (!loadedModel)
    {
        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
        return 1;
    }
    // construct the viewer.
    //osgViewer::Viewer viewer;
        osg::ref_ptr<osgViewer::CompositeViewer> viewer = new osgViewer::CompositeViewer();
        osg::ref_ptr<osgViewer::Viewer> viewer1= new osgViewer::Viewer() ;
        osg::ref_ptr<osgViewer::Viewer> viewer2= new osgViewer::Viewer() ;
    int xoffset = 40;
    int yoffset = 40;
    // left window + left slave camera
    {
        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
        traits->x = xoffset + 0;
        traits->y = yoffset + 0;
        traits->width = 600;
        traits->height = 480;
        traits->windowDecoration = true;
        traits->doubleBuffer = true;
        traits->sharedContext = 0;
        osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
        camera->setGraphicsContext(gc.get());
        camera->setViewport(new osg::Viewport(0,0, traits->width, traits->height));
        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
        camera->setDrawBuffer(buffer);
        camera->setReadBuffer(buffer);
        // add this slave camera to the viewer, with a shift left of the projection matrix
        viewer1->addSlave(camera.get(), osg::Matrixd::translate(0.0,0.0,0.0), osg::Matrixd());
                viewer1->setSceneData(osgDB::readNodeFile("cow.osg")) ;
    }
   
    // right window + right slave camera
    {
        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
        traits->x = xoffset + 600;
        traits->y = yoffset + 0;
        traits->width = 600;
        traits->height = 480;
        traits->windowDecoration = true;
        traits->doubleBuffer = true;
        traits->sharedContext = 0;
        osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
        camera->setGraphicsContext(gc.get());
        camera->setViewport(new osg::Viewport(0,0, traits->width, traits->height));
        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
        camera->setDrawBuffer(buffer);
        camera->setReadBuffer(buffer);
        // add this slave camera to the viewer, with a shift right of the projection matrix
       viewer2->addSlave(camera.get(), osg::Matrixd::translate(0.0,0.0,0.0), osg::Matrixd());
           viewer2->setSceneData(osgDB::readNodeFile("cow.osg")) ;
    }

        viewer->addView(viewer1.get()) ;
        viewer->addView(viewer2.get()) ;

    // optimize the scene graph, remove rendundent nodes and state etc.
    osgUtil::Optimizer optimizer;
    optimizer.optimize(loadedModel.get());
    // set the scene to render
   // viewer.setSceneData(loadedModel.get());
    return viewer->run();

}




问题出在上面红色两行,请问有人知道为什么吗?

该用户从未签到

发表于 2009-10-7 22:28:24 | 显示全部楼层
因为访问域的原因,您的viewer1和viewer2都处在{}所限制的作用域中,超出之后它们就被ref_ptr自动卸载了。只需要简单地去掉那两个无用的{}作用域就可以了

该用户从未签到

 楼主| 发表于 2009-10-8 11:14:01 | 显示全部楼层
2# array

可是VIEWER1 和 VIEWER2定义都在括号外,应该不受作用域的限制

而且我写一个小程序也会出错,比如

#....头文件

int main()
{
    osgViewer::CompositeView * composite = new....
    osgViewer::Viewer * view1 = new ..  

   composite.addview(view1);
}

也是在addview那里崩溃的///
难道大家都没遇到这样的问题吗? 上面的代码是FLYSKY在之前一个帖子上贴出来的/


}

该用户从未签到

发表于 2009-10-8 13:58:54 | 显示全部楼层
哦,那我没注意看。CompositeViewer我用得很多,没有这样的问题。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表