Пример #1
0
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
Пример #2
0
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;
Пример #3
0
    # 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
s.setRenderingHint(osg.StateSet.TRANSPARENT_BIN)
s.setMode(osg.GL_BLEND, osg.StateAttribute.ON)
Пример #4
0
        geometry = (osg.Geometry*)templateGeometry.clone( osg.CopyOp.DEEP_COPY_PRIMITIVES )
        primSet = dynamic_cast<osg.DrawArrays*>( geometry.getPrimitiveSet(0) )
        primSet.setNumInstances( cell._trees.size() )
        geode = osg.Geode()
        geode.addDrawable(geometry)

        treeParamsImage = osg.Image()
        treeParamsImage.allocateImage( 3*cell._trees.size(), 1, 1, GL_RGBA, GL_FLOAT )

        i = 0
        for(TreeList.iterator itr=cell._trees.begin()
            not = cell._trees.end()
            ++itr,++i)
            ptr = (osg.Vec4f*)treeParamsImage.data(3*i)
            tree = **itr
            ptr[0] = osg.Vec4f(tree._position.x(),tree._position.y(),tree._position.z(),1.0)
            ptr[1] = osg.Vec4f((float)tree._color.r()/255.0,(float)tree._color.g()/255.0, (float)tree._color.b()/255.0, 1.0)
            ptr[2] = osg.Vec4f(tree._width, tree._height, 1.0, 1.0)
        tbo = osg.TextureBuffer()
        tbo.setImage( treeParamsImage )
        tbo.setInternalFormat(GL_RGBA32F_ARB)
        geometry.getOrCreateStateSet().setTextureAttribute(1, tbo)
        geometry.setInitialBound( cell._bb )

    if needGroup :
        group = osg.Group()
        for(Cell.CellList.iterator itr=cell._cells.begin()
            not = cell._cells.end()
            ++itr)
            group.addChild(createTextureBufferGraph(itr,templateGeometry))
Пример #5
0
        # get the pointers to the required fbo, color and depth textures for each camera instance
        # we perform ping ponging between two depth textures
        fbo0 =  fbos[0] if ((i >= 1)) else  NULL
        fbo =  fbos[1] if ((i >= 1)) else  fbos[0]
        colorTexture = _colorTextures[i]
        depthTexture =  _depthTextures[1+(i-1)%2] if ((i >= 1)) else  _depthTextures[i]
        prevDepthTexture =  _depthTextures[1+(i-2)%2] if ((i >= 2)) else  NULL

        # all our peeling layer cameras are post render
        camera = osg.Camera()
        camera.setDataVariance(osg.Object.DYNAMIC)
        camera.setInheritanceMask(osg.Camera.ALL_VARIABLES)
        camera.setRenderOrder(osg.Camera.POST_RENDER, i)
        camera.setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT)
        camera.setClearColor(osg.Vec4f(0, 0, 0, 0))
        camera.setComputeNearFarMode(osg.Camera.DO_NOT_COMPUTE_NEAR_FAR)
        camera.setPreDrawCallback(PreDrawFBOCallback(fbo, fbo0, _texWidth, _texHeight, depthTexture, colorTexture))
        camera.setPostDrawCallback(PostDrawFBOCallback(i == _numPasses - 1))
        camera.setDrawBuffer(GL_COLOR_ATTACHMENT0_EXT)
        camera.setReadBuffer(GL_COLOR_ATTACHMENT0_EXT)
        camera.setAllowEventFocus(False)

        # the peeled layers are rendered with blending forced off
        # and the depth buffer is directly taken from camera 0 via framebuffer blit
        if i > 0 : 
            camera.getOrCreateStateSet().setMode(GL_BLEND, osg.StateAttribute.OFF | osg.StateAttribute.OVERRIDE)
            camera.setClearMask(GL_COLOR_BUFFER_BIT)
         else:
            # camera 0 has to clear both the depth and color buffers
            camera.setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT)
