###
### applies to revision 110
### supersedes previous horde3d-blender-exporter-performance-patch

This exporter patch will fix 3 things:

1.  Speeding things up (50 times faster for 35000 quads model) by
keeping tracks of reverse mapping of TriGroup#posIndexToVertices.

2.  Bone weights were normalized only for numverts >4 polygons, but
now with this patch, weights are normalized for all polygons.  This
behavior is more close to Blender's one.  This also prevents weight
could be >1.0 which does not fit with Horde3D's implementation.

3.  Converter#__AdditionalJointMat is now properly initialized.

--- /home/hayasaka/tmp/horde3d/svn-vendor/Horde3D/trunk/Tools/Exporters/Blender/Horde3DExport.py	2008-11-10 02:26:25.000000000 +0900
+++ Tools/Exporters/Blender/Horde3DExport.py	2008-11-22 05:30:38.000000000 +0900
@@ -337,6 +337,7 @@
                self.vertRStart, self.vertREnd = vStart,0 
                self.material = None
                self.posIndexToVertices = []
+               self.vertexToPosIndex = {} # reverse mapping of self.posIndexToVertices
 
 class HordeMesh():
        def __init__(self, name, bName, matRel, matAbs):
@@ -408,7 +409,7 @@
                self.__matPath = matPath
                self.__animPath = animPath
                self.__images = []
-               __AdditionalJointMat = Matrix()
+               self.__AdditionalJointMat = Matrix()
 
        def convertModel(self, scn):
                ##################
@@ -693,14 +694,9 @@
                                                normal = Vector(vert.no)
 
                                        found = False
-                                       pindex = -1
+                                       pindex = t.vertexToPosIndex.get(vert.index, -1)
                                        pi = 0
                                        vertCo = Vector(vert.co)
-                                       for posindex in t.posIndexToVertices:
-                                               if posindex[0] == vert.index:
-                                                       pindex = pi
-                                                       break
-                                               pi += 1
 
                                        if pindex != -1:
                                                pi = 1
@@ -728,6 +724,7 @@
                                        else:
                                                # Position not found -> append new vertex in new position list
                                                self.__indices.append(vlen)
+                                               t.vertexToPosIndex[vert.index] = len(t.posIndexToVertices)
                                                t.posIndexToVertices.append([vert.index, vlen])
                                                t.count += 1
                                                self.__vertices.append( Vertex(vertCo, normal, uv, len(t.posIndexToVertices)-1, vert.index) )
@@ -945,8 +942,9 @@
                                # Trim weights and joints
                                vert.weights = vert.weights[:4]
                                vert.joints = vert.joints[:4]
-                               # Make sure weights add up to 1
-                               weightSum = float(sum(vert.weights))
+                       # Make sure weights add up to 1
+                       weightSum = float(sum(vert.weights))
+                       if weightSum != 0:
                                for w in range(len(vert.weights)):
                                        vert.weights[w] = vert.weights[w] / weightSum
 

