Exemple #1
0
    def texture_build(self):
        texture = osg.Texture2D()
        texture.setTextureSize(self.width(), self.height())
        texture.setInternalFormat(GL.GL_RGBA)
        texture.setResizeNonPowerOfTwoHint(False)

        # bug detected here, if I enable mipmap osg seems to use the view buffer to
        # do something. If I disable the mipmap it works.
        # you can view the issue with test_09_gaussian_filter.py
        #texture.setFilter(osg.Texture.MIN_FILTER, osg.Texture.LINEAR_MIPMAP_LINEAR)
        texture.setFilter(osg.Texture.MIN_FILTER, osg.Texture.LINEAR)
        texture.setFilter(osg.Texture.MAG_FILTER, osg.Texture.LINEAR)
        return texture
Exemple #2
0
    def setImage(self, image):
        """
        Set the texture data from an image in memory.
        
        The image parameter must be an osg.Image instance.
        
        Returns True if the texture was set successfully.
        """

        if isinstance(image, osg.Image) and image.valid():
            self._texture = osg.Texture2D()
            self._texture.setFilter(osg.Texture.MIN_FILTER, osg.Texture.LINEAR)
            self._texture.setFilter(osg.Texture.MAG_FILTER, osg.Texture.LINEAR)
            self._texture.setImage(image)
            self._texture.setWrap(osg.Texture.WRAP_S, osg.Texture.REPEAT)
            self._texture.setWrap(osg.Texture.WRAP_T, osg.Texture.REPEAT)
            self._texture.setWrap(osg.Texture.WRAP_R, osg.Texture.REPEAT)
        else:
            self._texture = None

        return self._texture is not None
Exemple #3
0
def createPreRenderSubGraph(subgraph, tex_width, tex_height, renderImplementation, \
                                                useImage, useTextureRectangle, useHDR, \
                                                samples, colorSamples):
    if not subgraph:
        return 0

    # create a group to contain the flag and the pre rendering camera.
    parent = osg.Group()

    #texture to render to and to use for rendering of flag.
    texture = 0
    if (useTextureRectangle):
        textureRect = osg.TextureRectangle()
        textureRect.setTextureSize(tex_width, tex_height)
        textureRect.setInternalFormat(osg.GL_RGBA)
        textureRect.setFilter(osg.Texture2D.MIN_FILTER, osg.Texture2D.LINEAR)
        textureRect.setFilter(osg.Texture2D.MAG_FILTER, osg.Texture2D.LINEAR)
        texture = textureRect
    else:
        texture2D = osg.Texture2D()
        texture2D.setTextureSize(tex_width, tex_height)
        texture2D.setInternalFormat(osg.GL_RGBA)
        texture2D.setFilter(osg.Texture2D.MIN_FILTER, osg.Texture2D.LINEAR)
        texture2D.setFilter(osg.Texture2D.MAG_FILTER, osg.Texture2D.LINEAR)
        texture = texture2D

    if useHDR:
        texture.setInternalFormat(osg.GL_RGBA16F_ARB)
        texture.setSourceFormat(osg.GL_RGBA)
        texture.setSourceType(osg.GL_FLOAT)

    #first create the geometry of the flag of which to view.

    if True:
        # create the to visualize.
        polyGeom = osg.Geometry()

        polyGeom.setName("PolyGeom")

        polyGeom.setDataVariance(osg.Object.DYNAMIC)
        polyGeom.setSupportsDisplayList(False)

        origin = osg.Vec3(0.0, 0.0, 0.0)
        xAxis = osg.Vec3(1.0, 0.0, 0.0)
        yAxis = osg.Vec3(0.0, 0.0, 1.0)
        zAxis = osg.Vec3(0.0, -1.0, 0.0)
        height = 100.0
        width = 200.0
        noSteps = 20

        vertices = osg.Vec3Array()
        bottom = osg.Vec3(origin[0], origin[1], origin[2])
        top = osg.Vec3(origin[0], origin[1], origin[2])
        top[2] += height
        dv = xAxis * (width / (float(noSteps - 1)))

        texcoords = osg.Vec2Array()

        # note, when we use TextureRectangle we have to scale the tex coords up to compensate.
        bottom_texcoord = osg.Vec2(0.0, 0.0)

        if useTextureRectangle:
            ltex_height = tex_height
            ltex_width = tex_width
        else:
            ltex_height = 1.0
            ltex_width = 1.0

        top_texcoord = osg.Vec2(0.0, ltex_height)
        dv_texcoord = osg.Vec2(ltex_width / float((noSteps - 1)), 0.0)

        for i in range(noSteps):
            vertices.push_back(top)
            vertices.push_back(bottom)
            top += dv
            bottom += dv

            texcoords.push_back(top_texcoord)
            texcoords.push_back(bottom_texcoord)
            top_texcoord += dv_texcoord
            bottom_texcoord += dv_texcoord

        # pass the created vertex array to the points geometry object.
        polyGeom.setVertexArray(vertices)

        polyGeom.setTexCoordArray(0, texcoords)

        colors = osg.Vec4Array()
        colors.push_back(osg.Vec4(1.0, 1.0, 1.0, 1.0))
        polyGeom.setColorArray(colors)
        polyGeom.setColorBinding(osg.Geometry.BIND_OVERALL)

        polyGeom.addPrimitiveSet(
            osg.DrawArrays(osg.PrimitiveSet.QUAD_STRIP, 0, vertices.size()))

        # new we need to add the texture to the Drawable, we do so by creating a
        # StateSet to contain the Texture StateAttribute.
        stateset = osg.StateSet()

        stateset.setTextureAttributeAndModes(0, texture, osg.StateAttribute.ON)

        polyGeom.setStateSet(stateset)

        #changes to the geometry
        polyGeom.setUpdateCallback(
            MyGeometryCallback(origin, xAxis, yAxis, zAxis, 1.0, 1.0 / width,
                               0.2).__disown__())

        geode = osg.Geode()
        geode.addDrawable(polyGeom)

        parent.addChild(geode)

    # then create the camera node to do the render to texture
    if True:
        camera = osg.Camera()

        # set up the background color and clear mask.
        camera.setClearColor(osg.Vec4(0.1, 0.1, 0.3, 1.0))
        camera.setClearMask(osg.GL_COLOR_BUFFER_BIT | osg.GL_DEPTH_BUFFER_BIT)

        bs = subgraph.getBound()
        if not bs.valid():
            return subgraph

        znear = 1.0 * bs.radius()
        zfar = 3.0 * bs.radius()

        # 2:1 aspect ratio as per flag geometry below.
        proj_top = 0.25 * znear
        proj_right = 0.5 * znear

        znear *= 0.9
        zfar *= 1.1

        # set up projection.
        camera.setProjectionMatrixAsFrustum(-proj_right, proj_right, -proj_top,
                                            proj_top, znear, zfar)

        # set view
        camera.setReferenceFrame(osg.Transform.ABSOLUTE_RF)
        camera.setViewMatrixAsLookAt(osg.Vec3d(bs.center())-osg.Vec3d(0.0,2.0,0.0)*bs.radius(), \
                                                          osg.Vec3d(bs.center()),\
                                                        osg.Vec3d(0.0,0.0,1.0))

        # set viewport
        camera.setViewport(0, 0, tex_width, tex_height)

        # set the camera to render before the main camera.
        camera.setRenderOrder(osg.Camera.PRE_RENDER)

        # tell the camera to use OpenGL frame buffer object where supported.
        camera.setRenderTargetImplementation(renderImplementation)

        if useImage:
            image = osg.Image()
            #image.allocateImage(tex_width, tex_height, 1, GL_RGBA, GL_UNSIGNED_BYTE);
            image.allocateImage(tex_width, tex_height, 1, osg.GL_RGBA,
                                osg.GL_FLOAT)

            # attach the image so its copied on each frame.
            camera.attach(osg.Camera.COLOR_BUFFER, image, samples,
                          colorSamples)

            camera.setPostDrawCallback(MyCameraPostDrawCallback(image))

            # Rather than attach the texture directly to illustrate the texture's ability to
            # detect an image update and to subload the image onto the texture.  You needn't
            # do this when using an Image for copying to, as a separate camera.attach(..)
            # would suffice as well, but we'll do it the long way round here just for demonstration
            # purposes (long way round meaning we'll need to copy image to main memory, then
            # copy it back to the graphics card to the texture in one frame).
            # The long way round allows us to manually modify the copied image via the callback
            # and then let this modified image by reloaded back.
            texture.setImage(0, image)

        else:
            # attach the texture and use it as the color buffer.
            camera.attach(osg.Camera.COLOR_BUFFER, texture, 0, 0, False,
                          samples, colorSamples)

        # add subgraph to render
        camera.addChild(subgraph)

        parent.addChild(camera)

    return parent
