osg实现三视图

osg实现三视图

 

#include <osg/Geode>
#include <osg/Geometry>
#include <osg/LineWidth>
#include <osgViewer/Viewer>
#include <osgViewer/CompositeViewer>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgViewer/ViewerEventHandlers>  
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/LineWidth>
#include <osgViewer/Viewer>
#include <osg/Node>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgViewer/ViewerEventHandlers> 
#include <osgViewer/CompositeViewer> 
#include <osg/PositionAttitudeTransform> 
#include <osg/MatrixTransform> 
#include <osgFX/Scribe> 
#include <osgParticle/PrecipitationEffect> 
#include <osg/NodeCallback> 
#include <osg/DrawPixels> 
#include <osg/ShapeDrawable>
#include <osg/ComputeBoundsVisitor>
#include <osgGA/TrackballManipulator>
#include <osgGA/StateSetManipulator>
#include <osg/GraphicsContext>
#include <osgViewer/GraphicsWindow>

#include <iostream>

#define M_PI 3.1415926



//绘制几何体
osg::Geode* createShpe()
{
    //
    osg::Geode *geode = new osg::Geode();
    //半径
    float radius = 0.8f;
    //高度
    float height = 1.6f;

    //精细度
    osg::TessellationHints* hints1 = new osg::TessellationHints();
    //设置精细度
    hints1->setDetailRatio(0.9f);

    //创建正方体
    //osg::Box *box = new osg::Box(osg::Vec3(0.0f, 0.0f, 0.0f), 1 * radius);

    osg::Box *box = new osg::Box(osg::Vec3(0.0f, 0.0f, 0.0f), 1.0f, 2.0f, 0.6f);

    osg::ShapeDrawable *draw1 = new osg::ShapeDrawable(box, hints1);

    geode->addDrawable(draw1);

    return geode;
}




