|
楼主 |
发表于 2011-4-25 18:14:12
|
显示全部楼层
我不会上传附件,所以分文件粘贴了一下代码:
FuncDemo.cpp 与FuncDemo.h是实现GIF的显示
如下为FuncDemo.h:- #include <osgDB/ReadFile>
- #include <osg/Geode>
- #include <osg/Camera>
- #include <osg/ImageStream>
- #include <osgAnimation/UpdateCallback>
- #include<osgViewer/Viewer>
- #include<osg/Image>
- #include<osg/Texture2D>
- #include<osgViewer/ViewerEventHandlers>
- #include<windows.h>
- using namespace std;
- class TrInterfaceVedio: public osg::Group
- {
- public:
- /* 播放一段或多段视频*/
- TrInterfaceVedio(std::string VediosPath);
- //读出视频后,得到名字,帧等信息
- ~TrInterfaceVedio(void);
- // 显示视频序列
- void loadVedios();
- // 创建四边形几何面片,为了纹理贴图
- osg::Geometry* myCreateTexturedQuadGeometry(const osg::Vec3& pos,float width,float height, osg::Image* image);
- // 返回 geode, 不作抬头显示
- osg::Geode* getGeode();
- // 作抬头显示,返回相机
- osg::Camera* getCamera();
- bool setVedioPath(string _VedioPath);
- private:
- std::string _vediosPath; // 存储视频序列中每段视频的存储路径
- osg::ref_ptr<osg::Geode> geode;// 图片可作为一个 geode 节点显示
- osg::ref_ptr<osg::ImageStream> imageStream;// 动画作为一个图片序列
- osg::ref_ptr<osg::Camera> _camera;// 要作为最外显示时需要一个post render 相机
- osg::Vec3 pos;// 视频位置
- };
复制代码 下面是FuncDemo.cpp
- #include "FuncDemo.h"
- unsigned int Width,Height;
- TrInterfaceVedio::TrInterfaceVedio(std::string vediosPath)
- {
- //获取分辨率
- osg::GraphicsContext::WindowingSystemInterface*wsi=osg::GraphicsContext::getWindowingSystemInterface();
- if(!wsi)
- {
- //return 0;
- }
-
- wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0),Width,Height);
- //设置图形环境特性
- osg::ref_ptr<osg::GraphicsContext::Traits> traits=new osg::GraphicsContext::Traits();
- traits->x=0;
- traits->y=0;
- traits->width=Width;
- traits->height=Height;
- traits->windowDecoration=false;
- traits->doubleBuffer=true;
- traits->sharedContext=0;
- _vediosPath=vediosPath;
- //GIf路径存储
- geode = new osg::Geode;
- _camera=new osg::Camera;
- _camera->setProjectionMatrixAsOrtho2D(0,Width, 0, Height);
- // set the view matrix
- _camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
- _camera->setViewMatrix(osg::Matrix::identity());
- // only clear the depth buffer
- _camera->setClearMask(GL_DEPTH_BUFFER_BIT);
- // draw subgraph after main camera view.
- _camera->setRenderOrder(osg::Camera::POST_RENDER);
- // we don't want the camera to grab event focus from the viewers main camera(s).
- _camera->setAllowEventFocus(false);
- _camera->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
- }
- TrInterfaceVedio::~TrInterfaceVedio()
- {
- }
- bool TrInterfaceVedio::setVedioPath(string _VedioPath)
- {
- return true;
- }
- osg::Geometry* TrInterfaceVedio::myCreateTexturedQuadGeometry(const osg::Vec3& pos,float width,float height, osg::Image* image)
- {
- osg::Geometry* pictureQuad = osg::createTexturedQuadGeometry(pos,osg::Vec3(width,0.0f,0.0f),osg::Vec3(0.0f,height,0.0f),0.0f, 0.0f , 1.0f, 1.0f);
- osg::Texture2D* texture = new osg::Texture2D(image);
- texture->setResizeNonPowerOfTwoHint(false);
- texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR);
- texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
- texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
- pictureQuad->getOrCreateStateSet()->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
- return pictureQuad;
-
- }
- void TrInterfaceVedio::loadVedios()
- {
- osg::Image* image = osgDB::readImageFile(_vediosPath);
- imageStream = dynamic_cast<osg::ImageStream*>(image);
- if (imageStream) imageStream->play();
- if (image)
- {
- geode->addDrawable(myCreateTexturedQuadGeometry(pos, Width, Height, image));
- }
- else
- {
- std::cout<<"Unable to read file "<<"arguments[i]"<<std::endl;
- }
- _camera->addChild(geode);
- }
- osg::Camera* TrInterfaceVedio::getCamera()
- {
- return _camera;
- }
- osg::Geode* TrInterfaceVedio::getGeode()
- {
- return geode;
- }
- int main()
- {
- /**开始动画*/
- osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
- osg::ref_ptr<osg::Group> group = new osg::Group;
- int _timegif = 0;
- //计算帧速
- int counts = 0;
- //控制帧速使用的睡眠时间
- float sleep_time = 0.0;
- float last_sleep = 0.0001;
- //每帧的实际使用时间
- float current_time = 0.0;
- //每帧控制法
- float per_str_time = 0.0;
- float per_end_time = 0.0;
- //申请一个定时器类
- osg::Timer* timer = new osg::Timer;
- osg::Timer_t start_frame_time = 0;
- osg::Timer_t end_frame_time = 0;
-
- TrInterfaceVedio* gifReader = new TrInterfaceVedio("FuncDemo.gif");
- gifReader->loadVedios();
- group->addChild(gifReader->getCamera());
- viewer->setSceneData(group);
- while(!viewer->done())
- {
- per_str_time = timer->tick();
- if(counts == 0)
- {
- start_frame_time = timer->tick();
- }
- counts++;
- viewer->frame();
- per_end_time = timer->tick();
- sleep_time = 0.1 - timer->delta_s(per_str_time, per_end_time);
- if(sleep_time < 0)
- {
- sleep_time = last_sleep * 0.8;
- }
- last_sleep = sleep_time;
- OpenThreads::Thread::microSleep(sleep_time*1000000);
- last_sleep = sleep_time;
- if(counts == 3)
- {
- //限制帧速为10,每帧绘制0.1s
- counts = 0;
- end_frame_time = timer->tick();
- std::cout<<"当前帧速为:"<<3/(timer->delta_s(start_frame_time, end_frame_time))<<std::endl;
- _timegif++;
- if(_timegif == 14)
- {
- break;
- }
- }
- }
- }
复制代码 |
|