osg::Texture2D 贴纹理

Posted herd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了osg::Texture2D 贴纹理相关的知识,希望对你有一定的参考价值。

 

#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* createTextHUD()

    osg::ref_ptr<osg::Geode> geode1 = new osg::Geode;
    osg::ref_ptr<osgText::Text> text1 = new osgText::Text;
    osg::ref_ptr<osg::Camera> camera1 = new osg::Camera;

    osg::ref_ptr<osg::Geometry> 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<osg::Image> 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<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");
    osg::ref_ptr<osg::Node> 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 贴纹理的主要内容,如果未能解决你的问题,请参考以下文章

OpenGL入门之纹理Texture

全屏效果的opengl纹理坐标

openGL之API学习(二一二)固定管线的纹理

在硬件中高效实现 DXT1 纹理解压

以视频的纵横比在纹理视图上播放视频

opengl使用bmp纹理映射画不出东西