osg change model texture

The code on the Internet reads PNG and jpg, but I can't read it here

After researching it, I found that I can read rbg files

Here is the texture for changing the model, but I can't see the principle

 

#include <osgDB/ReadFile>
#include <osgViewer/Viewer> 
#include <osg/Node>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/Group>
#include <osg/Texture2D>
#include <osg/TexGen>
#include <osg/TexEnv>

void main() 
{ 
    osgViewer::Viewer viewer;
    osg::ref_ptr<osg::Group> root=new osg::Group();
    osg::ref_ptr<osg::Node>  node=osgDB::readNodeFile("cow.osg");

    osg::ref_ptr<osg::Image> image=osgDB::readImageFile("Images\\lz.rgb");// osg能读取rgb格式 , data文件夹里带有这张图

    if (image.get())
    {
        osg::ref_ptr<osg::Texture2D> texture=new osg::Texture2D();
        texture->setImage(image.get());

        //设置自动生成纹理坐标
        osg::ref_ptr<osg::TexGen> texgen=new osg::TexGen();
        texgen->setMode(osg::TexGen::SPHERE_MAP);

        //设置纹理环境,模式为BLEND
        osg::ref_ptr<osg::TexEnv> texenv=new osg::TexEnv;
		texenv->setMode(osg::TexEnv::Mode::BLEND);// ADD
        texenv->setColor(osg::Vec4(0.6,0.6,0.6,0.0));

        //启动单元一自动生成纹理坐标,并使用纹理
        osg::ref_ptr<osg::StateSet> state=new osg::StateSet;
        state->setTextureAttributeAndModes(1,texture.get(),osg::StateAttribute::ON);
        state->setTextureAttributeAndModes(1,texgen.get(),osg::StateAttribute::ON);
        state->setTextureAttribute(0,texenv.get());

        node->setStateSet(state.get());

    }
	else
	{
		osg::notify( osg::FATAL ) << "Unable to load data file Exiting." << std::endl;
	}
    root->addChild(node.get());
    viewer.setSceneData(root.get()); 
    viewer.realize(); 
    viewer.run();
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324440617&siteId=291194637