Example #1
0
def initBuffers(w, h):
    print "initBuffers(%s,%s)" % (w,h)
    global width, height, depthTexture, tmpTexture, directDepthCopy
    if width == w and height == h: return

    depthTexture = GL.createTexture()
    tmpTexture = GL.createTexture()
	
    if not GL.hasExtension("GL_NV_texture_rectangle"): return

    width,height = w,h

    rect = 1
    targ = "TEXTURE_RECTANGLE_NV"
    if directDepthCopy:
        depthTexture.loadNull2D(targ, 0, "DEPTH_COMPONENT24",
				width, height, 0, "DEPTH_COMPONENT", "INT")
    else:
        depthTexture.loadNull2D(targ, 0, "RGBA8",
				width, height, 0, "BGRA", "INT")

	tmpTexture.loadNull2D(targ, 0, "RGB8",
			      width, height, 0, "RGB",  "INT")
	tmpTexture.setTexParameter(targ, "TEXTURE_MIN_FILTER", "LINEAR")
	tmpTexture.setTexParameter(targ, "TEXTURE_MAG_FILTER", "LINEAR")
	
    depthTexture.setTexParameter(targ, "TEXTURE_MIN_FILTER", "LINEAR")
    depthTexture.setTexParameter(targ, "TEXTURE_MAG_FILTER", "LINEAR")
Example #2
0
def getCachedTexture(args, shade_all_levels=0):
    global texcache

    name = args[6]
    file = "../libvob/src/texture/" + name
    binfile = file + ".bin"
    srcfile = file + ".texture"

    bintime = File(binfile).lastModified()
    srctime = File(srcfile).lastModified()

    if srctime > bintime:
        os.system("make -C ./libvob/src/texture " + name + ".bin")

    key = str((args, shade_all_levels))
    if texcache.has_key(key) and texcache[(key, "ctime")] == srctime:
        #print "Returning cached texture"
        return texcache[key]

    tex = GL.createTexture()
    if shade_all_levels:
        res = tex.shade_all_levels(*args)
    else:
        res = tex.shade(*args)

    texcache[key] = tex
    texcache[(key, "ctime")] = srctime
    if dbg:
        print "SHADER: ", res, tex.getTexId()
    return tex
Example #3
0
def getTexture(*args):
    key = str(args)
    if not textures.has_key(key):
        textures[key] = GL.createTexture()
        if dbg:
            print "Generating texture: ", args
        res = textures[key].shade(*args)
        if dbg:
            print "SHADER: ", res
    return textures[key]