Exemple #4
0
import ctypes
import osgDB, osgViewer, osg
group = osg.Group()
image = osgDB.readImageFile(
    "/home/trigrou/Pictures/backgrounds/games/Another_World_by_Orioto.jpg")
loadedModel = osg.Group()

corner = osg.Vec3(0, 0, 0)
width = osg.Vec3(1, 0, 0)
height = osg.Vec3(0, 1, 0)
geom = osg.createTexturedQuadGeometry(corner, width, height, 0.0, 0.0, 1.0,
                                      1.0)
geom.getOrCreateStateSet().setTextureAttributeAndModes(0, osg.Texture2D(image))
geom.getOrCreateStateSet().setTextureAttributeAndModes(
    1,
    osg.Texture2D(
        osgDB.readImageFile(
            "/home/trigrou/Pictures/backgrounds/games/Big_Blue_by_Orioto.jpg"))
)

vertProg = """
void main()
	{	
		gl_Position = ftransform();
                gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
	}
"""
fragProg = """
uniform float test;
uniform int index;
uniform sampler2D tex0;
Exemple #5
0
    filename = imagesSrc[i % len(imagesSrc)]
    iseq = osg.ImageSequence()
    iseq.addImageFile(filename)
    iseq.setMode(osg.ImageSequence.PAGE_AND_DISCARD_USED_IMAGES)
    iseq.play()
    iseq.setLength(1e7)
    images.append(iseq)

sequence = osg.Sequence()
for i in range(0, iteration):
    corner = osg.Vec3(0, 0, 0)
    width = osg.Vec3(1, 0, 0)
    height = osg.Vec3(0, 0, 1)
    geom = osg.createTexturedQuadGeometry(corner, width, height, 0.0, 0.0, 1.0,
                                          1.0)
    texture = osg.Texture2D(images[i])
    texture.setResizeNonPowerOfTwoHint(False)
    texture.setUnRefImageDataAfterApply(True)
    texture.setFilter(osg.Texture.MIN_FILTER, osg.Texture.LINEAR)
    texture.setFilter(osg.Texture.MAG_FILTER, osg.Texture.LINEAR)
    geom.getOrCreateStateSet().setTextureAttributeAndModes(0, texture)
    addShader(geom)
    geode = osg.Geode()
    geode.addDrawable(geom)
    sequence.addChild(geode, duration / iteration)

sequence.setDuration(1.0, -1)
sequence.setMode(osg.Sequence.START)

viewer = osgViewer.Viewer()
viewer.setSceneData(sequence)