|
楼主 |
发表于 2011-1-18 10:37:53
|
显示全部楼层
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgViewer/CompositeViewer>
#include <osg/Node>
#include <osg/Geode>
#include <osg/Group>
#include <osg/Geometry>
#include <osg/MatrixTransform>
#include <osg/PositionAttitudeTransform>
#include <osg/Camera>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgUtil/Optimizer>
#include <osgGA/TrackballManipulator>
#include <iostream>
int main()
{
osg::ref_ptr<osgViewer::CompositeViewer> viewer=new osgViewer::CompositeViewer();
//osg::ref_ptr<osgViewer::Viewer> viewer=new osgViewer::Viewer();
osg::ref_ptr<osg::Node> cow=osgDB::readNodeFile("cow.osg");
osg::ref_ptr<osg::Node> cessna=osgDB::readNodeFile("di_1.flt");
osgUtil::Optimizer optimizer;
optimizer.optimize(cow.get());
optimizer.optimize(cessna.get());
//设置图形环境特性
osg::ref_ptr<osg::GraphicsContext::Traits> traits=new osg::GraphicsContext::Traits();
traits->x=0;
traits->y=0;
traits->width=1280;
traits->height=768;
traits->windowDecoration=true;
traits->doubleBuffer=true;
traits->sharedContext=0;
//创建图形环境特性
osg::ref_ptr<osg::GraphicsContext>gc=osg::GraphicsContext::createGraphicsContext(traits.get());
if(gc->valid())
{
osg::notify(osg::INFO)<<"wewrererwrw"<<std::endl;
gc->setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f));
gc->setClearMask(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
}
else
{
osg::notify(osg::NOTICE)<<"hjhbhjsbjdf"<<std::endl;
}
//////////////////////
//////////////////////省略
//视图1
{
osg::ref_ptr<osgViewer::View> view=new osgViewer::View;
viewer->addView(view.get());
view->setSceneData(cessna.get());
//根据分辨率确定合适的投影来保证显示的图形不变形
double fovy,aspectRatio,zNear,zFar;
view->getCamera()->getProjectionMatrixAsPerspective(fovy,aspectRatio,zNear,zFar);
double newAspectRatio=double(traits->width)/double(traits->height/4);
double aspectRatioChange=newAspectRatio/aspectRatio;
if (aspectRatioChange!=1.0)
{
view->getCamera()->getProjectionMatrix()*=osg::Matrixd::scale(1.0/aspectRatioChange,1.0,1.0);
}
view->getCamera()->setViewMatrixAsLookAt(osg::Vec3f(0.0,70000.0,60000.0),osg::Vec3f(50000,80000,0),osg::Vec3f(0.0,1.0,0.0));
view->getCamera()->setViewport(new osg::Viewport(0,0,1280,768));
view->getCamera()->setGraphicsContext(gc.get());
//view->setCameraManipulator(new osgGA::TrackballManipulator);//
///添加事件处理
view->addEventHandler(new osgViewer::StatsHandler);
view->addEventHandler(new osgViewer::WindowSizeHandler);
view->addEventHandler(new osgViewer::ThreadingHandler);
view->addEventHandler(new osgViewer::RecordCameraPathHandler);
}
//视图二
{
osg::ref_ptr<osgViewer::View> view=new osgViewer::View;
viewer->addView(view.get());
view->setSceneData(cessna.get());
view->getCamera()->setViewMatrixAsLookAt(osg::Vec3f(0.0,60000.0,60000.0),osg::Vec3f(50000,50000,0),osg::Vec3f(0.0,1.0,0.0));
view->getCamera()->setViewport(new osg::Viewport(980,0,300,300));
view->getCamera()->setGraphicsContext(gc.get());
//view->setCameraManipulator(new osgGA::TrackballManipulator);
}
viewer->realize();
viewer.addEventHandler(new UseEventHandler) ;
while( !viewer->done() )
{
viewer->frame();
}
//viewer->frame();
//viewer->run();
return 0;
} |
|