class OcclusionQueryVisitor (osg.NodeVisitor) : OcclusionQueryVisitor() virtual ~OcclusionQueryVisitor() # Specify the vertex count threshold for performing occlusion # query tests. Nodes in the scene graph whose total child geometry # contains fewer vertices than the specified threshold will # never be tested, just drawn. (In fact, they will br treated as # potential occluders and rendered first in front-to-back order.) setOccluderThreshold = void( int vertices ) int getOccluderThreshold() apply = virtual void( osg.OcclusionQueryNode oqn ) apply = virtual void( osg.Group group ) apply = virtual void( osg.Geode geode ) addOQN = void( osg.Node node ) # When an OQR creates all OQNs and each OQN shares the same OQC, # these methods are used to uniquely name all OQNs. Handy # for debugging. getNextOQNName = str() def getNameIdx(): return _nameIdx _state = osg.StateSet() _debugState = osg.StateSet() _nameIdx = unsigned int() _occluderThreshold = int()
def createState(): # read 4 2d images image_0 = osgDB.readImageFile("Images/lz.rgb") image_1 = osgDB.readImageFile("Images/reflect.rgb") image_2 = osgDB.readImageFile("Images/tank.rgb") image_3 = osgDB.readImageFile("Images/skymap.jpg") if not image_0 or not image_1 or not image_2 or not image_3 : print "Warning: could not open files." return osg.StateSet() if image_0.getPixelFormat() not =image_1.getPixelFormat() or image_0.getPixelFormat() not =image_2.getPixelFormat() or image_0.getPixelFormat() not =image_3.getPixelFormat() : print "Warning: image pixel formats not compatible." return osg.StateSet()
def build_world(root): terrainGeode = osg.Geode() # create terrain stateset = osg.StateSet() image = osgDB.readImageFile("Images/lz.rgb") if image : texture = osg.Texture2D() texture.setImage(image) stateset.setTextureAttributeAndModes(0,texture,osg.StateAttribute.ON) terrainGeode.setStateSet( stateset ) size = 1000 # 10km scale = size/39.0 # 10km z_scale = scale*3.0 grid = osg.HeightField() grid.allocate(38,39) grid.setXInterval(scale) grid.setYInterval(scale) for(unsigned int r=0r<39++r) for(unsigned int c=0c<38++c) grid.setHeight(c,r,z_scale*vertex[r+c*39][2])
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 createSpotLightDecoratorState(lightNum, textureUnit): stateset = osg.StateSet() stateset.setMode(GL_LIGHT0+lightNum, osg.StateAttribute.ON) centerColour = osg.Vec4(1.0,1.0,1.0,1.0) ambientColour = osg.Vec4(0.05,0.05,0.05,1.0) # set up spot light texture texture = osg.Texture2D() texture.setImage(createSpotLightImage(centerColour, ambientColour, 64, 1.0)) texture.setBorderColor(osg.Vec4(ambientColour)) texture.setWrap(osg.Texture.WRAP_S,osg.Texture.CLAMP_TO_BORDER) texture.setWrap(osg.Texture.WRAP_T,osg.Texture.CLAMP_TO_BORDER) texture.setWrap(osg.Texture.WRAP_R,osg.Texture.CLAMP_TO_BORDER) stateset.setTextureAttributeAndModes(textureUnit, texture, osg.StateAttribute.ON) # set up tex gens stateset.setTextureMode(textureUnit, GL_TEXTURE_GEN_S, osg.StateAttribute.ON) stateset.setTextureMode(textureUnit, GL_TEXTURE_GEN_T, osg.StateAttribute.ON) stateset.setTextureMode(textureUnit, GL_TEXTURE_GEN_R, osg.StateAttribute.ON) stateset.setTextureMode(textureUnit, GL_TEXTURE_GEN_Q, osg.StateAttribute.ON) return stateset
def setupBin3(rootNode, mirror,z): #// set up the stencil ops so that only operator on this mirrors stencil value. stencil = osg.Stencil() stencil.setFunction(osg.Stencil.EQUAL,1,4294967295); 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(osg.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);
def test_create3DText(center, radius): geode = osg.Geode() characterSize = radius*0.2 characterDepth = characterSize*0.2 pos = osg.Vec3(center.x()-radius*.5,center.y()-radius*.5,center.z()-radius*.5) #define SHOW_INTESECTION_CEASH #ifdef SHOW_INTESECTION_CEASH text3 = osgText.Text3D() text3.setFont("fonts/dirtydoz.ttf") text3.setCharacterSize(characterSize) text3.setCharacterDepth(characterDepth) text3.setPosition(pos) text3.setDrawMode(osgText.Text3D.TEXT | osgText.Text3D.BOUNDINGBOX) text3.setAxisAlignment(osgText.Text3D.XZ_PLANE) text3.setText("CRAS H") #intersection crash geode.addDrawable(text3) #else: text7 = osgText.Text3D() text7.setFont("fonts/times.ttf") text7.setCharacterSize(characterSize) text7.setCharacterDepth(characterSize*2.2) text7.setPosition(center - osg.Vec3(0.0, 0.0, 0.6)) text7.setDrawMode(osgText.Text3D.TEXT | osgText.Text3D.BOUNDINGBOX) text7.setAxisAlignment(osgText.Text3D.SCREEN) text7.setCharacterSizeMode(osgText.Text3D.OBJECT_COORDS) text7.setText("ABCDE") #wrong intersection geode.addDrawable(text7) #endif shape = osg.ShapeDrawable(osg.Sphere(center,characterSize*0.2)) shape.getOrCreateStateSet().setMode(GL_LIGHTING,osg.StateAttribute.ON) geode.addDrawable(shape) rootNode = osg.Group() rootNode.addChild(geode) #define SHOW_WRONG_NORMAL #ifdef SHOW_WRONG_NORMAL front = osg.Material() # front.setAlpha(osg.Material.FRONT_AND_BACK,1) front.setAmbient(osg.Material.FRONT_AND_BACK,osg.Vec4(0.2,0.2,0.2,1.0)) front.setDiffuse(osg.Material.FRONT_AND_BACK,osg.Vec4(.0,.0,1.0,1.0)) rootNode.getOrCreateStateSet().setAttributeAndModes(front) #else: stateset = osg.StateSet() #Show wireframe polymode = osg.PolygonMode() polymode.setMode(osg.PolygonMode.FRONT_AND_BACK,osg.PolygonMode.LINE) stateset.setAttributeAndModes(polymode,osg.StateAttribute.OVERRIDE|osg.StateAttribute.ON) rootNode.setStateSet(stateset) #endif return rootNode
def createBase(center, radius): geode = osg.Geode() # set up the texture of the base. stateset = osg.StateSet() image = osgDB.readImageFile("Images/lz.rgb") if image : texture = osg.Texture2D() texture.setImage(image) stateset.setTextureAttributeAndModes(0,texture,osg.StateAttribute.ON) geode.setStateSet( stateset ) grid = osg.HeightField() grid.allocate(38,39) grid.setOrigin(center+osg.Vec3(-radius,-radius,0.0)) grid.setXInterval(radius*2.0/(float)(38-1)) grid.setYInterval(radius*2.0/(float)(39-1)) minHeight = FLT_MAX maxHeight = -FLT_MAX r = unsigned int() for(r=0r<39++r) for(unsigned int c=0c<38++c) h = vertex[r+c*39][2] if h>maxHeight : maxHeight=h if h<minHeight : minHeight=h
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 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 createMirrorTexturedState(filename): dstate = osg.StateSet(); dstate.setMode(osg.GL_CULL_FACE,osg.StateAttribute.OFF|osg.StateAttribute.PROTECTED); #// set up the texture. image = osgDB.readImageFile(filename); if (image): texture = osg.Texture2D(); texture.setImage(image); dstate.setTextureAttributeAndModes(0,texture,osg.StateAttribute.ON|osg.StateAttribute.PROTECTED); return dstate;
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().setDescription(arguments.getApplicationName()+" is the example which demonstrates how to use glBlendEquation for mixing rendered scene and the frame-buffer.") arguments.getApplicationUsage().setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...") arguments.getApplicationUsage().addCommandLineOption("-h or --help","Display this information") # construct the viewer. viewer = osgViewer.Viewer() # load the nodes from the commandline arguments. loadedModel = osgDB.readNodeFiles(arguments) # if not loaded assume no arguments passed in, try use default mode instead. if not loadedModel : loadedModel = osgDB.readNodeFile("cessnafire.osgt") if not loadedModel : print arguments.getApplicationName(), ": No data loaded" return 1 root = osg.Group() root.addChild(loadedModel) stateset = osg.StateSet() stateset.setDataVariance(osg.Object.DYNAMIC) blendEquation = osg.BlendEquation(osg.BlendEquation.FUNC_ADD) blendEquation.setDataVariance(osg.Object.DYNAMIC) stateset.setAttributeAndModes(blendEquation,osg.StateAttribute.OVERRIDE|osg.StateAttribute.ON) #tell to sort the mesh before displaying it stateset.setRenderingHint(osg.StateSet.TRANSPARENT_BIN) loadedModel.setStateSet(stateset) viewer.addEventHandler(TechniqueEventHandler(blendEquation)) # add a viewport to the viewer and attach the scene graph. viewer.setSceneData( root ) return viewer.run()
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 setupBin2(rootNode, model,z): stencil = osg.Stencil(); stencil.setFunction(osg.Stencil.ALWAYS,0,4294967295); 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);
def createMirrorTexturedState(filename): dstate = osg.StateSet() dstate.setMode(GL_CULL_FACE,osg.StateAttribute.OFF|osg.StateAttribute.PROTECTED) # set up the texture. image = osgDB.readImageFile(filename.c_str()) if image : texture = osg.Texture2D() texture.setImage(image) dstate.setTextureAttributeAndModes(0,texture,osg.StateAttribute.ON|osg.StateAttribute.PROTECTED) return dstate
def createColorToGreyscaleStateSet(): stateset = osg.StateSet() program = osg.Program() stateset.setAttribute(program) fragSource = "uniform sampler2D baseTexture\n" "uniform mat4 colorMatrix\n" "void main(void)\n" "\n" " vec4 color = texture2D( baseTexture, gl_TexCoord[0].st )\n" " gl_FragColor = colorMatrix * color\n" "\n"
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(toRad(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. setupBin1(rootNode,mirror,z) setupBin2(rootNode,model,z) setupBin3(rootNode,mirror,z) setupBin4(rootNode,model,z) setupBin5(rootNode,mirror,z) return rootNode;
def setupStateSet(): st = osg.StateSet() st.setAttributeAndModes(osg.Material, True)() st.setMode(GL_BLEND, True) callback = AnimtkStateSetUpdateCallback() keys = callback._sampler.getOrCreateKeyframeContainer() keys.push_back(osgAnimation.Vec4Keyframe(0, osg.Vec4(0,0,0,0))) keys.push_back(osgAnimation.Vec4Keyframe(2, osg.Vec4(0.5,0,0,0.5))) keys.push_back(osgAnimation.Vec4Keyframe(4, osg.Vec4(0,0.5,0,1))) keys.push_back(osgAnimation.Vec4Keyframe(6, osg.Vec4(0,0,0.5,1))) keys.push_back(osgAnimation.Vec4Keyframe(8, osg.Vec4(1,1,1,0.5))) keys.push_back(osgAnimation.Vec4Keyframe(10, osg.Vec4(0,0,0,0))) callback.start() st.setUpdateCallback(callback) return st
def main(argv): # use an ArgumentParser object to manage the program arguments. arguments = osg.ArgumentParser(argv) # load the nodes from the commandline arguments. loadedModel = osgDB.readNodeFiles(arguments) # if not loaded assume no arguments passed in, try use default mode instead. if not loadedModel : loadedModel = osgDB.readNodeFile("glider.osgt") if not loadedModel : osg.notify(osg.NOTICE), "Please specify model filename on the command line." return 1 root = osg.Group() root.addChild(loadedModel) stateset = osg.StateSet() logicOp = osg.LogicOp(osg.LogicOp.OR_INVERTED) stateset.setAttributeAndModes(logicOp,osg.StateAttribute.OVERRIDE|osg.StateAttribute.ON) #tell to sort the mesh before displaying it stateset.setRenderingHint(osg.StateSet.TRANSPARENT_BIN) loadedModel.setStateSet(stateset) # construct the viewer. viewer = osgViewer.Viewer() viewer.addEventHandler(TechniqueEventHandler(logicOp)) # run optimization over the scene graph optimzer = osgUtil.Optimizer() optimzer.optimize(root) # add a viewport to the viewer and attach the scene graph. viewer.setSceneData( root ) return viewer.run()
def createSquare(corner, width, height, image): # set up the Geometry. geom = osg.Geometry() coords = osg.Vec3Array(4) (*coords)[0] = corner (*coords)[1] = corner+width (*coords)[2] = corner+width+height (*coords)[3] = corner+height geom.setVertexArray(coords) norms = osg.Vec3Array(1) (*norms)[0] = width^height (*norms)[0].normalize() geom.setNormalArray(norms, osg.Array.BIND_OVERALL) tcoords = osg.Vec2Array(4) (*tcoords)[0].set(0.0,0.0) (*tcoords)[1].set(1.0,0.0) (*tcoords)[2].set(1.0,1.0) (*tcoords)[3].set(0.0,1.0) geom.setTexCoordArray(0,tcoords) colours = osg.Vec4Array(1) (*colours)[0].set(1.0,1.0,1.0,1.0) geom.setColorArray(colours, osg.Array.BIND_OVERALL) geom.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.QUADS,0,4)) if image : stateset = osg.StateSet() texture = osg.Texture2D() texture.setImage(image) stateset.setTextureAttributeAndModes(0,texture,osg.StateAttribute.ON) stateset.setMode(GL_LIGHTING, osg.StateAttribute.OFF) stateset.setMode(GL_BLEND, osg.StateAttribute.ON) stateset.setRenderingHint(osg.StateSet.TRANSPARENT_BIN) geom.setStateSet(stateset) return geom
def createSectorForImage(image, texmat, s, t, radius, height, length): flip = image.getOrigin()==osg.Image.TOP_LEFT numSegments = 20 Theta = length/radius dTheta = Theta/(float)(numSegments-1) ThetaZero = height*s/(t*radius) # set up the texture. texture = osg.Texture2D() texture.setFilter(osg.Texture2D.MIN_FILTER,osg.Texture2D.LINEAR) texture.setFilter(osg.Texture2D.MAG_FILTER,osg.Texture2D.LINEAR) texture.setWrap(osg.Texture2D.WRAP_S,osg.Texture2D.CLAMP_TO_BORDER) texture.setWrap(osg.Texture2D.WRAP_T,osg.Texture2D.CLAMP_TO_BORDER) texture.setResizeNonPowerOfTwoHint(False) texture.setImage(image) # set up the drawstate. dstate = osg.StateSet() dstate.setMode(GL_CULL_FACE,osg.StateAttribute.OFF) dstate.setMode(GL_LIGHTING,osg.StateAttribute.OFF) dstate.setTextureAttributeAndModes(0, texture,osg.StateAttribute.ON) dstate.setTextureAttribute(0, texmat) # set up the geoset. geom = osg.Geometry() geom.setStateSet(dstate) coords = osg.Vec3Array() tcoords = osg.Vec2Array() i = int() angle = -Theta/2.0 for(i=0 i<numSegments ++i, angle+=dTheta) coords.push_back(osg.Vec3(sinf(angle)*radius,cosf(angle)*radius,height*0.5)) # top coords.push_back(osg.Vec3(sinf(angle)*radius,cosf(angle)*radius,-height*0.5)) # bottom. tcoords.push_back(osg.Vec2(angle/ThetaZero+0.5, 0.0 if (flip) else 1.0)) # top tcoords.push_back(osg.Vec2(angle/ThetaZero+0.5, 1.0 if (flip) else 0.0)) # bottom.
def createShapes(): geode = osg.Geode() # --------------------------------------- # Set up a StateSet to texture the objects # --------------------------------------- stateset = osg.StateSet() image = osgDB.readImageFile( "Images/lz.rgb" ) if image : texture = osg.Texture2D() texture.setImage(image) texture.setFilter(osg.Texture.MIN_FILTER, osg.Texture.LINEAR) stateset.setTextureAttributeAndModes(0,texture, osg.StateAttribute.ON) stateset.setMode(GL_LIGHTING, osg.StateAttribute.ON) geode.setStateSet( stateset ) radius = 0.8 height = 1.0 hints = osg.TessellationHints() hints.setDetailRatio(0.5) geode.addDrawable(osg.ShapeDrawable(osg.Sphere(osg.Vec3(0.0,0.0,0.0),radius),hints)) geode.addDrawable(osg.ShapeDrawable(osg.Box(osg.Vec3(2.0,0.0,0.0),2*radius),hints)) geode.addDrawable(osg.ShapeDrawable(osg.Cone(osg.Vec3(4.0,0.0,0.0),radius,height),hints)) geode.addDrawable(osg.ShapeDrawable(osg.Cylinder(osg.Vec3(6.0,0.0,0.0),radius,height),hints)) geode.addDrawable(osg.ShapeDrawable(osg.Capsule(osg.Vec3(8.0,0.0,0.0),radius,height),hints)) grid = osg.HeightField() grid.allocate(38,39) grid.setXInterval(0.28) grid.setYInterval(0.28) for(unsigned int r=0r<39++r) for(unsigned int c=0c<38++c) grid.setHeight(c,r,vertex[r+c*39][2])
class Album (osg.Referenced) : Album(osg.ArgumentParser ap, float width, float height) def getScene(): return _group def getScene(): return _group osg.Matrix getPageOffset(unsigned int pageNo) def nextPage(timeToRotateBy): gotoPage = return(_currentPageNo+1,timeToRotateBy) def previousPage(timeToRotateBy): return gotoPage(_currentPageNo-1,timeToRotateBy) if (_currentPageNo>=1) else False gotoPage = bool(unsigned int pageNo, float timeToRotateBy) def getBackgroundStateSet(): return _backgroundStateSet setVisibility = void() typedef std.vector< Page > PageList _group = osg.Group() _pages = PageList() _backgroundStateSet = osg.StateSet() _currentPageNo = unsigned int() _radiusOfRings = float() _startAngleOfPages = float() _deltaAngleBetweenPages = float()
def createAxis(corner, xdir, ydir, zdir): # set up the Geometry. geom = osg.Geometry() coords = osg.Vec3Array(6) (*coords)[0] = corner (*coords)[1] = corner+xdir (*coords)[2] = corner (*coords)[3] = corner+ydir (*coords)[4] = corner (*coords)[5] = corner+zdir geom.setVertexArray(coords) x_color = osg.Vec4(0.0,1.0,1.0,1.0) y_color = osg.Vec4(0.0,1.0,1.0,1.0) z_color = osg.Vec4(1.0,0.0,0.0,1.0) color = osg.Vec4Array(6) (*color)[0] = x_color (*color)[1] = x_color (*color)[2] = y_color (*color)[3] = y_color (*color)[4] = z_color (*color)[5] = z_color geom.setColorArray(color, osg.Array.BIND_PER_VERTEX) geom.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.LINES,0,6)) stateset = osg.StateSet() linewidth = osg.LineWidth() linewidth.setWidth(4.0) stateset.setAttributeAndModes(linewidth,osg.StateAttribute.ON) stateset.setMode(GL_LIGHTING,osg.StateAttribute.OFF) geom.setStateSet(stateset) return geom
def setupBin1(rootNode, mirror,z): #// 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,4294967295) 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(osg.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);
# use the shared color array. polyGeom.setColorArray(shared_colors, osg.Array.BIND_OVERALL) # use the shared normal array. polyGeom.setNormalArray(shared_normals, osg.Array.BIND_OVERALL) # This time we simply use primitive, and hardwire the number of coords to use # since we know up front, polyGeom.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.TRIANGLES,0,6)) polyGeom.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.TRIANGLE_STRIP,6,6)) polyGeom.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.TRIANGLE_FAN,12,5)) # polygon stipple stateSet = osg.StateSet() polyGeom.setStateSet(stateSet) #if not defined(OSG_GLES1_AVAILABLE) and not defined(OSG_GLES2_AVAILABLE) and not defined(OSG_GL3_AVAILABLE) polygonStipple = osg.PolygonStipple() stateSet.setAttributeAndModes(polygonStipple,osg.StateAttribute.OVERRIDE|osg.StateAttribute.ON) #endif printTriangles("Triangles/Strip/Fan",*polyGeom) # add the points geometry to the geode. geode.addDrawable(polyGeom) return geode
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 createRoom(loadedModel): # default scale for this model. bs = osg.BoundingSphere(osg.Vec3(0.0,0.0,0.0),1.0) root = osg.Group() if loadedModel : loaded_bs = loadedModel.getBound() pat = osg.PositionAttitudeTransform() pat.setPivotPoint(loaded_bs.center()) pat.setUpdateCallback(ModelTransformCallback(loaded_bs)) pat.addChild(loadedModel) bs = pat.getBound() root.addChild(pat) bs.radius()*=1.5 # create a bounding box, which we'll use to size the room. bb = osg.BoundingBox() bb.expandBy(bs) # create statesets. rootStateSet = osg.StateSet() root.setStateSet(rootStateSet) wall = osg.StateSet() wall.setMode(GL_CULL_FACE,osg.StateAttribute.ON) floor = osg.StateSet() floor.setMode(GL_CULL_FACE,osg.StateAttribute.ON) roof = osg.StateSet() roof.setMode(GL_CULL_FACE,osg.StateAttribute.ON) geode = osg.Geode() # create front side. geode.addDrawable(createWall(bb.corner(0), bb.corner(4), bb.corner(1), wall)) # right side geode.addDrawable(createWall(bb.corner(1), bb.corner(5), bb.corner(3), wall)) # left side geode.addDrawable(createWall(bb.corner(2), bb.corner(6), bb.corner(0), wall)) # back side geode.addDrawable(createWall(bb.corner(3), bb.corner(7), bb.corner(2), wall)) # floor geode.addDrawable(createWall(bb.corner(0), bb.corner(1), bb.corner(2), floor)) # roof geode.addDrawable(createWall(bb.corner(6), bb.corner(7), bb.corner(4), roof)) root.addChild(geode) root.addChild(createLights(bb,rootStateSet)) return root
def test_classAvailable(self): s = osg.StateSet()
def test_methodsAvailable(self): #todo s = osg.StateSet() s.setRenderBinDetails(1, 'test')