網絡上的代碼, 讀的是PNG 與 jpg ,但我這裏讀不了node
研究了一下, 發現能讀rbg文件網絡
這裏是改變模型的貼圖, 還看不出是什麼原理code
#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(); }