+
+class VertexCache:
+ def __init__(self, size):
+ self.size = size
+ self.slots = [-1]*self.size
+
+ def fetch(self, v):
+ hit = v.index in self.slots
+ if hit:
+ self.slots.remove(v.index)
+ self.slots.append(v.index)
+ if not hit:
+ del self.slots[0]
+ return hit
+
+ def fetch_strip(self, strip):
+ hits = 0
+ for v in strip:
+ if self.fetch(v):
+ hits += 1
+ return hits
+
+ def test_strip(self, strip):
+ hits = 0
+ for i in range(len(strip)):
+ if i>=self.size:
+ break
+ if strip[i].index in self.slots[i:]:
+ hits += 1
+ return hits
+
+
+class Exporter:
+ def __init__(self, fn):
+ self.filename = fn
+ if fn==None:
+ self.out_file = sys.stdout
+ else:
+ self.out_file = file(fn, "w")
+ self.use_strips = True
+ self.use_degen_tris = True
+ self.max_strip_len = 1024
+ self.optimize_cache = False
+ self.cache_size = 64
+ self.export_lines = True
+ self.tbn_vecs = False
+ self.debug = False
+ self.strip_debug = False
+ self.split_debug = False
+
+ def stripify(self, mesh):
+ for f in mesh.faces:
+ f.flag = False
+
+ strips = []
+
+ while 1:
+ best = 5
+ face = None
+ for f in mesh.faces:
+ if f.flag:
+ continue
+ score = 0
+ for e in f.edges:
+ other = e.other_face(f)
+ if other and not other.flag:
+ score += 1
+ if score>0 and score<best:
+ face = f
+ best = score
+
+ if not face:
+ break
+
+ strip = mesh.create_strip(face, self.max_strip_len, self.strip_debug)
+ if strip:
+ strips.append(strip)
+
+ loose = [f for f in mesh.faces if not f.flag]
+
+ if self.debug:
+ print "%d strips:"%len(strips)
+ for i in range(len(strips)):
+ print " %d: %d indices"%(i, len(strips[i]))
+ print "%d loose faces"%len([f for f in mesh.faces if not f.flag])
+ nind = sum([len(s) for s in strips])+sum([len(f.verts) for f in loose])
+ print "%d indices total"%nind
+
+ if self.use_degen_tris and strips:
+ big_strip = []
+
+ cache = None
+ total_hits = 0
+ if self.optimize_cache:
+ cache = VertexCache(self.cache_size)
+
+ while strips:
+ best = 0
+ if cache:
+ best_hits = 0
+ for i in range(len(strips)):
+ hits = cache.test_strip(strips[i])
+ if hits>best_hits:
+ best = i
+ best_hits = hits
+
+ s = strips[best]
+
+ if big_strip:
+ glue = [big_strip[-1], s[0]]
+ if len(big_strip)%2:
+ glue += [s[0]]
+
+ big_strip += glue
+ if cache:
+ total_hits += cache.fetch_strip(glue)
+
+ big_strip += s
+ if cache:
+ total_hits += cache.fetch_strip(s)
+
+ del strips[best]
+
+ for f in loose:
+ if len(big_strip)%2:
+ order = (-1, -2, 0, 1)
+ else:
+ order = (0, 1, -1, -2)
+ verts = [f.verts[i] for i in order[:len(f.verts)]]
+ if big_strip:
+ glue = [big_strip[-1], verts[0]]
+ big_strip += glue
+ if cache:
+ total_hits += cache.fetch_strip(glue)
+ big_strip += verts
+ if cache:
+ total_hits += cache.fetch_strip(verts)
+
+ strips = [big_strip]
+ loose = []
+
+ if self.debug:
+ nind = len(big_strip)
+ print "Big strip has %d indices"%nind
+ if self.optimize_cache:
+ print "%d cache hits"%total_hits
+
+ if self.debug:
+ ntris = sum([len(f.verts)-2 for f in mesh.faces])
+ print "%.2f indices per triangle"%(float(nind)/max(ntris, 1))
+
+ return strips, loose
+