OSG能够在当前帧截图,也就是能转换视角后马上截图
2024-10-07 23:51:59
#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 center;
int captureAngelCount = ;
osg::Vec3d shotEye[];
osg::Vec3d shotup[];
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(), width, height);
static int cnt = ;
//读取像素信息抓图
captureImage->readPixels((width - ) / , (height - ) / , , , GL_RGBA, GL_UNSIGNED_BYTE);
char filename[];
sprintf(filename, "ScreenShot//ScreenShot%d.png", cnt++);
osgDB::writeImageFile(*captureImage, filename);
}
}; int CountLines(char *filename)
{
std::ifstream ReadFile;
int n = ;
std::string tmp;
ReadFile.open(filename, std::ios::in);//ios::in 表示以只读的方式读取文件
if (ReadFile.fail())//文件打开失败:返回0
{
return ;
}
else//文件存在
{
while (getline(ReadFile, tmp, '\n'))
{
n++;
}
ReadFile.close();
return n;
}
} 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("F://C++代码//CaptureTest//tio//osg_vertex_visitor_test//model2.ive");
root->addChild(node);
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
{
traits->x = ;
traits->y = ;
traits->width = ;
traits->height = ;
//支持窗口扩展,默认是不支持的
traits->windowDecoration = true;
//支持双缓存,默认不支持
traits->doubleBuffer = true;
traits->sharedContext = ;
traits->red = ;
traits->blue = ;
traits->green = ;
//支持alpha,默认不支持为0,这里改为支持,使截出来的图片具有alpha值
traits->alpha = ; 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(, , , );
viewer->getCamera()->setClearColor(osg::Vec4(, , , ));
viewer->setCameraManipulator(track);
viewer->getCamera()->setGraphicsContext(gc.get());
osgUtil::Optimizer optimizer;
optimizer.optimize(root.get()); viewer->setSceneData(root.get());
viewer->realize();
viewer->getCamera()->setFinalDrawCallback(new CaptureCallback());
}
{
const osg::BoundingSphere bs = node->getBound();
std::cout << "NodeRadius: " << bs.radius() << std::endl;
//设置getTextureDistance以后开始使用贴图
//float getTextureDistance = 1000000.0f;
float getTextureDistance = bs.radius() * ;
osg::Vec3 viewDirection[];
center = bs.center();
//读取拍照点,并求up值集合
{
std::ifstream file;
int LINES;
char filename[] = "shotEye258.txt";
file.open(filename, std::ios::in);
if (file.fail())
{
std::cout << "文件不存在." << std::endl;
file.close();
}
else//文件存在
{
LINES = CountLines(filename);
while (LINES--) //读取数据到数组
{
float a, b, c;
file >> a;
file >> b;
file >> c;
viewDirection[++captureAngelCount].set(a, b, c);
}
} file.close(); for (int i = ; i <= captureAngelCount; i++)
{
shotEye[i] = center + viewDirection[i] * getTextureDistance;
}
int cnt = ;
for (int i = ; i <= captureAngelCount; i++)
{
osg::Vec3 temp = center - shotEye[i];
//如果与X轴平行,那么与Y轴叉乘 if ((temp^osg::Vec3(, , )) == osg::Vec3(, , ))
{
cnt++;
shotup[i] = temp^osg::Vec3(, , );
shotup[i].normalize();
}
else
{
shotup[i] = temp^osg::Vec3(, , );
shotup[i].normalize();
}
} }
}
static int cnt = ; for (; cnt <= captureAngelCount; cnt++)
{
track->setByInverseMatrix(osg::Matrix::lookAt(shotEye[cnt], center, shotup[cnt]));
viewer->frame();
//Sleep(100); }
return ;
}
最新文章
- 从零开始学 Java - Windows 下安装 JDK
- 反人类的MyEclipse之-调整JavaScript代码-花括号换行显示
- Thinkphp源码分析系列(一)–入口文件
- redis配置注意事项
- 【动态规划】流水作业调度问题与Johnson法则
- oracle数据库创建表空间和表临时空间
- 去掉cajviewer 右上角的“中国知网数字出版物超市
- HTTP使用BASIC认证的原理及实现方法 (转载)
- Tecplot: Legend显示与否
- Maven 打包项目 部署到服务器 重启服务 插件
- Map Labeler POJ - 2296(2 - sat 具体关系建边)
- Python - 内置函数 选例
- PAT 乙级 1064 朋友数(20) C++版
- MFC中获取命令行参数的几种方法
- sourcetree管理git
- Python爬虫教程-09-error 模块
- 配置hadoop集群的lzo压缩
- 全渠道价值链整合云服务 B2B SOLOMO(组图)
- wwnjld团队第二轮迭代成员分数
- mysql中删除完全重复数据的准确SQL语句