]> git.tdb.fi Git - libs/gl.git/commitdiff
Blender exporter:
authorMikko Rasa <tdb@tdb.fi>
Sun, 21 Oct 2007 18:07:02 +0000 (18:07 +0000)
committerMikko Rasa <tdb@tdb.fi>
Sun, 21 Oct 2007 18:07:02 +0000 (18:07 +0000)
Fix big strip generation
Improve locality optimizer
Add user interface for specifying flags

mesh_export.py

index d63206dd4184eb9bdd0abc2f156062dd779ab7bf..dd8e5414e95ab3dcb3531eb968aec7d96c5f215e 100644 (file)
@@ -52,6 +52,7 @@ class Vertex:
                        self._mvert=mv
                        self.uv=None
                self.orig_index=self._mvert.index
+               self.flag=False
 
        def __getattr__(self, attr):
                return getattr(self._mvert, attr)
@@ -170,6 +171,7 @@ class Exporter:
                        self.out_file=file(fn, "w")
                self.use_strips=True
                self.use_degen_tris=True
+               self.optimize_locality=True
                self.debug=False
                self.strip_debug=False
 
@@ -242,6 +244,20 @@ class Exporter:
                                total+=1.0/(abs(strip[i].index-strip[i-1].index))
                return total/len(strip)
 
+       def get_followers(self, strip):
+               result={}
+               for i in range(len(strip)-1):
+                       v=strip[i]
+                       n=strip[i+1]
+                       if v.index!=n.index:
+                               if v.index not in result:
+                                       result[v.index]={}
+                               if n.index not in result[v.index]:
+                                       result[v.index][n.index]=1
+                               else:
+                                       result[v.index][n.index]+=1
+               return result
+
        def export(self):
                scene=bpy.data.scenes.active
 
@@ -332,16 +348,13 @@ class Exporter:
 
                                for f in faces:
                                        if not f.flag:
-                                               if big_strip:
-                                                       big_strip+=[big_strip[-1], f.verts[0]]
                                                if len(big_strip)%2:
-                                                       big_strip+=[f.verts[i] for i in (-1, -2, 0)]
-                                                       if len(f.verts)==4:
-                                                               big_strip.append(f.verts[1])
+                                                       order=(-1, -2, 0, 1)
                                                else:
-                                                       big_strip+=[f.verts[i] for i in (0, 1, -1)]
-                                                       if len(f.verts)==4:
-                                                               big_strip.append(f.verts[-2])
+                                                       order=(0, 1, -1, -2)
+                                               if big_strip:
+                                                       big_strip+=[big_strip[-1], f.verts[order[0]]]
+                                               big_strip+=[f.verts[i] for i in order[:len(f.verts)]]
                                                f.flag=True
 
                                strips=[big_strip]
@@ -351,22 +364,46 @@ class Exporter:
                                        print "Big strip has %d indices"%len(big_strip)
 
                if self.debug:
-                       print "%.2f vertices per triangle"%(float(nind)/ntris)
+                       print "%.2f indices per triangle"%(float(nind)/ntris)
                        print "Locality before optimization: "+" ".join(["%.3f"%self.get_locality(s) for s in strips])
 
-               used=[False]*len(verts)
-               i=0
-               for s in strips:
-                       for v in s:
-                               if not used[v.index]:
-                                       used[v.index]=True
-                                       v.index=i
-                                       i+=1
+               if self.optimize_locality and self.use_strips:
+                       followers={}
+                       for s in strips:
+                               followers.update(self.get_followers(s))
 
-               verts.sort(cmp=lambda x,y: cmp(x.index, y.index))
+                       verts2=[]
+                       vert=strips[0][0]
+                       while 1:
+                               vert.flag=True
+                               verts2.append(vert)
+
+                               next=None
+                               if vert.index in followers:
+                                       flw=followers[vert.index]
+                                       best=0
+                                       for n in flw:
+                                               if flw[n]>best and not verts[n].flag:
+                                                       next=verts[n]
+                                                       best=flw[n]+0.9/abs(vert.index-n)
+
+                               if not next:
+                                       for v in verts:
+                                               if not v.flag:
+                                                       next=v
+                                                       break
+                                       if not next:
+                                               break
 
-               if self.debug:
-                       print "Locality after optimization: "+" ".join(["%.3f"%self.get_locality(s) for s in strips])
+                               vert=next
+
+                       verts=verts2
+
+                       for i in range(len(verts)):
+                               verts[i].index=i
+
+                       if self.debug:
+                               print "Locality after optimization: "+" ".join(["%.3f"%self.get_locality(s) for s in strips])
 
                self.out_file.write("vertices NORMAL3")
                if mesh.faceUV:
@@ -407,14 +444,31 @@ class Exporter:
 
 class FrontEnd:
        def run(self):
-               #self.export(None)
-               Blender.Window.FileSelector(self.export, "Export MSP GL mesh", Blender.sys.makename(ext='.mesh'))
+               self.use_strips=Blender.Draw.Create(True)
+               self.use_degen_tris=Blender.Draw.Create(True)
+               self.optimize_locality=Blender.Draw.Create(True)
+               self.debug=Blender.Draw.Create(False)
+               self.strip_debug=Blender.Draw.Create(False)
+               ret=Blender.Draw.PupBlock("Export MSP GL mesh",
+                       [("Use strips", self.use_strips, "Generage OpenGL triangle strips"),
+                               ("Use degen tris", self.use_degen_tris, "Use degenerate triangles to combine triangle strips"),
+                               ("Optimize locality", self.optimize_locality),
+                               ("Debugging options"),
+                               ("Debug", self.debug),
+                               ("Debug strips", self.strip_debug)])
+               if ret:
+                       Blender.Window.FileSelector(self.export, "Export MSP GL mesh", Blender.sys.makename(ext='.mesh'))
+
+       def draw(self):
+               pass
 
        def export(self, fn):
                exp=Exporter(fn)
-               #exp.use_degen_tris=False
-               #exp.debug=True
-               #exp.strip_debug=True
+               exp.use_strips=self.use_strips.val
+               exp.use_degen_tris=self.use_degen_tris.val
+               exp.optimize_locality=self.optimize_locality.val
+               exp.debug=self.debug.val
+               exp.strip_debug=self.strip_debug.val
                exp.export()