基本步驟以下描述:測試
Step1:CMake3.10編譯openscenegraph3.4.0,生成makefile文件(中間過程可能會涉及到不少三方庫,須要下載編譯,而後按cmake-gui界面的提示正確配置路徑便可);優化
Step2:在生成的makefile文件目錄下,新建run.bat文件,雙擊運行,便可生成動態庫dll文件和靜態庫a文件ui
填寫兩行命令:mingw32-make3d
mingw32-make installorm
Step3:demo測試,新建Qt控制檯工程osgVR。對象
1)將生成的include文件和lib文件放置到工程根目錄下,而後將工程文件osgVR.pro的內容更改以下:blog
2)將main.cpp的內容修改以下:get
#include <osgViewer/Viewer>
it
#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;
}
運行效果: