def createMirrorSurface(xMin,xMax,yMin,yMax,z): #// set up the drawstate. #// set up the Geometry. geom = osg.Geometry(); coords = osg.Vec3Array(); coords.append(osg.Vec3f(xMin,yMax,z)) coords.append(osg.Vec3f(xMin,yMin,z)) coords.append(osg.Vec3f(xMax,yMin,z)) coords.append(osg.Vec3f(xMax,yMax,z)) geom.setVertexArray(coords); norms = osg.Vec3Array(); norms.append(osg.Vec3f(0.0,0.0,1.0)) geom.setNormalArray(norms); geom.normalBinding = osg.Geometry.BIND_OVERALL tcoords = osg.Vec2Array() tcoords.append(osg.Vec2f(0.0,1.0)) tcoords.append(osg.Vec2f(0.0,0.0)) tcoords.append(osg.Vec2f(1.0,0.0)) tcoords.append(osg.Vec2f(1.0,1.0)) geom.setTexCoordArray(0,tcoords); colours = osg.Vec4Array(); colours.append(osg.Vec4f(1.0,1.0,1.0,1.0)) geom.setColorArray(colours); geom.colorBinding = osg.Geometry.BIND_OVERALL; geom.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.QUADS,0,4)); return geom;
def createTexturedQuadGeometry(corner, widthVec, heightVec, l, b, r, t): g = osg.Geometry() # Create vertex array vertices = osg.Vec3Array() vertices.append(corner + widthVec) vertices.append(corner) vertices.append(corner + heightVec) vertices.append(corner + widthVec + heightVec) g.setVertexArray(vertices) # Create texcoord array texcoords = osg.Vec2Array() texcoords.append(osg.Vec2f(l, t)) texcoords.append(osg.Vec2f(l, b)) texcoords.append(osg.Vec2f(r, b)) texcoords.append(osg.Vec2f(r, t)) g.setTexCoordArray(0, texcoords) # Create color array (single value, white) colors = osg.Vec4Array() colors.append(osg.Vec4f(1, 1, 1, 1)) g.setColorArray(colors) g.colorBinding = osg.Geometry.BIND_OVERALL # Create normal array (single value for all vertices) normals = osg.Vec3Array() normals.append(osg.Vec3f(0, -1, 0)) g.setNormalArray(normals) g.normalBinding = osg.Geometry.BIND_OVERALL # Add primitive set g.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.QUADS, 0, 4)) return g
def main(argv): # use an ArgumentParser object to manage the program arguments. arguments = osg.ArgumentParser(argv) arguments.getApplicationUsage().addKeyboardMouseBinding("m", "Increase the number of depth peeling layers") arguments.getApplicationUsage().addKeyboardMouseBinding("n", "Decrease the number of depth peeling layers") arguments.getApplicationUsage().addKeyboardMouseBinding("l", "Toggle display of the individual or composed layer textures") arguments.getApplicationUsage().addKeyboardMouseBinding("p", "Increase the layer offset") arguments.getApplicationUsage().addKeyboardMouseBinding("o", "Decrease the layer offset") # Have the usual viewer viewer = osgViewer.Viewer(arguments) displaySettings = osg.DisplaySettings() viewer.setDisplaySettings(displaySettings) # Add the stats handler viewer.addEventHandler(osgViewer.StatsHandler)() # add the help handler viewer.addEventHandler(osgViewer.HelpHandler(arguments.getApplicationUsage())) # any option left unread are converted into errors to write out later. arguments.reportRemainingOptionsAsUnrecognized() # read the dump truck, we will need it twice dt = osgDB.readNodeFile("dumptruck.osg") # display a solid version of the dump truck solidModel = osg.PositionAttitudeTransform() solidModel.setPosition(osg.Vec3f(7.0, -2.0, 7.0)) solidModel.addChild(dt) # generate the 3D heatmap surface to display hm = Heatmap(30, 30, 10, 30, 30, 1.0, 0.25) float data[30][30] for (int x=0 x < 30 ++x) for (int y=0 y < 30 ++y) data[y][x] = (double)rand() / RAND_MAX
g.colorBinding = osg.Geometry.BIND_OVERALL # Create normal array (single value for all vertices) normals = osg.Vec3Array() normals.append(osg.Vec3f(0, -1, 0)) g.setNormalArray(normals) g.normalBinding = osg.Geometry.BIND_OVERALL # Add primitive set g.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.QUADS, 0, 4)) return g # Create a 1x1 quad in XZ plane g = osg.createTexturedQuadGeometry(osg.Vec3f(0, 0, 0), osg.Vec3f(1, 0, 0), osg.Vec3f(0, 0, 1), 0, 0, 1, 1) g.getColorArray()[0] = osg.Vec4f(1, 1, 1, 0.5) # change color to semitransparent # Add it to a geode geode = osg.Geode() geode.addDrawable(g) # Add texture i = osgDB.readImageFile("Images/osg256.png") t = osg.Texture2D(i) s = geode.stateSet s.setTextureAttributeAndModes(0, t, osg.StateAttribute.Values.ON) # Make sure blending is active and the geode is in the transparent (depth sorted) bin
traverse(node, nv) cullVisitor.popStateSet() osg.Node* DepthPeeling.createQuad(unsigned int layerNumber, unsigned int numTiles) tileSpan = 1 tileOffsetX = 0 tileOffsetY = 0 if _showAllLayers : tileSpan /= numTiles tileOffsetX = tileSpan * (layerNumber%numTiles) tileOffsetY = 1 - tileSpan * (1 + layerNumber/numTiles) vertices = osg.Vec3Array() vertices.push_back(osg.Vec3f(tileOffsetX , tileOffsetY , 0)) vertices.push_back(osg.Vec3f(tileOffsetX , tileOffsetY + tileSpan, 0)) vertices.push_back(osg.Vec3f(tileOffsetX + tileSpan, tileOffsetY + tileSpan, 0)) vertices.push_back(osg.Vec3f(tileOffsetX + tileSpan, tileOffsetY , 0)) colors = osg.Vec3Array() colors.push_back(osg.Vec3(1, 1, 1)) texcoords = osg.Vec2Array() texcoords.push_back(osg.Vec2f(0, 0)) texcoords.push_back(osg.Vec2f(0, 1)) texcoords.push_back(osg.Vec2f(1, 1)) texcoords.push_back(osg.Vec2f(1, 0)) geometry = osg.Geometry() geometry.setVertexArray(vertices)
# add the window size toggle handler viewer.addEventHandler(osgViewer.WindowSizeHandler)() # add the stats handler viewer.addEventHandler(osgViewer.StatsHandler)() # add the help handler viewer.addEventHandler(osgViewer.HelpHandler(arguments.getApplicationUsage())) # add the screen capture handler viewer.addEventHandler(osgViewer.ScreenCaptureHandler)() # load the data loadedModel = osgDB.readNodeFiles(arguments) if not loadedModel : quad = osg.createTexturedQuadGeometry(osg.Vec3f(-2.0, 0.0, -2.0), osg.Vec3f(2.0, 0.0, 0.0), osg.Vec3f(0.0, 0.0, 2.0) ) geode = osg.Geode() geode.addDrawable(quad) loadedModel = geode # any option left unread are converted into errors to write out later. arguments.reportRemainingOptionsAsUnrecognized() # report any errors if they have occurred when parsing the program arguments. if arguments.errors() : arguments.writeErrorMessages(std.cout) return 1
_device = osgGA.Device() bool PickHandler.handle( osgGA.GUIEventAdapter ea,osgGA.GUIActionAdapter aa) switch(ea.getEventType()) case(osgGA.GUIEventAdapter.PUSH): view = dynamic_cast<osgViewer.View*>(aa) if view : pick(view,ea) return False case(osgGA.GUIEventAdapter.KEYUP): if ea.getKey() == ord("t") : user_event = osgGA.GUIEventAdapter() user_event.setEventType(osgGA.GUIEventAdapter.USER) user_event.setUserValue("vec2f", osg.Vec2f(1.0,2.0)) user_event.setUserValue("vec3f", osg.Vec3f(1.0,2.0, 3.0)) user_event.setUserValue("vec4f", osg.Vec4f(1.0,2.0, 3.0, 4.0)) user_event.setUserValue("vec2d", osg.Vec2d(1.0,2.0)) user_event.setUserValue("vec3d", osg.Vec3d(1.0,2.0, 3.0)) user_event.setUserValue("vec4d", osg.Vec4d(1.0,2.0, 3.0, 4.0)) user_event.setName("osc_test_1") _device.sendEvent(*user_event) default: return False void PickHandler.pick(osgViewer.View* view, osgGA.GUIEventAdapter ea)
" \n" "in vec4 color \n" "out vec4 fragData \n" " \n" "void main() \n" " \n" " fragData = color \n" " \n" fShader = osg.Shader( osg.Shader.FRAGMENT, fragmentSource ) program = osg.Program() program.addShader( vShader ) program.addShader( fShader ) stateSet.setAttribute( program ) lightDir = osg.Vec3f( 0., 0.5, 1. ) lightDir.normalize() stateSet.addUniform( osg.Uniform( "ecLightDir", lightDir ) ) def main(argv): arguments = osg.ArgumentParser( argc, argv ) root = osgDB.readNodeFiles( arguments ) if root == NULL : osg.notify( osg.FATAL ), "Unable to load model from command line." return( 1 ) configureShaders( root.getOrCreateStateSet() ) int width( 800 ), height( 450 )