Пример #6
0
def main(argv):


    

    # use an ArgumentParser object to manage the program arguments.
    arguments = osg.ArgumentParser(argv)

    # read the scene from the list of file specified commandline args.
    scene = osgDB.readNodeFiles(arguments)

    if  not scene :
        print argv[0], ": requires filename argument."
        return 1

    # construct the viewer.
    viewer = osgViewer.CompositeViewer(arguments)

    if arguments.read("-1") :
            view = osgViewer.View()
            view.setName("Single view")
            view.setSceneData(osgDB.readNodeFile("fountain.osgt"))

            view.addEventHandler( osgViewer.StatsHandler )()

            view.setUpViewAcrossAllScreens()
            view.setCameraManipulator(osgGA.TrackballManipulator)()
            viewer.addView(view)

    if arguments.read("-2") :

        # view one
            view = osgViewer.View()
            view.setName("View one")
            viewer.addView(view)

            view.setUpViewOnSingleScreen(0)
            view.setSceneData(scene)
            view.setCameraManipulator(osgGA.TrackballManipulator)()

            # add the state manipulator
            statesetManipulator = osgGA.StateSetManipulator()
            statesetManipulator.setStateSet(view.getCamera().getOrCreateStateSet())

            view.addEventHandler( statesetManipulator )

        # view two
            view = osgViewer.View()
            view.setName("View two")
            viewer.addView(view)

            view.setUpViewOnSingleScreen(1)
            view.setSceneData(scene)
            view.setCameraManipulator(osgGA.TrackballManipulator)()

            view.addEventHandler( osgViewer.StatsHandler )()


            # add the handler for doing the picking
            view.addEventHandler(PickHandler())


    if arguments.read("-3")  or  viewer.getNumViews()==0 :

        wsi = osg.GraphicsContext.getWindowingSystemInterface()
        if  not wsi :
            osg.notify(osg.NOTICE), "Error, no WindowSystemInterface available, cannot create windows."
            return 1

        unsigned int width, height
        wsi.getScreenResolution(osg.GraphicsContext.ScreenIdentifier(0), width, height)

        traits = osg.GraphicsContext.Traits()
        traits.x = 100
        traits.y = 100
        traits.width = 1000
        traits.height = 800
        traits.windowDecoration = True
        traits.doubleBuffer = True
        traits.sharedContext = 0

        gc = osg.GraphicsContext.createGraphicsContext(traits)
        if gc.valid() :
            osg.notify(osg.INFO), "  GraphicsWindow has been created successfully."

            # need to ensure that the window is cleared make sure that the complete window is set the correct colour
            # rather than just the parts of the window that are under the camera's viewports
            gc.setClearColor(osg.Vec4f(0.2,0.2,0.6,1.0))
            gc.setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        else:
            osg.notify(osg.NOTICE), "  GraphicsWindow has not been created successfully."

        # view one
            view = osgViewer.View()
            view.setName("View one")
            viewer.addView(view)

            view.setSceneData(scene)
            view.getCamera().setName("Cam one")
            view.getCamera().setViewport(osg.Viewport(0,0, traits.width/2, traits.height/2))
            view.getCamera().setGraphicsContext(gc)
            view.setCameraManipulator(osgGA.TrackballManipulator)()

            # add the state manipulator
            statesetManipulator = osgGA.StateSetManipulator()
            statesetManipulator.setStateSet(view.getCamera().getOrCreateStateSet())

            view.addEventHandler( statesetManipulator )

            view.addEventHandler( osgViewer.StatsHandler )()
            view.addEventHandler( osgViewer.HelpHandler )()
            view.addEventHandler( osgViewer.WindowSizeHandler )()
            view.addEventHandler( osgViewer.ThreadingHandler )()
            view.addEventHandler( osgViewer.RecordCameraPathHandler )()

        # view two
            view = osgViewer.View()
            view.setName("View two")
            viewer.addView(view)

            view.setSceneData(scene)
            view.getCamera().setName("Cam two")
            view.getCamera().setViewport(osg.Viewport(traits.width/2,0, traits.width/2, traits.height/2))
            view.getCamera().setGraphicsContext(gc)
            view.setCameraManipulator(osgGA.TrackballManipulator)()

            # add the handler for doing the picking
            view.addEventHandler(PickHandler())


        # view three
            view = osgViewer.View()
            view.setName("View three")
            viewer.addView(view)

            view.setSceneData(osgDB.readNodeFile("cessnafire.osgt"))

            view.getCamera().setName("Cam three")
            view.getCamera().setProjectionMatrixAsPerspective(30.0, double(traits.width) / double(traits.height/2), 1.0, 1000.0)
            view.getCamera().setViewport(osg.Viewport(0, traits.height/2, traits.width, traits.height/2))
            view.getCamera().setGraphicsContext(gc)
            view.setCameraManipulator(osgGA.TrackballManipulator)()



    while arguments.read("-s") :  viewer.setThreadingModel(osgViewer.CompositeViewer.SingleThreaded) 
    while arguments.read("-g") :  viewer.setThreadingModel(osgViewer.CompositeViewer.CullDrawThreadPerContext) 
    while arguments.read("-c") :  viewer.setThreadingModel(osgViewer.CompositeViewer.CullThreadPerCameraDrawThreadPerContext) 

     # run the viewer's main frame loop
     return viewer.run()
