标签:hot viewer share call blog adf sig pixel node
#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; }
标签:hot viewer share call blog adf sig pixel node
原文地址:http://www.cnblogs.com/tangmiao/p/7753539.html