1. 程式人生 > >OpenSceneGraph3.4.0+Qt5.6.1MinGW開發環境部署

OpenSceneGraph3.4.0+Qt5.6.1MinGW開發環境部署

borde lighting GC AD nor 內容修改 translate ali 提示

技術分享圖片 技術分享圖片 技術分享圖片

基本步驟如下描述:

Step1:CMake3.10編譯openscenegraph3.4.0,生成makefile文件(中間過程可能會涉及到很多三方庫,需要下載編譯,然後按cmake-gui界面的提示正確配置路徑即可);


技術分享圖片

技術分享圖片

技術分享圖片

Step2:在生成的makefile文件目錄下,新建run.bat文件,雙擊運行,即可生成動態庫dll文件和靜態庫a文件

填寫兩行命令:mingw32-make

mingw32-make install

Step3:demo測試,新建Qt控制臺工程osgVR。

1)將生成的include文件和lib文件放置到工程根目錄下,然後將工程文件osgVR.pro的內容更改如下:

技術分享圖片

2)將main.cpp的內容修改如下:

#include <osgViewer/Viewer>

            #include <osg/Node>
            #include <osg/Geode>
            #include <osg/Geometry>
            #include <osg/Group>
            #include <osg/Billboard>
            #include <osg/Texture2D>
            #include <osg/Image>
            #include <osg/PositionAttitudeTransform>
            #include <osg/MatrixTransform>

            #include <osgDB/ReadFile>
            #include <osgDB/WriteFile>

            #include <osgUtil/Optimizer>

            int xoffset = 200;
            int yoffset = 200;

           osg::ref_ptr<osg::Node> createBillboardTree(osg::ref_ptr<osg::Image> image)
         {
                  //創建四邊形
                 osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry();

                 //設置頂點
                 osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array();
                 v->push_back(osg::Vec3(-0.5f, 0.0f, -0.5f));
                 v->push_back(osg::Vec3(0.5f, 0.0f, -0.5f));
                 v->push_back(osg::Vec3(0.5f, 0.0f, 0.5f));
                 v->push_back(osg::Vec3(-0.5f, 0.0f, 0.5f));

                geometry->setVertexArray(v.get());

                  //設置法線
                osg::ref_ptr<osg::Vec3Array> normal = new osg::Vec3Array();
                normal->push_back(osg::Vec3(1.0f, 0.0f, 0.0f) ^ osg::Vec3(0.0f, 0.0f, 1.0f));

                geometry->setNormalArray(normal.get());
                geometry->setNormalBinding(osg::Geometry::BIND_OVERALL);

                 //設置紋理坐標
               osg::ref_ptr<osg::Vec2Array> vt = new osg::Vec2Array();
               vt->push_back(osg::Vec2(0.0f, 0.0f));
               vt->push_back(osg::Vec2(1.0f, 0.0f));
               vt->push_back(osg::Vec2(1.0f, 1.0f));
               vt->push_back(osg::Vec2(0.0f, 1.0f));

               geometry->setTexCoordArray(0, vt.get());

                 //繪制四邊形
              geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 4));

              if (image.get())
           {
                //狀態屬性對象
               osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet();

                //創建一個Texture2D屬性對象
               osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D();
                //關聯image
              texture->setImage(image.get());
                //關聯Texture2D紋理對象,第三個參數默認為ON
              stateset->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);
                //啟用混合
              stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
                //關閉光照
              stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);

               geometry->setStateSet(stateset.get());
           }

               //創建Billboard對象一
             osg::ref_ptr<osg::Billboard> billboard1 = new osg::Billboard();

             //設置旋轉模式為繞視點
             billboard1->setMode(osg::Billboard::POINT_ROT_EYE);
             //添加Drawable,並設置其位置,默認位置為osg::Vec3(0.0f,0.0f,0.0f) ;
             billboard1->addDrawable(geometry.get(), osg::Vec3(5.0f, 0.0f, 0.0f));

            osg::ref_ptr<osg::Billboard> billboard2 = new osg::Billboard();
            //設置旋轉模式為繞軸轉,因此還需要設置轉軸
             billboard2->setMode(osg::Billboard::AXIAL_ROT);
            //設置旋轉軸
            billboard2->setAxis(osg::Vec3(0.0f, 0.0f, 1.0f));
            billboard2->addDrawable(geometry.get(), osg::Vec3(10.0f, 0.0f, 0.0f));

            osg::ref_ptr<osg::Group> billboard = new osg::Group();
            billboard->addChild(billboard1.get());
            billboard->addChild(billboard2.get());

            return billboard.get();
        }

         int main()
      {
           osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();

           osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
           traits->x = xoffset + 0;
           traits->y = yoffset + 0;
           traits->width = 800;
           traits->height = 600;
           traits->windowDecoration = true;
           traits->doubleBuffer = true;
           traits->sharedContext = 0;
           osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
           osg::ref_ptr<osg::Camera> camera = new osg::Camera;
           camera->setGraphicsContext(gc.get());
           camera->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));
           GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
           camera->setDrawBuffer(buffer);
           camera->setReadBuffer(buffer);

           viewer->addSlave(camera.get(), osg::Matrixd::translate(0.0, 0.0, 0.0), osg::Matrixd());

           osg::ref_ptr<osg::Group> root = new osg::Group();

           //讀取圖像
           osg::ref_ptr<osg::Image> image = osgDB::readImageFile("images/tree0.rgba");

           //縮放一下,以達到合適的大小
           osg::ref_ptr<osg::PositionAttitudeTransform> pat = new osg::PositionAttitudeTransform();
           pat->setScale(osg::Vec3(5.0f, 5.0f, 5.0f));
           pat->addChild(createBillboardTree(image.get()));

           root->addChild(pat.get());

          //讀取robot的模型,以對比
          root->addChild(osgDB::readNodeFile("robot.osg"));

         //優化場景數據
         osgUtil::Optimizer optimizer;
         optimizer.optimize(root.get());

         viewer->setSceneData(root.get());

         viewer->realize();

         viewer->run();

         return 0;
     }

運行效果:

                                             技術分享圖片
                                             技術分享圖片

OpenSceneGraph3.4.0+Qt5.6.1MinGW開發環境部署