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
def __init__(self, mv):
if mv.__class__==Vertex:
self._mvert=mv._mvert
+ self.no=mv.no
self.uv=mv.uv
else:
self._mvert=mv
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)
__repr__=__str__
- def get_vertices_from(self, reverse, *vt):
+ def pivot_vertices(self, reverse, *vt):
verts=self.verts[:]
if reverse:
verts.reverse()
for e in self.edges:
if e.key==key:
return e
+ raise KeyError, "No edge %s"%(key,)
class Line:
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)):
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:
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
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
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:
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))
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:
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))
[("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),
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
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