📄 mesh.cpp
字号:
* \return None * * \warning WIN32: Should only be used in a console window not in a GUI. * * \ingroup mesh */voidlib3ds_mesh_dump(Lib3dsMesh *mesh){ unsigned i; Lib3dsVector p; ASSERT(mesh); printf(" %s vertices=%d faces=%d\n", mesh->name, mesh->points, mesh->faces ); printf(" matrix:\n"); lib3ds_matrix_dump(mesh->matrix); printf(" point list:\n"); for (i=0; i<mesh->points; ++i) { lib3ds_vector_copy(p, mesh->pointL[i].pos); printf (" %8f %8f %8f\n", p[0], p[1], p[2]); } printf(" facelist:\n"); for (i=0; i<mesh->points; ++i) { printf (" %4d %4d %4d smoothing:%X\n", mesh->faceL[i].points[0], mesh->faceL[i].points[1], mesh->faceL[i].points[2], static_cast<int>(mesh->faceL[i].smoothing) ); }}/*! * \ingroup mesh */Lib3dsBoollib3ds_mesh_read(Lib3dsMesh *mesh, FILE *f){ Lib3dsChunk c; Lib3dsWord chunk; if (!lib3ds_chunk_read_start(&c, LIB3DS_N_TRI_OBJECT, f)) { return(LIB3DS_FALSE); } while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { switch (chunk) { case LIB3DS_MESH_MATRIX: { int i,j; lib3ds_matrix_identity(mesh->matrix); for (i=0; i<4; i++) { for (j=0; j<3; j++) { mesh->matrix[i][j]=lib3ds_float_read(f); } } } break; case LIB3DS_MESH_COLOR: { mesh->color=lib3ds_byte_read(f); } break; case LIB3DS_POINT_ARRAY: { unsigned i,j; unsigned points; lib3ds_mesh_free_point_list(mesh); points=lib3ds_word_read(f); if (points) { if (!lib3ds_mesh_new_point_list(mesh, points)) { LIB3DS_ERROR_LOG; return(LIB3DS_FALSE); } for (i=0; i<mesh->points; ++i) { for (j=0; j<3; ++j) { mesh->pointL[i].pos[j]=lib3ds_float_read(f); } } ASSERT((!mesh->flags) || (mesh->points==mesh->flags)); ASSERT((!mesh->texels) || (mesh->points==mesh->texels)); } } break; case LIB3DS_POINT_FLAG_ARRAY: { unsigned i; unsigned flags; lib3ds_mesh_free_flag_list(mesh); flags=lib3ds_word_read(f); if (flags) { if (!lib3ds_mesh_new_flag_list(mesh, flags)) { LIB3DS_ERROR_LOG; return(LIB3DS_FALSE); } for (i=0; i<mesh->flags; ++i) { mesh->flagL[i]=lib3ds_word_read(f); } ASSERT((!mesh->points) || (mesh->flags==mesh->points)); ASSERT((!mesh->texels) || (mesh->flags==mesh->texels)); } } break; case LIB3DS_FACE_ARRAY: { lib3ds_chunk_read_reset(&c, f); if (!face_array_read(mesh, f)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MESH_TEXTURE_INFO: { int i,j; for (i=0; i<2; ++i) { mesh->map_data.tile[i]=lib3ds_float_read(f); } for (i=0; i<3; ++i) { mesh->map_data.pos[i]=lib3ds_float_read(f); } mesh->map_data.scale=lib3ds_float_read(f); lib3ds_matrix_identity(mesh->map_data.matrix); for (i=0; i<4; i++) { for (j=0; j<3; j++) { mesh->map_data.matrix[i][j]=lib3ds_float_read(f); } } for (i=0; i<2; ++i) { mesh->map_data.planar_size[i]=lib3ds_float_read(f); } mesh->map_data.cylinder_height=lib3ds_float_read(f); } break; case LIB3DS_TEX_VERTS: { unsigned i; unsigned texels; lib3ds_mesh_free_texel_list(mesh); texels=lib3ds_word_read(f); if (texels) { if (!lib3ds_mesh_new_texel_list(mesh, texels)) { LIB3DS_ERROR_LOG; return(LIB3DS_FALSE); } for (i=0; i<mesh->texels; ++i) { mesh->texelL[i][0]=lib3ds_float_read(f); mesh->texelL[i][1]=lib3ds_float_read(f); } ASSERT((!mesh->points) || (mesh->texels==mesh->points)); ASSERT((!mesh->flags) || (mesh->texels==mesh->flags)); } } break; default: lib3ds_chunk_unknown(chunk); } } { unsigned j; for (j=0; j<mesh->faces; ++j) { ASSERT(mesh->faceL[j].points[0]<mesh->points); ASSERT(mesh->faceL[j].points[1]<mesh->points); ASSERT(mesh->faceL[j].points[2]<mesh->points); lib3ds_vector_normal( mesh->faceL[j].normal, mesh->pointL[mesh->faceL[j].points[0]].pos, mesh->pointL[mesh->faceL[j].points[1]].pos, mesh->pointL[mesh->faceL[j].points[2]].pos ); } } lib3ds_chunk_read_end(&c, f); return(LIB3DS_TRUE);}static Lib3dsBoolpoint_array_write(Lib3dsMesh *mesh, FILE *f){ Lib3dsChunk c; unsigned i; if (!mesh->points || !mesh->pointL) { return(LIB3DS_TRUE); } ASSERT(mesh->points<0x10000); c.chunk=LIB3DS_POINT_ARRAY; c.size=8+12*mesh->points; lib3ds_chunk_write(&c, f); lib3ds_word_write((Lib3dsWord)mesh->points, f); for (i=0; i<mesh->points; ++i) { lib3ds_vector_write(mesh->pointL[i].pos, f); } return(LIB3DS_TRUE);}static Lib3dsBoolflag_array_write(Lib3dsMesh *mesh, FILE *f){ Lib3dsChunk c; unsigned i; if (!mesh->flags || !mesh->flagL) { return(LIB3DS_TRUE); } ASSERT(mesh->flags<0x10000); c.chunk=LIB3DS_POINT_FLAG_ARRAY; c.size=8+2*mesh->flags; lib3ds_chunk_write(&c, f); lib3ds_word_write((Lib3dsWord)mesh->flags, f); for (i=0; i<mesh->flags; ++i) { lib3ds_word_write(mesh->flagL[i], f); } return(LIB3DS_TRUE);}static Lib3dsBoolface_array_write(Lib3dsMesh *mesh, FILE *f){ Lib3dsChunk c; if (!mesh->faces || !mesh->faceL) { return(LIB3DS_TRUE); } ASSERT(mesh->faces<0x10000); c.chunk=LIB3DS_FACE_ARRAY; if (!lib3ds_chunk_write_start(&c, f)) { return(LIB3DS_FALSE); } { unsigned i; lib3ds_word_write((Lib3dsWord)mesh->faces, f); for (i=0; i<mesh->faces; ++i) { lib3ds_word_write(mesh->faceL[i].points[0], f); lib3ds_word_write(mesh->faceL[i].points[1], f); lib3ds_word_write(mesh->faceL[i].points[2], f); lib3ds_word_write(mesh->faceL[i].flags, f); } } { /*---- MSH_MAT_GROUP ----*/ Lib3dsChunk c; unsigned i,j; Lib3dsWord num; char *matf=(char*)calloc(sizeof(char), mesh->faces); if (!matf) { return(LIB3DS_FALSE); } for (i=0; i<mesh->faces; ++i) { if (!matf[i] && strlen(mesh->faceL[i].material)) { matf[i]=1; num=1; for (j=i+1; j<mesh->faces; ++j) { if (strcmp(mesh->faceL[i].material, mesh->faceL[j].material)==0) ++num; } c.chunk=LIB3DS_MSH_MAT_GROUP; c.size=6+ strlen(mesh->faceL[i].material)+1 +2+2*num; lib3ds_chunk_write(&c, f); lib3ds_string_write(mesh->faceL[i].material, f); lib3ds_word_write(num, f); lib3ds_word_write((Lib3dsWord)i, f); for (j=i+1; j<mesh->faces; ++j) { if (strcmp(mesh->faceL[i].material, mesh->faceL[j].material)==0) { lib3ds_word_write((Lib3dsWord)j, f); matf[j]=1; } } } } free(matf); } { /*---- SMOOTH_GROUP ----*/ Lib3dsChunk c; unsigned i; c.chunk=LIB3DS_SMOOTH_GROUP; c.size=6+4*mesh->faces; lib3ds_chunk_write(&c, f); for (i=0; i<mesh->faces; ++i) { lib3ds_dword_write(mesh->faceL[i].smoothing, f); } } { /*---- MSH_BOXMAP ----*/ Lib3dsChunk c; if (strlen(mesh->box_map.front) || strlen(mesh->box_map.back) || strlen(mesh->box_map.left) || strlen(mesh->box_map.right) || strlen(mesh->box_map.top) || strlen(mesh->box_map.bottom)) { c.chunk=LIB3DS_MSH_BOXMAP; if (!lib3ds_chunk_write_start(&c, f)) { return(LIB3DS_FALSE); } lib3ds_string_write(mesh->box_map.front, f); lib3ds_string_write(mesh->box_map.back, f); lib3ds_string_write(mesh->box_map.left, f); lib3ds_string_write(mesh->box_map.right, f); lib3ds_string_write(mesh->box_map.top, f); lib3ds_string_write(mesh->box_map.bottom, f); if (!lib3ds_chunk_write_end(&c, f)) { return(LIB3DS_FALSE); } } } if (!lib3ds_chunk_write_end(&c, f)) { return(LIB3DS_FALSE); } return(LIB3DS_TRUE);}static Lib3dsBooltexel_array_write(Lib3dsMesh *mesh, FILE *f){ Lib3dsChunk c; unsigned i; if (!mesh->texels || !mesh->texelL) { return(LIB3DS_TRUE); } ASSERT(mesh->texels<0x10000); c.chunk=LIB3DS_TEX_VERTS; c.size=8+8*mesh->texels; lib3ds_chunk_write(&c, f); lib3ds_word_write((Lib3dsWord)mesh->texels, f); for (i=0; i<mesh->texels; ++i) { lib3ds_float_write(mesh->texelL[i][0], f); lib3ds_float_write(mesh->texelL[i][1], f); } return(LIB3DS_TRUE);}/*! * \ingroup mesh */Lib3dsBoollib3ds_mesh_write(Lib3dsMesh *mesh, FILE *f){ Lib3dsChunk c; c.chunk=LIB3DS_N_TRI_OBJECT; if (!lib3ds_chunk_write_start(&c,f)) { return(LIB3DS_FALSE); } if (!point_array_write(mesh, f)) { return(LIB3DS_FALSE); } if (!texel_array_write(mesh, f)) { return(LIB3DS_FALSE); } if (mesh->map_data.maptype!=LIB3DS_MAP_NONE) { /*---- LIB3DS_MESH_TEXTURE_INFO ----*/ Lib3dsChunk c; int i,j; c.chunk=LIB3DS_MESH_TEXTURE_INFO; c.size=92; if (!lib3ds_chunk_write(&c,f)) { return(LIB3DS_FALSE); } lib3ds_word_write(mesh->map_data.maptype, f); for (i=0; i<2; ++i) { lib3ds_float_write(mesh->map_data.tile[i], f); } for (i=0; i<3; ++i) { lib3ds_float_write(mesh->map_data.pos[i], f); } lib3ds_float_write(mesh->map_data.scale, f); for (i=0; i<4; i++) { for (j=0; j<3; j++) { lib3ds_float_write(mesh->map_data.matrix[i][j], f); } } for (i=0; i<2; ++i) { lib3ds_float_write(mesh->map_data.planar_size[i], f); } lib3ds_float_write(mesh->map_data.cylinder_height, f); } if (!flag_array_write(mesh, f)) { return(LIB3DS_FALSE); } { /*---- LIB3DS_MESH_MATRIX ----*/ Lib3dsChunk c; int i,j; c.chunk=LIB3DS_MESH_MATRIX; c.size=54; if (!lib3ds_chunk_write(&c,f)) { return(LIB3DS_FALSE); } for (i=0; i<4; i++) { for (j=0; j<3; j++) { lib3ds_float_write(mesh->matrix[i][j], f); } } } if (mesh->color) { /*---- LIB3DS_MESH_COLOR ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MESH_COLOR; c.size=7; if (!lib3ds_chunk_write(&c,f)) { return(LIB3DS_FALSE); } lib3ds_byte_write(mesh->color, f); } if (!face_array_write(mesh, f)) { return(LIB3DS_FALSE); } if (!lib3ds_chunk_write_end(&c,f)) { return(LIB3DS_FALSE); } return(LIB3DS_TRUE);}/*!\typedef Lib3dsFace \ingroup mesh \sa _Lib3dsFace*//*!\typedef Lib3dsBoxMap \ingroup mesh \sa _Lib3dsBoxMap*//*!\typedef Lib3dsMapData \ingroup mesh \sa _Lib3dsMapData*//*!\typedef Lib3dsMesh \ingroup mesh \sa _Lib3dsMesh*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -