Changeset View
Changeset View
Standalone View
Standalone View
source/blender/collada/MeshImporter.cpp
| Context not available. | |||||
| } | } | ||||
| UVDataWrapper::UVDataWrapper(COLLADAFW::MeshVertexData& vdata) : mVData(&vdata) | MVDataWrapper::MVDataWrapper(COLLADAFW::MeshVertexData& vdata) : mVData(&vdata) | ||||
| { | { | ||||
| } | } | ||||
| Context not available. | |||||
| } | } | ||||
| #endif | #endif | ||||
| void UVDataWrapper::getUV(int uv_index, float *uv) | void MVDataWrapper::getvcol(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<float> *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<double> *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; | |||||
| case COLLADAFW::MeshVertexData::DATA_TYPE_UNKNOWN: | |||||
| default: | |||||
| fprintf(stderr, "MVDataWrapper getvcol (): unknown data type\n"); | |||||
| } | |||||
| } | |||||
| void MVDataWrapper::getUV(int uv_index, float *uv) | |||||
| { | |||||
| int stride = mVData->getStride(0); | int stride = mVData->getStride(0); | ||||
| if (stride == 0) stride = 2; | if (stride == 0) stride = 2; | ||||
gaiaclary: If the mVData->getType() can be converted into a meaningful string, then wouldn't it be more… | |||||
| Context not available. | |||||
| } | } | ||||
| } | } | ||||
| void MeshImporter::set_face_uv(MLoopUV *mloopuv, UVDataWrapper &uvs, | void MeshImporter::set_face_uv(MLoopUV *mloopuv, MVDataWrapper &uvs, | ||||
| int start_index, COLLADAFW::IndexList& index_list, int count) | int start_index, COLLADAFW::IndexList& index_list, int count) | ||||
| { | { | ||||
| // per face vertex indices, this means for quad we have 4 indices, not 8 | // per face vertex indices, this means for quad we have 4 indices, not 8 | ||||
| Context not available. | |||||
| } | } | ||||
| } | } | ||||
| void MeshImporter::set_vcol(MLoopCol *mlc, MVDataWrapper &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.getvcol(v_index,mlc); | |||||
| } | |||||
| } | |||||
| #ifdef COLLADA_DEBUG | #ifdef COLLADA_DEBUG | ||||
| void MeshImporter::print_index_list(COLLADAFW::IndexList& index_list) | void MeshImporter::print_index_list(COLLADAFW::IndexList& index_list) | ||||
| { | { | ||||
| Context not available. | |||||
| get_vector(mvert->co, pos, i, stride); | get_vector(mvert->co, pos, i, stride); | ||||
| } | } | ||||
| } | } | ||||
| bool MeshImporter::has_vcolor(COLLADAFW::MeshPrimitive *mp) | |||||
| { | |||||
| bool hasvcolor = false; | |||||
| if(mp->hasColorIndices()) | |||||
| hasvcolor = true; | |||||
| return hasvcolor; | |||||
| } | |||||
| // ===================================================================== | // ===================================================================== | ||||
| // condition 1: The Primitive has normals | // condition 1: The Primitive has normals | ||||
| // condition 2: The number of normals equals the number of faces. | // condition 2: The number of normals equals the number of faces. | ||||
| Context not available. | |||||
| // activate the first uv map | // activate the first uv map | ||||
| me->mtpoly = (MTexPoly *)CustomData_get_layer_n(&me->pdata, CD_MTEXPOLY, 0); | me->mtpoly = (MTexPoly *)CustomData_get_layer_n(&me->pdata, CD_MTEXPOLY, 0); | ||||
| me->mloopuv = (MLoopUV *) CustomData_get_layer_n(&me->ldata, CD_MLOOPUV, 0); | me->mloopuv = (MLoopUV *) CustomData_get_layer_n(&me->ldata, CD_MLOOPUV, 0); | ||||
| } | } | ||||
Not Done Inline ActionsFrom where do we know that the mesh has vertex colors ? Isn't this unnecessary to do when no vcols are available ? gaiaclary: From where do we know that the mesh has vertex colors ? Isn't this unnecessary to do when no… | |||||
| CustomData_add_layer(&me->ldata,CD_MLOOPCOL,CD_DEFAULT,NULL,me->totloop); | |||||
| me->mloopcol = (MLoopCol *) CustomData_get_layer(&me->ldata,CD_MLOOPCOL); | |||||
| } | } | ||||
| } | } | ||||
| Context not available. | |||||
| allocate_poly_data(collada_mesh, me); | allocate_poly_data(collada_mesh, me); | ||||
Not Done Inline ActionsWhat happens if the mesh has no uvs or no vcols ? gaiaclary: What happens if the mesh has no uvs or no vcols ? | |||||
gaiaclaryUnsubmitted Not Done Inline ActionsBelow this line something odd seems to happen with the whitespaces. In order to get an easier comparison, would it be possible to fix this ? (Probably just replace whitespaces by tabs ?) gaiaclary: Below this line something odd seems to happen with the whitespaces. In order to get an easier… | |||||
| UVDataWrapper uvs(collada_mesh->getUVCoords()); | MVDataWrapper uvs(collada_mesh->getUVCoords()); | ||||
| MVDataWrapper vcol(collada_mesh->getColors()); | |||||
| MPoly *mpoly = me->mpoly; | MPoly *mpoly = me->mpoly; | ||||
| MLoop *mloop = me->mloop; | MLoop *mloop = me->mloop; | ||||
| int loop_index = 0; | int loop_index = 0; | ||||
| MaterialIdPrimitiveArrayMap mat_prim_map; | MaterialIdPrimitiveArrayMap mat_prim_map; | ||||
| COLLADAFW::MeshPrimitiveArray& prim_arr = collada_mesh->getMeshPrimitives(); | COLLADAFW::MeshPrimitiveArray& prim_arr = collada_mesh->getMeshPrimitives(); | ||||
| COLLADAFW::MeshVertexData& nor = collada_mesh->getNormals(); | COLLADAFW::MeshVertexData& nor = collada_mesh->getNormals(); | ||||
| for (i = 0; i < prim_arr.getCount(); i++) { | MLoopCol *mloopcol = (MLoopCol *)CustomData_get_layer(&me->ldata,CD_MLOOPCOL); | ||||
| for (i = 0; i < prim_arr.getCount(); i++) { | |||||
| COLLADAFW::MeshPrimitive *mp = prim_arr[i]; | |||||
| // faces | COLLADAFW::MeshPrimitive *mp = prim_arr[i]; | ||||
| 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); | // faces | ||||
| bool mp_has_faces = primitive_has_faces(mp); | size_t prim_totpoly = mp->getFaceCount(); | ||||
| unsigned int *position_indices = mp->getPositionIndices().getData(); | |||||
| unsigned int *normal_indices = mp->getNormalIndices().getData(); | |||||
| int collada_meshtype = mp->getPrimitiveType(); | bool mp_has_normals = primitive_has_useable_normals(mp); | ||||
Not Done Inline Actionsnot needed imho (see below) gaiaclary: not needed imho (see below) | |||||
| bool mp_has_faces = primitive_has_faces(mp); | |||||
| // since we cannot set mpoly->mat_nr here, we store a portion of me->mpoly in Primitive | bool mp_has_vcolor = has_vcolor(mp); | ||||
| Primitive prim = {mpoly, 0}; | |||||
| COLLADAFW::IndexListArray& index_list_array = mp->getUVCoordIndicesArray(); | |||||
| // If MeshPrimitive is TRIANGLE_FANS we split it into triangles | int collada_meshtype = mp->getPrimitiveType(); | ||||
| // The first trifan vertex will be the first vertex in every triangle | |||||
| // XXX The proper function of TRIANGLE_FANS is not tested!!! | |||||
| // XXX In particular the handling of the normal_indices looks very wrong to me | |||||
| if (collada_meshtype == COLLADAFW::MeshPrimitive::TRIANGLE_FANS) { | |||||
| unsigned grouped_vertex_count = mp->getGroupedVertexElementsCount(); | |||||
| for (unsigned int group_index = 0; group_index < grouped_vertex_count; group_index++) { | |||||
| unsigned int first_vertex = position_indices[0]; // Store first trifan vertex | |||||
| unsigned int first_normal = normal_indices[0]; // Store first trifan vertex normal | |||||
| unsigned int vertex_count = mp->getGroupedVerticesVertexCount(group_index); | |||||
| for (unsigned int vertex_index = 0; vertex_index < vertex_count - 2; vertex_index++) { | // since we cannot set mpoly->mat_nr here, we store a portion of me->mpoly in Primitive | ||||
| // For each triangle store indeces of its 3 vertices | Primitive prim = {mpoly, 0}; | ||||
Not Done Inline ActionsCould this be moved just before line 691 ? gaiaclary: Could this be moved just before line 691 ? | |||||
| unsigned int triangle_vertex_indices[3] = {first_vertex, position_indices[1], position_indices[2]}; | COLLADAFW::IndexListArray& index_list_array = mp->getUVCoordIndicesArray(); | ||||
| set_poly_indices(mpoly, mloop, loop_index, triangle_vertex_indices, 3); | COLLADAFW::IndexListArray& index_list_array_vcolor =mp->getColorIndicesArray(); | ||||
| if (mp_has_normals) { // vertex normals, same inplementation as for the triangles | // If MeshPrimitive is TRIANGLE_FANS we split it into triangles | ||||
| // the same for vertces normals | // The first trifan vertex will be the first vertex in every triangle | ||||
| unsigned int vertex_normal_indices[3] = {first_normal, normal_indices[1], normal_indices[2]}; | // XXX The proper function of TRIANGLE_FANS is not tested!!! | ||||
| if (!is_flat_face(vertex_normal_indices, nor, 3)) | // XXX In particular the handling of the normal_indices looks very wrong to me | ||||
| mpoly->flag |= ME_SMOOTH; | if (collada_meshtype == COLLADAFW::MeshPrimitive::TRIANGLE_FANS) { | ||||
| normal_indices++; | unsigned grouped_vertex_count = mp->getGroupedVertexElementsCount(); | ||||
| } | for (unsigned int group_index = 0; group_index < grouped_vertex_count; group_index++) { | ||||
| unsigned int first_vertex = position_indices[0]; // Store first trifan vertex | |||||
| mpoly++; | unsigned int first_normal = normal_indices[0]; // Store first trifan vertex normal | ||||
| mloop += 3; | unsigned int vertex_count = mp->getGroupedVerticesVertexCount(group_index); | ||||
| loop_index += 3; | |||||
| prim.totpoly++; | |||||
| } | for (unsigned int vertex_index = 0; vertex_index < vertex_count - 2; vertex_index++) { | ||||
| // For each triangle store indeces of its 3 vertices | |||||
| unsigned int triangle_vertex_indices[3] = {first_vertex, position_indices[1], position_indices[2]}; | |||||
| set_poly_indices(mpoly, mloop, loop_index, triangle_vertex_indices, 3); | |||||
| // Moving cursor to the next triangle fan. | if (mp_has_normals) { // vertex normals, same inplementation as for the triangles | ||||
| if (mp_has_normals) | // the same for vertces normals | ||||
| normal_indices += 2; | unsigned int vertex_normal_indices[3] = {first_normal, normal_indices[1], normal_indices[2]}; | ||||
| if (!is_flat_face(vertex_normal_indices, nor, 3)) | |||||
| mpoly->flag |= ME_SMOOTH; | |||||
| normal_indices++; | |||||
| } | |||||
| position_indices += 2; | mpoly++; | ||||
| } | mloop += 3; | ||||
| } | loop_index += 3; | ||||
| prim.totpoly++; | |||||
| if (collada_meshtype == COLLADAFW::MeshPrimitive::POLYLIST || | } | ||||
| collada_meshtype == COLLADAFW::MeshPrimitive::POLYGONS || | |||||
| collada_meshtype == COLLADAFW::MeshPrimitive::TRIANGLES) { | |||||
| COLLADAFW::Polygons *mpvc = (COLLADAFW::Polygons *)mp; | |||||
| unsigned int start_index = 0; | |||||
| for (unsigned int j = 0; j < prim_totpoly; j++) { | // Moving cursor to the next triangle fan. | ||||
| if (mp_has_normals) | |||||
| // Vertices in polygon: | normal_indices += 2; | ||||
| int vcount = get_vertex_count(mpvc, j); | |||||
| set_poly_indices(mpoly, mloop, loop_index, position_indices, vcount); | |||||
| position_indices += 2; | |||||
| } | |||||
| } | |||||
| for (unsigned int uvset_index = 0; uvset_index < index_list_array.getCount(); uvset_index++) { | if (collada_meshtype == COLLADAFW::MeshPrimitive::POLYLIST || | ||||
| // get mtface by face index and uv set index | collada_meshtype == COLLADAFW::MeshPrimitive::POLYGONS || | ||||
| MLoopUV *mloopuv = (MLoopUV *)CustomData_get_layer_n(&me->ldata, CD_MLOOPUV, uvset_index); | collada_meshtype == COLLADAFW::MeshPrimitive::TRIANGLES) { | ||||
| set_face_uv(mloopuv+loop_index, uvs, start_index, *index_list_array[uvset_index], vcount); | COLLADAFW::Polygons *mpvc = (COLLADAFW::Polygons *)mp; | ||||
| } | unsigned int start_index = 0; | ||||
| if (mp_has_normals) { | for (unsigned int j = 0; j < prim_totpoly; j++) { | ||||
| if (!is_flat_face(normal_indices, nor, vcount)) | |||||
| mpoly->flag |= ME_SMOOTH; | // Vertices in polygon: | ||||
| } | int vcount = get_vertex_count(mpvc, j); | ||||
| set_poly_indices(mpoly, mloop, loop_index, position_indices, vcount); | |||||
| for (unsigned int uvset_index = 0; uvset_index < index_list_array.getCount(); uvset_index++) { | |||||
| // get mtface by face index and uv set index | |||||
| MLoopUV *mloopuv = (MLoopUV *)CustomData_get_layer_n(&me->ldata, CD_MLOOPUV, uvset_index); | |||||
| set_face_uv(mloopuv+loop_index, uvs, start_index, *index_list_array[uvset_index], vcount); | |||||
| } | |||||
| if (mp_has_normals) { | |||||
| if (!is_flat_face(normal_indices, nor, vcount)) | |||||
| mpoly->flag |= ME_SMOOTH; | |||||
| } | |||||
Not Done Inline Actionsif (has_vcolor(mp)) ... gaiaclary: if (has_vcolor(mp)) ... | |||||
| if(mp_has_vcolor) | |||||
| { | |||||
| for(unsigned int vcolor_index = 0 ; vcolor_index < index_list_array_vcolor.getCount();vcolor_index++) | |||||
| { | |||||
| MLoopCol *mlc = mloopcol+mpoly->loopstart; | |||||
| set_vcol(mlc, vcol, start_index, *index_list_array_vcolor[vcolor_index],vcount); | |||||
| } | |||||
| } | |||||
| mpoly++; | mpoly++; | ||||
| mloop += vcount; | mloop += vcount; | ||||
| Context not available. | |||||
If the mVData->getType() can be converted into a meaningful string, then wouldn't it be more user friendly to write:
case COLLADAFW::MeshVertexData::DATA_TYPE_UNKNOWN:
fprintf(stderr, "MeshImporter.getvcol(): unknown data type\n");
default:
fprintf(stderr, "MeshImporter.getvcol(): unsupported data type %s\n", mVData->getType());
If there is no good way to create a meaningful datatype string, then imho you could remove line 201