X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=mesh_export.py;h=7f59e671b01decaa542207e681a64ae2e7899d73;hb=83bf78244e92ca6731edd8075f158fcec6e53027;hp=7d21956a6cd1d14bab49c27161f2757c3b60ddd9;hpb=b8fef9be469eb16e54068bac7e275ea225efc71c;p=libs%2Fgl.git diff --git a/mesh_export.py b/mesh_export.py index 7d21956a..7f59e671 100644 --- a/mesh_export.py +++ b/mesh_export.py @@ -20,6 +20,8 @@ class Edge: def __init__(self, me): if me.__class__==Edge: self._medge=me._medge + self.v1=me.v1 + self.v2=me.v2 self.smooth=me.smooth else: self._medge=me @@ -54,6 +56,7 @@ class Vertex: def __init__(self, mv): if mv.__class__==Vertex: self._mvert=mv._mvert + self.no=mv.no self.uv=mv.uv else: self._mvert=mv @@ -61,6 +64,8 @@ class Vertex: self.orig_index=self._mvert.index self.flag=False self.faces=[] + self.tan=None + self.bino=None def __getattr__(self, attr): return getattr(self._mvert, attr) @@ -96,7 +101,7 @@ class Face: __repr__=__str__ - def get_vertices_from(self, reverse, *vt): + def pivot_vertices(self, reverse, *vt): verts=self.verts[:] if reverse: verts.reverse() @@ -111,6 +116,7 @@ class Face: for e in self.edges: if e.key==key: return e + raise KeyError, "No edge %s"%(key,) class Line: @@ -145,7 +151,10 @@ class Mesh: self.lines=[Line(e) for e in self.edges.itervalues() if not e.faces] - smooth_limit=math.cos(m.degr*math.pi/180) + if m.mode&Blender.Mesh.Modes.AUTOSMOOTH: + smooth_limit=math.cos(m.degr*math.pi/180) + else: + smooth_limit=-1 for e in self.edges.itervalues(): e.v1=self.verts[e.v1.index] e.v2=self.verts[e.v2.index] @@ -180,7 +189,7 @@ class Mesh: self.verts.append(v) if debug: - print " -> %d"%v.index + print " -> %d %s"%(v.index, [f.index for f in g]) for f in g: for j in range(len(f.edges)): @@ -263,6 +272,26 @@ class Mesh: if v.faces: v.uv=v.faces[0].uv[v.faces[0].verts.index(v)] + def compute_tbn(self): + for v in self.verts: + v.tan=Blender.Mathutils.Vector() + v.bino=Blender.Mathutils.Vector() + for f in v.faces: + fverts=f.pivot_vertices(False, v) + v1=fverts[1] + v2=fverts[-1] + du1=v1.uv[0]-v.uv[0] + du2=v2.uv[0]-v.uv[0] + dv1=v1.uv[1]-v.uv[1] + dv2=v2.uv[1]-v.uv[1] + div=du1*dv2-du2*dv1 + edge1=fverts[1].co-fverts[0].co + edge2=fverts[-1].co-fverts[0].co + v.tan+=(edge1*dv2-edge2*dv1)/div + v.bino+=(edge2*du1-edge1*du2)/div + v.tan.normalize() + v.bino.normalize() + def create_strip(self, face, reverse, debug): edge=None for e in face.edges: @@ -277,14 +306,14 @@ class Mesh: if debug: print "Starting strip from %s, edge %s, reverse=%s"%([v.index for v in face.verts], (edge.v1.index, edge.v2.index), reverse) - verts=face.get_vertices_from(reverse, edge.v1, edge.v2) + verts=face.pivot_vertices(reverse, edge.v1, edge.v2) if len(verts)==3: result=[verts[-1], verts[0]] else: result=[verts[-2], verts[-1]] while 1: - verts=face.get_vertices_from(reverse, *result[-2:]) + verts=face.pivot_vertices(reverse, *result[-2:]) k=len(result)%2 if debug: print " Adding %s"%face @@ -323,6 +352,7 @@ class Exporter: self.use_degen_tris=True self.optimize_locality=True self.export_lines=True + self.tbn_vecs=False self.debug=False self.strip_debug=False self.split_debug=False @@ -355,7 +385,9 @@ class Exporter: if obj.getType()!="Mesh": raise Exception, "Can only export Mesh data" - mesh=Mesh(obj.getData(mesh=True)) + mesh=Blender.Mesh.New("export_tmp") + mesh.getFromObject(obj) + mesh=Mesh(mesh) if self.debug: ntris=sum([len(f.verts)-2 for f in mesh.faces]) @@ -374,6 +406,8 @@ class Exporter: print "After UV splitting %d vertices and %d edges"%(len(mesh.verts), len(mesh.edges)) mesh.compute_uv() + if self.tbn_vecs: + mesh.compute_tbn() strips=[] if self.use_strips: @@ -479,9 +513,13 @@ class Exporter: self.out_file.write("vertices NORMAL3") if mesh.faceUV: self.out_file.write("_TEXCOORD2") + if self.tbn_vecs: + self.out_file.write("_ATTRIB33_ATTRIB34") self.out_file.write("_VERTEX3\n{\n") norm=None uv=None + tan=None + bino=None for v in mesh.verts: if v.no!=norm: self.out_file.write("\tnormal3 %f %f %f;\n"%tuple(v.no)) @@ -489,6 +527,12 @@ class Exporter: if v.uv!=uv: self.out_file.write("\ttexcoord2 %f %f;\n"%tuple(v.uv)) uv=v.uv + if v.tan!=tan: + self.out_file.write("\tattrib3 3 %f %f %f;\n"%tuple(v.tan)) + tan=v.tan + if v.bino!=bino: + self.out_file.write("\tattrib3 4 %f %f %f;\n"%tuple(v.bino)) + bino=v.bino self.out_file.write("\tvertex3 %f %f %f;\n"%tuple(v.co)) self.out_file.write("};\n") for s in strips: @@ -529,6 +573,7 @@ class FrontEnd: self.use_degen_tris=Blender.Draw.Create(self.config.get('use_degen_tris', True)) self.optimize_locality=Blender.Draw.Create(self.config.get('optimize_locality', True)) self.export_lines=Blender.Draw.Create(self.config.get('export_lines', False)) + self.tbn_vecs=Blender.Draw.Create(self.config.get('tbn_vecs', False)) self.debug=Blender.Draw.Create(self.config.get('debug', False)) self.strip_debug=Blender.Draw.Create(self.config.get('strip_debug', False)) self.split_debug=Blender.Draw.Create(self.config.get('split_debug', False)) @@ -536,7 +581,8 @@ class FrontEnd: [("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), - ("Export lines", self.export_lines), + ("Export lines", self.export_lines, "Export lone edges as lines"), + ("Compute T/B vecs", self.tbn_vecs, "Compute tangent/binormal vectors for bumpmapping"), ("Debugging options"), ("Debug", self.debug), ("Debug strips", self.strip_debug), @@ -554,6 +600,7 @@ class FrontEnd: self.config['use_degen_tris']=self.use_degen_tris.val self.config['optimize_locality']=self.optimize_locality.val self.config['export_lines']=self.export_lines.val + self.config['tbn_vecs']=self.tbn_vecs.val self.config['debug']=self.debug.val self.config['strip_debug']=self.strip_debug.val self.config['split_debug']=self.split_debug.val @@ -568,6 +615,7 @@ class FrontEnd: exp.use_degen_tris=self.use_degen_tris.val exp.optimize_locality=self.optimize_locality.val exp.export_lines=self.export_lines.val + exp.tbn_vecs=self.tbn_vecs.val exp.debug=self.debug.val exp.strip_debug=self.strip_debug.val exp.split_debug=self.split_debug.val