[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [21938] branches/soc-2009-chingachgook/ source/blender/collada/DocumentImporter.cpp: Changed textures import.

Chingiz Dyussenov chingiz.ds at gmail.com
Mon Jul 27 19:43:14 CEST 2009


Revision: 21938
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=21938
Author:   chingachgook
Date:     2009-07-27 19:43:13 +0200 (Mon, 27 Jul 2009)

Log Message:
-----------
Changed textures import. Although code is still difficult to understand.

Modified Paths:
--------------
    branches/soc-2009-chingachgook/source/blender/collada/DocumentImporter.cpp

Modified: branches/soc-2009-chingachgook/source/blender/collada/DocumentImporter.cpp
===================================================================
--- branches/soc-2009-chingachgook/source/blender/collada/DocumentImporter.cpp	2009-07-26 20:20:25 UTC (rev 21937)
+++ branches/soc-2009-chingachgook/source/blender/collada/DocumentImporter.cpp	2009-07-27 17:43:13 UTC (rev 21938)
@@ -154,6 +154,8 @@
 	}
 };
 
+typedef std::map<COLLADAFW::TextureMapId, std::vector<MTex*> > TexIndexTextureArrayMap;
+
 class ArmatureImporter
 {
 private:
@@ -359,11 +361,12 @@
 	};
 	typedef std::map<COLLADAFW::MaterialId, std::vector<Primitive> > MaterialIdPrimitiveArrayMap;
 	std::map<COLLADAFW::UniqueId, MaterialIdPrimitiveArrayMap> geom_uid_mat_mapping_map; // crazy name!
