]> git.tdb.fi Git - libs/gl.git/commitdiff
Blender export: Create triangle strips (incomplete)
authorMikko Rasa <tdb@tdb.fi>
Tue, 4 Sep 2007 14:16:17 +0000 (14:16 +0000)
committerMikko Rasa <tdb@tdb.fi>
Tue, 4 Sep 2007 14:16:17 +0000 (14:16 +0000)
mesh_export.py

index 932433360f93649db45d61b6c63b290990a37f0b..016db5bf40c1c3914a30ccb3535d74821538f52c 100644 (file)
@@ -55,13 +55,29 @@ class Face:
                self.smooth_group=None
                self.edges=[]
                self.verts=[v for v in mf.verts]
+               self.flag=False
        
        def __getattr__(self, attr):
                return getattr(self._mface, attr)
 
+       def get_following_vertex(self, *vt):
+               seen=False
+               indices=[v.index for v in vt]
+               for v in self.verts:
+                       if v.index in indices:
+                               seen=True
+                       elif seen:
+                               return v
+
+               if seen:
+                       return self.verts[0]
+
+               return None
+
 
 class SmoothGroup:
-       def __init__(self):
+       def __init__(self, index):
+               self.index=index
                self.faces=[]
                self.verts=[]
 
@@ -94,14 +110,60 @@ class Exporter:
                sg.faces.append(face)
                queue=[face]
                while queue:
-                       f=queue.pop(0)
-                       for e in f.edges:
+                       cur=queue.pop(0)
+                       for e in cur.edges:
                                if e.smooth:
-                                       f2=e.other_face(f)
-                                       if f2 and not f2.smooth_group:
-                                               f2.smooth_group=sg
-                                               sg.faces.append(f2)
-                                               queue.append(f2)
+                                       other=e.other_face(cur)
+                                       if other and not other.smooth_group:
+                                               other.smooth_group=sg
+                                               sg.faces.append(other)
+                                               queue.append(other)
+
+       def create_strip(self, face):
+               edge=None
+               for e in face.edges:
+                       other=e.other_face(face)
+                       if other and other.smooth_group.index==face.smooth_group.index and not other.flag:
+                               edge=e
+                               break
+
+               if not edge:
+                       return None
+
+               v1=face.get_following_vertex(edge.v1, edge.v2)
+               v2=face.get_following_vertex(v1)
+               if len(face.verts)==4:
+                       result=[v2, v1]
+               else:
+                       result=[v1, v2]
+                       
+               print edge.key
+               print [v.index for v in result]
+
+               while 1:
+                       face.flag=True
+                       print "face =",[v.index for v in face.verts]
+                       print [e.key for e in face.edges]
+                       for i in range(2, len(face.verts)):
+                               v=face.get_following_vertex(result[-2], result[-1])
+                               print v.index
+                               result.append(v)
+                       print [v.index for v in result]
+
+                       i1=result[-2].index
+                       i2=result[-1].index
+                       ekey=(min(i1, i2), max(i1, i2))
+                       for e in face.edges:
+                               if e.key==ekey:
+                                       edge=e
+                                       break
+
+                       next=edge.other_face(face)
+                       if next.smooth_group.index!=face.smooth_group.index or next.flag:
+                               break
+                       face=next
+
+               return result
 
        def export(self):
                scene=Blender.Scene.GetCurrent()
@@ -127,10 +189,24 @@ class Exporter:
                smooth_groups=[]
                for f in faces:
                        if not f.smooth_group:
-                               sg=SmoothGroup()
+                               sg=SmoothGroup(len(smooth_groups))
                                smooth_groups.append(sg)
                                self.find_smooth_group(f, sg)
 
+               strips=[]
+               """XXX Stripping and smoothing are currently imcompatible
+               for sg in smooth_groups:
+                       for f in sg.faces:
+                               if not f.flag:
+                                       strip=self.create_strip(f)
+                                       if strip:
+                                               strips.append(strip)"""
+
+               for i in mesh.verts:
+                       print i.co
+
+               #print [v.index for v in strips[0]]
+
                verts=[]
                for sg in smooth_groups:
                        sg.find_vertices()
@@ -138,16 +214,29 @@ class Exporter:
                                v.index=len(verts)
                                verts.append(v)
 
+               #print [v.index for v in strips[0]]
+
                self.out_file.write("vertices NORMAL3_VERTEX3\n{\n")
                for v in verts:
-                       self.out_file.write("\tnormal3 %g %g %g;\n"%tuple(v.no))
-                       self.out_file.write("\tvertex3 %g %g %g;\n"%tuple(v.co))
+                       self.out_file.write("\tnormal3 %f %f %f;\n"%tuple(v.no))
+                       self.out_file.write("\tvertex3 %f %f %f;\n"%tuple(v.co))
                self.out_file.write("};\n")
-               self.out_file.write("batch TRIANGLES\n{\n")
+               for s in strips:
+                       self.out_file.write("batch TRIANGLE_STRIP\n{\n\tindices")
+                       for v in s:
+                               self.out_file.write(" %u"%v.index)
+                       self.out_file.write(";\n};\n")
+
+               first=True
                for f in faces:
-                       for i in range(2, len(f.verts)):
-                               self.out_file.write("\tindices %u %u %u;\n"%(f.verts[0].index, f.verts[i-1].index, f.verts[i].index))
-               self.out_file.write("};\n")
+                       if not f.flag:
+                               if first:
+                                       self.out_file.write("batch TRIANGLES\n{\n")
+                                       first=False
+                               for i in range(2, len(f.verts)):
+                                       self.out_file.write("\tindices %u %u %u;\n"%(f.verts[0].index, f.verts[i-1].index, f.verts[i].index))
+               if not first:
+                       self.out_file.write("};\n")
 
 
 class FrontEnd: