osg::Texture2D 贴纹理

 

#ifdef _WIN32
#include 
#endif // _WIN32
#include

#include 
#include  
#include  

#include 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 
#include 
#include 
#include 

#include 
#include 

osg::Camera* createTextHUD()
{
    osg::ref_ptr geode1 = new osg::Geode;
    osg::ref_ptr text1 = new osgText::Text;
    osg::ref_ptr camera1 = new osg::Camera;

    osg::ref_ptr geometry1 = new osg::Geometry;

    camera1->setViewMatrix(osg::Matrix::identity());
    camera1->setRenderOrder(osg::Camera::POST_RENDER);
    camera1->setClearMask(GL_DEPTH_BUFFER_BIT);

    camera1->setAllowEventFocus(false);
    camera1->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
    camera1->setProjectionMatrixAsOrtho2D(-20, 600, -20, 400);

    text1->setFont("Fonts/simhei.ttf");
    text1->setCharacterSize(50);
    text1->setText(L"OSG 中文字体");
    text1->setPosition(osg::Vec3(0.0,0.0,0.0));

    //压入顶点
    osg::Vec3Array *vecArray = new osg::Vec3Array;
    vecArray->push_back(osg::Vec3(0.0,0.0,0.0));
    vecArray->push_back(osg::Vec3(200.0, 0.0, 0.0));
    vecArray->push_back(osg::Vec3(200.0, 100.0, 0.0));
    vecArray->push_back(osg::Vec3(0.0, 100.0, 0.0));
    geometry1->setVertexArray(vecArray);

    //法线
    osg::Vec3Array *vecNorm = new osg::Vec3Array;
    vecNorm->push_back(osg::Vec3(0.0,0.0,1.0));
    geometry1->setNormalArray(vecNorm);
    geometry1->setNormalBinding(osg::Geometry::BIND_OVERALL);

    //设置纹理
    osg::Vec2Array *vec2Coord = new osg::Vec2Array;
    vec2Coord->push_back(osg::Vec2(0.0,0.0));
    vec2Coord->push_back(osg::Vec2(1.0, 0.0));
    vec2Coord->push_back(osg::Vec2(1.0, 1.0));
    vec2Coord->push_back(osg::Vec2(0.0, 1.0));
    geometry1->setTexCoordArray(0,vec2Coord);

    osg::DrawArrays *drawArrays1 = new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 4);
    //geometry1->setPrimitiveSet(0,drawArrays1);
    geometry1->addPrimitiveSet(drawArrays1);

    //贴纹理
    osg::ref_ptr image1 = osgDB::readImageFile("D:\\image_1\\20190325092002-save.jpg");
    osg::Texture2D *texture2d1 = new osg::Texture2D;
    texture2d1->setImage(image1);
    geometry1->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture2d1, osg::StateAttribute::ON);
    //关闭灯光
    geometry1->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);

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

    camera1->addChild(geode1.get());
    return camera1.release();
}

int main()
{
    osg::ref_ptr viewer1 = new osgViewer::Viewer;
    osg::ref_ptr group1 = new osg::Group;
    
    //osg::ref_ptr node1 = osgDB::readNodeFile("D:\\参考手册\\BIM\\osg\\build20190628.osgb");
    osg::ref_ptr node2 = osgDB::readNodeFile("D:\\参考手册\\BIM\\osg\\build1.osgb");

    group1->addChild(node2.get());
    group1->addChild(createTextHUD());

    viewer1->setSceneData(group1.get());
    viewer1->setUpViewInWindow(200,200,800,600,0);

    viewer1->run();
}

osg::Texture2D 贴纹理_第1张图片

osg::Texture2D 贴纹理_第2张图片

 

转载于:https://www.cnblogs.com/herd/p/11107965.html

你可能感兴趣的:(osg::Texture2D 贴纹理)