diff options
Diffstat (limited to 'source/blender/io/collada/MeshImporter.cpp')
-rw-r--r-- | source/blender/io/collada/MeshImporter.cpp | 102 |
1 files changed, 57 insertions, 45 deletions
diff --git a/source/blender/io/collada/MeshImporter.cpp b/source/blender/io/collada/MeshImporter.cpp index 6e109353be8..e7a4f7b6b51 100644 --- a/source/blender/io/collada/MeshImporter.cpp +++ b/source/blender/io/collada/MeshImporter.cpp @@ -33,6 +33,8 @@ #include "MeshImporter.h" #include "collada_utils.h" +using blender::MutableSpan; + /* get node name, or fall back to original id if not present (name is optional) */ template<class T> static std::string bc_get_dae_name(T *node) { @@ -145,6 +147,27 @@ VCOLDataWrapper::VCOLDataWrapper(COLLADAFW::MeshVertexData &vdata) : mVData(&vda { } +template<typename T> +static void colladaAddColor(T values, MLoopCol *mloopcol, int v_index, int stride) +{ + if (values->empty() || values->getCount() < (v_index + 1) * stride) { + fprintf(stderr, + "VCOLDataWrapper.getvcol(): Out of Bounds error: index %d points outside value " + "list of length %zd (with stride=%d) \n", + v_index, + values->getCount(), + stride); + return; + } + + mloopcol->r = unit_float_to_uchar_clamp((*values)[v_index * stride]); + mloopcol->g = unit_float_to_uchar_clamp((*values)[v_index * stride + 1]); + mloopcol->b = unit_float_to_uchar_clamp((*values)[v_index * stride + 2]); + if (stride == 4) { + mloopcol->a = unit_float_to_uchar_clamp((*values)[v_index * stride + 3]); + } +} + void VCOLDataWrapper::get_vcol(int v_index, MLoopCol *mloopcol) { int stride = mVData->getStride(0); @@ -155,25 +178,14 @@ void VCOLDataWrapper::get_vcol(int v_index, MLoopCol *mloopcol) switch (mVData->getType()) { case COLLADAFW::MeshVertexData::DATA_TYPE_FLOAT: { COLLADAFW::ArrayPrimitiveType<float> *values = mVData->getFloatValues(); - if (values->empty() || values->getCount() <= (v_index * stride + 2)) { - return; /* XXX: need to create an error instead. */ - } - - mloopcol->r = unit_float_to_uchar_clamp((*values)[v_index * stride]); - mloopcol->g = unit_float_to_uchar_clamp((*values)[v_index * stride + 1]); - mloopcol->b = unit_float_to_uchar_clamp((*values)[v_index * stride + 2]); + colladaAddColor<COLLADAFW::ArrayPrimitiveType<float> *>(values, mloopcol, v_index, stride); } break; case COLLADAFW::MeshVertexData::DATA_TYPE_DOUBLE: { COLLADAFW::ArrayPrimitiveType<double> *values = mVData->getDoubleValues(); - if (values->empty() || values->getCount() <= (v_index * stride + 2)) { - return; /* XXX: need to create an error instead. */ - } - - mloopcol->r = unit_float_to_uchar_clamp((*values)[v_index * stride]); - mloopcol->g = unit_float_to_uchar_clamp((*values)[v_index * stride + 1]); - mloopcol->b = unit_float_to_uchar_clamp((*values)[v_index * stride + 2]); + colladaAddColor<COLLADAFW::ArrayPrimitiveType<double> *>(values, mloopcol, v_index, stride); } break; + default: fprintf(stderr, "VCOLDataWrapper.getvcol(): unknown data type\n"); } @@ -331,13 +343,10 @@ void MeshImporter::read_vertices(COLLADAFW::Mesh *mesh, Mesh *me) } me->totvert = pos.getFloatValues()->getCount() / stride; - me->mvert = (MVert *)CustomData_add_layer(&me->vdata, CD_MVERT, CD_CALLOC, nullptr, me->totvert); - - MVert *mvert; - int i; - - for (i = 0, mvert = me->mvert; i < me->totvert; i++, mvert++) { - get_vector(mvert->co, pos, i, stride); + CustomData_add_layer(&me->vdata, CD_MVERT, CD_SET_DEFAULT, nullptr, me->totvert); + MutableSpan<MVert> verts = me->verts_for_write(); + for (const int i : verts.index_range()) { + get_vector(verts[i].co, pos, i, stride); } } @@ -438,10 +447,8 @@ void MeshImporter::allocate_poly_data(COLLADAFW::Mesh *collada_mesh, Mesh *me) if (total_poly_count > 0) { me->totpoly = total_poly_count; me->totloop = total_loop_count; - me->mpoly = (MPoly *)CustomData_add_layer( - &me->pdata, CD_MPOLY, CD_CALLOC, nullptr, me->totpoly); - me->mloop = (MLoop *)CustomData_add_layer( - &me->ldata, CD_MLOOP, CD_CALLOC, nullptr, me->totloop); + CustomData_add_layer(&me->pdata, CD_MPOLY, CD_SET_DEFAULT, nullptr, me->totpoly); + CustomData_add_layer(&me->ldata, CD_MLOOP, CD_SET_DEFAULT, nullptr, me->totloop); unsigned int totuvset = collada_mesh->getUVCoords().getInputInfosArray().getCount(); for (int i = 0; i < totuvset; i++) { @@ -458,10 +465,10 @@ void MeshImporter::allocate_poly_data(COLLADAFW::Mesh *collada_mesh, Mesh *me) COLLADAFW::String &uvname = info->mName; /* Allocate space for UV_data */ CustomData_add_layer_named( - &me->ldata, CD_MLOOPUV, CD_DEFAULT, nullptr, me->totloop, uvname.c_str()); + &me->ldata, CD_MLOOPUV, CD_SET_DEFAULT, nullptr, me->totloop, uvname.c_str()); } /* activate the first uv map */ - me->mloopuv = (MLoopUV *)CustomData_get_layer_n(&me->ldata, CD_MLOOPUV, 0); + CustomData_set_layer_active(&me->ldata, CD_MLOOPUV, 0); } int totcolset = collada_mesh->getColors().getInputInfosArray().getCount(); @@ -471,9 +478,9 @@ void MeshImporter::allocate_poly_data(COLLADAFW::Mesh *collada_mesh, Mesh *me) collada_mesh->getColors().getInputInfosArray()[i]; COLLADAFW::String colname = extract_vcolname(info->mName); CustomData_add_layer_named( - &me->ldata, CD_PROP_BYTE_COLOR, CD_DEFAULT, nullptr, me->totloop, colname.c_str()); + &me->ldata, CD_PROP_BYTE_COLOR, CD_SET_DEFAULT, nullptr, me->totloop, colname.c_str()); } - me->mloopcol = (MLoopCol *)CustomData_get_layer_n(&me->ldata, CD_PROP_BYTE_COLOR, 0); + CustomData_set_layer_active(&me->ldata, CD_PROP_BYTE_COLOR, 0); } } } @@ -536,19 +543,20 @@ void MeshImporter::mesh_add_edges(Mesh *mesh, int len) totedge = mesh->totedge + len; /* Update custom-data. */ - CustomData_copy(&mesh->edata, &edata, CD_MASK_MESH.emask, CD_DEFAULT, totedge); + CustomData_copy(&mesh->edata, &edata, CD_MASK_MESH.emask, CD_SET_DEFAULT, totedge); CustomData_copy_data(&mesh->edata, &edata, 0, 0, mesh->totedge); if (!CustomData_has_layer(&edata, CD_MEDGE)) { - CustomData_add_layer(&edata, CD_MEDGE, CD_CALLOC, nullptr, totedge); + CustomData_add_layer(&edata, CD_MEDGE, CD_SET_DEFAULT, nullptr, totedge); } CustomData_free(&mesh->edata, mesh->totedge); mesh->edata = edata; - BKE_mesh_update_customdata_pointers(mesh, false); /* new edges don't change tessellation */ + + MutableSpan<MEdge> edges = mesh->edges_for_write(); /* set default flags */ - medge = &mesh->medge[mesh->totedge]; + medge = &edges[mesh->totedge]; for (int i = 0; i < len; i++, medge++) { medge->flag = ME_EDGEDRAW | ME_EDGERENDER | SELECT; } @@ -565,7 +573,8 @@ void MeshImporter::read_lines(COLLADAFW::Mesh *mesh, Mesh *me) /* unsigned int total_edge_count = loose_edge_count + face_edge_count; */ /* UNUSED */ mesh_add_edges(me, loose_edge_count); - MEdge *med = me->medge + face_edge_count; + MutableSpan<MEdge> edges = me->edges_for_write(); + MEdge *med = edges.data() + face_edge_count; COLLADAFW::MeshPrimitiveArray &prim_arr = mesh->getMeshPrimitives(); @@ -598,12 +607,17 @@ void MeshImporter::read_polys(COLLADAFW::Mesh *collada_mesh, Mesh *me) UVDataWrapper uvs(collada_mesh->getUVCoords()); VCOLDataWrapper vcol(collada_mesh->getColors()); - MPoly *mpoly = me->mpoly; - MLoop *mloop = me->mloop; + MutableSpan<MPoly> polys = me->polys_for_write(); + MutableSpan<MLoop> loops = me->loops_for_write(); + MPoly *mpoly = polys.data(); + MLoop *mloop = loops.data(); int loop_index = 0; MaterialIdPrimitiveArrayMap mat_prim_map; + int *material_indices = (int *)CustomData_add_layer_named( + &me->pdata, CD_PROP_INT32, CD_SET_DEFAULT, nullptr, me->totpoly, "material_index"); + COLLADAFW::MeshPrimitiveArray &prim_arr = collada_mesh->getMeshPrimitives(); COLLADAFW::MeshVertexData &nor = collada_mesh->getNormals(); @@ -622,7 +636,7 @@ void MeshImporter::read_polys(COLLADAFW::Mesh *collada_mesh, Mesh *me) int collada_meshtype = mp->getPrimitiveType(); /* since we cannot set mpoly->mat_nr here, we store a portion of me->mpoly in Primitive */ - Primitive prim = {mpoly, 0}; + Primitive prim = {mpoly, material_indices, 0}; /* If MeshPrimitive is TRIANGLE_FANS we split it into triangles * The first triangle-fan vertex will be the first vertex in every triangle @@ -652,6 +666,9 @@ void MeshImporter::read_polys(COLLADAFW::Mesh *collada_mesh, Mesh *me) } mpoly++; + if (material_indices) { + material_indices++; + } mloop += 3; loop_index += 3; prim.totpoly++; @@ -887,11 +904,9 @@ static bool bc_has_same_material_configuration(Object *ob1, Object *ob2) } /** - * * Caution here: This code assumes that all materials are assigned to Object * and no material is assigned to Data. * That is true right after the objects have been imported. - * */ static void bc_copy_materials_to_data(Object *ob, Mesh *me) { @@ -902,9 +917,7 @@ static void bc_copy_materials_to_data(Object *ob, Mesh *me) } /** - * - * Remove all references to materials from the object - * + * Remove all references to materials from the object. */ static void bc_remove_materials_from_object(Object *ob, Mesh *me) { @@ -1000,10 +1013,9 @@ void MeshImporter::assign_material_to_geom( for (it = prims.begin(); it != prims.end(); it++) { Primitive &prim = *it; - MPoly *mpoly = prim.mpoly; - for (int i = 0; i < prim.totpoly; i++, mpoly++) { - mpoly->mat_nr = mat_index; + for (int i = 0; i < prim.totpoly; i++) { + prim.material_indices[i] = mat_index; } } } |