#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 ;
}

最新文章

  1. 从零开始学 Java - Windows 下安装 JDK
  2. 反人类的MyEclipse之-调整JavaScript代码-花括号换行显示
  3. Thinkphp源码分析系列(一)–入口文件
  4. redis配置注意事项
  5. 【动态规划】流水作业调度问题与Johnson法则
  6. oracle数据库创建表空间和表临时空间
  7. 去掉cajviewer 右上角的“中国知网数字出版物超市
  8. HTTP使用BASIC认证的原理及实现方法 (转载)
  9. Tecplot: Legend显示与否
  10. Maven 打包项目 部署到服务器 重启服务 插件
  11. Map Labeler POJ - 2296(2 - sat 具体关系建边)
  12. Python - 内置函数 选例
  13. PAT 乙级 1064 朋友数(20) C++版
  14. MFC中获取命令行参数的几种方法
  15. sourcetree管理git
  16. Python爬虫教程-09-error 模块
  17. 配置hadoop集群的lzo压缩
  18. 全渠道价值链整合云服务 B2B SOLOMO(组图)
  19. wwnjld团队第二轮迭代成员分数
  20. mysql中删除完全重复数据的准确SQL语句

热门文章

  1. QT之QSettings 学习
  2. elementUI中的隐藏组件el-scrollbar
  3. 浅谈web应用的高可用
  4. 2019-9-23-asp-dotnet-core-3.0-接口返回-json-使用-PascalCase-格式
  5. Mysql的数据列类型效率
  6. Python-面向对象之封装与多态
  7. some方法过滤
  8. lavarel中如何使用memcache
  9. VS2010-MFC(对话框:设置对话框控件的Tab顺序)
  10. 【DM642学习笔记一】关于Can&#39;t Initialize Target CPU的一种解决方法 : Error 0x80000240