class KeyboardEventHandler (osgGA.GUIEventHandler) : KeyboardEventHandler(osg.StateSet* stateset): _stateset(stateset) _point = osg.Point() _point.setDistanceAttenuation(osg.Vec3(0.0,0.0000,0.05)) _stateset.setAttribute(_point)
class SomePoints (osg.Geometry) : SomePoints() cAry = osg.Vec4Array() setColorArray( cAry, osg.Array.BIND_OVERALL ) cAry.push_back( osg.Vec4(1,1,1,1) ) vAry = osg.Vec3Array() setVertexArray( vAry ) vAry.push_back( osg.Vec3(0,0,0) ) vAry.push_back( osg.Vec3(0,1,0) ) vAry.push_back( osg.Vec3(1,0,0) ) vAry.push_back( osg.Vec3(1,1,0) ) vAry.push_back( osg.Vec3(0,0,1) ) vAry.push_back( osg.Vec3(0,1,1) ) vAry.push_back( osg.Vec3(1,0,1) ) vAry.push_back( osg.Vec3(1,1,1) ) addPrimitiveSet( osg.DrawArrays( GL_POINTS, 0, vAry.size() ) ) sset = getOrCreateStateSet() sset.setMode( GL_LIGHTING, osg.StateAttribute.OFF ) # if things go wrong, fall back to big points p = osg.Point() p.setSize(6) sset.setAttribute( p ) #ifdef ENABLE_GLSL sset.setAttribute( createShader() ) # a generic cyclic animation value u_anim1 = osg.Uniform*( osg.Uniform( "u_anim1", 0.0 ) ) u_anim1.setUpdateCallback( SineAnimation( 4, 0.5, 0.5 ) ) sset.addUniform( u_anim1 )
def makeStateSet(size): set = osg.StateSet() #/ Setup cool blending set.setMode(GL_BLEND, osg.StateAttribute.ON) fn = osg.BlendFunc() fn.setFunction(osg.BlendFunc.SRC_ALPHA, osg.BlendFunc.DST_ALPHA) set.setAttributeAndModes(fn, osg.StateAttribute.ON) #/ Setup the point sprites sprite = osg.PointSprite() set.setTextureAttributeAndModes(0, sprite, osg.StateAttribute.ON) #/ Give some size to the points to be able to see the sprite point = osg.Point() point.setSize(size) set.setAttribute(point) #/ Disable depth test to avoid sort problems and Lighting set.setMode(GL_DEPTH_TEST, osg.StateAttribute.OFF) set.setMode(GL_LIGHTING, osg.StateAttribute.OFF) #/ The texture for the sprites tex = osg.Texture2D() tex.setImage(osgDB.readImageFile("Images/particle.rgb")) set.setTextureAttributeAndModes(0, tex, osg.StateAttribute.ON) return set
animationPath.insert(6.0,osg.AnimationPath.ControlPoint(bb.corner(6))) animationPath.insert(7.0,osg.AnimationPath.ControlPoint(bb.corner(7))) animationPath.insert(8.0,osg.AnimationPath.ControlPoint(bb.corner(0))) animationPath.setLoopMode(osg.AnimationPath.SWING) mt.setUpdateCallback(osg.AnimationPathCallback(animationPath)) # create marker for point light. marker = osg.Geometry() vertices = osg.Vec3Array() vertices.push_back(osg.Vec3(0.0,0.0,0.0)) marker.setVertexArray(vertices) marker.addPrimitiveSet(osg.DrawArrays(GL_POINTS,0,1)) stateset = osg.StateSet() point = osg.Point() point.setSize(4.0) stateset.setAttribute(point) marker.setStateSet(stateset) markerGeode = osg.Geode() markerGeode.addDrawable(marker) mt.addChild(lightS2) mt.addChild(markerGeode) lightGroup.addChild(mt) return lightGroup
def main(argv): arguments = osg.ArgumentParser( argc, argv ) textureFile = str("Images/smoke.rgb") while arguments.read("--texture", textureFile) : pointSize = 20.0 while arguments.read("--point", pointSize) : visibilityDistance = -1.0 while arguments.read("--visibility", visibilityDistance) : customShape = False while arguments.read("--enable-custom") : customShape = True useShaders = True while arguments.read("--disable-shaders") : useShaders = False #** # Customize particle template and system attributes # ** ps = osgParticle.ParticleSystem() ps.getDefaultParticleTemplate().setLifeTime( 5.0 ) if customShape : # osgParticle now supports making use of customized drawables. The draw() method will be executed # and display lists will be called for each particle. It is always a huge consumption of memory, and # hardly to use shaders to render them, so please be careful using this feature. ps.getDefaultParticleTemplate().setShape( osgParticle.Particle.USER ) ps.getDefaultParticleTemplate().setDrawable( osg.ShapeDrawable(osg.Box(osg.Vec3(), 1.0)) ) useShaders = False else: # The shader only supports rendering points at present. ps.getDefaultParticleTemplate().setShape( osgParticle.Particle.POINT ) # Set the visibility distance of particles, due to their Z-value in the eye coordinates. # Particles that are out of the distance (or behind the eye) will not be rendered. ps.setVisibilityDistance( visibilityDistance ) if useShaders : # Set using local GLSL shaders to render particles. # At present, this is slightly efficient than ordinary methods. The bottlenack here seems to be the cull # traversal time. Operators go through the particle list again and again... ps.setDefaultAttributesUsingShaders( textureFile, True, 0 ) else: # The default methods uses glBegin()/glEnd() pairs. Fortunately the GLBeginEndAdapter does improve the # process, which mimics the immediate mode with glDrawArrays(). ps.setDefaultAttributes( textureFile, True, False, 0 ) # Without the help of shaders, we have to sort particles to make the visibility distance work. Sorting is # also useful in rendering transparent particles in back-to-front order. if visibilityDistance>0.0 : ps.setSortMode( osgParticle.ParticleSystem.SORT_BACK_TO_FRONT ) # At last, to make the point sprite work, we have to set the points size and the sprite attribute. stateset = ps.getOrCreateStateSet() stateset.setAttribute( osg.Point(pointSize) ) stateset.setTextureAttributeAndModes( 0, osg.PointSprite, osg.StateAttribute.ON )() #** # Construct other particle system elements, including the emitter and program # ** emitter = osgParticle.ModularEmitter() emitter.setParticleSystem( ps ) program = osgParticle.ModularProgram() program.setParticleSystem( ps ) createFountainEffect( emitter, program ) #** # Add the entire particle system to the scene graph # ** parent = osg.MatrixTransform() parent.addChild( emitter ) parent.addChild( program ) # The updater can receive particle systems as child drawables now. The addParticleSystem() method # is still usable, with which we should define another geode to contain a particle system. updater = osgParticle.ParticleSystemUpdater() #updater.addDrawable( ps ) root = osg.Group() root.addChild( parent ) root.addChild( updater ) # FIXME 2010.9.19: the updater can't be a drawable otehrwise the ParticleEffect will not work properly. why? updater.addParticleSystem( ps ) geode = osg.Geode() geode.addDrawable( ps ) root.addChild( geode ) #** # Start the viewer # ** viewer = osgViewer.Viewer() viewer.addEventHandler( osgGA.StateSetManipulator(viewer.getCamera().getOrCreateStateSet()) ) viewer.addEventHandler( osgViewer.StatsHandler )() viewer.addEventHandler( osgViewer.WindowSizeHandler )() viewer.setSceneData( root ) viewer.setCameraManipulator( osgGA.TrackballManipulator )() # A floating error of delta-time should be explained here: # The particles emitter, program and updater all use a 'dt' to compute the time value in every frame. # Because the 'dt' is a double value, it is not suitable to keep three copies of it seperately, which # is the previous implementation. The small error makes some opeartors unable to work correctly, e.g. # the BounceOperator. # Now we make use of the getDeltaTime() of ParticleSystem to maintain and dispatch the delta time. But.. # it is not the best solution so far, since there are still very few particles acting unexpectedly. return viewer.run() # FIXME 2010.9.19: At present, getDeltaTime() is not used and the implementations in the updater and processors still # use a (t - _t0) as the delta time, which is of course causing floating errors. ParticleEffect will not work if we # replace the delta time with getDeltaTime()... Need to find a solution. if __name__ == "__main__": main(sys.argv)