]> git.tdb.fi Git - libs/gl.git/blobdiff - mesh_export.py
Rewrite VertexFormat to support an arbitary amount of components
[libs/gl.git] / mesh_export.py
index fbb282281e3dce09e3ef3af62d14ec264a69f65b..7d21956a6cd1d14bab49c27161f2757c3b60ddd9 100644 (file)
@@ -66,6 +66,8 @@ class Vertex:
                return getattr(self._mvert, attr)
 
        def __cmp__(self, other):
+               if other is None:
+                       return 1
                return cmp(self.index, other.index)
 
        def __str__(self):
@@ -85,6 +87,8 @@ class Face:
                return getattr(self._mface, attr)
 
        def __cmp__(self, other):
+               if other is None:
+                       return 1
                return cmp(self.index, other.index)
 
        def __str__(self):
@@ -96,8 +100,7 @@ class Face:
                verts=self.verts[:]
                if reverse:
                        verts.reverse()
-               indices=[u.index for u in vt]
-               flags=[(v.index in indices) for v in verts]
+               flags=[(v in vt) for v in verts]
                l=len(verts)
                for i in range(l):
                        if flags[i] and not flags[(i+l-1)%l]:
@@ -151,7 +154,7 @@ class Mesh:
        def __getattr__(self, attr):
                return getattr(self._mesh, attr)
 
-       def split_vertices(self, debug=False):
+       def split_vertices(self, find_group_func, debug):
                groups=[]
                for v in self.verts:
                        for f in v.faces:
@@ -160,7 +163,7 @@ class Mesh:
                        vg=[]
                        for f in v.faces:
                                if not f.flag:
-                                       vg.append(self.find_group(v, f))
+                                       vg.append(find_group_func(v, f))
 
                        groups.append(vg)
 
@@ -189,15 +192,14 @@ class Mesh:
                                                if debug:
                                                        print "  Splitting edge %s with faces %s"%(e.key, e.faces)
 
-                                               if not e.smooth:
-                                                       if len(e.faces)>=2:
-                                                               k=e.faces.index(f)
-                                                               e.faces.remove(f)
-                                                               e=Edge(e)
-                                                               f.edges[j]=e
-                                                               e.faces.append(f)
-                                                       else:
-                                                               del self.edges[e.key]
+                                               if e.other_face(f) not in g and len(e.faces)>=2:
+                                                       k=e.faces.index(f)
+                                                       e.faces.remove(f)
+                                                       e=Edge(e)
+                                                       f.edges[j]=e
+                                                       e.faces.append(f)
+                                               else:
+                                                       del self.edges[e.key]
 
                                                if e.v1==self.verts[i]:
                                                        e.v1=v
@@ -205,23 +207,27 @@ class Mesh:
                                                        e.v2=v
 
                                                e.key=make_edge_key(e.v1.index, e.v2.index)
-                                               if not e.smooth:
-                                                       self.edges[e.key]=e
+                                               self.edges[e.key]=e
 
                                        self.verts[i].faces.remove(f)
                                        f.verts[f.verts.index(self.verts[i])]=v
                                        v.faces.append(f)
 
-       def find_group(self, vert, face):
-               face_indices=[f.index for f in vert.faces]
+       def split_smooth(self, debug=False):
+               self.split_vertices(self.find_smooth_group, debug)
+
+       def split_uv(self, debug=False):
+               self.split_vertices(self.find_uv_group, debug)
 
+       def find_smooth_group(self, vert, face):
                face.flag=True
                queue=[face]
 
                for f in queue:
                        for e in f.edges:
                                other=e.other_face(f)
-                               if not other or other.index not in face_indices:
+                               #if not other or other.index not in face_indices:
+                               if other not in vert.faces:
                                        continue
 
                                if e.smooth:
@@ -231,6 +237,16 @@ class Mesh:
 
                return queue
 
