基于osg的python三维程序开发(六)------纹理

以简单数字地球为例, 演示posg 中纹理的用法。

首先需要读取图像文件作为纹理

img = osgDB.readImageFile(filename)

然后,声明一个2D的纹理对象

texture = osg.Texture2D()
  texture.setImage(img)

创建一个叶子节点

 geode = osg.Geode()
 sphere = osg.Sphere((0,0,0), osg.WGS_84_RADIUS_POLAR)
 drawb = osg.ShapeDrawable(sphere)
 geode.addDrawable(drawb)

将纹理设置到叶子节点的属性

geode.getOrCreateStateSet().setTextureAttributeAndModes( 0, texture)

完整代码如下:

def createEarth(filename):
    texture = osg.Texture2D()
    img = osgDB.readImageFile(filename)
    texture.setImage(img)
    geode = osg.Geode()
    sphere = osg.Sphere((0,0,0), osg.WGS_84_RADIUS_POLAR)
    drawb = osg.ShapeDrawable(sphere)
    geode.addDrawable(drawb)
    geode.getOrCreateStateSet().setTextureAttributeAndModes( 0, texture)
    return geode



def main():
    viewer = osgViewer.Viewer()
    viewer.setUpViewInWindow(600, 100, 800, 600)
    root = osg.Group()
    root.addChild(createEarth("./land_shallow_topo_2048.jpg") )
    viewer.setSceneData(root)
    return viewer.run()

if __name__ == '__main__':
    main()

 

上一篇:Vue集成报表系列教程4


下一篇:一个强大的3D模型查看器:Open 3D Model Viewer