[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [21217] branches/soc-2009-chingachgook/ source/blender/collada: Added per-face materials exporting.

Chingiz Dyussenov chingiz.ds at gmail.com
Sun Jun 28 15:20:38 CEST 2009


Revision: 21217
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=21217
Author:   chingachgook
Date:     2009-06-28 15:20:37 +0200 (Sun, 28 Jun 2009)

Log Message:
-----------
Added per-face materials exporting. If mesh has no materials it won't be 
exported.

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

Modified: branches/soc-2009-chingachgook/source/blender/collada/DocumentExporter.cpp
===================================================================
--- branches/soc-2009-chingachgook/source/blender/collada/DocumentExporter.cpp	2009-06-28 12:30:50 UTC (rev 21216)
+++ branches/soc-2009-chingachgook/source/blender/collada/DocumentExporter.cpp	2009-06-28 13:20:37 UTC (rev 21217)
@@ -165,7 +165,7 @@
 		//writes <source> for uv coords
 		//if mesh has uv coords
 		checkTexcoords = createTexcoordsSource(geom_name, dm, (Mesh*)ob->data);
-
+		
 		//<vertices>
 		COLLADASW::Vertices verts(mSW);
 		verts.setId(getIdBySemantics(geom_name, COLLADASW::VERTEX));
@@ -174,109 +174,126 @@
 							   getUrlBySemantics(geom_name, COLLADASW::POSITION));
 		input_list.push_back(input);
 		verts.add();
-
-		//polylist
-		COLLADASW::Polylist polylist(mSW);
 		
