From bbae0664a3df0ec3b3dcc199ab3a52a998bc5290 Mon Sep 17 00:00:00 2001 From: Gaia Clary Date: Thu, 16 Apr 2015 19:02:00 +0200 Subject: Added Support for Custom Vertex Normals to Collada exporter --- source/blender/collada/GeometryExporter.cpp | 38 ++++++++++++++++++++--------- source/blender/collada/GeometryExporter.h | 19 ++++++++++++--- 2 files changed, 41 insertions(+), 16 deletions(-) (limited to 'source/blender/collada') diff --git a/source/blender/collada/GeometryExporter.cpp b/source/blender/collada/GeometryExporter.cpp index 28cee05ec4b..1353d988137 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 nor; std::vector 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 for normals void GeometryExporter::createNormalsSource(std::string geom_id, Mesh *me, std::vector& nor) @@ -596,11 +604,18 @@ void GeometryExporter::createNormalsSource(std::string geom_id, Mesh *me, std::v void GeometryExporter::create_normals(std::vector &normals, std::vector &polygons_normals, Mesh *me) { - std::map shared_normal_indices; + std::map 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 &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 4d54e79d796..69d1067e6f4 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; -- cgit v1.2.3