osg学习(五十三)绘制纹理贴图Texture示例

Posted hankern

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了osg学习(五十三)绘制纹理贴图Texture示例相关的知识,希望对你有一定的参考价值。

效果图:

代码:

//osg纹理贴图
    osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
    osg::ref_ptr<osg::Geode> geode = new osg::Geode;
    geode->addDrawable(geom.get());
    osg::ref_ptr<osg::Vec3Array> vArr = new osg::Vec3Array;
    vArr->push_back(osg::Vec3(-5.0f,0.0f,-5.0f));
    vArr->push_back(osg::Vec3(5.0f,0.0f,-5.0f));
    vArr->push_back(osg::Vec3(5.0f,0.0f,5.0f));
    vArr->push_back(osg::Vec3(-5.0f,0.0f,5.0f));
    geom->setVertexArray(vArr.get());
    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));
    osg::ref_ptr<osg::Vec3Array> nArr = new osg::Vec3Array;
    nArr->push_back(osg::Vec3(0.0, 0.0, 1.0));
    geom->setNormalArray(nArr.get());
    geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
    osg::ref_ptr<osg::Vec4Array> cArr = new osg::Vec4Array;
    cArr->push_back(osg::Vec4(1.0,1.0,1.0,1.0));
    geom->setColorArray(cArr.get());
    geom->setColorBinding(osg::Geometry::BIND_OVERALL);
    osg::ref_ptr<osg::Vec2Array> tArr0 = new osg::Vec2Array;//创建一个 Vec2Array对象以保存纹理单元 0 的纹理坐标
    tArr0->push_back(osg::Vec2(0,0));
    tArr0->push_back(osg::Vec2(0,1));
    tArr0->push_back(osg::Vec2(1,1));
    tArr0->push_back(osg::Vec2(1,0));
    geom->setTexCoordArray(0,tArr0.get());
    osg::StateSet* ss = geode->getOrCreateStateSet();
    ss->setMode( GL_LIGHTING, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED );
    ss->setRenderingHint( osg::StateSet::TRANSPARENT_BIN );
    //std::string fileName("Images/blueFlowers.png");
    //std::string fileName("Images/clockface.JPG");
    std::string fileName("Images/reflect.rgb");
    osg::ref_ptr<osg::Image> image = osgDB::readImageFile( fileName );
    if (!image.valid())
    {
        osg::notify( osg::FATAL ) << "Unable to load data file Exiting." << std::endl;
        return;
    }
    osg::ref_ptr<osg::Texture2D> tex = new osg::Texture2D;
    tex->setImage(image.get());
    tex->setUnRefImageDataAfterApply( true );
    ss->setTextureAttributeAndModes( 0, tex.get() );
    osg::BlendFunc* bf = new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA );
    ss->setAttributeAndModes( bf );
    osg::AlphaFunc* af = new osg::AlphaFunc(osg::AlphaFunc::GREATER, 0.05f );
    ss->setAttributeAndModes( af );

以上是关于osg学习(五十三)绘制纹理贴图Texture示例的主要内容,如果未能解决你的问题,请参考以下文章

转载D3D中的Texture应用示例

osg学习(六十五)图片到纹理

osg学习(六十四)osg的纹理模式 纹理属性

(四)Unity纹理、贴图和材质

Unity3D纹理贴图 ( 纹理 Texture 简介 | 为 3D 模型设置纹理贴图 )

OpenGL学习——纹理