int main(int argc, char **argv)
{

    osg::ref_ptr<osg::Geode> m_Root = createShpe();

    //osg::ref_ptr<osg::Referenced> windata = new osgViewer::GraphicsWindowWin32::WindowData();
    osg::ref_ptr<osg::Referenced> windata = new osgViewer::GraphicsWindow();

    // Create the viewer for this window
    //创建复合视图compositeViewer对象,及4个视图
    osgViewer::CompositeViewer* m_ViewerSet = new osgViewer::CompositeViewer();
    osg::ref_ptr<osgViewer::View> Viewer1 = new osgViewer::View();
    osg::ref_ptr<osgViewer::View> Viewer2 = new osgViewer::View();
    osg::ref_ptr<osgViewer::View> Viewer3 = new osgViewer::View();
    osg::ref_ptr<osgViewer::View> Viewer4 = new osgViewer::View();
    //为四个视图创建四个独立的轨迹球
    osgGA::TrackballManipulator* m_traceball1 = new osgGA::TrackballManipulator();
    osgGA::TrackballManipulator* m_traceball2 = new osgGA::TrackballManipulator();
    osgGA::TrackballManipulator* m_traceball3 = new osgGA::TrackballManipulator();
    osgGA::TrackballManipulator* m_traceball4 = new osgGA::TrackballManipulator();
    


    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
    traits->x = 0;
    traits->y = 0;
    traits->width = 1200;
    traits->height = 560;
    traits->windowDecoration = false;
    traits->doubleBuffer = true;
    traits->sharedContext = 0;
    traits->setInheritedWindowPixelFormat = true;
    traits->inheritedWindowData = windata;
    //设置透视的相关参数
    const double fov = 3;
    const double aspect = (double)traits->width / (double)traits->height;
    osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());

    //视图1窗口
    Viewer1->addEventHandler(new osgViewer::StatsHandler);
    Viewer1->setName(("Viewer1"));
    osg::ref_ptr<osg::Camera> camera1 = new osg::Camera;
    camera1->setClearColor(osg::Vec4(255.0, 255.0, 255.0, 1.0));
    camera1->setGraphicsContext(gc);
    camera1->setViewport(new osg::Viewport(traits->x, traits->y, (traits->width) / 2, (traits->height) / 2));
    camera1->setProjectionMatrixAsPerspective(fov, aspect, 1, 10);
    Viewer1->setCamera(camera1.get());
    osg::Light* defaultLight1 = Viewer1->getLight();
    defaultLight1->setAmbient(osg::Vec4(0.2f, 0.2f, 0.2f, 1.0f));
    Viewer1->setSceneData(m_Root.get());
    Viewer1->setCameraManipulator(m_traceball1);
    Viewer1->getCamera()->setCullingMode(Viewer1->getCamera()->getCullingMode() & ~osg::CullStack::SMALL_FEATURE_CULLING);
    
    
    //视图2窗口
    Viewer2->addEventHandler(new osgViewer::StatsHandler);
    Viewer2->setName(("Viewer2"));
    osg::ref_ptr<osg::Camera> camera2 = new osg::Camera;
    camera2->setClearColor(osg::Vec4(255.0, 255.0, 255.0, 1.0));
    camera2->setGraphicsContext(gc);
    camera2->setViewport(new osg::Viewport(traits->x + (traits->width) / 2, traits->y, (traits->width) / 2, (traits->height) / 2));
    camera2->setProjectionMatrixAsPerspective(fov, aspect, 1, 10);
    Viewer2->setCamera(camera2.get());
    osg::Light* defaultLight2 = Viewer2->getLight();
    defaultLight2->setAmbient(osg::Vec4(0.2f, 0.2f, 0.2f, 1.0f));
    Viewer2->setSceneData(m_Root.get());
    Viewer2->setCameraManipulator(m_traceball2);
    Viewer2->getCamera()->setCullingMode(Viewer2->getCamera()->getCullingMode() & ~osg::CullStack::SMALL_FEATURE_CULLING);
    
    
    //视图3窗口
    Viewer3->addEventHandler(new osgViewer::StatsHandler);
    Viewer3->setName(("Viewer3"));
    osg::ref_ptr<osg::Camera> camera3 = new osg::Camera;
    camera3->setClearColor(osg::Vec4(255.0, 255.0, 255.0, 1.0));
    camera3->setGraphicsContext(gc);
    camera3->setViewport(new osg::Viewport(traits->x, traits->y + (traits->height) / 2, (traits->width) / 2, (traits->height) / 2));
    camera3->setProjectionMatrixAsPerspective(fov, aspect, 1, 10);
    Viewer3->setCamera(camera3.get());
    osg::Light* defaultLight3 = Viewer3->getLight();
    defaultLight3->setAmbient(osg::Vec4(0.2f, 0.2f, 0.2f, 1.0f));
    Viewer3->setSceneData(m_Root.get());
    Viewer3->setCameraManipulator(m_traceball3);
    Viewer3->getCamera()->setCullingMode(Viewer3->getCamera()->getCullingMode() & ~osg::CullStack::SMALL_FEATURE_CULLING);
    
    
    //视图4窗口
    Viewer4->addEventHandler(new osgViewer::StatsHandler);
    Viewer4->setName(("Viewer4"));
    osg::ref_ptr<osg::Camera> camera4 = new osg::Camera;
    camera4->setClearColor(osg::Vec4(255.0, 255.0, 255.0, 1.0));
    camera4->setGraphicsContext(gc);
    camera4->setViewport(new osg::Viewport(traits->x + (traits->width) / 2, traits->y + (traits->height) / 2, (traits->width) / 2, (traits->height) / 2));
    camera4->setProjectionMatrixAsPerspective(fov, aspect, 1, 10);
    Viewer4->setCamera(camera4.get());
    osg::Light* defaultLight4 = Viewer4->getLight();
    defaultLight4->setAmbient(osg::Vec4(0.2f, 0.2f, 0.2f, 1.0f));
    Viewer4->setSceneData(m_Root.get());
    Viewer4->setCameraManipulator(m_traceball4);
    Viewer4->getCamera()->setCullingMode(Viewer4->getCamera()->getCullingMode() & ~osg::CullStack::SMALL_FEATURE_CULLING);


    //设置视图初始状态
    //TOP_VIEW
    m_traceball1->setRotation(osg::Quat(1.0f, 0.0, 0.0, 0.0));
    m_traceball2->setRotation(osg::Quat(0.0f, 0.0f, 0.0f, 1.0f));
    //WEST_VIEW
    m_traceball3->setRotation(osg::Quat(1.0f, -1.0, -1.0, 1.0f));
    //NORTH_VIEW
    m_traceball4->setRotation(osg::Quat(0.0f, 1.0f, 1.0f, 0.0f));

    //添加视图
    m_ViewerSet->addView(Viewer1);
    m_ViewerSet->addView(Viewer2);
    m_ViewerSet->addView(Viewer3);
    m_ViewerSet->addView(Viewer4);


    // Realize the Viewer
    m_ViewerSet->realize();
    m_ViewerSet->setKeyEventSetsDone(0);

    m_ViewerSet->run();

    return 0;
}

 

 

 

参考:https://blog.csdn.net/pengjiaqi1028/article/details/78582366

 

posted @ 2021-09-24 16:21  西北逍遥  阅读(519)  评论(0编辑  收藏  举报