-		//sets count attribute in <polylist>
-		polylist.setCount(totfaces);
-				
-		COLLADASW::InputList &til = polylist.getInputList();
-				
-		//creates list of attributes in <polylist> <input> for vertices 
-		COLLADASW::Input input2(COLLADASW::VERTEX, getUrlBySemantics
-								(geom_name, COLLADASW::VERTEX), 0);
-		//creates list of attributes in <polylist> <input> for normals
-		COLLADASW::Input input3(COLLADASW::NORMAL, getUrlBySemantics
-								(geom_name, COLLADASW::NORMAL), 0);
-				
-		til.push_back(input2);
-		til.push_back(input3);
-				
-		//if mesh has uv coords writes <input> attributes for TEXCOORD
-		if (checkTexcoords == true)
-			{
-				COLLADASW::Input input4(COLLADASW::TEXCOORD,
-										getUrlBySemantics(geom_name, COLLADASW::TEXCOORD), 1, 0);
-				til.push_back(input4);
-				polylist.setMaterial("material-symbol");
+		for(int a = 0; a < ob->totcol; a++)	{
+			//<vcount>
+			int i;
+			int faces_in_polylist = 0;
+			std::vector<unsigned long> VCountList;
+			for (i = 0; i < totfaces; i++) {
+				MFace *f = &mfaces[i];
+				if (f->mat_nr == a) {
+					faces_in_polylist += 1;
+					if (f->v4 == 0) {
+						VCountList.push_back(3);
+					}
+					else {
+						VCountList.push_back(4);
+					}
+				}
 			}
-		
-		
-		//<vcount>
-		int i;
-		std::vector<unsigned long> VCountList;
-		for (i = 0; i < totfaces; i++) {
-			MFace *f = &mfaces[i];
+			if (faces_in_polylist == 0) {
+				continue;
+			}
 			
-			if (f->v4 == 0) {
-				VCountList.push_back(3);
-			}
-			else {
-				VCountList.push_back(4);
-			}
-		}
-		polylist.setVCountList(VCountList);
-		
-		//performs the actual writing
-		polylist.prepareToAppendValues();
-		
-		int texindex = 0;
-		//<p>
-		for (i = 0; i < totfaces; i++) {
-			MFace *f = &mfaces[i];
-			//if mesh has uv coords writes uv and
-			//vertex indexes
-			if (checkTexcoords == true)	{
-				// if triangle
-				if (f->v4 == 0) {
-					polylist.appendValues(f->v1);
-					polylist.appendValues(texindex++);
-					polylist.appendValues(f->v2);
-					polylist.appendValues(texindex++);
-					polylist.appendValues(f->v3);
-					polylist.appendValues(texindex++);
+			Material *ma = give_current_material(ob, a+1);
+			COLLADASW::Polylist polylist(mSW);
+			
+			// sets count attribute in <polylist>
+			polylist.setCount(faces_in_polylist);
+			
+			// sets material name
+			polylist.setMaterial(ma->id.name);
+				
+			COLLADASW::InputList &til = polylist.getInputList();
+			
+			//creates list of attributes in <polylist> <input> for vertices 
+			COLLADASW::Input input1(COLLADASW::VERTEX, getUrlBySemantics
+									(geom_name, COLLADASW::VERTEX), 0);
+			//creates list of attributes in <polylist> <input> for normals
+			COLLADASW::Input input2(COLLADASW::NORMAL, getUrlBySemantics
+									(geom_name, COLLADASW::NORMAL), 0);
+			
+			til.push_back(input1);
+			til.push_back(input2);
+			
+			// if mesh has uv coords writes <input> attributes for TEXCOORD
+			// XXX doesn't work when mesh has no uv channel
+			if (checkTexcoords == true)
+				{
+					COLLADASW::Input input3(COLLADASW::TEXCOORD,
+											getUrlBySemantics(geom_name, COLLADASW::TEXCOORD), 1, 0);
+					til.push_back(input3);
 				}
-				// quad
-				else {
-					polylist.appendValues(f->v1);
-					polylist.appendValues(texindex++);
-					polylist.appendValues(f->v2);
-					polylist.appendValues(texindex++);
-					polylist.appendValues(f->v3);
-					polylist.appendValues(texindex++);
-					//tris.appendValues(f->v3);
-					//tris.appendValues(texindex++);
-					polylist.appendValues(f->v4);
-					polylist.appendValues(texindex++);
-					//tris.appendValues(f->v1);
-					//tris.appendValues(texindex++);
+			// <vcount>
+			polylist.setVCountList(VCountList);
+			
+			//performs the actual writing
+			polylist.prepareToAppendValues();
+			
+			int texindex = 0;
+			//<p>
+			for (i = 0; i < totfaces; i++) {
+				MFace *f = &mfaces[i];
+				//if mesh has uv coords writes uv and
+				//vertex indexes
+				if (checkTexcoords == true && f->mat_nr == a)	{
+					// if triangle
+					if (f->v4 == 0) {
+						polylist.appendValues(f->v1);
+						polylist.appendValues(texindex++);
+						polylist.appendValues(f->v2);
+						polylist.appendValues(texindex++);
+						polylist.appendValues(f->v3);
+						polylist.appendValues(texindex++);
+					}
+					// quad
+					else {
+						polylist.appendValues(f->v1);
+						polylist.appendValues(texindex++);
+						polylist.appendValues(f->v2);
+						polylist.appendValues(texindex++);
+						polylist.appendValues(f->v3);
+						polylist.appendValues(texindex++);
+						//tris.appendValues(f->v3);
+						//tris.appendValues(texindex++);
+						polylist.appendValues(f->v4);
+						polylist.appendValues(texindex++);
+						//tris.appendValues(f->v1);
+						//tris.appendValues(texindex++);
+					}
 				}
-			}
-			//if mesh has no uv coords writes only 
-			//vertex indexes
-			else {
-				// if triangle
-				if (f->v4 == 0) {
-					polylist.appendValues(f->v1, f->v2, f->v3);	
+				//if mesh has no uv coords writes only 
+				//vertex indexes
+				else if(f->mat_nr == a){
+					// if triangle
+					if (f->v4 == 0) {
+						polylist.appendValues(f->v1, f->v2, f->v3);	
+					}
+					// quad
+					else {
+						polylist.appendValues(f->v1, f->v2, f->v3, f->v4);
+						//tris.appendValues(f->v3, f->v4, f->v1);
+					}
 				}
-				// quad
-				else {
-					polylist.appendValues(f->v1, f->v2, f->v3, f->v4);
-					//tris.appendValues(f->v3, f->v4, f->v1);
+				else if(f->mat_nr != a) {
+					if (f->v4 == 0) {
+						texindex += 3;
+					}
+					else {
+						texindex += 4;
+					}
 				}
-						
-			} 
+			}
+			//polylist.closeElement();
+			polylist.finish();
 		}
-
-		polylist.closeElement();
-		polylist.finish();
-					
+		
 		closeMesh();
 		closeGeometry();
 					
 		dm->release(dm);
-						
+		
 	}
 	
 	//creates <source> for positions
@@ -507,12 +524,12 @@
 			COLLADASW::BindMaterial& bm = instGeom.getBindMaterial();
 			COLLADASW::InstanceMaterialList& iml = bm.getInstanceMaterialList();
 			std::string matid = std::string(ma->id.name);
-			COLLADASW::InstanceMaterial im("material-symbol", COLLADASW::URI
+			COLLADASW::InstanceMaterial im(matid, COLLADASW::URI
 										   (COLLADABU::Utils::EMPTY_STRING,
 											matid));
 			//iterate over all textures
 			//if any add to list
-			int c = 0;
+			/*int c = 0;
 			for (int b = 0; b < MAX_MTEX; b++) {
 				MTex *mtex = ma->mtex[b];
 				if (mtex && mtex->tex && mtex->tex->ima) {
@@ -523,7 +540,7 @@
 					im.push_back(bvi);
 				}
 			}
-			
+			*/
 		    iml.push_back(im);
 		}
 

Modified: branches/soc-2009-chingachgook/source/blender/collada/DocumentImporter.cpp
===================================================================
--- branches/soc-2009-chingachgook/source/blender/collada/DocumentImporter.cpp	2009-06-28 12:30:50 UTC (rev 21216)
+++ branches/soc-2009-chingachgook/source/blender/collada/DocumentImporter.cpp	2009-06-28 13:20:37 UTC (rev 21217)
@@ -351,7 +351,7 @@
 		}
 
 		COLLADAFW::Mesh *cmesh = (COLLADAFW::Mesh*)cgeom;
-
+		
 		// first check if we can import this mesh
 		COLLADAFW::MeshPrimitiveArray& prim_arr = cmesh->getMeshPrimitives();
 
@@ -363,7 +363,7 @@
 			COLLADAFW::MeshPrimitive::PrimitiveType type = mp->getPrimitiveType();
 
 			const char *type_str = primTypeToStr(type);
-
+			
 			// OpenCollada passes POLYGONS type for <polylist>
 			if (type == COLLADAFW::MeshPrimitive::POLYLIST || type == COLLADAFW::MeshPrimitive::POLYGONS) {
 





More information about the Bf-blender-cvs mailing list