-
+	/*
 	// maps for assigning textures to uv layers
 	//std::map<COLLADAFW::TextureMapId, char*> set_layername_map;
-	std::map<COLLADAFW::TextureMapId, std::vector<MTex*> > index_mtex_map;
-
+	typedef std::map<COLLADAFW::TextureMapId, std::vector<MTex*> > TexIndexTextureArrayMap;
+	std::map<Material*, TexIndexTextureArrayMap> material_texture_mapping_map;
+	*/
 	class UVDataWrapper
 	{
 		COLLADAFW::MeshVertexData *mVData;
@@ -389,6 +392,7 @@
 			case COLLADAFW::MeshVertexData::DATA_TYPE_FLOAT:
 				{
 					COLLADAFW::ArrayPrimitiveType<float>* values = mVData->getFloatValues();
+					if (values->empty()) return;
 					uv[0] = (*values)[uv_index[0]];
 					uv[1] = (*values)[uv_index[1]];
 					
@@ -397,7 +401,7 @@
 			case COLLADAFW::MeshVertexData::DATA_TYPE_DOUBLE:
 				{
 					COLLADAFW::ArrayPrimitiveType<double>* values = mVData->getDoubleValues();
-					
+					if (values->empty()) return;
 					uv[0] = (float)(*values)[uv_index[0]];
 					uv[1] = (float)(*values)[uv_index[1]];
 					
@@ -413,6 +417,10 @@
 		mface->v2 = indices[1];
 		mface->v3 = indices[2];
 		if (quad) mface->v4 = indices[3];
+		else mface->v4 = 0;
+#ifdef COLLADA_DEBUG
+		fprintf(stderr, "%u, %u, %u \n", indices[0], indices[1], indices[2]);
+#endif
 	}
 
 	// change face indices order so that v4 is not 0
@@ -630,7 +638,7 @@
 					
 					set_face_indices(mface, indices, false);
 					indices += 3;
-					
+
 					for (k = 0; k < totuvset; k++) {
 						// get mtface by face index and uv set index
 						MTFace *mtface = (MTFace*)CustomData_get_layer_n(&me->fdata, CD_MTFACE, k);
@@ -684,14 +692,105 @@
 public:
 
 	MeshImporter(ArmatureImporter *arm, Scene *sce) : scene(sce), armature_importer(arm) {}
+	
+	MTex *assign_textures_to_uvlayer(COLLADAFW::InstanceGeometry::TextureCoordinateBinding &ctexture, Mesh **me, TexIndexTextureArrayMap& texindex_texarray_map, MTex *color_texture)
+	{
+		COLLADAFW::TextureMapId texture_index = ctexture.textureMapId;
+		size_t set_index = ctexture.setIndex;
+		char *uvname = CustomData_get_layer_name(&(*me)->fdata, CD_MTFACE, set_index);
+		
+		if (texindex_texarray_map.find(texture_index) == texindex_texarray_map.end()) {
+			fprintf(stderr, "Cannot find texture array by texture index.\n");
+			return NULL;
+		}
+		// assign uvlayer name to 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) {
+				strcpy(texture->uvname, uvname);
+				if (texture->mapto == MAP_COL) color_texture = texture;
+			}
+		}
+		return color_texture;
+	}
+	
+	MTFace *assign_material_to_geom(COLLADAFW::InstanceGeometry::MaterialBinding cmaterial,
+								 std::map<COLLADAFW::UniqueId, Material*>& uid_material_map,
+								 Object *ob, Mesh *me, const COLLADAFW::UniqueId *geom_uid, 
+								 MTex **color_texture, char *layername, MTFace *texture_face,
+								 std::map<Material*, TexIndexTextureArrayMap>& material_texture_mapping_map, int mat_index)
+	{
+		const COLLADAFW::UniqueId& ma_uid = cmaterial.getReferencedMaterial();
+		unsigned int j;
+		// 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;
+		}
+		Material *ma = uid_material_map[ma_uid];
+		
+		TexIndexTextureArrayMap texindex_texarray_map = material_texture_mapping_map[ma];
+		
+		COLLADAFW::InstanceGeometry::TextureCoordinateBindingArray& tex_array = cmaterial.getTextureCoordinateBindingArray();
+		
+		// loop through material's textures
+		for (j = 0; j < tex_array.getCount(); j++) {
+			*color_texture = assign_textures_to_uvlayer(tex_array[j], &me, texindex_texarray_map, *color_texture);
+		}
+		// if material has color texture
+		if (*color_texture && strlen((*color_texture)->uvname)) {
+			// multiple color textures may refer to the same uvlayer, 
+			// set tface only once, otherwise images will rewrite each other 
+			if (strcmp(layername, (*color_texture)->uvname) != 0) {
+				texture_face = (MTFace*)CustomData_get_layer_named(&me->fdata, CD_MTFACE, (*color_texture)->uvname);
+				strcpy(layername, (*color_texture)->uvname);
+			}
+		}
+		
+		assign_material(ob, ma, ob->totcol + 1);
+		
+		MaterialIdPrimitiveArrayMap& mat_prim_map = geom_uid_mat_mapping_map[*geom_uid];
+		COLLADAFW::MaterialId mat_id = cmaterial.getMaterialId();
+		
+		// assign material indices to mesh faces
+		if (mat_prim_map.find(mat_id) != mat_prim_map.end()) {
+			
+			std::vector<Primitive>& prims = mat_prim_map[mat_id];
+			
+			std::vector<Primitive>::iterator it;
+			
+			for (it = prims.begin(); it != prims.end(); it++) {
+				Primitive& prim = *it;
+				j = 0;
+				while (j++ < prim.totface) {
+					prim.mface->mat_nr = mat_index;
+					prim.mface++;
+					
+					// if tface was set
+					// bind image to tface
+					if (texture_face && (*color_texture)) {
+						texture_face->mode = TF_TEX;
+						texture_face->tpage = (Image*)(*color_texture)->tex->ima;
+						texture_face++;
+					}
+				}
+			}
+		}
 
-	// bind object with a mesh assigning materials and textures
+		return texture_face;
+	}
+	
+	// bind object to mesh
 	Object *create_mesh_object(COLLADAFW::Node *node, COLLADAFW::InstanceGeometry *geom,
-							   bool isController, std::map<COLLADAFW::UniqueId, Material*>& uid_material_map)
+							   bool isController,
+							   std::map<COLLADAFW::UniqueId, Material*>& uid_material_map,
+							   std::map<Material*, TexIndexTextureArrayMap>& material_texture_mapping_map)
 	{
 		const COLLADAFW::UniqueId *geom_uid = &geom->getInstanciatedObjectId();
-	 
-		// checking if node instanciates controller or geometry
+		
+		// check if node instanciates controller or geometry
 		if (isController) {
 			geom_uid = armature_importer->get_geometry_uid(*geom_uid);
 			if (!geom_uid) {
@@ -707,9 +806,10 @@
 				return NULL;
 			}
 		}
-
+		if (!uid_mesh_map[*geom_uid]) return NULL;
+		
 		Object *ob = add_object(scene, OB_MESH);
-
+		
 		// name Object
 		const std::string& id = node->getOriginalId();
 		if (id.length())
@@ -723,107 +823,19 @@
 		if (old_mesh->id.us == 0) free_libblock(&G.main->mesh, old_mesh);
 		
 		Mesh *me = (Mesh*)ob->data;
-		MTex *diffuse_mtex = NULL;
-		/*
-		MTFace *tface = NULL;
 		char layername[100];
-		bool first_time = true;
-		*/
+		MTFace *texture_face = NULL;
+		MTex *color_texture = NULL;
 		
-		// assign material indices to mesh faces
-		for (unsigned int k = 0; k < geom->getMaterialBindings().getCount(); k++) {
+		COLLADAFW::InstanceGeometry::MaterialBindingArray& mat_array = geom->getMaterialBindings();
+		
+		// loop through geom's materials
+		for (unsigned int i = 0; i < mat_array.getCount(); i++)	{
+			texture_face = assign_material_to_geom(mat_array[i], uid_material_map, ob, me, geom_uid, &color_texture, layername, texture_face, material_texture_mapping_map, i);
 			
-			const COLLADAFW::UniqueId& ma_uid = geom->getMaterialBindings()[k].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");
-				continue;
-			}
-			
-			Material *ma = uid_material_map[ma_uid];
-
-			unsigned int l;
-
-			// XXX I don't understand this! (Arystan)
-
-			/*
-			// assign textures to uv layers
-			// bvi_array "bind_vertex_input array"
-			COLLADAFW::InstanceGeometry::TextureCoordinateBindingArray& bvi_array = 
-				geom->getMaterialBindings()[k].getTextureCoordinateBindingArray();
-			for (l = 0; l < bvi_array.getCount(); l++) {
-				COLLADAFW::TextureMapId tex_index = bvi_array[l].textureMapId;
-				size_t set_index = bvi_array[l].setIndex;
-				
-				// if (set_layername_map.find(set_index) == set_layername_map.end()) {
-				// 	fprintf(stderr, "Cannot find uvlayer name by set index.\n");
-				// 	continue;
-				// }
-				// char *uvname = set_layername_map[set_index];
-				char *uvname = CustomData_get_layer_name(&me->fdata, CD_MTFACE, set_index);
-				
-				// check if mtexes were properly added to vector
-				if (index_mtex_map.find(tex_index) == index_mtex_map.end()) {
-					fprintf(stderr, "Cannot find mtexes by texmap id.\n");
-					continue;
-				}
-				std::vector<MTex*> mtexes = index_mtex_map[tex_index];
-				std::vector<MTex*>::iterator it;
-				for (it = mtexes.begin(); it != mtexes.end(); it++) {
-					MTex *mtex = *it;
-					if (mtex && mtex->uvname) strcpy(mtex->uvname, uvname);
-				}	
-			}
-			for (l = 0; l < 18; l++) {
-				if (ma->mtex[l] && ma->mtex[l]->mapto == MAP_COL) {
-					diffuse_mtex = ma->mtex[l];
-				}
-			}
-			if (diffuse_mtex && strlen(diffuse_mtex->uvname)) {
-				if (first_time) {
-					tface = (MTFace*)CustomData_get_layer_named(&me->fdata, CD_MTFACE, diffuse_mtex->uvname);
-					strcpy(layername, diffuse_mtex->uvname);
-					first_time = false;
-				}
-				else if (strcmp(diffuse_mtex->uvname, layername) != 0) {
-					tface = (MTFace*)CustomData_get_layer_named(&me->fdata, CD_MTFACE, diffuse_mtex->uvname);
-					strcpy(layername, diffuse_mtex->uvname);
-				}
-			}
-			*/
-
-			assign_material(ob, ma, ob->totcol + 1);
-			
-			MaterialIdPrimitiveArrayMap& mat_prim_map = geom_uid_mat_mapping_map[*geom_uid];
-			COLLADAFW::MaterialId mat_id = geom->getMaterialBindings()[k].getMaterialId();
-			
-			// set material index on each face
-			if (mat_prim_map.find(mat_id) != mat_prim_map.end()) {
-				
-				std::vector<Primitive>& prims = mat_prim_map[mat_id];
-				
-				std::vector<Primitive>::iterator it;
-				
-				for (it = prims.begin(); it != prims.end(); it++) {
-					Primitive& prim = *it;
-					l = 0;
-					while (l++ < prim.totface) {
-						prim.mface->mat_nr = k;

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list