在osg的图形上贴一张纹理图片
哈哈哈,继今天早上完成的成果,又接着进行了修改完善。
先上图:
(在球上贴路费的照片都贴胖了...)
接着贴代码了
继续上篇的代码中填上一个createTexture2DState函数:
1 osg::ref_ptr<osg::StateSet> createTexture2DState(osg::ref_ptr<osg::Image>image) 2 { 3 //创建状态集对象 4 osg::ref_ptr<osg::StateSet>stateset = new osg::StateSet(); 5 //创建二维纹理对象 6 osg::ref_ptr<osg::Texture2D>texture = new osg::Texture2D(); 7 texture->setDataVariance(osg::Object::DYNAMIC); 8 //设置贴图 9 texture->setImage(image.get()); 10 texture->setFilter(osg:: Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR); 11 texture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); 12 texture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT); 13 texture->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT); 14 15 stateset->setTextureAttributeAndModes(0, texture.get(), osg::StateAttribute::ON); 16 return stateset.get(); 17 }
main函数是这样的:
1 int main(int argc, char **argv) 2 { 3 QApplication app(argc, argv); 4 osg::Camera* camera = createCamera(50, 50, 640, 480); 5 osg::ref_ptr<osg::Image>image = osgDB::readImageFile("Images/timg.jpg"); 6 osg::ref_ptr<osg::StateSet>stateset = new osg::StateSet(); 7 stateset = createTexture2DState(image.get()); 8 osg::Group * scene = new osg::Group(); 9 scene->setStateSet(stateset.get()); 10 scene->addChild(createShape().get()); 11 ViewerWidget* widget = new ViewerWidget(camera, scene); 12 widget->setGeometry(100, 100, 800, 600); 13 widget->show(); 14 return app.exec(); 15 }
剩下的不用改就ok啦!
附:关于图片的路径我的路径是(D:\osg3.4.0\data\Images);
按照自己的data文件夹设置就可以啦!