[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [27223] branches/soc-2009-chingachgook/ source/blender/collada/DocumentExporter.cpp: COLLADA branch: export vertex colors.

Arystanbek Dyussenov arystan.d at gmail.com
Tue Mar 2 18:33:05 CET 2010


Revision: 27223
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=27223
Author:   kazanbas
Date:     2010-03-02 18:33:05 +0100 (Tue, 02 Mar 2010)

Log Message:
-----------
COLLADA branch: export vertex colors. Write only active layer for now, all layer export can be added later easily.

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

Modified: branches/soc-2009-chingachgook/source/blender/collada/DocumentExporter.cpp
===================================================================
--- branches/soc-2009-chingachgook/source/blender/collada/DocumentExporter.cpp	2010-03-02 17:17:34 UTC (rev 27222)
+++ branches/soc-2009-chingachgook/source/blender/collada/DocumentExporter.cpp	2010-03-02 17:33:05 UTC (rev 27223)
@@ -359,6 +359,8 @@
 		std::vector<Normal> nor;
 		std::vector<Face> norind;
 
+		bool has_color = (bool)CustomData_has_layer(&me->fdata, CD_MCOL);
+
 		create_normals(nor, norind, me);
 
 		// openMesh(geoId, geoName, meshId)
@@ -370,12 +372,15 @@
 		// writes <source> for normal coords
 		createNormalsSource(geom_id, me, nor);
 
-		int has_uvs = CustomData_has_layer(&me->fdata, CD_MTFACE);
+		bool has_uvs = (bool)CustomData_has_layer(&me->fdata, CD_MTFACE);
 		
 		// writes <source> for uv coords if mesh has uv coords
-		if (has_uvs) {
-			createTexcoordsSource(geom_id, (Mesh*)ob->data);
-		}
+		if (has_uvs)
+			createTexcoordsSource(geom_id, me);
+
+		if (has_color)
+			createVertexColorSource(geom_id, me);
+
 		// <vertices>
 		COLLADASW::Vertices verts(mSW);
 		verts.setId(getIdBySemantics(geom_id, COLLADASW::VERTEX));
@@ -389,11 +394,11 @@
 			for(int a = 0; a < ob->totcol; a++)	{
 				// account for NULL materials, this should not normally happen?
 				Material *ma = give_current_material(ob, a + 1);
-				createPolylist(ma != NULL, a, has_uvs, ob, geom_id, norind);
+				createPolylist(ma != NULL, a, has_uvs, has_color, ob, geom_id, norind);
 			}
 		}
 		else {
-			createPolylist(false, 0, has_uvs, ob, geom_id, norind);
+			createPolylist(false, 0, has_uvs, has_color, ob, geom_id, norind);
 		}
 		
 		closeMesh();
@@ -408,14 +413,11 @@
 	void createPolylist(bool has_material,
 						int material_index,
 						bool has_uvs,
+						bool has_color,
 						Object *ob,
 						std::string& geom_id,
 						std::vector<Face>& norind)
 	{
-#if 0
-		MFace *mfaces = dm->getFaceArray(dm);
-		int totfaces = dm->getNumFaces(dm);
-#endif
 		Mesh *me = (Mesh*)ob->data;
 		MFace *mfaces = me->mface;
 		int totfaces = me->totface;
@@ -479,6 +481,11 @@
 									);
 			til.push_back(input3);
 		}
+
+		if (has_color) {
+			COLLADASW::Input input4(COLLADASW::COLOR, getUrlBySemantics(geom_id, COLLADASW::COLOR), has_uvs ? 3 : 2);
+			til.push_back(input4);
+		}
 			
 		// sets <vcount>
 		polylist.setVCountList(vcount_list);
@@ -501,6 +508,9 @@
 
 					if (has_uvs)
 						polylist.appendValues(texindex + j);
+
+					if (has_color)
+						polylist.appendValues(texindex + j);
 				}
 			}
 
@@ -545,6 +555,42 @@
 	
 	}
 
+	void createVertexColorSource(std::string geom_id, Mesh *me)
+	{
+		if (!CustomData_has_layer(&me->fdata, CD_MCOL))
+			return;
+
+		MFace *f;
+		int totcolor = 0, i, j;
+
+		for (i = 0, f = me->mface; i < me->totface; i++, f++)
+			totcolor += f->v4 ? 4 : 3;
+
+		COLLADASW::FloatSourceF source(mSW);
+		source.setId(getIdBySemantics(geom_id, COLLADASW::COLOR));
+		source.setArrayId(getIdBySemantics(geom_id, COLLADASW::COLOR) + ARRAY_ID_SUFFIX);
+		source.setAccessorCount(totcolor);
+		source.setAccessorStride(3);
+
+		COLLADASW::SourceBase::ParameterNameList &param = source.getParameterNameList();
+		param.push_back("R");
+		param.push_back("G");
+		param.push_back("B");
+
+		source.prepareToAppendValues();
+
+		int index = CustomData_get_active_layer_index(&me->fdata, CD_MCOL);
+
+		MCol *mcol = (MCol*)me->fdata.layers[index].data;
+		MCol *c = mcol;
+
+		for (i = 0, f = me->mface; i < me->totface; i++, c += 4, f++)
+			for (j = 0; j < (f->v4 ? 4 : 3); j++)
+				source.appendValues(c[j].b / 255.0f, c[j].g / 255.0f, c[j].r / 255.0f);
+		
+		source.finish();
+	}
+
 	std::string makeTexcoordSourceId(std::string& geom_id, int layer_index)
 	{
 		char suffix[20];





More information about the Bf-blender-cvs mailing list