Example #4
0
class IrreguFrame(IrregularFrame):
    dicefactor = .4
    # dicefactor = 0.03

    if dbg: print "Start Irregu shading..."

    tex = GL.createTexture()
    tex = getCachedTexture([
        128, 128, 0, 1, "INTENSITY", "LUMINANCE", "sawnoise",
        [
            "bias", "0.5", "scale", "0.15", "freq", "1", "df", "2", "scale2",
            "0.25", "freq2", "10", "df2", ".5"
        ]
    ])
    #                       "scale", "0.2", "freq", "1", "df", "2",
    #                       "scale2", "0.05", "freq2", "12", "df2", "1.5"])

    if dbg: print "part 1 done"
    ripple_scale = 0.25
    tex2 = getCachedTexture([
        256, 256, 0, 4, "RGBA", "RGBA", "irregu",
        [
            "radius", "2", "ripple_scale",
            str(ripple_scale), "angle", "0", "angles", "3", "eps", ".250",
            "scaling", "const"
        ]
    ],
                            shade_all_levels=1)
    tex3 = getCachedTexture([
        256, 256, 0, 1, "INTENSITY", "LUMINANCE", "irregu",
        [
            "ripple_scale",
            str(ripple_scale),
        ]
    ],
                            shade_all_levels=1)
    if dbg: print "done"

    boxtex = GL.createTexture()

    combiners = "Enable"

    def __init__(self,
                 x0,
                 y0,
                 x1,
                 y1,
                 border,
                 ripple,
                 typeInt=0,
                 contentColor=java.awt.Color.white,
                 frameColor=java.awt.Color.black,
                 type="square"):

        self.dbg = 0
        if typeInt == 1:
            type = 'square'
        elif typeInt == 2:
            type = 'ellipse'

        if self.dbg:
            print "Texture id:", self.tex.getTexId()

        def code(color):
            return """
                PushAttrib ENABLE_BIT TEXTURE_BIT CURRENT_BIT
                Enable ALPHA_TEST
                AlphaFunc GREATER 0.0
		Disable BLEND

		Color %(color)s

                ActiveTexture TEXTURE1
                BindTexture TEXTURE_2D %(boxtex)s
                Enable TEXTURE_2D
                TexImage2D TEXTURE_2D 0 ALPHA 4 4 0 ALPHA 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0
                TexParameter TEXTURE_2D TEXTURE_BASE_LEVEL 0
                TexParameter TEXTURE_2D TEXTURE_MAX_LEVEL 0
		TexParameter TEXTURE_2D TEXTURE_WRAP_S CLAMP
		TexParameter TEXTURE_2D TEXTURE_WRAP_T CLAMP
		TexParameter TEXTURE_2D TEXTURE_MIN_FILTER NEAREST
		TexParameter TEXTURE_2D TEXTURE_MAG_FILTER NEAREST

                #TexGen S TEXTURE_GEN_MODE EYE_LINEAR
                #Enable TEXTURE_GEN_S
                #TexGen T TEXTURE_GEN_MODE EYE_LINEAR
                #Enable TEXTURE_GEN_T

                ActiveTexture TEXTURE0

                BindTexture TEXTURE_2D %(tex)s
                Enable TEXTURE_2D
                %(comb)s REGISTER_COMBINERS_NV
                CombinerParameterNV NUM_GENERAL_COMBINERS_NV 1
                CombinerInputNV COMBINER0_NV ALPHA VARIABLE_A_NV TEXTURE1 UNSIGNED_IDENTITY_NV ALPHA
                CombinerInputNV COMBINER0_NV ALPHA VARIABLE_B_NV TEXTURE0 SIGNED_NEGATE_NV ALPHA
                CombinerInputNV COMBINER0_NV ALPHA VARIABLE_C_NV TEXTURE1 UNSIGNED_IDENTITY_NV ALPHA
                CombinerInputNV COMBINER0_NV ALPHA VARIABLE_D_NV SECONDARY_COLOR_NV UNSIGNED_IDENTITY_NV BLUE
                CombinerOutputNV COMBINER0_NV ALPHA DISCARD_NV DISCARD_NV SPARE0_NV NONE NONE FALSE FALSE FALSE

                FinalCombinerInputNV VARIABLE_A_NV ZERO UNSIGNED_IDENTITY_NV RGB
                FinalCombinerInputNV VARIABLE_B_NV ZERO UNSIGNED_IDENTITY_NV RGB
                FinalCombinerInputNV VARIABLE_C_NV ZERO UNSIGNED_IDENTITY_NV RGB
                FinalCombinerInputNV VARIABLE_D_NV PRIMARY_COLOR_NV UNSIGNED_IDENTITY_NV RGB
                FinalCombinerInputNV VARIABLE_G_NV SPARE0_NV UNSIGNED_IDENTITY_NV ALPHA
        """ % {
                "boxtex": self.boxtex.getTexId(),
                "tex": self.tex.getTexId(),
                "comb": self.combiners,
                "color": vob.util.ColorUtil.colorGLString(color)
            }

        def code2(color):
            return parseCombiner("""
                PushAttrib ENABLE_BIT TEXTURE_BIT COLOR_BUFFER_BIT
                CombinerParameterNV CONSTANT_COLOR0_NV %(color)s 1

                Enable REGISTER_COMBINERS_NV

                SPARE0 = TEX0 . COL0
                SPARE0.alpha = TEX0.alpha * COL0.alpha + COL1.blue

                SPARE0.alpha = SPARE0.blue + SPARE0.alpha

                alpha = SPARE0.alpha
                color = CONST0

                BindTexture TEXTURE_2D %(tex)s
                TexParameter TEXTURE_2D TEXTURE_MIN_FILTER LINEAR_MIPMAP_LINEAR
                TexParameter TEXTURE_2D TEXTURE_MAG_FILTER LINEAR
                Enable TEXTURE_2D

                Enable ALPHA_TEST
                AlphaFunc GEQUAL 1.0

                Color 0 0 0 1
        """) % {
                "tex": self.tex2.getTexId(),
                "color": vob.util.ColorUtil.colorGLString(color)
            }

        def code3(color):
            return parseCombiner("""
                PushAttrib ENABLE_BIT TEXTURE_BIT CURRENT_BIT COLOR_BUFFER_BIT

                BindTexture TEXTURE_2D %(tex)s
                TexParameter TEXTURE_2D TEXTURE_MIN_FILTER LINEAR_MIPMAP_LINEAR
                TexParameter TEXTURE_2D TEXTURE_MAG_FILTER LINEAR
                Enable TEXTURE_2D
                TexEnv TEXTURE_ENV TEXTURE_ENV_MODE ADD

                Enable ALPHA_TEST
                AlphaFunc GEQUAL 1.0

        """) % {
                "tex": self.tex3.getTexId(),
                "color": vob.util.ColorUtil.colorGLString(color)
            }

        if type == "square":
            self._content = GLRen.createIrregularQuad(x0, y0, x1, y1, border,
                                                      ripple, 0,
                                                      code(contentColor),
                                                      self.dicefactor)
            self._frame = GLRen.createIrregularQuad(x0, y0, x1, y1,
                                                    border, ripple, 1,
                                                    code(frameColor),
                                                    self.dicefactor)
        elif type == "ellipse":
            texscale = ripple
            ripple_scale = border / ripple

            ratio = float(ripple_scale) / self.ripple_scale

            if ratio < 3. / 4 or ratio > 4. / 3:
                if self.dbg:
                    print "WARNING: anisotropy ratio", round(
                        ratio, 2), "is far from one"

            # Irregu flags
            Y_COLOR = 1
            Y_SECCOLOR = 2
            DOTVEC_COLOR = 4
            INTERP_DOTVEC = 8
            SLICE_1D = 16
            SLICE_2D = 32
            SHIFTS = 64
            INSIDE = 128
            SHIFTS8 = 256

            if GL.hasExtension("GL_NV_register_combiners"):
                self._content = GLRen.createIrregularEdge(
                    8, texscale, 2.0, 128, 0, -1 * ripple_scale * texscale,
                    0 * ripple_scale * texscale, 0, "1 1 1 1 0 0 0 0",
                    "", 3, 0, SLICE_1D + Y_SECCOLOR + INSIDE,
                    code2(contentColor), 1.0)

                self._frame = GLRen.createIrregularEdge(
                    8, texscale, 2.0, 128, 0, -1 * ripple_scale * texscale,
                    0 * ripple_scale * texscale, 0, "1 1 1 1 0 0 0 0", "", 3,
                    0, SLICE_1D + Y_SECCOLOR + DOTVEC_COLOR + INTERP_DOTVEC,
                    code2(frameColor), 1.0)
            else:
                self._content = GLRen.createIrregularEdge(
                    8, texscale, 2.0, 128, 0, -1 * ripple_scale * texscale,
                    0 * ripple_scale * texscale, 0, "1 1 1 1 0 0 0 0", "", 0,
                    0, SLICE_1D + Y_COLOR + INSIDE, code3(contentColor), 1.0)

                self._frame = GLRen.createIrregularEdge(
                    8, texscale, 2.0, 128, 0, -1 * ripple_scale * texscale,
                    0 * ripple_scale * texscale, 0, "1 1 1 1 0 0 0 0", "", 0,
                    0, SLICE_1D + Y_COLOR + SHIFTS,
                    code3(frameColor) + """
                    BlendFunc ZERO ZERO
                    Enable BLEND
                    """, 1.0)

    def getContent(self):
        return self._content

    def getFrame(self):
        return self._frame

    def getBlank(self):
        return self._content
