osg入门系列2-公告板的使用

//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;
}

//运行效果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑不溜秋的

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值