Merge branch 'master' into blender2.8
[blender.git] / source / blender / collada / MeshImporter.cpp
index da85038bf909835a62187493ecaae136e24572ec..4c6d00a79cb7c5f764a53a57b868a8c2f4560968 100644 (file)
@@ -207,7 +207,11 @@ void VCOLDataWrapper::get_vcol(int v_index, MLoopCol *mloopcol)
 
 }
 
-MeshImporter::MeshImporter(UnitConverter *unitconv, ArmatureImporter *arm, Scene *sce) : unitconverter(unitconv), scene(sce), armature_importer(arm) {
+MeshImporter::MeshImporter(UnitConverter *unitconv, ArmatureImporter *arm, Scene *sce, ViewLayer *view_layer):
+       unitconverter(unitconv), 
+       scene(sce),
+       view_layer(view_layer),
+       armature_importer(arm) {
 }
 
 bool MeshImporter::set_poly_indices(MPoly *mpoly, MLoop *mloop, int loop_index, unsigned int *indices, int loop_count)
@@ -472,11 +476,9 @@ void MeshImporter::allocate_poly_data(COLLADAFW::Mesh *collada_mesh, Mesh *me)
                                COLLADAFW::MeshVertexData::InputInfos *info = collada_mesh->getUVCoords().getInputInfosArray()[i];
                                COLLADAFW::String &uvname = info->mName;
                                // Allocate space for UV_data
-                               CustomData_add_layer_named(&me->pdata, CD_MTEXPOLY, CD_DEFAULT, NULL, me->totpoly, uvname.c_str());
                                CustomData_add_layer_named(&me->ldata, CD_MLOOPUV, CD_DEFAULT, NULL, me->totloop, uvname.c_str());
                        }
                        // activate the first uv map
-                       me->mtpoly  = (MTexPoly *)CustomData_get_layer_n(&me->pdata, CD_MTEXPOLY, 0);
                        me->mloopuv = (MLoopUV *) CustomData_get_layer_n(&me->ldata, CD_MLOOPUV, 0);
                }
 
@@ -877,48 +879,6 @@ std::string *MeshImporter::get_geometry_name(const std::string &mesh_name)
        return NULL;
 }
 
-MTex *MeshImporter::assign_textures_to_uvlayer(COLLADAFW::TextureCoordinateBinding &ctexture,
-                                               Mesh *me, TexIndexTextureArrayMap& texindex_texarray_map,
-                                               MTex *color_texture)
-{
-       const COLLADAFW::TextureMapId texture_index = ctexture.getTextureMapId();
-       size_t setindex = ctexture.getSetIndex();
-       std::string uvname = ctexture.getSemantic();
-       
-       if (setindex == -1) return NULL;
-       
-       const CustomData *data = &me->fdata;
-       int layer_index = CustomData_get_layer_index(data, CD_MTFACE);
-
-       if (layer_index == -1) return NULL;
-
-       CustomDataLayer *cdl = &data->layers[layer_index + setindex];
-       
-       /* set uvname to bind_vertex_input semantic */
-       BLI_strncpy(cdl->name, uvname.c_str(), sizeof(cdl->name));
-
-       if (texindex_texarray_map.find(texture_index) == texindex_texarray_map.end()) {
-               
-               fprintf(stderr, "Cannot find texture array by texture index.\n");
-               return color_texture;
-       }
-       
-       std::vector<MTex *> textures = texindex_texarray_map[texture_index];
-       
-       std::vector<MTex *>::iterator it;
-       
-       for (it = textures.begin(); it != textures.end(); it++) {
-               
-               MTex *texture = *it;
-               
-               if (texture) {
-                       BLI_strncpy(texture->uvname, uvname.c_str(), sizeof(texture->uvname));
-                       if (texture->mapto == MAP_COL) color_texture = texture;
-               }
-       }
-       return color_texture;
-}
-
 /**
  * this function checks if both objects have the same
  * materials assigned to Object (in the same order)
@@ -1052,21 +1012,19 @@ void MeshImporter::optimize_material_assignements()
  * which materials shall be moved to the created geometries. Also see
  * optimize_material_assignements() above.
  */