Example #5
0
    def getOptimizedPaper(self,
                          seed,
                          passmask=[1, 1, 1, 1, 1, 1, 1],
                          numcolors=8,
                          minlum=80,
                          blend=0):
        pap = self.getPaper(seed, passmask, numcolors, minlum, blend)

        if not GL.hasExtension("GL_SGIS_generate_mipmap"):
            print "Warning: not returning optimized paper because"
            print "GL_SGIS_generate_mipmap extension is required but not available"
            return pap
        if GL.workaroundStupidBuggyAtiDrivers:
            print "Warning: not returning optimized paper because"
            print "copyTexImage2D has problems on ATI drivers"
            return pap

        # Now, we render a region.
        v = pap.repeat._getSTVectors()

        vs = optimizingWindow.createVobScene()
        vs.map.put(vob.vobs.SolidBackdropVob(java.awt.Color.red))

        cs1 = vs.coords.ortho(0, 0, 0, 0, optimizedPaperSize + 1,
                              optimizedPaperSize + 1)
        cs2 = vs.coords.affine(0, 0, 0, 0, v[0][0], v[0][1], v[1][0], v[1][1])
        vs.map.put(GLRen.createPaperQuad(pap, 0, 0, 1, 1, 1), cs1, cs2)
        optimizingWindow.renderStill(vs, 1)

        tex = GL.createTexture()
        texid = tex.getTexId()
        GL.call("""
	    BindTexture TEXTURE_2D %(texid)s
	    TexParameter TEXTURE_2D TEXTURE_MAX_ANISOTROPY_EXT 2
	    TexParameter TEXTURE_2D  GENERATE_MIPMAP_SGIS TRUE
	    TexParameter TEXTURE_2D  TEXTURE_MIN_FILTER LINEAR_MIPMAP_LINEAR
	    TexParameter TEXTURE_2D  TEXTURE_MAG_FILTER LINEAR
	    BindTexture TEXTURE_2D 0
	""" % locals())

        tex.copyTexImage2D(optimizingWindow.getRenderingSurface(), "FRONT",
                           "TEXTURE_2D", 0, "RGB5", 0, 0, optimizedPaperSize,
                           optimizedPaperSize, 0)

        # Apparently, NV drivers 44.96 (maybe others) have some trouble
        # with the 1x1 mipmap getting clobbered.
        # Usually, that wouldn't be a problem, but papers will be viewed
        # 1) at largely different scales
        # 2) blurred for text background
        # so this matters.
        # We shall forbid the use of that mipmap
        tex.setTexParameter("TEXTURE_2D", "TEXTURE_MAX_LEVEL",
                            optimizedPaperMaxLevel)

        if dbg:
            vob.putil.texture.printTex(tex.getTexId())

        npap = PaperHanger()
        npap.setNPasses(1)
        npap.cachedTexture = tex
        npap.addDepend(tex)  # Need this for clones to survive

        ppass = npap.getPass(0)

        ppass.setSetupcode("""
            PushAttrib ENABLE_BIT TEXTURE_BIT DEPTH_BUFFER_BIT COLOR_BUFFER_BIT CURRENT_BIT
            Disable BLEND
	    ActiveTexture TEXTURE1
	    Disable TEXTURE_2D
	    ActiveTexture TEXTURE0
	    Enable DEPTH_TEST
	    DepthFunc LESS
	    BindTexture TEXTURE_2D %(texid)s
	    TexEnv TEXTURE_ENV TEXTURE_ENV_MODE REPLACE
	    Color 0 1 0
	    Enable TEXTURE_2D
	    SecondaryColorEXT 0 0 0
	""" % locals())

        ppass.setNTexGens(1)
        # t = pap.repeat.vecs
        t = v
        if dbg:
            print "T ", t
        ppass.putNormalTexGen(0, [
            t[0][0],
            t[0][1],
            0,
            0,
            -t[1][0],
            -t[1][1],
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
        ])

        ppass.setTeardowncode("""
            PopAttrib
            ActiveTexture TEXTURE0
        """)

        if dbg:
            print "Ret: ", npap.toString(), npap.getPass(0).getNTexGens()

        return npap
