class AddHelperBone (osg.NodeVisitor) : AddHelperBone() : osg.NodeVisitor(osg.NodeVisitor.TRAVERSE_ALL_CHILDREN) def apply(node): bone = dynamic_cast<osgAnimation.Bone*>(node) if bone : bone.addChild(createAxis()) traverse(node)
class SetupRigGeometry (osg.NodeVisitor) : _hardware = bool() SetupRigGeometry( bool hardware = True) : osg.NodeVisitor(osg.NodeVisitor.TRAVERSE_ALL_CHILDREN), _hardware(hardware) def apply(geode): for (unsigned int i = 0 i < geode.getNumDrawables() i++) apply(*geode.getDrawable(i))
class GeometryFinder (osg.NodeVisitor) : _geom = osg.Geometry() GeometryFinder() : osg.NodeVisitor(osg.NodeVisitor.TRAVERSE_ALL_CHILDREN) def apply(geode): if _geom.valid() : return for (unsigned int i = 0 i < geode.getNumDrawables() i++) geom = dynamic_cast<osg.Geometry*>(geode.getDrawable(i)) if geom : _geom = geom return
class AnimationManagerFinder (osg.NodeVisitor) : _am = osgAnimation.BasicAnimationManager() AnimationManagerFinder() : osg.NodeVisitor(osg.NodeVisitor.TRAVERSE_ALL_CHILDREN) def apply(node): if _am.valid() : return if node.getUpdateCallback() : b = dynamic_cast<osgAnimation.AnimationManagerBase*>(node.getUpdateCallback()) if b : _am = osgAnimation.BasicAnimationManager(*b) return traverse(node)
class AlphaSetterVisitor (osg.NodeVisitor) : _alpha = float() AlphaSetterVisitor( float alpha = 1.0):osg.NodeVisitor(TRAVERSE_ALL_CHILDREN) _alpha = alpha def apply(node): win = dynamic_cast<osgWidget.Window*>(node) if win : # osgWidget.warn(), "I am in Window: ", win.getName() for (osgWidget.Window.Iterator it = win.begin() it not = win.end() it++) # osgWidget.warn(), " I am operating on Widget: ", it.getName() color = it.getColor() color[3] = color[3] *_alpha it.setColor(color) color = win.getBackground().getColor() color[3] = color[3] *_alpha win.getBackground().setColor(color) traverse(node)
class ColorSetterVisitor (osg.NodeVisitor) : _color = osgWidget.Color() ColorSetterVisitor( osgWidget.Color color):osg.NodeVisitor(TRAVERSE_ALL_CHILDREN) _color = color def apply(node): win = dynamic_cast<osgWidget.Window*>(node) if win : # osgWidget.warn(), "I am in Window: ", win.getName() for (osgWidget.Window.Iterator it = win.begin() it not = win.end() it++) # osgWidget.warn(), " I am operating on Widget: ", it.getName() # color = it.getColor() # color[3] = color[3] *_alpha it.setColor(_color) # color = win.getBackground().getColor() # color[3] = color[3] *_alpha win.getBackground().setColor(osgWidget.Color(0,0,0,0)) traverse(node)
def main(argv): arguments = osg.ArgumentParser( argc, argv ) usage = arguments.getApplicationUsage() usage.setDescription( arguments.getApplicationName() + " is the example which demonstrates how to render high-resolution images (posters).") usage.setCommandLineUsage( arguments.getApplicationName() + " [options] scene_file" ) usage.addCommandLineOption( "-h or --help", "Display this information." ) usage.addCommandLineOption( "--color <r> <g> <b>", "The background color." ) usage.addCommandLineOption( "--ext <ext>", "The output tiles' extension (Default: bmp)." ) usage.addCommandLineOption( "--poster <filename>", "The output poster's name (Default: poster.bmp)." ) usage.addCommandLineOption( "--tilesize <w> <h>", "Size of each image tile (Default: 640 480)." ) usage.addCommandLineOption( "--finalsize <w> <h>", "Size of the poster (Default: 6400 4800)." ) usage.addCommandLineOption( "--enable-output-poster", "Output the final poster file (Default)." ) usage.addCommandLineOption( "--disable-output-poster", "Don't output the final poster file." ) #usage.addCommandLineOption( "--enable-output-tiles", "Output all tile files." ) #usage.addCommandLineOption( "--disable-output-tiles", "Don't output all tile files (Default)." ) usage.addCommandLineOption( "--use-fb", "Use Frame Buffer for rendering tiles (Default, recommended).") usage.addCommandLineOption( "--use-fbo", "Use Frame Buffer Object for rendering tiles.") usage.addCommandLineOption( "--use-pbuffer","Use Pixel Buffer for rendering tiles.") usage.addCommandLineOption( "--use-pbuffer-rtt","Use Pixel Buffer RTT for rendering tiles.") usage.addCommandLineOption( "--inactive", "Inactive capturing mode." ) usage.addCommandLineOption( "--camera-eye <x> <y> <z>", "Set eye position in inactive mode." ) usage.addCommandLineOption( "--camera-latlongheight <lat> <lon> <h>", "Set eye position on earth in inactive mode." ) usage.addCommandLineOption( "--camera-hpr <h> <p> <r>", "Set eye rotation in inactive mode." ) if arguments.read("-h") or arguments.read("--help") : usage.write( std.cout ) return 1 # Poster arguments activeMode = True outputPoster = True #bool outputTiles = False tileWidth = 640, tileHeight = 480 posterWidth = 640*2, posterHeight = 480*2 posterName = "poster.bmp", extName = "bmp" bgColor = osg.Vec4(0.2, 0.2, 0.6, 1.0) renderImplementation = osg.Camera.FRAME_BUFFER_OBJECT while arguments.read("--inactive") : activeMode = False while arguments.read("--color", bgColor.r(), bgColor.g(), bgColor.b()) : while arguments.read("--tilesize", tileWidth, tileHeight) : while arguments.read("--finalsize", posterWidth, posterHeight) : while arguments.read("--poster", posterName) : while arguments.read("--ext", extName) : while arguments.read("--enable-output-poster") : outputPoster = True while arguments.read("--disable-output-poster") : outputPoster = False #while arguments.read("--enable-output-tiles") : outputTiles = True #while arguments.read("--disable-output-tiles") : outputTiles = False while arguments.read("--use-fbo") : renderImplementation = osg.Camera.FRAME_BUFFER_OBJECT while arguments.read("--use-pbuffer") : renderImplementation = osg.Camera.PIXEL_BUFFER while arguments.read("--use-pbuffer-rtt") : renderImplementation = osg.Camera.PIXEL_BUFFER_RTT while arguments.read("--use-fb") : renderImplementation = osg.Camera.FRAME_BUFFER # Camera settings for inactive screenshot useLatLongHeight = True eye = osg.Vec3d() latLongHeight = osg.Vec3d( 50.0, 10.0, 2000.0 ) hpr = osg.Vec3d( 0.0, 0.0, 0.0 ) if arguments.read("--camera-eye", eye.x(), eye.y(), eye.z()) : useLatLongHeight = False activeMode = False elif arguments.read("--camera-latlongheight", latLongHeight.x(), latLongHeight.y(), latLongHeight.z()) : activeMode = False latLongHeight.x() = osg.DegreesToRadians( latLongHeight.x() ) latLongHeight.y() = osg.DegreesToRadians( latLongHeight.y() ) if arguments.read("--camera-hpr", hpr.x(), hpr.y(), hpr.z()) : activeMode = False hpr.x() = osg.DegreesToRadians( hpr.x() ) hpr.y() = osg.DegreesToRadians( hpr.y() ) hpr.z() = osg.DegreesToRadians( hpr.z() ) # Construct scene graph scene = osgDB.readNodeFiles( arguments ) if not scene : scene = osgDB.readNodeFile( "cow.osgt" ) if not scene : print arguments.getApplicationName(), ": No data loaded" return 1 # Create camera for rendering tiles offscreen. FrameBuffer is recommended because it requires less memory. camera = osg.Camera() camera.setClearColor( bgColor ) camera.setClearMask( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) camera.setReferenceFrame( osg.Transform.ABSOLUTE_RF ) camera.setRenderOrder( osg.Camera.PRE_RENDER ) camera.setRenderTargetImplementation( renderImplementation ) camera.setViewport( 0, 0, tileWidth, tileHeight ) camera.addChild( scene ) # Set the printer printer = PosterPrinter() printer.setTileSize( tileWidth, tileHeight ) printer.setPosterSize( posterWidth, posterHeight ) printer.setCamera( camera ) posterImage = 0 if outputPoster : posterImage = osg.Image() posterImage.allocateImage( posterWidth, posterHeight, 1, GL_RGBA, GL_UNSIGNED_BYTE ) printer.setFinalPoster( posterImage ) printer.setOutputPosterName( posterName ) #if 0 # While recording sub-images of the poster, the scene will always be traversed twice, from its two # parent node: root and camera. Sometimes this may not be so comfortable. # To prevent this behaviour, we can use a switch node to enable one parent and disable the other. # However, the solution also needs to be used with care, as the window will go blank while taking # snapshots and recover later. root = osg.Switch() root.addChild( scene, True ) root.addChild( camera, False ) #else: root = osg.Group() root.addChild( scene ) root.addChild( camera ) #endif viewer = osgViewer.Viewer() viewer.setSceneData( root ) viewer.getDatabasePager().setDoPreCompile( False ) if renderImplementation==osg.Camera.FRAME_BUFFER : # FRAME_BUFFER requires the window resolution equal or greater than the to-be-copied size viewer.setUpViewInWindow( 100, 100, tileWidth, tileHeight ) else: # We want to see the console output, so just render in a window viewer.setUpViewInWindow( 100, 100, 800, 600 ) if activeMode : viewer.addEventHandler( PrintPosterHandler(printer) ) viewer.addEventHandler( osgViewer.StatsHandler )() viewer.addEventHandler( osgGA.StateSetManipulator(viewer.getCamera().getOrCreateStateSet()) ) viewer.setCameraManipulator( osgGA.TrackballManipulator )() viewer.run() else: camera = viewer.getCamera() if not useLatLongHeight : computeViewMatrix( camera, eye, hpr ) computeViewMatrixOnEarth = else( camera, scene, latLongHeight, hpr ) renderer = CustomRenderer( camera ) camera.setRenderer( renderer ) viewer.setThreadingModel( osgViewer.Viewer.SingleThreaded ) # Realize and initiate the first PagedLOD request viewer.realize() viewer.frame() printer.init( camera ) while not printer.done() : viewer.advance() # Keep updating and culling until full level of detail is reached renderer.setCullOnly( True ) while viewer.getDatabasePager().getRequestsInProgress() : viewer.updateTraversal() viewer.renderingTraversals() renderer.setCullOnly( False ) printer.frame( viewer.getFrameStamp(), viewer.getSceneData() ) viewer.renderingTraversals() return 0 # Translated from file 'PosterPrinter.cpp' #include <osg/ClusterCullingCallback> #include <osgDB/ReadFile> #include <osgDB/WriteFile> #include <string.h> #include <iostream> #include <sstream> #include "PosterPrinter.h" # PagedLoadingCallback: Callback for loading paged nodes while doing intersecting test class PagedLoadingCallback (osgUtil.IntersectionVisitor.ReadCallback) : def readNodeFile(filename): return osgDB.readNodeFile( filename ) static PagedLoadingCallback g_pagedLoadingCallback = PagedLoadingCallback() # LodCullingCallback: Callback for culling LODs and selecting the highest level class LodCullingCallback (osg.NodeCallback) : virtual void operator()( osg.Node* node, osg.NodeVisitor* nv ) lod = static_cast<osg.LOD*>(node) if lod and lod.getNumChildren()>0 : lod.getChild(lod.getNumChildren()-1).accept(*nv) static LodCullingCallback g_lodCullingCallback = LodCullingCallback() # PagedCullingCallback: Callback for culling paged nodes and selecting the highest level class PagedCullingCallback (osg.NodeCallback) : virtual void operator()( osg.Node* node, osg.NodeVisitor* nv ) pagedLOD = static_cast<osg.PagedLOD*>(node) if pagedLOD and pagedLOD.getNumChildren()>0 : numChildren = pagedLOD.getNumChildren() updateTimeStamp = nv.getVisitorType()==osg.NodeVisitor.CULL_VISITOR if nv.getFrameStamp() and updateTimeStamp : timeStamp = nv.getFrameStamp().getReferenceTime() if (nv.getFrameStamp()) else 0.0 frameNumber = nv.getFrameStamp().getFrameNumber() if (nv.getFrameStamp()) else 0 pagedLOD.setFrameNumberOfLastTraversal( frameNumber ) pagedLOD.setTimeStamp( numChildren-1, timeStamp ) pagedLOD.setFrameNumber( numChildren-1, frameNumber ) pagedLOD.getChild(numChildren-1).accept(*nv) # Request for child if not pagedLOD.getDisableExternalChildrenPaging() and nv.getDatabaseRequestHandler() and numChildren<pagedLOD.getNumRanges() : if pagedLOD.getDatabasePath().empty() : nv.getDatabaseRequestHandler().requestNodeFile( pagedLOD.getFileName(numChildren), nv.getNodePath(), 1.0, nv.getFrameStamp(), pagedLOD.getDatabaseRequest(numChildren), pagedLOD.getDatabaseOptions() ) else: nv.getDatabaseRequestHandler().requestNodeFile( pagedLOD.getDatabasePath()+pagedLOD.getFileName(numChildren), nv.getNodePath(), 1.0, nv.getFrameStamp(), pagedLOD.getDatabaseRequest(numChildren), pagedLOD.getDatabaseOptions() ) #node.traverse(*nv) static PagedCullingCallback g_pagedCullingCallback = PagedCullingCallback() # PosterVisitor: A visitor for adding culling callbacks to newly allocated paged nodes PosterVisitor.PosterVisitor() : osg.NodeVisitor(osg.NodeVisitor.TRAVERSE_ALL_CHILDREN), _appliedCount(0), _needToApplyCount(0), _addingCallbacks(True) void PosterVisitor.apply( osg.LOD node ) #if not hasCullCallback(node.getCullCallback(), g_lodCullingCallback) : # # if not node.getName().empty() : # # itr = _pagedNodeNames.find( node.getName() ) # if itr not =_pagedNodeNames.end() : # # insertCullCallback( node, g_lodCullingCallback ) # _appliedCount++ # # # # def if _addingCallbacks . # # node.removeCullCallback( g_lodCullingCallback ) # _appliedCount-- # traverse( node ) void PosterVisitor.apply( osg.PagedLOD node ) if not hasCullCallback(node.getCullCallback(), g_pagedCullingCallback) : for ( unsigned int i=0 i<node.getNumFileNames() ++i ) if node.getFileName(i).empty() : continue itr = _pagedNodeNames.find( node.getFileName(i) ) if itr not =_pagedNodeNames.end() : node.addCullCallback( g_pagedCullingCallback ) _appliedCount++ break
class VertexCounter (osg.NodeVisitor) : VertexCounter( int limit ) : osg.NodeVisitor( osg.NodeVisitor.TRAVERSE_ALL_CHILDREN ), _limit( limit ), _total( 0 )
class DebugDisplayVisitor (osg.NodeVisitor) : DebugDisplayVisitor( bool debug=True ) : osg.NodeVisitor( osg.NodeVisitor.TRAVERSE_ALL_CHILDREN ), _debug( debug )
class WriteOutPagedLODSubgraphsVistor (osg.NodeVisitor) : WriteOutPagedLODSubgraphsVistor(): osg.NodeVisitor(osg.NodeVisitor.TRAVERSE_ALL_CHILDREN)
class NameVistor (osg.NodeVisitor) : NameVistor(): osg.NodeVisitor(osg.NodeVisitor.TRAVERSE_ALL_CHILDREN), _count(0)
#include <osg/Depth> #include <iostream> #include <osg/Switch> #include <osgSim/MultiSwitch> #include <osgSim/DOFTransform> #include <osg/AlphaFunc> #include <osg/BlendFunc> using namespace osg using namespace osgGA class SwitchDOFVisitor : public osg.NodeVisitor, public osgGA.GUIEventHandler SwitchDOFVisitor(): osg.NodeVisitor(osg.NodeVisitor.TRAVERSE_ALL_CHILDREN) def apply(node): pMSwitch = dynamic_cast<osgSim.MultiSwitch*>(node) if pMSwitch : mSwitches.push_back(pMSwitch) osg.NodeVisitor.apply(node) def apply(node): pDof = dynamic_cast<osgSim.DOFTransform*>(node)
class ReplaceTextureVisitor (osg.NodeVisitor) : ReplaceTextureVisitor(osg.Texture* tex) : osg.NodeVisitor(TRAVERSE_ALL_CHILDREN) , _tex(tex)
class DeleteSelectedNodesVisitor (osg.NodeVisitor) : DeleteSelectedNodesVisitor(): osg.NodeVisitor(osg.NodeVisitor.TRAVERSE_ALL_CHILDREN)
class CreateModelToSaveVisitor (osg.NodeVisitor) : CreateModelToSaveVisitor(): osg.NodeVisitor(osg.NodeVisitor.TRAVERSE_ALL_CHILDREN) _group = osg.Group() _addToModel = False
class FindTopMostNodeOfTypeVisitor (osg.NodeVisitor) : FindTopMostNodeOfTypeVisitor() : osg.NodeVisitor(osg.NodeVisitor.TRAVERSE_ALL_CHILDREN), _foundNode(0)
class ConvertToPageLODVistor (osg.NodeVisitor) : ConvertToPageLODVistor( str basename, str extension, bool makeAllChildrenPaged): osg.NodeVisitor(osg.NodeVisitor.TRAVERSE_ALL_CHILDREN), _basename(basename), _extension(extension), _makeAllChildrenPaged(makeAllChildrenPaged)
geom = dynamic_cast<osg.Geometry *>(geode.getDrawable(i)) if not geom : continue _total += countGeometryVertices( geom ) if _total > _limit : break _limit = int() _total = int() OcclusionQueryVisitor.OcclusionQueryVisitor() : osg.NodeVisitor( osg.NodeVisitor.TRAVERSE_ALL_CHILDREN ), _nameIdx( 0 ), _occluderThreshold( 5000 ) # Create a dummy OcclusionQueryNode just so we can get its state. # We'll then share that state between all OQNs we add to the visited scene graph. oqn = osg.OcclusionQueryNode() _state = oqn.getQueryStateSet() _debugState = oqn.getDebugStateSet() OcclusionQueryVisitor.~OcclusionQueryVisitor() osg.notify( osg.INFO ), "osgOQ: OcclusionQueryVisitor: Added ", getNameIdx(), " OQNodes." void OcclusionQueryVisitor.setOccluderThreshold( int vertices ) _occluderThreshold = vertices
class CheckVisitor (osg.NodeVisitor) : CheckVisitor(): osg.NodeVisitor(osg.NodeVisitor.TRAVERSE_ALL_CHILDREN)
class VisibilityThresholdVisitor (osg.NodeVisitor) : VisibilityThresholdVisitor( unsigned int threshold=500 ) : osg.NodeVisitor( osg.NodeVisitor.TRAVERSE_ALL_CHILDREN ), _visThreshold( threshold )
class FindNamedNodeVisitor (osg.NodeVisitor) : FindNamedNodeVisitor( str name): osg.NodeVisitor(osg.NodeVisitor.TRAVERSE_ALL_CHILDREN), _name(name)
class QueryFrameCountVisitor (osg.NodeVisitor) : QueryFrameCountVisitor( int count=5 ) : osg.NodeVisitor( osg.NodeVisitor.TRAVERSE_ALL_CHILDREN ), _count( count )
class EnableQueryVisitor (osg.NodeVisitor) : EnableQueryVisitor( bool enable=True ) : osg.NodeVisitor( osg.NodeVisitor.TRAVERSE_ALL_CHILDREN ), _enabled( enable )
class InsertCallbacksVisitor (osg.NodeVisitor) : InsertCallbacksVisitor():osg.NodeVisitor(osg.NodeVisitor.TRAVERSE_ALL_CHILDREN)