Пример #7
0

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)
    intersections = osgUtil.LineSegmentIntersector.Intersections()
Пример #8
0
def singleWindowSideBySideCameras(viewer):

    
    wsi = osg.GraphicsContext.getWindowingSystemInterface()
    if  not wsi : 
        osg.notify(osg.NOTICE), "Error, no WindowSystemInterface available, cannot create windows."
        return
    
    unsigned int width, height
    wsi.getScreenResolution(osg.GraphicsContext.ScreenIdentifier(0), width, height)



    # Not fullscreen
    width /= 2
    height /= 2

    traits = osg.GraphicsContext.Traits()
    traits.x = 100
    traits.y = 100
    traits.width = width
    traits.height = height
    traits.windowDecoration = True
    traits.doubleBuffer = True
    traits.sharedContext = 0

    gc = osg.GraphicsContext.createGraphicsContext(traits)
    if gc.valid() :
        osg.notify(osg.INFO), "  GraphicsWindow has been created successfully."

        # need to ensure that the window is cleared make sure that the complete window is set the correct colour
        # rather than just the parts of the window that are under the camera's viewports
        gc.setClearColor(osg.Vec4f(0.2,0.2,0.6,1.0))
        gc.setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    else:
        osg.notify(osg.NOTICE), "  GraphicsWindow has not been created successfully."


    master = viewer.getCamera()

    # get the default settings for the camera
    double fovy, aspectRatio, zNear, zFar
    master.getProjectionMatrixAsPerspective(fovy, aspectRatio, zNear, zFar)

    # reset this for the actual apsect ratio of out created window
    windowAspectRatio = double(width)/double(height)
    master.setProjectionMatrixAsPerspective(fovy, windowAspectRatio, 1.0, 10000.0)

    master.setName("MasterCam")

    camera = osg.Camera()
    camera.setCullMask(1)
    camera.setName("Left")
    camera.setGraphicsContext(gc)
    camera.setViewport(osg.Viewport(0, 0, width/2, height))
    buffer =  GL_BACK if (traits.doubleBuffer) else  GL_FRONT
    camera.setDrawBuffer(buffer)
    camera.setReadBuffer(buffer)
    viewer.addSlave(camera, osg.Matrixd.scale(1.0,0.5,1.0), osg.Matrixd())

    camera = osg.Camera()
    camera.setCullMask(2)
    camera.setName("Right")
    camera.setGraphicsContext(gc)
    camera.setViewport(osg.Viewport(width/2, 0, width/2, height))
    buffer =  GL_BACK if (traits.doubleBuffer) else  GL_FRONT
    camera.setDrawBuffer(buffer)
    camera.setReadBuffer(buffer)
    viewer.addSlave(camera, osg.Matrixd.scale(1.0,0.5,1.0), osg.Matrixd())