osg HUD 背景图片设置

 

#ifdef _WIN32
#include <Windows.h>
#endif // _WIN32
#include<iostream>

#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers> 
#include <osgViewer/CompositeViewer> 

#include <osgDB/ReadFile>

#include <osg/Geode>
#include <osg/Node>
#include <osg/Geometry>
#include <osg/GraphicsContext>
#include <osg/ShapeDrawable>
#include <osg/Material>
#include <osg/Image>
#include <osg/Texture2D>
#include <osg/TexEnv>
#include <osg/TexGen>
#include <osg/NodeVisitor>
#include <osg/MatrixTransform>
#include <osg/PositionAttitudeTransform>
#include <osg/AnimationPath>
#include <osg/Matrixd>
#include <osg/PagedLOD>
#include <osg/Camera>
#include <osgText/Text>

#include <osgGA/TrackballManipulator>
#include <osgGA/GUIEventHandler>
#include <osgGA/CameraManipulator>
#include <osgGA/StandardManipulator>
#include <osgGA/OrbitManipulator>
#include <osgGA/TrackballManipulator>

#include <osgUtil/IntersectionVisitor>
#include <osgUtil/LineSegmentIntersector>

osg::Camera* createBackground(std::string strImg)
{
    osg::ref_ptr<osg::Geode> geode1 = new osg::Geode;
    osg::ref_ptr<osg::Geometry> geometry1 = new osg::Geometry;
    osg::ref_ptr<osg::Camera> camera1 = new osg::Camera;

    camera1->setAllowEventFocus(false);
    camera1->setProjectionMatrixAsOrtho2D(0, 960, 0, 600);
    camera1->setViewport(0, 0, 960, 600);

    camera1->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
    camera1->setRenderOrder(osg::Camera::PRE_RENDER);
    camera1->setClearMask(GL_DEPTH_BUFFER_BIT  |  GL_COLOR_BUFFER_BIT);
    //camera1->setClearColor(osg::Vec4());
    camera1->setViewMatrix(osg::Matrix::identity());

    //压入顶点
    osg::ref_ptr<osg::Vec3Array> vertex = new osg::Vec3Array;
    vertex->push_back(osg::Vec3(0.0,0.0,0.0));
    vertex->push_back(osg::Vec3(960.0, 0.0, 0.0));
    vertex->push_back(osg::Vec3(960.0, 600.0, 0.0));
    vertex->push_back(osg::Vec3(0.0, 600.0, 0.0));
    geometry1->setVertexArray(vertex);

    //压入法线
    osg::ref_ptr<osg::Vec3Array> norml = new osg::Vec3Array;
    norml->push_back(osg::Vec3(0.0, 0.0, 1.0));
    geometry1->setNormalArray(norml);
    geometry1->setNormalBinding(osg::Geometry::BIND_OVERALL);

    //纹理坐标
    osg::ref_ptr<osg::Vec2Array> coord = new osg::Vec2Array;
    coord->push_back(osg::Vec2(0.0,0.0));
    coord->push_back(osg::Vec2(1.0, 0.0));
    coord->push_back(osg::Vec2(1.0, 1.0));
    coord->push_back(osg::Vec2(0.0, 1.0));
    geometry1->setTexCoordArray(0, coord);
    geometry1->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 4));

    osg::ref_ptr<osg::Image> img1 = osgDB::readImageFile(strImg);
    if (!img1.valid())
    {
        std::cout << "" << std::endl;
    }

    osg::ref_ptr<osg::Texture2D> texture2d = new osg::Texture2D;
    texture2d->setImage(0, img1);
    geometry1->getOrCreateStateSet()->setTextureAttributeAndModes(0,texture2d,osg::StateAttribute::ON);


    camera1->addChild(geode1);
    geode1->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
    geode1->addDrawable(geometry1);


    return camera1.release();
}


int main()
{
    osg::ref_ptr<osgViewer::Viewer> viewer1 = new osgViewer::Viewer;
    osg::ref_ptr<osg::Group> group1 = new osg::Group;

    osg::ref_ptr<osg::Node> node1 = osgDB::readNodeFile("D:\\参考手册\\BIM\\osg\\build20190628.osgb");

    group1->addChild(createBackground("D:\\参考手册\\images\\104.jpg"));
    group1->addChild(node1.get());

    viewer1->getCamera()->setClearMask(GL_DEPTH_BUFFER_BIT);
    viewer1->setSceneData(group1.get());
    viewer1->setUpViewInWindow(200, 200, 960, 600, 0);

    return viewer1->run();
}

 

posted @ 2019-07-01 20:13  西北逍遥  阅读(993)  评论(0编辑  收藏  举报