osg雖然不是很火,可是對於非計算機專業的人來講確實是一個神器,osg其實也是來自於opengl,它能夠理解爲一個高度封裝的opengl圖像庫,因爲其沒有太多太多的技術門檻以及擴展性不高,致使其市場一直不溫不火,可是其封裝的LOD技術,多線程技術以及顯示仍是爲急需作平臺又不想投入大量時間的人提供了便利。博主就是屬於其中之一,因爲點雲數據愈來愈大,現在數據點不超過1個億都很差意思稱做大數據。因爲opengl的LOD技術國內在這方面保密甚爲嚴格,固然各行各業都是這樣,這也是國內技術一直遠遠落後於國外的緣由之一吧!其實不少工具真的頗有用,可是苦於沒有入門教程,因此只能望洋興嘆,這也是國內編程技術的現狀之一吧!目前博主也是盡本身的一點綿薄之力把一些有用的入門教程奉獻給你們,但願能給那些還在點雲顯示道路上掙扎的人一點點幫助!同時也呼籲你們能把一些並無什麼核心競爭力的編程知識共享一下,由於每一篇有用的博客都有可能開啓一我的的編程之路,廢話很少說,直接上代碼,保證直接運行可用。ios
bool rgbIsInt=false;編程
/*osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer(); viewer->addEventHandler(new osgGA::StateSetManipulator(viewer->getCamera()->getOrCreateStateSet()));*/ osg::ref_ptr<osg::Group> root = new osg::Group(); osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer; { osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; traits->x = 40; traits->y = 40; traits->width = 600; traits->height = 480; 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); // add this slave camera to the viewer, with a shift left of the projection matrix viewer->addSlave(camera.get()); } //建立頂點數組 osg::ref_ptr<osg::Vec3Array> coords = new osg::Vec3Array(); osg::ref_ptr<osg::Vec4Array> color = new osg::Vec4Array(); //快速讀取文件 string filePath = "整數.txt";//mcolor_小數 ZSWZ2015111 FILE* pfData = fopen(filePath.c_str(), "r"); ifstream fileIn(filePath, ios::in);//讀取文件 string line;//讀取了每一行的字符 getline(fileIn, line);//獲取第一行 stringstream sstr(line);//給他轉一下 string token; int lineInNum=0; while (getline(sstr, token, ' ')) { lineInNum ++ ; if (lineInNum >= 3) { if (stof(token)> 1) { rgbIsInt = true; } } } int num = 0;//數據點數量 if (pfData == NULL) { cout << "DataFile does not exist!!!" << endl; return NULL; } else { if (rgbIsInt == false)//讀小數 { while (!feof(pfData)) { float fx, fy, fz, n, m, k; float fa, fb, fc; fscanf(pfData, "%f", &fx); fscanf(pfData, "%f", &fy); fscanf(pfData, "%f", &fz); fscanf(pfData, "%f", &fa); fscanf(pfData, "%f", &fb); fscanf(pfData, "%f", &fc); coords->push_back(osg::Vec3(fx, fy, fz)); color->push_back(osg::Vec4(fa, fb, fc, 1.0f)); num++; } } else//讀整數 { while (!feof(pfData)) { float fx, fy, fz; int fa, fb, fc; fscanf(pfData, "%f", &fx); fscanf(pfData, "%f", &fy); fscanf(pfData, "%f", &fz); fscanf(pfData, "%d", &fa); fscanf(pfData, "%d", &fb); fscanf(pfData, "%d", &fc); float aa = (float)fa / 255.00; float bb = (float)fb / 255.00; float cc = (float)fc / 255.00; coords->push_back(osg::Vec3(fx, fy, fz)); color->push_back(osg::Vec4(aa, bb, cc, 1.0f)); num++; } } fclose(pfData); } //建立幾何體 osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry(); //設置頂點數組 geometry->setVertexArray(coords.get()); geometry->setColorArray(color.get()); geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX); osg::Vec3Array *normals = new osg::Vec3Array; normals->push_back(osg::Vec3(0.0f, 1.0f, 0.0f)); //geometry->setNormalArray(normals); //geometry->setNormalBinding(osg::Geometry::BIND_OVERALL); geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS, 0, num)); //設置關聯方式 //添加到葉節點 osg::ref_ptr<osg::Geode> geode = new osg::Geode(); geode->addDrawable(geometry.get()); root->addChild(geode.get()); root->getOrCreateStateSet()->setMode(GL_LIGHTING, StateAttribute::OFF | StateAttribute::OVERRIDE); //優化場景數據 osgUtil::Optimizer optimizer; optimizer.optimize(root.get()); viewer->setSceneData(root.get()); viewer->realize(); viewer->run(); return viewer->run();
這是運行效果:數組
後期會把osg嵌入到Qt的方法貢獻給你們,因爲目前博主也在學習階段,感受仍是控制檯學習較爲方便。多線程