def createTeapot(): geode = osg.Geode() # add the teapot to the geode. geode.addDrawable( Teapot )() # add a reflection map to the teapot. image = osgDB.readImageFile("Images/reflect.rgb") if image : texture = osg.Texture2D() texture.setImage(image) texgen = osg.TexGen() texgen.setMode(osg.TexGen.SPHERE_MAP) stateset = osg.StateSet() stateset.setTextureAttributeAndModes(0,texture,osg.StateAttribute.ON) stateset.setTextureAttributeAndModes(0,texgen,osg.StateAttribute.ON) geode.setStateSet(stateset) return geode
def main(argv): # use an ArgumentParser object to manage the program arguments. arguments = osg.ArgumentParser(argv) # construct the viewer. viewer = osgViewer.Viewer() # load the nodes from the commandline arguments. rootnode = osgDB.readNodeFiles(arguments) # if not loaded assume no arguments passed in, try use default mode instead. if not rootnode : rootnode = osgDB.readNodeFile("cessnafire.osgt") if not rootnode : osg.notify(osg.NOTICE), "Please specify a model filename on the command line." return 1 image = osgDB.readImageFile("Images/reflect.rgb") if image : texture = osg.Texture2D() texture.setImage(image) texgen = osg.TexGen() texgen.setMode(osg.TexGen.SPHERE_MAP) texenv = osg.TexEnv() texenv.setMode(osg.TexEnv.BLEND) texenv.setColor(osg.Vec4(0.3,0.3,0.3,0.3)) stateset = osg.StateSet() stateset.setTextureAttributeAndModes(1,texture,osg.StateAttribute.ON) stateset.setTextureAttributeAndModes(1,texgen,osg.StateAttribute.ON) stateset.setTextureAttribute(1,texenv) rootnode.setStateSet(stateset) else: osg.notify(osg.NOTICE), "unable to load reflect map, model will not be mutlitextured" # run optimization over the scene graph optimzer = osgUtil.Optimizer() optimzer.optimize(rootnode) # add a viewport to the viewer and attach the scene graph. viewer.setSceneData( rootnode ) # create the windows and run the threads. viewer.realize() for(unsigned int contextID = 0 contextID<osg.DisplaySettings.instance().getMaxNumberOfGraphicsContexts() ++contextID) textExt = osg.Texture.getExtensions(contextID,False) if textExt : if not textExt.isMultiTexturingSupported() : print "Warning: multi-texturing not supported by OpenGL drivers, unable to run application." return 1
def createSkyBox(): stateset = osg.StateSet() te = osg.TexEnv() te.setMode(osg.TexEnv.REPLACE) stateset.setTextureAttributeAndModes(0, te, osg.StateAttribute.ON) tg = osg.TexGen() tg.setMode(osg.TexGen.NORMAL_MAP) stateset.setTextureAttributeAndModes(0, tg, osg.StateAttribute.ON) tm = osg.TexMat() stateset.setTextureAttribute(0, tm) skymap = readCubeMap() stateset.setTextureAttributeAndModes(0, skymap, osg.StateAttribute.ON) stateset.setMode( GL_LIGHTING, osg.StateAttribute.OFF ) stateset.setMode( GL_CULL_FACE, osg.StateAttribute.OFF ) # clear the depth to the far plane. depth = osg.Depth() depth.setFunction(osg.Depth.ALWAYS) depth.setRange(1.0,1.0) stateset.setAttributeAndModes(depth, osg.StateAttribute.ON ) stateset.setRenderBinDetails(-1,"RenderBin") drawable = osg.ShapeDrawable(osg.Sphere(osg.Vec3(0.0,0.0,0.0),1)) geode = osg.Geode() geode.setCullingActive(False) geode.setStateSet( stateset ) geode.addDrawable(drawable) transform = MoveEarthySkyWithEyePointTransform() transform.setCullingActive(False) transform.addChild(geode) clearNode = osg.ClearNode() # clearNode.setRequiresClear(False) clearNode.setCullCallback(TexMatCallback(*tm)) clearNode.addChild(transform) return clearNode
def create_specular_highlights(node): ss = node.getOrCreateStateSet() # create and setup the texture object 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) # generate the six highlight map images (light direction = [1, 1, -1]) mapgen = osgUtil.HighlightMapGenerator( osg.Vec3(1, 1, -1), # light direction osg.Vec4(1, 0.9, 0.8, 1), # light color 8) # specular exponent mapgen.generateMap() # assign the six images to the texture object tcm.setImage(osg.TextureCubeMap.POSITIVE_X, mapgen.getImage(osg.TextureCubeMap.POSITIVE_X)) tcm.setImage(osg.TextureCubeMap.NEGATIVE_X, mapgen.getImage(osg.TextureCubeMap.NEGATIVE_X)) tcm.setImage(osg.TextureCubeMap.POSITIVE_Y, mapgen.getImage(osg.TextureCubeMap.POSITIVE_Y)) tcm.setImage(osg.TextureCubeMap.NEGATIVE_Y, mapgen.getImage(osg.TextureCubeMap.NEGATIVE_Y)) tcm.setImage(osg.TextureCubeMap.POSITIVE_Z, mapgen.getImage(osg.TextureCubeMap.POSITIVE_Z)) tcm.setImage(osg.TextureCubeMap.NEGATIVE_Z, mapgen.getImage(osg.TextureCubeMap.NEGATIVE_Z)) # enable texturing, replacing any textures in the subgraphs ss.setTextureAttributeAndModes(0, tcm, osg.StateAttribute.OVERRIDE | osg.StateAttribute.ON) # texture coordinate generation tg = osg.TexGen() tg.setMode(osg.TexGen.REFLECTION_MAP) ss.setTextureAttributeAndModes(0, tg, osg.StateAttribute.OVERRIDE | osg.StateAttribute.ON) # use TexEnvCombine to add the highlights to the original lighting te = osg.TexEnvCombine() te.setCombine_RGB(osg.TexEnvCombine.ADD) te.setSource0_RGB(osg.TexEnvCombine.TEXTURE) te.setOperand0_RGB(osg.TexEnvCombine.SRC_COLOR) te.setSource1_RGB(osg.TexEnvCombine.PRIMARY_COLOR) te.setOperand1_RGB(osg.TexEnvCombine.SRC_COLOR) ss.setTextureAttributeAndModes(0, te, osg.StateAttribute.OVERRIDE | osg.StateAttribute.ON)
# note, well set the filtering up so that mip mapping is disabled, # gluBuild3DMipsmaps doesn't do a very good job of handled the # inbalanced dimensions of the 256x256x4 texture. texture3D = osg.Texture3D() texture3D.setFilter(osg.Texture3D.MIN_FILTER,osg.Texture3D.LINEAR) texture3D.setFilter(osg.Texture3D.MAG_FILTER,osg.Texture3D.LINEAR) texture3D.setWrap(osg.Texture3D.WRAP_R,osg.Texture3D.REPEAT) texture3D.setImage(image_3d) # create a texgen to generate a R texture coordinate, the geometry # itself will supply the S T texture coordinates. # in the animateStateSet callback well alter this R value to # move the texture through the 3d texture, 3d texture filtering # will do the blending for us. texgen = osg.TexGen() texgen.setMode(osg.TexGen.OBJECT_LINEAR) texgen.setPlane(osg.TexGen.R, osg.Plane(0.0,0.0,0.0,0.2)) # create the StateSet to store the texture data stateset = osg.StateSet() stateset.setTextureMode(0,GL_TEXTURE_GEN_R,osg.StateAttribute.ON) stateset.setTextureAttribute(0,texgen) stateset.setTextureAttributeAndModes(0,texture3D,osg.StateAttribute.ON) return stateset class UpdateStateCallback (osg.NodeCallback) : UpdateStateCallback()