def setupBin5(rootNode, mirror,z): #// set up depth so all writing to depth goes to maximum depth. depth = osg.Depth(); depth.setFunction(osg.Depth.ALWAYS); stencil = osg.Stencil(); stencil.setFunction(osg.Stencil.EQUAL,1,4294967295); stencil.setOperation(osg.Stencil.KEEP, osg.Stencil.KEEP, osg.Stencil.ZERO); #// set up additive blending. trans = osg.BlendFunc(); trans.setFunction(osg.BlendFunc.ONE,osg.BlendFunc.ONE); statesetBin5 = createMirrorTexturedState("Images/tank.rgb"); statesetBin5.setRenderBinDetails(5,"RenderBin"); statesetBin5.setMode(osg.GL_CULL_FACE,osg.StateAttribute.OFF); statesetBin5.setAttributeAndModes(stencil,osg.StateAttribute.ON); statesetBin5.setAttributeAndModes(trans,osg.StateAttribute.ON); statesetBin5.setAttribute(depth); #// set up the mirror geode. geode = osg.Geode(); geode.addDrawable(mirror); geode.setStateSet(statesetBin5); rootNode.addChild(geode);
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
def createMirroredScene(model): # calculate where to place the mirror according to the # loaded models bounding sphere. bs = model.getBound() width_factor = 1.5 height_factor = 0.3 xMin = bs.center().x()-bs.radius()*width_factor xMax = bs.center().x()+bs.radius()*width_factor yMin = bs.center().y()-bs.radius()*width_factor yMax = bs.center().y()+bs.radius()*width_factor z = bs.center().z()-bs.radius()*height_factor # create a textured, transparent node at the appropriate place. mirror = createMirrorSurface(xMin,xMax,yMin,yMax,z) rootNode = osg.MatrixTransform() rootNode.setMatrix(osg.Matrix.rotate(osg.inDegrees(45.0),1.0,0.0,0.0)) # make sure that the global color mask exists. rootColorMask = osg.ColorMask() rootColorMask.setMask(True,True,True,True) # set up depth to be inherited by the rest of the scene unless # overrideen. this is overridden in bin 3. rootDepth = osg.Depth() rootDepth.setFunction(osg.Depth.LESS) rootDepth.setRange(0.0,1.0) rootStateSet = osg.StateSet() rootStateSet.setAttribute(rootColorMask) rootStateSet.setAttribute(rootDepth) rootNode.setStateSet(rootStateSet) # bin1 - set up the stencil values and depth for mirror. # set up the stencil ops so that the stencil buffer get set at # the mirror plane stencil = osg.Stencil() stencil.setFunction(osg.Stencil.ALWAYS,1,~0u) stencil.setOperation(osg.Stencil.KEEP, osg.Stencil.KEEP, osg.Stencil.REPLACE) # switch off the writing to the color bit planes. colorMask = osg.ColorMask() colorMask.setMask(False,False,False,False) statesetBin1 = osg.StateSet() statesetBin1.setRenderBinDetails(1,"RenderBin") statesetBin1.setMode(GL_CULL_FACE,osg.StateAttribute.OFF) statesetBin1.setAttributeAndModes(stencil,osg.StateAttribute.ON) statesetBin1.setAttribute(colorMask) # set up the mirror geode. geode = osg.Geode() geode.addDrawable(mirror) geode.setStateSet(statesetBin1) rootNode.addChild(geode) # bin one - draw scene without mirror or reflection, unset # stencil values where scene is infront of mirror and hence # occludes the mirror. stencil = osg.Stencil() stencil.setFunction(osg.Stencil.ALWAYS,0,~0u) stencil.setOperation(osg.Stencil.KEEP, osg.Stencil.KEEP, osg.Stencil.REPLACE) statesetBin2 = osg.StateSet() statesetBin2.setRenderBinDetails(2,"RenderBin") statesetBin2.setAttributeAndModes(stencil,osg.StateAttribute.ON) groupBin2 = osg.Group() groupBin2.setStateSet(statesetBin2) groupBin2.addChild(model) rootNode.addChild(groupBin2) # bin3 - set up the depth to the furthest depth value # set up the stencil ops so that only operator on this mirrors stencil value. stencil = osg.Stencil() stencil.setFunction(osg.Stencil.EQUAL,1,~0u) stencil.setOperation(osg.Stencil.KEEP, osg.Stencil.KEEP, osg.Stencil.KEEP) # switch off the writing to the color bit planes. colorMask = osg.ColorMask() colorMask.setMask(False,False,False,False) # set up depth so all writing to depth goes to maximum depth. depth = osg.Depth() depth.setFunction(osg.Depth.ALWAYS) depth.setRange(1.0,1.0) statesetBin3 = osg.StateSet() statesetBin3.setRenderBinDetails(3,"RenderBin") statesetBin3.setMode(GL_CULL_FACE,osg.StateAttribute.OFF) statesetBin3.setAttributeAndModes(stencil,osg.StateAttribute.ON) statesetBin3.setAttribute(colorMask) statesetBin3.setAttribute(depth) # set up the mirror geode. geode = osg.Geode() geode.addDrawable(mirror) geode.setStateSet(statesetBin3) rootNode.addChild(geode) # bin4 - draw the reflection. # now create the 'reflection' of the loaded model by applying # create a Transform which flips the loaded model about the z axis # relative to the mirror node, the loadedModel is added to the # Transform so now appears twice in the scene, but is shared so there # is negligable memory overhead. Also use an osg.StateSet # attached to the Transform to override the face culling on the subgraph # to prevert an 'inside' out view of the reflected model. # set up the stencil ops so that only operator on this mirrors stencil value. # this clip plane removes any of the scene which when mirror would # poke through the mirror. However, this clip plane should really # flip sides once the eye point goes to the back of the mirror... clipplane = osg.ClipPlane() clipplane.setClipPlane(0.0,0.0,-1.0,z) clipplane.setClipPlaneNum(0) clipNode = osg.ClipNode() clipNode.addClipPlane(clipplane) dstate = clipNode.getOrCreateStateSet() dstate.setRenderBinDetails(4,"RenderBin") dstate.setMode(GL_CULL_FACE,osg.StateAttribute.OVERRIDE|osg.StateAttribute.OFF) stencil = osg.Stencil() stencil.setFunction(osg.Stencil.EQUAL,1,~0u) stencil.setOperation(osg.Stencil.KEEP, osg.Stencil.KEEP, osg.Stencil.KEEP) dstate.setAttributeAndModes(stencil,osg.StateAttribute.ON) reverseMatrix = osg.MatrixTransform() reverseMatrix.setStateSet(dstate) reverseMatrix.preMult(osg.Matrix.translate(0.0,0.0,-z)* osg.Matrix.scale(1.0,1.0,-1.0)* osg.Matrix.translate(0.0,0.0,z)) reverseMatrix.addChild(model) clipNode.addChild(reverseMatrix) rootNode.addChild(clipNode) # bin5 - draw the textured mirror and blend it with the reflection. # set up depth so all writing to depth goes to maximum depth. depth = osg.Depth() depth.setFunction(osg.Depth.ALWAYS) stencil = osg.Stencil() stencil.setFunction(osg.Stencil.EQUAL,1,~0u) stencil.setOperation(osg.Stencil.KEEP, osg.Stencil.KEEP, osg.Stencil.ZERO) # set up additive blending. trans = osg.BlendFunc() trans.setFunction(osg.BlendFunc.ONE,osg.BlendFunc.ONE) statesetBin5 = createMirrorTexturedState("Images/tank.rgb") statesetBin5.setRenderBinDetails(5,"RenderBin") statesetBin5.setMode(GL_CULL_FACE,osg.StateAttribute.OFF) statesetBin5.setAttributeAndModes(stencil,osg.StateAttribute.ON) statesetBin5.setAttributeAndModes(trans,osg.StateAttribute.ON) statesetBin5.setAttribute(depth) # set up the mirror geode. geode = osg.Geode() geode.addDrawable(mirror) geode.setStateSet(statesetBin5) rootNode.addChild(geode)
def main(argv): # use an ArgumentParser object to manage the program arguments. arguments = osg.ArgumentParser(argv) # set up the usage document, in case we need to print out how to use this program. arguments.getApplicationUsage().setApplicationName(arguments.getApplicationName()) arguments.getApplicationUsage().setDescription(arguments.getApplicationName()+" example provides an interactive viewer for visualising point clouds..") arguments.getApplicationUsage().setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...") arguments.getApplicationUsage().addCommandLineOption("-h or --help","Display this information") arguments.getApplicationUsage().addCommandLineOption("--sprites","Point sprites.") arguments.getApplicationUsage().addCommandLineOption("--points","Sets the polygon mode to GL_POINT for front and back faces.") # construct the viewer. viewer = osgViewer.Viewer() shader = False while arguments.read("--shader") : shader = True # if user request help write it out to cout. if arguments.read("-h") or arguments.read("--help") : arguments.getApplicationUsage().write(std.cout) return 1 usePointSprites = False while arguments.read("--sprites") : usePointSprites = True forcePointMode = False while arguments.read("--points") : forcePointMode = True if arguments.argc()<=1 : arguments.getApplicationUsage().write(std.cout,osg.ApplicationUsage.COMMAND_LINE_OPTION) return 1 # read the scene from the list of file specified commandline args. loadedModel = osgDB.readNodeFiles(arguments) # if no model has been successfully loaded report failure. if not loadedModel : print arguments.getApplicationName(), ": No data loaded" return 1 # optimize the scene graph, remove redundant nodes and state etc. optimizer = osgUtil.Optimizer() optimizer.optimize(loadedModel) # set the scene to render viewer.setSceneData(loadedModel) stateset = loadedModel.getOrCreateStateSet() if usePointSprites : #/ Setup cool blending fn = osg.BlendFunc() stateset.setAttributeAndModes(fn, osg.StateAttribute.ON) #/ Setup the point sprites sprite = osg.PointSprite() stateset.setTextureAttributeAndModes(0, sprite, osg.StateAttribute.ON) #/ The texture for the sprites tex = osg.Texture2D() tex.setImage(osgDB.readImageFile("Images/particle.rgb")) stateset.setTextureAttributeAndModes(0, tex, osg.StateAttribute.ON) if forcePointMode : #/ Set polygon mode to GL_POINT pm = osg.PolygonMode( osg.PolygonMode.FRONT_AND_BACK, osg.PolygonMode.POINT ) stateset.setAttributeAndModes( pm, osg.StateAttribute.ON | osg.StateAttribute.OVERRIDE) # register the handler for modifying the point size viewer.addEventHandler(KeyboardEventHandler(viewer.getCamera().getOrCreateStateSet())) if shader : stateset = loadedModel.getOrCreateStateSet() #################################/ # vertex shader using just Vec4 coefficients char vertexShaderSource[] = "void main(void) \n" " \n" "\n" " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex\n" "\n" program = osg.Program() stateset.setAttribute(program) vertex_shader = osg.Shader(osg.Shader.VERTEX, vertexShaderSource) program.addShader(vertex_shader) #if 0 ################################# # fragment shader # char fragmentShaderSource[] = "void main(void) \n" " \n" " gl_FragColor = gl_Color \n" "\n"
_compositeCamera.setClearMask(0) _compositeCamera.setReferenceFrame(osg.Transform.ABSOLUTE_RF) _compositeCamera.setViewMatrix(osg.Matrix()) _compositeCamera.setProjectionMatrix(osg.Matrix.ortho2D(0, 1, 0, 1)) _compositeCamera.setCullCallback(CullCallback(0, _texWidth, _texHeight, 0)) stateSet = _compositeCamera.getOrCreateStateSet() stateSet.setRenderBinDetails(100, "TraversalOrderBin", osg.StateSet.OVERRIDE_RENDERBIN_DETAILS) _root.addChild(_compositeCamera) # solid geometry is blended first, transparency layers are blended in back to front order. # this order is achieved by rendering using a TraversalOrderBin (see camera stateset). for (unsigned int i = _numPasses i > 0 --i) geode = createQuad(i%_numPasses, numTiles) stateSet = geode.getOrCreateStateSet() stateSet.setTextureAttributeAndModes(0, _colorTextures[i%_numPasses], osg.StateAttribute.ON) stateSet.setAttribute(osg.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA), osg.StateAttribute.ON) stateSet.setMode(GL_BLEND, osg.StateAttribute.ON) stateSet.setMode(GL_LIGHTING, osg.StateAttribute.OFF) depth = osg.Depth() depth.setWriteMask( False ) stateSet.setAttributeAndModes( depth, osg.StateAttribute.ON ) stateSet.setMode(GL_DEPTH_TEST, osg.StateAttribute.OFF) _compositeCamera.addChild(geode) DepthPeeling.DepthPeeling(unsigned int width, unsigned int height) : _numPasses(9), _texUnit(2), _texWidth(width), _texHeight(height), _showAllLayers(False), _offsetValue(8),