+       def find_uv_group(self, vert, face):
+               uv=face.uv[face.verts.index(vert)]
+               face.flag=True
+               group=[face]
+               for f in vert.faces:
+                       if not f.flag and f.uv[f.verts.index(vert)]==uv:
+                               f.flag=True
+                               group.append(f)
+               return group
+
        def compute_normals(self):
                for v in self.verts:
                        if v.faces:
@@ -242,6 +258,11 @@ class Mesh:
                                # XXX Should use edges to compute normal
                                v.no=Blender.Mathutils.Vector(0, 0, 1)
 
+       def compute_uv(self):
+               for v in self.verts:
+                       if v.faces:
+                               v.uv=v.faces[0].uv[v.faces[0].verts.index(v)]
+
        def create_strip(self, face, reverse, debug):
                edge=None
                for e in face.edges:
@@ -304,7 +325,7 @@ class Exporter:
                self.export_lines=True
                self.debug=False
                self.strip_debug=False
-               self.smooth_debug=False
+               self.split_debug=False
 
        def get_locality(self, strip):
                total=0
@@ -340,14 +361,20 @@ class Exporter:
                        ntris=sum([len(f.verts)-2 for f in mesh.faces])
                        print "Starting with %d vertices, %d faces (%d triangles) and %d edges"%(len(mesh.verts), len(mesh.faces), ntris, len(mesh.edges))
 
-               mesh.split_vertices(self.smooth_debug)
+               mesh.split_smooth(self.split_debug)
 
                if self.debug:
-                       ntris=sum([len(f.verts)-2 for f in mesh.faces])
-                       print "After splitting %d vertices, %d faces (%d triangles) and %d edges"%(len(mesh.verts), len(mesh.faces), ntris, len(mesh.edges))
+                       print "After smooth splitting %d vertices and %d edges"%(len(mesh.verts), len(mesh.edges))
 
                mesh.compute_normals()
 
+               if mesh.faceUV:
+                       mesh.split_uv(self.split_debug)
+                       if self.debug:
+                               print "After UV splitting %d vertices and %d edges"%(len(mesh.verts), len(mesh.edges))
+
+                       mesh.compute_uv()
+
                strips=[]
                if self.use_strips:
                        for f in mesh.faces:
@@ -504,7 +531,7 @@ class FrontEnd:
                self.export_lines=Blender.Draw.Create(self.config.get('export_lines', False))
                self.debug=Blender.Draw.Create(self.config.get('debug', False))
                self.strip_debug=Blender.Draw.Create(self.config.get('strip_debug', False))
-               self.smooth_debug=Blender.Draw.Create(self.config.get('smooth_debug', False))
+               self.split_debug=Blender.Draw.Create(self.config.get('split_debug', 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"),
@@ -513,7 +540,7 @@ class FrontEnd:
                                ("Debugging options"),
                                ("Debug", self.debug),
                                ("Debug strips", self.strip_debug),
-                               ("Debug smoothing", self.smooth_debug)])
+                               ("Debug splitting", self.split_debug)])
                if ret:
                        dirname=self.temp_config.get("dirname", Blender.sys.dirname(Blender.Get("filename")))
                        obj=bpy.data.scenes.active.objects.active
@@ -529,7 +556,7 @@ class FrontEnd:
                self.config['export_lines']=self.export_lines.val
                self.config['debug']=self.debug.val
                self.config['strip_debug']=self.strip_debug.val
-               self.config['smooth_debug']=self.smooth_debug.val
+               self.config['split_debug']=self.split_debug.val
                Blender.Registry.SetKey('mspgl_export', self.config, True)
 
                import os
@@ -543,7 +570,7 @@ class FrontEnd:
                exp.export_lines=self.export_lines.val
                exp.debug=self.debug.val
                exp.strip_debug=self.strip_debug.val
-               exp.smooth_debug=self.smooth_debug.val
+               exp.split_debug=self.split_debug.val
                exp.export()