mo' better

This commit is contained in:
David Rose 2009-02-05 03:30:40 +00:00
parent 8e6eb98afe
commit 163b4a0093

View File

@ -36,9 +36,11 @@ class TexMemWatcher(DirectObject):
self.winSize = (300, 300) self.winSize = (300, 300)
name = 'Texture Memory' name = 'Texture Memory'
props = WindowProperties() props = WindowProperties()
props.setOrigin(100, 100)
props.setSize(*self.winSize) props.setSize(*self.winSize)
props.setTitle(name) props.setTitle(name)
props.setFullscreen(False) props.setFullscreen(False)
props.setUndecorated(False)
fbprops = FrameBufferProperties.getDefault() fbprops = FrameBufferProperties.getDefault()
flags = GraphicsPipe.BFFbPropsOptional | GraphicsPipe.BFRequireWindow flags = GraphicsPipe.BFFbPropsOptional | GraphicsPipe.BFRequireWindow
@ -239,16 +241,22 @@ class TexMemWatcher(DirectObject):
self.repack() self.repack()
else: else:
# Pack in just the newly-loaded textures. overflowCount = sum(map(lambda tp: tp.overflowed, self.texPlacements.keys()))
if overflowCount:
# Shouldn't be overflowing any more. Better repack.
self.repack()
# Sort the regions from largest to smallest to maximize else:
# packing effectiveness. # Pack in just the newly-loaded textures.
texRecords.sort(key = lambda tr: (-tr.w, -tr.h))
self.overflowing = False # Sort the regions from largest to smallest to maximize
for tr in texRecords: # packing effectiveness.
self.placeTexture(tr) texRecords.sort(key = lambda tr: (-tr.w, -tr.h))
self.texRecords[tr.tex] = tr
self.overflowing = False
for tr in texRecords:
self.placeTexture(tr)
self.texRecords[tr.tex] = tr
return task.again return task.again
@ -370,6 +378,8 @@ class TexMemWatcher(DirectObject):
self.overflowing = True self.overflowing = True
tp = self.findHole(tr.w, tr.h, allowOverflow = True) tp = self.findHole(tr.w, tr.h, allowOverflow = True)
if tp: if tp:
if tp.p[1] > self.w or tp.p[3] > self.h:
tp.overflowed = 1
tr.placements = [tp] tr.placements = [tp]
tr.makeCard(self) tr.makeCard(self)
self.texPlacements[tp] = tr self.texPlacements[tp] = tr
@ -460,9 +470,12 @@ class TexMemWatcher(DirectObject):
nextY = min(nextY, overlap.p[3]) nextY = min(nextY, overlap.p[3])
# Shorten the available region. # Shorten the available region.
tpw = overlap.p[0] - x tpw0 = overlap.p[0] - x
if tpw <= 0.0: if tpw0 <= 0.0:
break break
if tpw0 == tpw:
tpw0 *= 0.999 # imprecision hack
tpw = tpw0
tph = area / tpw tph = area / tpw
assert nextX > x assert nextX > x
@ -553,9 +566,13 @@ class TexMemWatcher(DirectObject):
if tpw0 * tph > tpw * tph0: if tpw0 * tph > tpw * tph0:
# Shortening width results in larger. # Shortening width results in larger.
if tpw == tpw0:
tpw0 *= 0.999 # imprecision hack
tpw = tpw0 tpw = tpw0
else: else:
# Shortening height results in larger. # Shortening height results in larger.
if tph == tph0:
tph0 *= 0.999 # imprecision hack
tph = tph0 tph = tph0
assert nextX > x assert nextX > x
@ -609,9 +626,13 @@ class TexRecord:
def setActive(self, flag): def setActive(self, flag):
self.active = flag self.active = flag
if self.active: if self.active:
self.backing.clearColor()
self.matte.clearColor() self.matte.clearColor()
self.card.clearColor()
else: else:
self.matte.setColor((0.4, 0.4, 0.4, 1)) self.backing.setColor((0.2, 0.2, 0.2, 1), 2)
self.matte.setColor((0.2, 0.2, 0.2, 1), 2)
self.card.setColor((0.4, 0.4, 0.4, 1), 2)
def makeCard(self, tmw): def makeCard(self, tmw):
if self.root: if self.root:
@ -619,6 +640,9 @@ class TexRecord:
root = NodePath('root') root = NodePath('root')
# A backing to put behind the card.
backing = root.attachNewNode('backing')
# A card to display the texture. # A card to display the texture.
card = root.attachNewNode('card') card = root.attachNewNode('card')
@ -636,6 +660,12 @@ class TexRecord:
cy = (b + t) * 0.5 cy = (b + t) * 0.5
shrinkMat = Mat4.translateMat(-cx, 0, -cy) * Mat4.scaleMat(0.9) * Mat4.translateMat(cx, 0, cy) shrinkMat = Mat4.translateMat(-cx, 0, -cy) * Mat4.scaleMat(0.9) * Mat4.translateMat(cx, 0, cy)
cm = CardMaker('backing')
cm.setFrame(l, r, b, t)
cm.setColor(0.1, 0.3, 0.5, 1)
c = backing.attachNewNode(cm.generate())
c.setMat(shrinkMat)
cm = CardMaker('card') cm = CardMaker('card')
cm.setFrame(l, r, b, t) cm.setFrame(l, r, b, t)
if p.rotated: if p.rotated:
@ -658,28 +688,21 @@ class TexRecord:
f2 = f1.copyTo(frame) f2 = f1.copyTo(frame)
f2.setMat(shrinkMat) f2.setMat(shrinkMat)
# Instead of enabling transparency, we set a color blend matte.setBin('fixed', 0)
# attrib. We do this because plain transparency would also
# enable an alpha test, which we don't want; we want to draw
# every pixel.
card.setAttrib(ColorBlendAttrib.make(
ColorBlendAttrib.MAdd,
ColorBlendAttrib.OIncomingAlpha,
ColorBlendAttrib.OOneMinusIncomingAlpha))
card.setBin('fixed', 0)
card.setTexture(self.tex)
card.setY(-1) # the card gets pulled back, so the matte will z-test it out.
card.setDepthWrite(True)
card.setDepthTest(True)
#card.flattenStrong()
self.card = card
matte.setBin('fixed', 10)
matte.setDepthTest(True)
#matte.flattenStrong() #matte.flattenStrong()
self.matte = matte self.matte = matte
frame.setBin('fixed', 20) backing.setBin('fixed', 10)
#backing.flattenStrong()
self.backing = backing
card.setTransparency(TransparencyAttrib.MAlpha)
card.setBin('fixed', 20)
card.setTexture(self.tex)
#card.flattenStrong()
self.card = card
frame.setBin('fixed', 30)
#frame.flattenStrong() #frame.flattenStrong()
self.frame = frame self.frame = frame
@ -691,6 +714,7 @@ class TexPlacement:
def __init__(self, l, r, b, t): def __init__(self, l, r, b, t):
self.p = (l, r, b, t) self.p = (l, r, b, t)
self.rotated = False self.rotated = False
self.overflowed = 0
def intersects(self, other): def intersects(self, other):
""" Returns True if the placements intersect, False """ Returns True if the placements intersect, False