diff options
Diffstat (limited to 'source/blender/collada/GeometryExporter.cpp')
-rw-r--r-- | source/blender/collada/GeometryExporter.cpp | 53 |
1 files changed, 36 insertions, 17 deletions
diff --git a/source/blender/collada/GeometryExporter.cpp b/source/blender/collada/GeometryExporter.cpp index 28cee05ec4b..7c7c57f3305 100644 --- a/source/blender/collada/GeometryExporter.cpp +++ b/source/blender/collada/GeometryExporter.cpp @@ -532,8 +532,8 @@ void GeometryExporter::createTexcoordsSource(std::string geom_id, Mesh *me) // each <source> will get id like meshName + "map-channel-1" int active_uv_index = CustomData_get_active_layer_index(&me->ldata, CD_MLOOPUV); for (int a = 0; a < num_layers; a++) { - - if (!this->export_settings->active_uv_only || a == active_uv_index) { + int layer_index = CustomData_get_layer_index_n(&me->ldata, CD_MLOOPUV, a); + if (!this->export_settings->active_uv_only || layer_index == active_uv_index) { MLoopUV *mloops = (MLoopUV *)CustomData_get_layer_n(&me->ldata, CD_MLOOPUV, a); COLLADASW::FloatSourceF source(mSW); @@ -563,6 +563,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,15 +601,25 @@ 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] = NULL; + bool use_custom_normals = false; + + BKE_mesh_calc_normals_split(me); + if (CustomData_has_layer(&me->ldata, CD_NORMAL)) { + lnors = (float(*)[3])CustomData_get_layer(&me->ldata, CD_NORMAL); + use_custom_normals = true; + } + for (int poly_index = 0; poly_index < me->totpoly; poly_index++) { MPoly *mpoly = &me->mpoly[poly_index]; + bool use_vertex_normals = use_custom_normals || mpoly->flag & ME_SMOOTH; - if (!(mpoly->flag & ME_SMOOTH)) { + if (!use_vertex_normals) { // For flat faces use face normal as vertex normal: float vector[3]; @@ -615,25 +630,29 @@ 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; - 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 { + unsigned int loop_idx = mpoly->loopstart + loop_index; + if (use_vertex_normals) { + float normalized[3]; - float vector[3]; - normal_short_to_float_v3(vector, verts[vertex_index].no); + if (use_custom_normals) { + normalize_v3_v3(normalized, lnors[loop_idx]); + } + else { + normal_short_to_float_v3(normalized, verts[mloops[loop_index].v].no); + normalize_v3(normalized); + } + 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 { |