• osg抓图


    osg抓图、截图、保存图片

    #include <osgViewer/Viewer>
    #include <osgViewer/ViewerBase>
    #include <osgViewer/GraphicsWindow>
    #include <osg/Node>
    #include <osg/Geode>
    #include <osg/Group>
    #include <osg/Camera>
    #include <osg/Image>
    #include <osg/BufferObject>
    #include <osgDB/ReadFile>
    #include <osgDB/WriteFile>
    #include <osgUtil/Optimizer>
    #include <iostream>
    #include <osgGA/GUIEventHandler>
    #include <osgGA/TrackballManipulator>
    #include <osg/RenderInfo>
    #include <OpenThreads/Mutex>
    #include <OpenThreads/Thread>
    #include <OpenThreads/ScopedLock>
    
    //osg::ref_ptr<osg::Image> image_c = new osg::Image();
    
    struct CaptureCallback :public osg::Camera::DrawCallback
    {
        CaptureCallback()
        {
    
            std::cout << "=----0---=" << std::endl;
        }
    
        CaptureCallback(osg::ref_ptr<osg::Image> image)
        {
            image_ = image;
    
            std::cout << "=-------=" << std::endl;
        }
    
        ~CaptureCallback() {}
    
        virtual void operator()(const osg::Camera &camera) const
        {
            //得到窗口系统接口
            //osg::ref_ptr<osg::GraphicsContext::WindowingSystemInterface> wsi = osg::GraphicsContext::getWindowingSystemInterface();
            unsigned int width = 800, height = 500;
            //得到分辨率
            //wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
    
            //分配一个image
            image_->allocateImage(width, height, 1, GL_RGB, GL_UNSIGNED_BYTE);
            //读取像素信息抓图
            image_->readPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE);
        }
    
    
        //virtual void operator()(osg::RenderInfo& renderInfo) const
        //{
        //    //读取像素信息抓图
        //    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
        //    auto& camera = *renderInfo.getCurrentCamera();
        //    auto traits = camera.getGraphicsContext()->getTraits();
        //    glReadBuffer(traits->doubleBuffer ? GL_BACK : GL_FRONT);
        //    _image->readPixels(pos[0], pos[1], size[0], size[1], _image->getPixelFormat(), _image->getDataType());
        //}
        //osg::Vec2i pos;
        //osg::Vec2i size;
        //osg::observer_ptr<osg::Image> _image;
        //mutable OpenThreads::Mutex  _mutex;
    
        osg::ref_ptr<osg::Image> getOsgImage()
        {
            return image_;
        }
    
    private:
        osg::ref_ptr<osg::Image> image_ = new osg::Image();
    
    };
    
    //抓图事件处理器
    class ImageHandler :public osgGA::GUIEventHandler
    {
    public:
        ImageHandler()
        {
            _screenCaptureSequence = 0;
        }
        ~ImageHandler()
        {
    
        }
    
        bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
        {
            osg::ref_ptr<osgViewer::Viewer> viewer = dynamic_cast<osgViewer::Viewer *>(&aa);
    
            //std::cout << "======1======" << std::endl;
            if (viewer == NULL)
            {
                return false;
            }
    
            std::cout << "======2======" << ea.getEventType() << std::endl;
    
            if (ea.getEventType() == 16)
            {
                char filename[128];
                sprintf(filename, "ScreenShot%4d.jpg", _screenCaptureSequence);
                std::cout << filename << std::endl;
                _screenCaptureSequence++;
                osgDB::writeImageFile(*(captureCallbackObj->getOsgImage()), filename);
            }
    
            
            return false;
        }
    
        void setCaptureCallback(CaptureCallback* captureCallbackParam)
        {
            this->captureCallbackObj = captureCallbackParam;
        }
    
    private:
        //定义一个静态变量
        int _screenCaptureSequence;
        CaptureCallback* captureCallbackObj = nullptr;
    };
    
    int main()
    {
        osg::ref_ptr<osg::Node> node = osgDB::readNodeFile("L:\BIM\robot.osg");
        osg::ref_ptr<osg::Group> root = new osg::Group();
        root->addChild(node.get());
        osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
    
        osgUtil::Optimizer optimizer;
        optimizer.optimize(root.get());
    
    
    
    
        //osg::Vec2i pos(400,250);
        //osg::Vec2i size(800,500);
        //osg::observer_ptr<osg::Image> _image = new osg::Image();
        ////mutable OpenThreads::Mutex  _mutex;
    
        //osg::ref_ptr<CaptureCallback> cb = new CaptureCallback();
        //cb->pos = pos;
        //cb->size = size;
        //cb->_image = _image;
        //viewer->getCamera()->setFinalDrawCallback(cb);
        //auto viewer2= dynamic_cast<osgViewer::ViewerBase*>(viewer->getCamera()->getView());
        //auto oldModel = viewer2->getThreadingModel();
        //viewer2->setThreadingModel(osgViewer::ViewerBase::SingleThreaded);
        //viewer2->renderingTraversals();
        //viewer2->setThreadingModel(oldModel);
        //viewer->getCamera()->setFinalDrawCallback(nullptr);
    
        CaptureCallback* captureCallbackObj = new CaptureCallback();
        ImageHandler* imageHandlerObj = new ImageHandler();
        imageHandlerObj->setCaptureCallback(captureCallbackObj);
    
        viewer->getCamera()->setPostDrawCallback(captureCallbackObj);
        viewer->addEventHandler(imageHandlerObj);
    
    
        viewer->setSceneData(root.get());
        viewer->setThreadingModel(osgViewer::ViewerBase::SingleThreaded);
    
        viewer->setUpViewInWindow(200, 200, 800, 500, 0);
    
    
        viewer->realize();
        viewer->run();
        return 1;
    }

     

     

     

     

     

     

     

     

    ##############################################################

    QQ 3087438119
  • 相关阅读:
    使用ML.NET预测纽约出租车费
    .NET Core玩转机器学习
    使用SonarCloud对.NET Core项目进行静态代码分析
    用分布式缓存提升ASP.NET Core性能
    确保线程安全下使用Queue的Enqueue和Dequeue
    这个拖后腿的“in”
    站在巨人肩上的.NET Core 2.1
    4,xpath获取数据
    3-Requests网络请求
    2,urllib库-网络请求
  • 原文地址:https://www.cnblogs.com/herd/p/15334732.html
Copyright © 2020-2023  润新知