Merged 15170:15635 from trunk (no conflicts or even merges)
[blender.git] / source / gameengine / Rasterizer / RAS_MeshObject.cpp
index e4b654343e79c93136c6ad14449bb74a69aaccae..4420f16c56d63c146696fd7d8ce809e62575ffd2 100644 (file)
@@ -63,11 +63,12 @@ KX_ArrayOptimizer::~KX_ArrayOptimizer()
 
 
 
-RAS_MeshObject::RAS_MeshObject(int lightlayer)
+RAS_MeshObject::RAS_MeshObject(Mesh* mesh, int lightlayer)
        : m_bModified(true),
        m_lightlayer(lightlayer),
        m_zsort(false),
        m_MeshMod(true),
+       m_mesh(mesh),
        m_class(0)
 {
 }
@@ -256,27 +257,30 @@ int RAS_MeshObject::FindOrAddVertex(int vtxarray,
                                                                        const MT_Vector4& tangent,
                                                                        const unsigned int rgbacolor,
                                                                        const MT_Vector3& normal,
+                                                                       bool flat,
                                                                        RAS_IPolyMaterial* mat,
-                                                                       int orgindex)
+                                                                       int origindex)
 {
-       KX_ArrayOptimizer* ao = GetArrayOptimizer(mat);//*(m_matVertexArrays[*mat]);
+       KX_ArrayOptimizer* ao = GetArrayOptimizer(mat);
        
        int numverts = ao->m_VertexArrayCache1[vtxarray]->size();//m_VertexArrayCount[vtxarray];
-       RAS_TexVert newvert(xyz,uv,uv2,tangent,rgbacolor,normal, 0);
+       RAS_TexVert newvert(xyz,uv,uv2,tangent,rgbacolor,normal, flat? TV_CALCFACENORMAL: 0,origindex);
+
 #define KX_FIND_SHARED_VERTICES
 #ifdef KX_FIND_SHARED_VERTICES
-       
-       for (std::vector<RAS_MatArrayIndex>::iterator it = m_xyz_index_to_vertex_index_mapping[orgindex].begin();
-            it != m_xyz_index_to_vertex_index_mapping[orgindex].end();
-            it++)
-       {
-               if ((*it).m_arrayindex1 == ao->m_index1 &&
-                       (*it).m_array == vtxarray && 
-                       *(*it).m_matid == *mat &&
-                       (*ao->m_VertexArrayCache1[vtxarray])[(*it).m_index].closeTo(&newvert)
-                       )
+       if(!flat) {
+               for (std::vector<RAS_MatArrayIndex>::iterator it = m_xyz_index_to_vertex_index_mapping[origindex].begin();
+                        it != m_xyz_index_to_vertex_index_mapping[origindex].end();
+                        it++)
                {
-                       return (*it).m_index;
+                       if ((*it).m_arrayindex1 == ao->m_index1 &&
+                               (*it).m_array == vtxarray && 
+                               *(*it).m_matid == *mat &&
+                               (*ao->m_VertexArrayCache1[vtxarray])[(*it).m_index].closeTo(&newvert)
+                               )
+                       {
+                               return (*it).m_index;
+                       }
                }
        }
 #endif // KX_FIND_SHARED_VERTICES
@@ -289,22 +293,18 @@ int RAS_MeshObject::FindOrAddVertex(int vtxarray,
        idx.m_array = vtxarray;
        idx.m_index = numverts;
        idx.m_matid = mat;
-       m_xyz_index_to_vertex_index_mapping[orgindex].push_back(idx); 
+       m_xyz_index_to_vertex_index_mapping[origindex].push_back(idx); 
        
        return numverts;
 }
 
-
-
-const vecVertexArray& RAS_MeshObject::GetVertexCache (RAS_IPolyMaterial* mat)
+vecVertexArray& RAS_MeshObject::GetVertexCache (RAS_IPolyMaterial* mat)
 {
-       KX_ArrayOptimizer* ao = GetArrayOptimizer(mat);//*(m_matVertexArrays[*mat]);
+       KX_ArrayOptimizer* ao = GetArrayOptimizer(mat);
 
        return ao->m_VertexArrayCache1;
 }
 
-
-
 int RAS_MeshObject::GetVertexArrayLength(RAS_IPolyMaterial* mat)
 {
        int len = 0;
@@ -358,7 +358,7 @@ RAS_TexVert* RAS_MeshObject::GetVertex(unsigned int matid,
 
 const vecIndexArrays& RAS_MeshObject::GetIndexCache (RAS_IPolyMaterial* mat)
 {
-       KX_ArrayOptimizer* ao = GetArrayOptimizer(mat);//*(m_matVertexArrays[*mat]);
+       KX_ArrayOptimizer* ao = GetArrayOptimizer(mat);
 
        return ao->m_IndexArrayCache1;
 }
@@ -367,16 +367,27 @@ const vecIndexArrays& RAS_MeshObject::GetIndexCache (RAS_IPolyMaterial* mat)
 
 KX_ArrayOptimizer* RAS_MeshObject::GetArrayOptimizer(RAS_IPolyMaterial* polymat)
 {
-       KX_ArrayOptimizer** aop = (m_matVertexArrayS[*polymat]);
+       KX_ArrayOptimizer** aop = m_matVertexArrayS[polymat];
 
-       if (aop)
+       if(aop)
                return *aop;
        
+       // didn't find array, but an array might already exist
+       // for a material equal to this one
+       for(int i=0;i<m_matVertexArrayS.size();i++) {
+               RAS_IPolyMaterial *mat = (RAS_IPolyMaterial*)(m_matVertexArrayS.getKey(i)->getValue());
+               if(*mat == *polymat) {
+                       m_matVertexArrayS.insert(polymat, *m_matVertexArrayS.at(i));
+                       return *m_matVertexArrayS.at(i);
+               }
+       }
+
+       // create new array
        int numelements = m_matVertexArrayS.size();
        m_sortedMaterials.push_back(polymat);
-               
+
        KX_ArrayOptimizer* ao = new KX_ArrayOptimizer(numelements);
-       m_matVertexArrayS.insert(*polymat,ao);
+       m_matVertexArrayS.insert(polymat, ao);
        
        return ao;
 }
@@ -459,7 +470,7 @@ RAS_TexVert* RAS_MeshObject::GetVertex(short array,
                                                                           unsigned int index,
                                                                           RAS_IPolyMaterial* polymat)
 {
-        KX_ArrayOptimizer* ao = GetArrayOptimizer(polymat);//*(m_matVertexArrays[*polymat]);
+        KX_ArrayOptimizer* ao = GetArrayOptimizer(polymat);
        return &((*(ao->m_VertexArrayCache1)[array])[index]);
 }
 
@@ -467,13 +478,19 @@ RAS_TexVert* RAS_MeshObject::GetVertex(short array,
 
 void RAS_MeshObject::ClearArrayData()
 {
-       for (int i=0;i<m_matVertexArrayS.size();i++)
-       {
+       for (int i=0;i<m_matVertexArrayS.size();i++) {
                KX_ArrayOptimizer** ao = m_matVertexArrayS.at(i);
+
+               // we have duplicate entries, only free once
+               for(int j=i+1;j<m_matVertexArrayS.size();j++) {
+                       if(ao == m_matVertexArrayS.at(j)) {
+                               ao = NULL;
+                               break;
+                       }
+               }
+
                if (ao)
-               {
                        delete *ao;
-               }
        }
 }