//Ubuntu QT 编译通过,上代码
#include <iostream>
using namespace std;
#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>
osg::ref_ptr<osg::Node> createBillboardTree(osg::ref_ptr<osg::Image> image)
{
//Create a quadrilateral
osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry();
//Set the vertex
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());
//Set the normal
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);
//Set the texture coordinates
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());
//Draw the quadrilateral
geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 4));
//State attribute object
if(image.get())
{
osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet();
//Create a Texture2D object attribute
osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D();
//Associated with the image
texture->setImage(image.get());
//Associated withe Texture2D object and the third parameter is On by default
stateset->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);
//Enabled blend
stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
//Close light
stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
geometry->setStateSet(stateset.get());
}
//Create the Billboard object 1
osg::ref_ptr<osg::Billboard> billboard1 = new osg::Billboard();
//Set the rotation mode to around the point of view
billboard1->setMode(osg::Billboard::POINT_ROT_EYE);
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::Group> root = new osg::Group();
//Read the image
osg::ref_ptr<osg::Image> image = osgDB::readImageFile("/home/zhaogang/work/thirdparty/OpenSceneGraph-3.2.1/data/Images/tree0.rgba");
//Zoom to a suitable size
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());
root->addChild(osgDB::readNodeFile("/home/zhaogang/work/thirdparty/OpenSceneGraph-3.2.1/data/cow.osg"));
osgUtil::Optimizer optimizer;
optimizer.optimize(root.get());
viewer->setSceneData(root.get());
viewer->realize();
viewer->run();
return 0;
}
//运行效果: