[Bf-blender-cvs] [bbae066] master: Added Support for Custom Vertex Normals to Collada exporter

Gaia Clary noreply at git.blender.org
Thu Apr 16 19:02:38 CEST 2015


Commit: bbae0664a3df0ec3b3dcc199ab3a52a998bc5290
Author: Gaia Clary
Date:   Thu Apr 16 19:02:00 2015 +0200
Branches: master
https://developer.blender.org/rBbbae0664a3df0ec3b3dcc199ab3a52a998bc5290

Added Support for Custom Vertex Normals to Collada exporter

===================================================================

M	source/blender/collada/GeometryExporter.cpp
M	source/blender/collada/GeometryExporter.h

===================================================================

diff --git a/source/blender/collada/GeometryExporter.cpp b/source/blender/collada/GeometryExporter.cpp
index 28cee05..1353d98 100644
--- a/source/blender/collada/GeometryExporter.cpp
+++ b/source/blender/collada/GeometryExporter.cpp
@@ -82,6 +82,9 @@ void GeometryExporter::operator()(Object *ob)
 					this->export_settings->apply_modifiers,
 					this->export_settings->triangulate);
 
+	Mesh *mesh = (Mesh *) ob->data;
+	me->flag = mesh->flag;
+
 	std::string geom_id = get_geometry_id(ob, use_instantiation);
 	std::vector<Normal> nor;
 	std::vector<BCPolygonNormalsIndices> norind;
@@ -563,6 +566,11 @@ void GeometryExporter::createTexcoordsSource(std::string geom_id, Mesh *me)
 	}
 }
 
+bool operator<(const Normal &a, const Normal &b)
+{
+	/* only needed to sort normal vectors and find() them later in a map.*/
+	return a.x < b.x || (a.x == b.x && (a.y < b.y || (a.y == b.y && a.z < b.z)));
+}
 
 //creates <source> for normals
 void GeometryExporter::createNormalsSource(std::string geom_id, Mesh *me, std::vector<Normal>& nor)
@@ -596,11 +604,18 @@ void GeometryExporter::createNormalsSource(std::string geom_id, Mesh *me, std::v
 
 void GeometryExporter::create_normals(std::vector<Normal> &normals, std::vector<BCPolygonNormalsIndices> &polygons_normals, Mesh *me)
 {
-	std::map<unsigned int, unsigned int> shared_normal_indices;
+	std::map<Normal, unsigned int> shared_normal_indices;
 	int last_normal_index = -1;
 
 	MVert *verts  = me->mvert;
 	MLoop *mloops = me->mloop;
+	float(*lnors)[3];
+
+	BKE_mesh_calc_normals_split(me);
+	if (CustomData_has_layer(&me->ldata, CD_NORMAL)) {
+		lnors = (float(*)[3])CustomData_get_layer(&me->ldata, CD_NORMAL);
+	}
+
 	for (int poly_index = 0; poly_index < me->totpoly; poly_index++) {
 		MPoly *mpoly  = &me->mpoly[poly_index];
 
@@ -615,25 +630,24 @@ void GeometryExporter::create_normals(std::vector<Normal> &normals, std::vector<
 			last_normal_index++;
 		}
 
-
 		MLoop *mloop = mloops + mpoly->loopstart;
 		BCPolygonNormalsIndices poly_indices;
 		for (int loop_index = 0; loop_index < mpoly->totloop; loop_index++) {
-			unsigned int vertex_index = mloop[loop_index].v;
+			unsigned int loop_idx = mpoly->loopstart + loop_index;
 			if (mpoly->flag & ME_SMOOTH) {
-				if (shared_normal_indices.find(vertex_index) != shared_normal_indices.end())
-					poly_indices.add_index (shared_normal_indices[vertex_index]);
-				else {
 
-					float vector[3];
-					normal_short_to_float_v3(vector, verts[vertex_index].no);
+				float normalized[3];
+				normalize_v3_v3(normalized, lnors[loop_idx]);
+				Normal n = { normalized[0], normalized[1], normalized[2] };
 
-					Normal n = { vector[0], vector[1], vector[2] };
-					normals.push_back(n);
+				if (shared_normal_indices.find(n) != shared_normal_indices.end()) {
+					poly_indices.add_index(shared_normal_indices[n]);
+				}
+				else {
 					last_normal_index++;
-
 					poly_indices.add_index(last_normal_index);
-					shared_normal_indices[vertex_index] = last_normal_index;
+					shared_normal_indices[n] = last_normal_index;
+					normals.push_back(n);
 				}
 			}
 			else {
diff --git a/source/blender/collada/GeometryExporter.h b/source/blender/collada/GeometryExporter.h
index 4d54e79..69d1067 100644
--- a/source/blender/collada/GeometryExporter.h
+++ b/source/blender/collada/GeometryExporter.h
@@ -48,6 +48,20 @@
 
 extern Object *bc_get_highest_selected_ancestor_or_self(Object *ob);
 
+class Normal
+{
+	public:
+		float x;
+		float y;
+		float z;
+
+		friend bool operator<  (const Normal &, const Normal &);
+
+};
+
+bool operator<  (const Normal &, const Normal &);
+
+
 // TODO: optimize UV sets by making indexed list with duplicates removed
 class GeometryExporter : COLLADASW::LibraryGeometries
 {
@@ -56,10 +70,7 @@ class GeometryExporter : COLLADASW::LibraryGeometries
 		unsigned int v1, v2, v3, v4;
 	};
 
-	struct Normal
-	{
-		float x, y, z;
-	};
+	Normal n;
 
 	Scene *mScene;




More information about the Bf-blender-cvs mailing list