From 8bdac4d0bc21143bafbdbb9b45ab521dfedfa369 Mon Sep 17 00:00:00 2001 From: gaiaclary Date: Thu, 1 May 2014 14:52:10 +0200 Subject: fix T39967: Added support for Import/export of vertex color layers --- source/blender/collada/MeshImporter.cpp | 116 ++++++++++++++++++++++++++++---- 1 file changed, 103 insertions(+), 13 deletions(-) (limited to 'source/blender/collada/MeshImporter.cpp') diff --git a/source/blender/collada/MeshImporter.cpp b/source/blender/collada/MeshImporter.cpp index d631a43676f..8840e1174ea 100644 --- a/source/blender/collada/MeshImporter.cpp +++ b/source/blender/collada/MeshImporter.cpp @@ -173,6 +173,42 @@ void UVDataWrapper::getUV(int uv_index, float *uv) } } +VCOLDataWrapper::VCOLDataWrapper(COLLADAFW::MeshVertexData& vdata) : mVData(&vdata){ +} + +void VCOLDataWrapper::get_vcol(int v_index, MLoopCol *mloopcol) +{ + int stride = mVData->getStride(0); + if(stride == 0) stride =3; + + switch (mVData->getType()) { + case COLLADAFW::MeshVertexData::DATA_TYPE_FLOAT: + { + COLLADAFW::ArrayPrimitiveType *values = mVData->getFloatValues(); + if (values->empty()) return; + + mloopcol->r = FTOCHAR((*values)[v_index * stride]); + mloopcol->g = FTOCHAR((*values)[v_index * stride + 1]); + mloopcol->b = FTOCHAR((*values)[v_index * stride + 2]); + } + break; + + case COLLADAFW::MeshVertexData::DATA_TYPE_DOUBLE: + { + COLLADAFW::ArrayPrimitiveType *values = mVData->getDoubleValues(); + if (values->empty()) return; + + mloopcol->r = FTOCHAR((*values)[v_index * stride]); + mloopcol->g = FTOCHAR((*values)[v_index * stride + 1]); + mloopcol->b = FTOCHAR((*values)[v_index * stride + 2]); + } + break; + default: + fprintf(stderr, "VCOLDataWrapper.getvcol(): unknown data type\n"); + } + +} + MeshImporter::MeshImporter(UnitConverter *unitconv, ArmatureImporter *arm, Scene *sce) : unitconverter(unitconv), scene(sce), armature_importer(arm) { } @@ -187,6 +223,17 @@ void MeshImporter::set_poly_indices(MPoly *mpoly, MLoop *mloop, int loop_index, } } +void MeshImporter::set_vcol(MLoopCol *mlc, VCOLDataWrapper &vob, int loop_index, COLLADAFW::IndexList &index_list, int count) +{ + COLLADAFW::UIntValuesArray& indices =index_list.getIndices(); + int index; + for(index = 0; index < count; index++,mlc++) + { + int v_index = indices[index+loop_index]; + vob.get_vcol(v_index,mlc); + } +} + void MeshImporter::set_face_uv(MLoopUV *mloopuv, UVDataWrapper &uvs, int start_index, COLLADAFW::IndexList& index_list, int count) { @@ -328,6 +375,17 @@ bool MeshImporter::primitive_has_faces(COLLADAFW::MeshPrimitive *mp) { return has_faces; } + +static std::string extract_vcolname(const COLLADAFW::String &collada_id) { + std::string colname = collada_id; + int spos = colname.find("-mesh-colors-"); + if (spos != std::string::npos) { + colname = colname.substr(spos+13); + } + return colname; +} + + // ================================================================= // Return the number of faces by summing up // the facecounts of the parts. @@ -337,8 +395,9 @@ bool MeshImporter::primitive_has_faces(COLLADAFW::MeshPrimitive *mp) { void MeshImporter::allocate_poly_data(COLLADAFW::Mesh *collada_mesh, Mesh *me) { COLLADAFW::MeshPrimitiveArray& prim_arr = collada_mesh->getMeshPrimitives(); - int total_poly_count = 0; - int total_loop_count = 0; + int total_poly_count = 0; + int total_loop_count = 0; + int total_color_count = 0; // collect edge_count and face_count from all parts for (int i = 0; i < prim_arr.getCount(); i++) { @@ -360,6 +419,7 @@ void MeshImporter::allocate_poly_data(COLLADAFW::Mesh *collada_mesh, Mesh *me) total_poly_count += prim_poly_count; total_loop_count += prim_loop_count; + break; } default: @@ -394,6 +454,17 @@ void MeshImporter::allocate_poly_data(COLLADAFW::Mesh *collada_mesh, Mesh *me) me->mtpoly = (MTexPoly *)CustomData_get_layer_n(&me->pdata, CD_MTEXPOLY, 0); me->mloopuv = (MLoopUV *) CustomData_get_layer_n(&me->ldata, CD_MLOOPUV, 0); } + + int totcolset = collada_mesh->getColors().getInputInfosArray().getCount(); + if (totcolset > 0) { + for (int i = 0; i < totcolset; i++) { + COLLADAFW::MeshVertexData::InputInfos *info = collada_mesh->getColors().getInputInfosArray()[i]; + COLLADAFW::String colname = extract_vcolname(info->mName); + CustomData_add_layer_named(&me->ldata,CD_MLOOPCOL,CD_DEFAULT,NULL,me->totloop, colname.c_str()); + } + me->mloopcol = (MLoopCol *) CustomData_get_layer_n(&me->ldata, CD_MLOOPCOL, 0); + } + } } @@ -538,6 +609,7 @@ void MeshImporter::read_polys(COLLADAFW::Mesh *collada_mesh, Mesh *me) allocate_poly_data(collada_mesh, me); UVDataWrapper uvs(collada_mesh->getUVCoords()); + VCOLDataWrapper vcol(collada_mesh->getColors()); MPoly *mpoly = me->mpoly; MLoop *mloop = me->mloop; @@ -553,9 +625,9 @@ void MeshImporter::read_polys(COLLADAFW::Mesh *collada_mesh, Mesh *me) COLLADAFW::MeshPrimitive *mp = prim_arr[i]; // faces - size_t prim_totpoly = mp->getFaceCount(); - unsigned int *position_indices = mp->getPositionIndices().getData(); - unsigned int *normal_indices = mp->getNormalIndices().getData(); + size_t prim_totpoly = mp->getFaceCount(); + unsigned int *position_indices = mp->getPositionIndices().getData(); + unsigned int *normal_indices = mp->getNormalIndices().getData(); bool mp_has_normals = primitive_has_useable_normals(mp); bool mp_has_faces = primitive_has_faces(mp); @@ -564,7 +636,6 @@ void MeshImporter::read_polys(COLLADAFW::Mesh *collada_mesh, Mesh *me) // since we cannot set mpoly->mat_nr here, we store a portion of me->mpoly in Primitive Primitive prim = {mpoly, 0}; - COLLADAFW::IndexListArray& index_list_array = mp->getUVCoordIndicesArray(); // If MeshPrimitive is TRIANGLE_FANS we split it into triangles // The first trifan vertex will be the first vertex in every triangle @@ -611,6 +682,9 @@ void MeshImporter::read_polys(COLLADAFW::Mesh *collada_mesh, Mesh *me) COLLADAFW::Polygons *mpvc = (COLLADAFW::Polygons *)mp; unsigned int start_index = 0; + COLLADAFW::IndexListArray& index_list_array_uvcoord = mp->getUVCoordIndicesArray(); + COLLADAFW::IndexListArray& index_list_array_vcolor = mp->getColorIndicesArray(); + for (unsigned int j = 0; j < prim_totpoly; j++) { // Vertices in polygon: @@ -618,15 +692,15 @@ void MeshImporter::read_polys(COLLADAFW::Mesh *collada_mesh, Mesh *me) set_poly_indices(mpoly, mloop, loop_index, position_indices, vcount); - for (unsigned int uvset_index = 0; uvset_index < index_list_array.getCount(); uvset_index++) { + for (unsigned int uvset_index = 0; uvset_index < index_list_array_uvcoord.getCount(); uvset_index++) { // get mtface by face index and uv set index - COLLADAFW::IndexList& index_list = *index_list_array[uvset_index]; + COLLADAFW::IndexList& index_list = *index_list_array_uvcoord[uvset_index]; MLoopUV *mloopuv = (MLoopUV *)CustomData_get_layer_named(&me->ldata, CD_MLOOPUV, index_list.getName().c_str()); if (mloopuv == NULL) { - fprintf(stderr, "Collada import: Mesh [%s] : Unknown reference to TEXCOORD [#%s].", me->id.name, index_list.getName().c_str() ); + fprintf(stderr, "Collada import: Mesh [%s] : Unknown reference to TEXCOORD [#%s].\n", me->id.name, index_list.getName().c_str() ); } else { - set_face_uv(mloopuv+loop_index, uvs, start_index, *index_list_array[uvset_index], vcount); + set_face_uv(mloopuv+loop_index, uvs, start_index, *index_list_array_uvcoord[uvset_index], vcount); } } @@ -634,10 +708,23 @@ void MeshImporter::read_polys(COLLADAFW::Mesh *collada_mesh, Mesh *me) if (!is_flat_face(normal_indices, nor, vcount)) mpoly->flag |= ME_SMOOTH; } - + + for(unsigned int vcolor_index = 0 ; vcolor_index < index_list_array_vcolor.getCount();vcolor_index++) + { + COLLADAFW::IndexList& index_list = *index_list_array_vcolor[vcolor_index]; + COLLADAFW::String colname = extract_vcolname(index_list.getName()); + MLoopCol *mloopcol = (MLoopCol *)CustomData_get_layer_named(&me->ldata, CD_MLOOPCOL, colname.c_str()); + if (mloopcol == NULL) { + fprintf(stderr, "Collada import: Mesh [%s] : Unknown reference to VCOLOR [#%s].\n", me->id.name, index_list.getName().c_str() ); + } + else { + set_vcol(mloopcol+loop_index, vcol, start_index, *index_list_array_vcolor[vcolor_index], vcount); + } + } + mpoly++; - mloop += vcount; - loop_index += vcount; + mloop += vcount; + loop_index += vcount; start_index += vcount; prim.totpoly++; @@ -654,6 +741,8 @@ void MeshImporter::read_polys(COLLADAFW::Mesh *collada_mesh, Mesh *me) if (mp_has_faces) mat_prim_map[mp->getMaterialId()].push_back(prim); + + } geom_uid_mat_mapping_map[collada_mesh->getUniqueId()] = mat_prim_map; @@ -1114,5 +1203,6 @@ bool MeshImporter::write_geometry(const COLLADAFW::Geometry *geom) // read_lines() must be called after the face edges have been generated. // Oterwise the loose edges will be silently deleted again. read_lines(mesh, me); + return true; } -- cgit v1.2.3