OSG能够在当前帧截图,也就是能转换视角后马上截图

Posted 唐淼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OSG能够在当前帧截图,也就是能转换视角后马上截图相关的知识,希望对你有一定的参考价值。

#include <Windows.h>
#include <osg/GraphicsContext>
#include <osg/Group>
#include<osg/Node>
#include<osg/Geode>
#include <osgViewer/Viewer>
#include<osgDB/ReadFile>
#include<osgUtil/Optimizer>
#include<osgDB/WriteFile>
#include <osgGA/TrackballManipulator>
osg::ref_ptr<osg::Image> captureImage = new osg::Image;
osg::ref_ptr<osgGA::TrackballManipulator> track = new osgGA::TrackballManipulator;
osg::Vec3d lasteye, lastcenter, lastup;
struct CaptureCallback :public osg::Camera::DrawCallback
{
    CaptureCallback()
    {
    }
    ~CaptureCallback() {}

    virtual void operator()(const osg::Camera &camera) const
    {
        //得到窗口系统接口
        osg::ref_ptr<osg::GraphicsContext::WindowingSystemInterface> wsi = osg::GraphicsContext::getWindowingSystemInterface();
        unsigned int width, height;
        //得到分辨率
        wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);

        //读取像素信息抓图
        captureImage->readPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE);
    }

private:

};


int main()
{
    osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
    osg::ref_ptr<osg::Group> root = new osg::Group;
    osg::ref_ptr<osg::Node>node = osgDB::readNodeFile("E://迅雷下载//OpenSceneGraph-Data-3.0.0//ceep.ive");
    root->addChild(node);
    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
    traits->x = 0;
    traits->y = 0;
    traits->width = 1920;
    traits->height = 1080;
    //支持窗口扩展,默认是不支持的
    traits->windowDecoration = true;
    //支持双缓存,默认不支持
    traits->doubleBuffer = true;
    traits->sharedContext = 0;
    traits->red = 8;
    traits->blue = 8;
    traits->green = 8;
    //支持alpha,默认不支持为0,这里改为支持,使截出来的图片具有alpha值
    traits->alpha = 8;

    osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
    if (gc.valid())
    {
        std::cout << "create gc success" << std::endl;
    }
    double fovy, aspectRatio, zNear, zFar;
    viewer->getCamera()->getProjectionMatrixAsPerspective(fovy, aspectRatio, zNear, zFar);
    double newAspectRatio = double(traits->width) / double(traits->height);
    double aspectRatioChange = newAspectRatio / aspectRatio;
    if (aspectRatioChange != 1.0)
    {
        viewer->getCamera()->getProjectionMatrix() *= osg::Matrix::scale(1.0 / aspectRatioChange, 1.0, 1.0);
    }
    viewer->getCamera()->setViewport(0, 0, 1920, 1080);

    viewer->setCameraManipulator(track);
    viewer->getCamera()->setGraphicsContext(gc.get());
    osgUtil::Optimizer optimizer;
    optimizer.optimize(root.get());

    viewer->setSceneData(root.get());
    viewer->realize();
    viewer->getCamera()->setPostDrawCallback(new CaptureCallback());
    static int cnt = 0;
    while (!viewer->done())
    {
        if (cnt==1)
        {
            track->getTransformation(lasteye, lastcenter, lastup);
            track->setTransformation(lastcenter + osg::Vec3(0, 0, 1) * 1000, lastcenter, osg::Vec3(0, 1, 0));
            viewer->frame();
            char filename[128];
            sprintf(filename, "ScreenShot%d.png", cnt);
            osgDB::writeImageFile(*captureImage, filename);
        }
        else if (cnt==2)
        {
            track->setTransformation(lastcenter + osg::Vec3(0, 1, 0) * 1000, lastcenter, osg::Vec3(0, 0, 1));
            viewer->frame();
            char filename[128];
            sprintf(filename, "ScreenShot%d.png", cnt);
            osgDB::writeImageFile(*captureImage, filename);
        }
        else 
        {
            viewer->frame();
        }
    
        cnt++;
    }
    return 0;
}

 

以上是关于OSG能够在当前帧截图,也就是能转换视角后马上截图的主要内容,如果未能解决你的问题,请参考以下文章

截取嵌入式 Linux 帧缓冲区的屏幕截图

Video.js 截图

AVPlayer 截取视频流的当前帧

前端也能悄悄对视频截图?js实现对视频按帧缓存

Selenium2+python自动化60-异常后截图(screenshot)

Selenium2+python自动化60-异常后截图(screenshot)