Example #6
0
def makeMipzip(image,
               mipzip,
               maxwidth=-1,
               maxheight=-1,
               texformat=None,
               internalTexFormat=None,
               uncompressedTexType=None):
    """Convert the given image file into a mipzip file.

    image -- the image file name
    mipzip -- the mipzip file name
    maxwidth -- if image is wider than maxwidth, cut off edge
    maxheight -- if image is taller than maxheight, cut off edge
    texformat -- The texture format to use
    internalTexFormat -- The internal texture format to represent the data in
                         for compressed textures, same as texformat
    uncompressedTexType -- The datatype (relevant only for uncompressed
			   textures)
    """

    if texformat == None:
        if not defaultTexFormat:
            _init()
        texformat = defaultTexFormat

    print "TEXFORMAT: ", texformat, defaultTexFormat
    isCompressed = (java.lang.String(texformat).indexOf("COMPRESS") >= 0)

    if internalTexFormat == None:
        internalTexFormat = texformat

    if not isCompressed:
        if uncompressedTexType == None:
            uncompressedTexType = "UNSIGNED_BYTE"

    _init()
    GL.freeQueue()

    im = GL.createImage(image)
    d0 = im.getSize()
    print d0
    d = java.awt.Dimension(
        _clipmax(d0.width, maxwidth),
        _clipmax(d0.height, maxheight),
    )

    w = roundup2(d.width)
    h = roundup2(d.height)
    tex = GL.createTexture()
    tex.loadNull2D('TEXTURE_2D', 0, internalTexFormat, w, h, 0, "RGB", "BYTE")
    print "WH: ", w, h
    tex.loadSubImage(0, im, 0, 0, 0, 0, chomp4(d.width), chomp4(d.height))

    print "Write ", mipzip

    out = zip.ZipOutputStream(java.io.FileOutputStream(mipzip))

    def metaEntry(name, comment):
        entry = zip.ZipEntry(name)
        entry.setComment(comment)
        entry.setSize(0)
        out.putNextEntry(entry)
        out.closeEntry()

    metaEntry("texformat", texformat)

    if not isCompressed:
        metaEntry("internaltexformat", internalTexFormat)
        metaEntry("datatype", uncompressedTexType)

    metaEntry("origsize", "%sx%s" % (d.width / float(w), d.height / float(h)))

    l = 0
    while 1:
        w = int(tex.getLevelParameter(l, "TEXTURE_WIDTH")[0])
        h = int(tex.getLevelParameter(l, "TEXTURE_HEIGHT")[0])
        print "WH: ", w, h

        if isCompressed:
            bytes = tex.getCompressedTexImage(l)
        else:
            bpt = bytesPerTexel(texformat, uncompressedTexType)

            bytes = jarray.zeros(bpt * w * h, "b")
            tex.getTexImage(l, texformat, uncompressedTexType, bytes)

        print "Bytes: ", l, len(bytes)
        entry = zip.ZipEntry(str(l))
        entry.setComment("%sx%s" % (int(w), int(h)))
        entry.setSize(len(bytes))
        out.putNextEntry(entry)
        out.write(bytes)
        out.closeEntry()
        if w == 1 and h == 1: break
        l += 1
    out.close()
    del tex
    java.lang.System.gc()
    GL.freeQueue()
Example #7
0
# MA  02111-1307  USA
# 

from org.nongnu.libvob.gl import GL, GLRen
from org.nongnu.libvob.util import ColorUtil
from vob.putil import cg
from vob.putil.misc import *
from vob.putil.demokeys import *

from vob.paper.texcache import getCachedTexture
tex = getCachedTexture(
    [1024, 512, 0, 4, "RGBA", "RGBA", "noise", 
     [ "freq", "100", "bias", ".5", "scale", ".8" ]]
    )

tex = GL.createTexture()
GL.call("""
BindTexture TEXTURE_2D %s
TexImage2D TEXTURE_2D 0 ALPHA 16 16 0 ALPHA \
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 \
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 \
1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 \
1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 \
1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 \
1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 \
1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 \
1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 \
1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 \
1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 \
1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 \
1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 \