-MTFace *MeshImporter::assign_material_to_geom(COLLADAFW::MaterialBinding cmaterial,
-                                              std::map<COLLADAFW::UniqueId, Material *>& uid_material_map,
-                                              Object *ob, const COLLADAFW::UniqueId *geom_uid,
-                                              char *layername, MTFace *texture_face,
-                                              std::map<Material *, TexIndexTextureArrayMap>& material_texture_mapping_map, short mat_index)
+void MeshImporter::assign_material_to_geom(
+        COLLADAFW::MaterialBinding cmaterial,
+        std::map<COLLADAFW::UniqueId, Material *>& uid_material_map,
+        Object *ob, const COLLADAFW::UniqueId *geom_uid,
+        short mat_index)
 {
-       MTex *color_texture = NULL;
-       Mesh *me = (Mesh *)ob->data;
        const COLLADAFW::UniqueId& ma_uid = cmaterial.getReferencedMaterial();
        
        // do we know this material?
        if (uid_material_map.find(ma_uid) == uid_material_map.end()) {
                
                fprintf(stderr, "Cannot find material by UID.\n");
-               return NULL;
+               return;
        }
 
        // first time we get geom_uid, ma_uid pair. Save for later check.
@@ -1079,27 +1037,6 @@ MTFace *MeshImporter::assign_material_to_geom(COLLADAFW::MaterialBinding cmateri
        ob->actcol=0;
        assign_material(ob, ma, mat_index + 1, BKE_MAT_ASSIGN_OBJECT); 
        
-       COLLADAFW::TextureCoordinateBindingArray& tex_array = 
-           cmaterial.getTextureCoordinateBindingArray();
-       TexIndexTextureArrayMap texindex_texarray_map = material_texture_mapping_map[ma];
-       unsigned int i;
-       // loop through <bind_vertex_inputs>
-       for (i = 0; i < tex_array.getCount(); i++) {
-               
-               color_texture = assign_textures_to_uvlayer(tex_array[i], me, texindex_texarray_map,
-                                                           color_texture);
-       }
-       
-       // set texture face
-       if (color_texture &&
-           strlen((color_texture)->uvname) &&
-           !STREQ(layername, color_texture->uvname))
-       {
-               texture_face = (MTFace *)CustomData_get_layer_named(&me->fdata, CD_MTFACE,
-                                                                   color_texture->uvname);
-               strcpy(layername, color_texture->uvname);
-       }
-       
        MaterialIdPrimitiveArrayMap& mat_prim_map = geom_uid_mat_mapping_map[*geom_uid];
        COLLADAFW::MaterialId mat_id = cmaterial.getMaterialId();
        
@@ -1114,23 +1051,16 @@ MTFace *MeshImporter::assign_material_to_geom(COLLADAFW::MaterialBinding cmateri
                        Primitive& prim = *it;
                        MPoly *mpoly = prim.mpoly;
 
-                       for (i = 0; i < prim.totpoly; i++, mpoly++) {
+                       for (int i = 0; i < prim.totpoly; i++, mpoly++) {
                                mpoly->mat_nr = mat_index;
-                               // bind texture images to faces
-                               if (texture_face && color_texture) {
-                                       texture_face->tpage = (Image *)color_texture->tex->ima;
-                                       texture_face++;
-                               }
                        }
                }
        }       
-       return texture_face;
 }
 
 Object *MeshImporter::create_mesh_object(COLLADAFW::Node *node, COLLADAFW::InstanceGeometry *geom,
                                          bool isController,
-                                         std::map<COLLADAFW::UniqueId, Material *>& uid_material_map,
-                                         std::map<Material *, TexIndexTextureArrayMap>& material_texture_mapping_map)
+                                         std::map<COLLADAFW::UniqueId, Material *>& uid_material_map)
 {
        const COLLADAFW::UniqueId *geom_uid = &geom->getInstanciatedObjectId();
        
@@ -1160,7 +1090,7 @@ Object *MeshImporter::create_mesh_object(COLLADAFW::Node *node, COLLADAFW::Insta
        const char *name = (id.length()) ? id.c_str() : NULL;
        
        // add object
-       Object *ob = bc_add_object(scene, OB_MESH, name);
+       Object *ob = bc_add_object(scene, view_layer, OB_MESH, name);
        bc_set_mark(ob); // used later for material assignement optimization
 
 
@@ -1178,10 +1108,6 @@ Object *MeshImporter::create_mesh_object(COLLADAFW::Node *node, COLLADAFW::Insta
        id_us_plus(&old_mesh->id);  /* Because BKE_mesh_assign_object would have already decreased it... */
        BKE_libblock_free_us(G.main, old_mesh);
 
-       char layername[100];
-       layername[0] = '\0';
-       MTFace *texture_face = NULL;
-       
        COLLADAFW::MaterialBindingArray& mat_array =
            geom->getMaterialBindings();
        
@@ -1189,9 +1115,9 @@ Object *MeshImporter::create_mesh_object(COLLADAFW::Node *node, COLLADAFW::Insta
        for (unsigned int i = 0; i < mat_array.getCount(); i++) {
                
                if (mat_array[i].getReferencedMaterial().isValid()) {
-                       texture_face = assign_material_to_geom(mat_array[i], uid_material_map, ob, geom_uid,
-                                                              layername, texture_face,
-                                                              material_texture_mapping_map, i);
+                       assign_material_to_geom(
+                               mat_array[i], uid_material_map, ob, geom_uid,
+                               i);
                }
                else {
                        fprintf(stderr, "invalid referenced material for %s\n", mat_array[i].getName().c_str());