Home >> Python >> PyOSG Sixth


■PyOSGでのテキスチャ

☆2Dテキスチャの添付

	
import osg, osgViewer, osgDB
import sys

root = osg.Group( )
osgDB.setLibraryFilePathList(sys.path)

# プリミティブ図形を配置
box = osg.Box( osg.Vec3(0, 0, 0), 1, 1, 1 )
bb = osg.ShapeDrawable( box )
gg = osg.Geode( )
gg.addDrawable( bb )

# テキスチャの読込みと設定
img = osgDB.readImageFile( "texture.jpg" )
tex = osg.Texture2D( )
tex.setImage( img )
ss = gg.getOrCreateStateSet( )
ss.setTextureAttributeAndModes( 0, tex, osg.StateAttribute.ON )
root.addChild( gg )

# ビューワの設定
viewer = osgViewer.Viewer( )
viewer.setSceneData( root )
viewer.addEventHandler( osgViewer.WindowSizeHandler( ) )
viewer.addEventHandler( osgViewer.StatsHandler( ) )
viewer.run( )

	

☆テキスチャのキューブマップ

	
import osg, osgViewer, osgDB
import sys

root = osg.Group( )
osgDB.setLibraryFilePathList(sys.path)

# プリミティブ図形
box = osg.Box( osg.Vec3(0, 0, 0), 1, 1, 1 )
bb = osg.ShapeDrawable( box )
gg = osg.Geode( )
gg.addDrawable( bb )

# テキスチャのキューブマップ
tcm = osg.TextureCubeMap( )

# ラップの仕方の設定
tcm.setWrap( osg.Texture.WRAP_S, osg.Texture.CLAMP )
tcm.setWrap( osg.Texture.WRAP_T, osg.Texture.CLAMP )
tcm.setWrap( osg.Texture.WRAP_R, osg.Texture.CLAMP )

tcm.setFilter( osg.Texture.MIN_FILTER, osg.Texture.LINEAR_MIPMAP_LINEAR )
tcm.setFilter( osg.Texture.MAG_FILTER, osg.Texture.LINEAR )

# 画像の読込み(6面)と設定
img1 = osgDB.readImageFile( "back1.jpg" )
img2 = osgDB.readImageFile( "back2.jpg" )
img3 = osgDB.readImageFile( "back3.jpg" )
img4 = osgDB.readImageFile( "back4.jpg" )
img5 = osgDB.readImageFile( "back5.jpg" )
img6 = osgDB.readImageFile( "back6.jpg" )

tcm.setImage( osg.TextureCubeMap.POSITIVE_X, img1 )
tcm.setImage( osg.TextureCubeMap.NEGATIVE_X, img2 )
tcm.setImage( osg.TextureCubeMap.POSITIVE_Y, img3 )
tcm.setImage( osg.TextureCubeMap.NEGATIVE_Y, img4 )
tcm.setImage( osg.TextureCubeMap.POSITIVE_Z, img5 )
tcm.setImage( osg.TextureCubeMap.NEGATIVE_Z, img6 )

ss = gg.getOrCreateStateSet( )

# テクスチャ環境の設定
te = osg.TexEnv( )
te.setMode( osg.TexEnv.REPLACE )
ss.setTextureAttributeAndModes( 0, te, osg.StateAttribute.ON )

# テクスチャ生成方法の設定(必須)
tg = osg.TexGen( )
tg.setMode( osg.TexGen.OBJECT_LINEAR )
# 4つのオプションが使える:OBJECT_LINEAR, EYE_LINEAR, SPHERE_MAP, REFLECTION_MAP
ss.setTextureAttributeAndModes( 0, tg, osg.StateAttribute.ON )

# テクスチャマットの設定
tm = osg.TexMat( )
ss.setTextureAttribute( 0, tm )
ss.setTextureAttributeAndModes( 0, tcm, osg.StateAttribute.ON )

root.addChild( gg )

# ビューワの設定と実行
viewer = osgViewer.Viewer()
viewer.setSceneData( root )
viewer.addEventHandler(osgViewer.WindowSizeHandler())
viewer.addEventHandler(osgViewer.StatsHandler())

viewer.run()

	
PyOSG Fifth Python