static Lib3dsBool ambient_read(Lib3dsFile *file, Lib3dsIo *io) { Lib3dsChunk c; Lib3dsWord chunk; Lib3dsBool have_lin=LIB3DS_FALSE; if (!lib3ds_chunk_read_start(&c, LIB3DS_AMBIENT_LIGHT, io)) { return(LIB3DS_FALSE); } while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) { switch (chunk) { case LIB3DS_LIN_COLOR_F: { int i; for (i=0; i<3; ++i) { file->ambient[i]=lib3ds_io_read_float(io); } } have_lin=LIB3DS_TRUE; break; case LIB3DS_COLOR_F: { /* gamma corrected color chunk replaced in 3ds R3 by LIN_COLOR_24 */ if (!have_lin) { int i; for (i=0; i<3; ++i) { file->ambient[i]=lib3ds_io_read_float(io); } } } break; default: lib3ds_chunk_unknown(chunk); } } lib3ds_chunk_read_end(&c, io); return(LIB3DS_TRUE); }
static void v_gradient_read(Lib3dsBackground *background, Lib3dsIo *io) { Lib3dsChunk c; uint16_t chunk; int index[2]; float col[2][3][3]; int have_lin = 0; lib3ds_chunk_read_start(&c, CHK_V_GRADIENT, io); background->gradient_percent = lib3ds_io_read_float(io); lib3ds_chunk_read_tell(&c, io); index[0] = index[1] = 0; while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) { switch (chunk) { case CHK_COLOR_F: lib3ds_io_read_rgb(io, col[0][index[0]]); index[0]++; break; case CHK_LIN_COLOR_F: lib3ds_io_read_rgb(io, col[1][index[1]]); index[1]++; have_lin = 1; break; default: lib3ds_chunk_unknown(chunk, io); } } { int i; for (i = 0; i < 3; ++i) { background->gradient_top[i] = col[have_lin][0][i]; background->gradient_middle[i] = col[have_lin][1][i]; background->gradient_bottom[i] = col[have_lin][2][i]; } } lib3ds_chunk_read_end(&c, io); }
static Lib3dsBool fog_read(Lib3dsFog *fog, Lib3dsIo *io) { Lib3dsChunk c; Lib3dsWord chunk; if (!lib3ds_chunk_read_start(&c, LIB3DS_FOG, io)) { return(LIB3DS_FALSE); } fog->near_plane = lib3ds_io_read_float(io); fog->near_density=lib3ds_io_read_float(io); fog->far_plane=lib3ds_io_read_float(io); fog->far_density=lib3ds_io_read_float(io); lib3ds_chunk_read_tell(&c, io); while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) { switch (chunk) { case LIB3DS_LIN_COLOR_F: { int i; for (i=0; i<3; ++i) { fog->col[i]=lib3ds_io_read_float(io); } } break; case LIB3DS_COLOR_F: break; case LIB3DS_FOG_BGND: { fog->fog_background=LIB3DS_TRUE; } break; default: lib3ds_chunk_unknown(chunk); } } lib3ds_chunk_read_end(&c, io); return(LIB3DS_TRUE); }
static void int_percentage_read(float *p, Lib3dsIo *io) { Lib3dsChunk c; uint16_t chunk; lib3ds_chunk_read_start(&c, 0, io); while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) { switch (chunk) { case CHK_INT_PERCENTAGE: { int16_t i = lib3ds_io_read_intw(io); *p = (float)(1.0 * i / 100.0); break; } default: lib3ds_chunk_unknown(chunk, io); } } lib3ds_chunk_read_end(&c, io); }
static void ambient_read(Lib3dsFile *file, Lib3dsIo *io) { Lib3dsChunk c; uint16_t chunk; int have_lin = FALSE; lib3ds_chunk_read_start(&c, CHK_AMBIENT_LIGHT, io); while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) { switch (chunk) { case CHK_LIN_COLOR_F: { int i; for (i = 0; i < 3; ++i) { file->ambient[i] = lib3ds_io_read_float(io); } have_lin = TRUE; break; } case CHK_COLOR_F: { /* gamma corrected color chunk replaced in 3ds R3 by LIN_COLOR_24 */ if (!have_lin) { int i; for (i = 0; i < 3; ++i) { file->ambient[i] = lib3ds_io_read_float(io); } } break; } default: lib3ds_chunk_unknown(chunk, io); } } lib3ds_chunk_read_end(&c, io); }
void lib3ds_node_read(Lib3dsNode *node, Lib3dsIo *io) { Lib3dsChunk c; uint16_t chunk; assert(node); lib3ds_chunk_read_start(&c, 0, io); switch (c.chunk) { case CHK_AMBIENT_NODE_TAG: case CHK_OBJECT_NODE_TAG: case CHK_CAMERA_NODE_TAG: case CHK_TARGET_NODE_TAG: case CHK_LIGHT_NODE_TAG: case CHK_SPOTLIGHT_NODE_TAG: case CHK_L_TARGET_NODE_TAG: break; default: return; } while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) { switch (chunk) { case CHK_NODE_ID: { node->node_id = lib3ds_io_read_word(io); lib3ds_io_log_indent(io, 1); lib3ds_io_log(io, LIB3DS_LOG_INFO, "ID=%d", (short)node->node_id); lib3ds_io_log_indent(io, -1); break; } case CHK_NODE_HDR: { lib3ds_io_read_string(io, node->name, 64); node->flags = lib3ds_io_read_word(io); node->flags |= ((uint32_t)lib3ds_io_read_word(io)) << 16; node->user_id = lib3ds_io_read_word(io); lib3ds_io_log_indent(io, 1); lib3ds_io_log(io, LIB3DS_LOG_INFO, "NAME=%s", node->name); lib3ds_io_log(io, LIB3DS_LOG_INFO, "PARENT=%d", (short)node->user_id); lib3ds_io_log_indent(io, -1); break; } case CHK_PIVOT: { if (node->type == LIB3DS_NODE_MESH_INSTANCE) { Lib3dsMeshInstanceNode *n = (Lib3dsMeshInstanceNode*)node; lib3ds_io_read_vector(io, n->pivot); } else { lib3ds_chunk_unknown(chunk, io); } break; } case CHK_INSTANCE_NAME: { if (node->type == LIB3DS_NODE_MESH_INSTANCE) { Lib3dsMeshInstanceNode *n = (Lib3dsMeshInstanceNode*)node; lib3ds_io_read_string(io, n->instance_name, 64); } else { lib3ds_chunk_unknown(chunk, io); } break; } case CHK_BOUNDBOX: { if (node->type == LIB3DS_NODE_MESH_INSTANCE) { Lib3dsMeshInstanceNode *n = (Lib3dsMeshInstanceNode*)node; lib3ds_io_read_vector(io, n->bbox_min); lib3ds_io_read_vector(io, n->bbox_max); } else { lib3ds_chunk_unknown(chunk, io); } break; } case CHK_COL_TRACK_TAG: { Lib3dsTrack *track = 0; switch (node->type) { case LIB3DS_NODE_AMBIENT_COLOR: { Lib3dsAmbientColorNode *n = (Lib3dsAmbientColorNode*)node; track = &n->color_track; break; } case LIB3DS_NODE_OMNILIGHT: { Lib3dsOmnilightNode *n = (Lib3dsOmnilightNode*)node; track = &n->color_track; break; } case LIB3DS_NODE_SPOTLIGHT: { Lib3dsSpotlightNode *n = (Lib3dsSpotlightNode*)node; track = &n->color_track; break; } default: lib3ds_chunk_unknown(chunk, io); } if (track) { lib3ds_track_read(track, io); } break; } case CHK_POS_TRACK_TAG: { Lib3dsTrack *track = 0; switch (node->type) { case LIB3DS_NODE_MESH_INSTANCE: { Lib3dsMeshInstanceNode *n = (Lib3dsMeshInstanceNode*)node; track = &n->pos_track; break; } case LIB3DS_NODE_CAMERA: { Lib3dsCameraNode *n = (Lib3dsCameraNode*)node; track = &n->pos_track; break; } case LIB3DS_NODE_CAMERA_TARGET: { Lib3dsTargetNode *n = (Lib3dsTargetNode*)node; track = &n->pos_track; break; } case LIB3DS_NODE_OMNILIGHT: { Lib3dsOmnilightNode *n = (Lib3dsOmnilightNode*)node; track = &n->pos_track; break; } case LIB3DS_NODE_SPOTLIGHT: { Lib3dsSpotlightNode *n = (Lib3dsSpotlightNode*)node; track = &n->pos_track; break; } case LIB3DS_NODE_SPOTLIGHT_TARGET: { Lib3dsTargetNode *n = (Lib3dsTargetNode*)node; track = &n->pos_track; break; } default: lib3ds_chunk_unknown(chunk, io); } if (track) { lib3ds_track_read(track, io); } break; } case CHK_ROT_TRACK_TAG: { if (node->type == LIB3DS_NODE_MESH_INSTANCE) { Lib3dsMeshInstanceNode *n = (Lib3dsMeshInstanceNode*)node; n->rot_track.type = LIB3DS_TRACK_QUAT; lib3ds_track_read(&n->rot_track, io); } else { lib3ds_chunk_unknown(chunk, io); } break; } case CHK_SCL_TRACK_TAG: { if (node->type == LIB3DS_NODE_MESH_INSTANCE) { Lib3dsMeshInstanceNode *n = (Lib3dsMeshInstanceNode*)node; n->scl_track.type = LIB3DS_TRACK_VECTOR; lib3ds_track_read(&n->scl_track, io); } else { lib3ds_chunk_unknown(chunk, io); } break; } case CHK_FOV_TRACK_TAG: { if (node->type == LIB3DS_NODE_CAMERA) { Lib3dsCameraNode *n = (Lib3dsCameraNode*)node; n->fov_track.type = LIB3DS_TRACK_FLOAT; lib3ds_track_read(&n->fov_track, io); } else { lib3ds_chunk_unknown(chunk, io); } break; } case CHK_HOT_TRACK_TAG: { if (node->type == LIB3DS_NODE_SPOTLIGHT) { Lib3dsSpotlightNode *n = (Lib3dsSpotlightNode*)node; n->hotspot_track.type = LIB3DS_TRACK_FLOAT; lib3ds_track_read(&n->hotspot_track, io); } else { lib3ds_chunk_unknown(chunk, io); } break; } case CHK_FALL_TRACK_TAG: { if (node->type == LIB3DS_NODE_SPOTLIGHT) { Lib3dsSpotlightNode *n = (Lib3dsSpotlightNode*)node; n->falloff_track.type= LIB3DS_TRACK_FLOAT; lib3ds_track_read(&n->falloff_track, io); } else { lib3ds_chunk_unknown(chunk, io); } break; } case CHK_ROLL_TRACK_TAG: { switch (node->type) { case LIB3DS_NODE_CAMERA: { Lib3dsCameraNode *n = (Lib3dsCameraNode*)node; n->roll_track.type = LIB3DS_TRACK_FLOAT; lib3ds_track_read(&n->roll_track, io); break; } case LIB3DS_NODE_SPOTLIGHT: { Lib3dsSpotlightNode *n = (Lib3dsSpotlightNode*)node; n->roll_track.type = LIB3DS_TRACK_FLOAT; lib3ds_track_read(&n->roll_track, io); break; } default: lib3ds_chunk_unknown(chunk, io); } break; } case CHK_HIDE_TRACK_TAG: { if (node->type == LIB3DS_NODE_MESH_INSTANCE) { Lib3dsMeshInstanceNode *n = (Lib3dsMeshInstanceNode*)node; n->hide_track.type = LIB3DS_TRACK_BOOL; lib3ds_track_read(&n->hide_track, io); } else { lib3ds_chunk_unknown(chunk, io); } break; } case CHK_MORPH_SMOOTH: { if (node->type == LIB3DS_NODE_MESH_INSTANCE) { Lib3dsMeshInstanceNode *n = (Lib3dsMeshInstanceNode*)node; n->morph_smooth = lib3ds_io_read_float(io); } else { lib3ds_chunk_unknown(chunk, io); } } break; /* case LIB3DS_MORPH_TRACK_TAG: { if (node->type == LIB3DS_NODE_MESH_INSTANCE) { Lib3dsMeshInstanceNode *n = (Lib3dsMeshInstanceNode*)node; n->morph_track = lib3ds_track_new(node, LIB3DS_TRACK_MORPH, 0); lib3ds_track_read(n->morph_track, io); } else { lib3ds_chunk_unknown(chunk, io); } } break; */ default: lib3ds_chunk_unknown(chunk, io); } } lib3ds_chunk_read_end(&c, io); }
static Lib3dsBool named_object_read(Lib3dsFile *file, Lib3dsIo *io) { Lib3dsChunk c; char name[64]; Lib3dsWord chunk; Lib3dsMesh *mesh = NULL; Lib3dsCamera *camera = NULL; Lib3dsLight *light = NULL; Lib3dsDword object_flags; if (!lib3ds_chunk_read_start(&c, LIB3DS_NAMED_OBJECT, io)) { return(LIB3DS_FALSE); } if (!lib3ds_io_read_string(io, name, 64)) { return(LIB3DS_FALSE); } lib3ds_chunk_dump_info(" NAME=%s", name); lib3ds_chunk_read_tell(&c, io); object_flags = 0; while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) { switch (chunk) { case LIB3DS_N_TRI_OBJECT: { mesh=lib3ds_mesh_new(name); if (!mesh) { return(LIB3DS_FALSE); } lib3ds_chunk_read_reset(&c, io); if (!lib3ds_mesh_read(mesh, io)) { return(LIB3DS_FALSE); } lib3ds_file_insert_mesh(file, mesh); } break; case LIB3DS_N_CAMERA: { camera=lib3ds_camera_new(name); if (!camera) { return(LIB3DS_FALSE); } lib3ds_chunk_read_reset(&c, io); if (!lib3ds_camera_read(camera, io)) { return(LIB3DS_FALSE); } lib3ds_file_insert_camera(file, camera); } break; case LIB3DS_N_DIRECT_LIGHT: { light=lib3ds_light_new(name); if (!light) { return(LIB3DS_FALSE); } lib3ds_chunk_read_reset(&c, io); if (!lib3ds_light_read(light, io)) { return(LIB3DS_FALSE); } lib3ds_file_insert_light(file, light); } break; case LIB3DS_OBJ_HIDDEN: object_flags |= LIB3DS_OBJECT_HIDDEN; break; case LIB3DS_OBJ_DOESNT_CAST: object_flags |= LIB3DS_OBJECT_DOESNT_CAST; break; case LIB3DS_OBJ_VIS_LOFTER: object_flags |= LIB3DS_OBJECT_VIS_LOFTER; break; case LIB3DS_OBJ_MATTE: object_flags |= LIB3DS_OBJECT_MATTE; break; case LIB3DS_OBJ_DONT_RCVSHADOW: object_flags |= LIB3DS_OBJECT_DONT_RCVSHADOW; break; case LIB3DS_OBJ_FAST: object_flags |= LIB3DS_OBJECT_FAST; break; case LIB3DS_OBJ_FROZEN: object_flags |= LIB3DS_OBJECT_FROZEN; break; default: lib3ds_chunk_unknown(chunk); } } if (mesh) mesh->object_flags = object_flags; if (camera) camera->object_flags = object_flags; if (light) light->object_flags = object_flags; lib3ds_chunk_read_end(&c, io); return(LIB3DS_TRUE); }
static Lib3dsBool color_read(Lib3dsRgba rgb, Lib3dsIo *io) { Lib3dsChunk c; Lib3dsWord chunk; Lib3dsBool have_lin=LIB3DS_FALSE; if (!lib3ds_chunk_read_start(&c, 0, io)) { return(LIB3DS_FALSE); } while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) { switch (chunk) { case LIB3DS_LIN_COLOR_24: { int i; for (i=0; i<3; ++i) { rgb[i]=1.0f*lib3ds_io_read_byte(io)/255.0f; } rgb[3]=1.0f; } have_lin=LIB3DS_TRUE; break; case LIB3DS_COLOR_24: /* gamma corrected color chunk replaced in 3ds R3 by LIN_COLOR_24 */ if (!have_lin) { int i; for (i=0; i<3; ++i) { rgb[i]=1.0f*lib3ds_io_read_byte(io)/255.0f; } rgb[3]=1.0f; } break; case LIB3DS_LIN_COLOR_F: { int i; for (i=0; i<3; ++i) { rgb[i]=lib3ds_io_read_float(io); } rgb[3]=1.0f; } have_lin=LIB3DS_TRUE; break; case LIB3DS_COLOR_F: if (!have_lin) { int i; for (i=0; i<3; ++i) { rgb[i]=lib3ds_io_read_float(io); } rgb[3]=1.0f; } break; default: lib3ds_chunk_unknown(chunk); } } lib3ds_chunk_read_end(&c, io); return(LIB3DS_TRUE); }
static Lib3dsBool face_array_read(Lib3dsMesh *mesh, Lib3dsIo *io) { Lib3dsChunk c; Lib3dsWord chunk; int i; int faces; if (!lib3ds_chunk_read_start(&c, LIB3DS_FACE_ARRAY, io)) { return(LIB3DS_FALSE); } lib3ds_mesh_free_face_list(mesh); faces=lib3ds_io_read_word(io); if (faces) { if (!lib3ds_mesh_new_face_list(mesh, faces)) { LIB3DS_ERROR_LOG; return(LIB3DS_FALSE); } for (i=0; i<faces; ++i) { strcpy(mesh->faceL[i].material, ""); mesh->faceL[i].points[0]=lib3ds_io_read_word(io); mesh->faceL[i].points[1]=lib3ds_io_read_word(io); mesh->faceL[i].points[2]=lib3ds_io_read_word(io); mesh->faceL[i].flags=lib3ds_io_read_word(io); } lib3ds_chunk_read_tell(&c, io); while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) { switch (chunk) { case LIB3DS_SMOOTH_GROUP: { unsigned i; for (i=0; i<mesh->faces; ++i) { mesh->faceL[i].smoothing=lib3ds_io_read_dword(io); } } break; case LIB3DS_MSH_MAT_GROUP: { char name[64]; unsigned faces; unsigned i; unsigned index; if (!lib3ds_io_read_string(io, name, 64)) { return(LIB3DS_FALSE); } faces=lib3ds_io_read_word(io); for (i=0; i<faces; ++i) { index=lib3ds_io_read_word(io); ASSERT(index<mesh->faces); strcpy(mesh->faceL[index].material, name); } } break; case LIB3DS_MSH_BOXMAP: { char name[64]; if (!lib3ds_io_read_string(io, name, 64)) { return(LIB3DS_FALSE); } strcpy(mesh->box_map.front, name); if (!lib3ds_io_read_string(io, name, 64)) { return(LIB3DS_FALSE); } strcpy(mesh->box_map.back, name); if (!lib3ds_io_read_string(io, name, 64)) { return(LIB3DS_FALSE); } strcpy(mesh->box_map.left, name); if (!lib3ds_io_read_string(io, name, 64)) { return(LIB3DS_FALSE); } strcpy(mesh->box_map.right, name); if (!lib3ds_io_read_string(io, name, 64)) { return(LIB3DS_FALSE); } strcpy(mesh->box_map.top, name); if (!lib3ds_io_read_string(io, name, 64)) { return(LIB3DS_FALSE); } strcpy(mesh->box_map.bottom, name); } break; default: lib3ds_chunk_unknown(chunk); } } } lib3ds_chunk_read_end(&c, io); return(LIB3DS_TRUE); }
static void texture_map_read(Lib3dsTextureMap *map, Lib3dsIo *io) { Lib3dsChunk c; uint16_t chunk; lib3ds_chunk_read_start(&c, 0, io); while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) { switch (chunk) { case CHK_INT_PERCENTAGE: { map->percent = 1.0f * lib3ds_io_read_intw(io) / 100.0f; break; } case CHK_MAT_MAPNAME: { lib3ds_io_read_string(io, map->name, 64); lib3ds_io_log(io, LIB3DS_LOG_INFO, " NAME=%s", map->name); break; } case CHK_MAT_MAP_TILING: { map->flags = lib3ds_io_read_word(io); break; } case CHK_MAT_MAP_TEXBLUR: map->blur = lib3ds_io_read_float(io); break; case CHK_MAT_MAP_USCALE: map->scale[0] = lib3ds_io_read_float(io); break; case CHK_MAT_MAP_VSCALE: { map->scale[1] = lib3ds_io_read_float(io); break; } case CHK_MAT_MAP_UOFFSET: { map->offset[0] = lib3ds_io_read_float(io); break; } case CHK_MAT_MAP_VOFFSET: { map->offset[1] = lib3ds_io_read_float(io); break; } case CHK_MAT_MAP_ANG: { map->rotation = lib3ds_io_read_float(io); break; } case CHK_MAT_MAP_COL1: { map->tint_1[0] = 1.0f * lib3ds_io_read_byte(io) / 255.0f; map->tint_1[1] = 1.0f * lib3ds_io_read_byte(io) / 255.0f; map->tint_1[2] = 1.0f * lib3ds_io_read_byte(io) / 255.0f; break; } case CHK_MAT_MAP_COL2: { map->tint_2[0] = 1.0f * lib3ds_io_read_byte(io) / 255.0f; map->tint_2[1] = 1.0f * lib3ds_io_read_byte(io) / 255.0f; map->tint_2[2] = 1.0f * lib3ds_io_read_byte(io) / 255.0f; break; } case CHK_MAT_MAP_RCOL: { map->tint_r[0] = 1.0f * lib3ds_io_read_byte(io) / 255.0f; map->tint_r[1] = 1.0f * lib3ds_io_read_byte(io) / 255.0f; map->tint_r[2] = 1.0f * lib3ds_io_read_byte(io) / 255.0f; break; } case CHK_MAT_MAP_GCOL: { map->tint_g[0] = 1.0f * lib3ds_io_read_byte(io) / 255.0f; map->tint_g[1] = 1.0f * lib3ds_io_read_byte(io) / 255.0f; map->tint_g[2] = 1.0f * lib3ds_io_read_byte(io) / 255.0f; break; } case CHK_MAT_MAP_BCOL: { map->tint_b[0] = 1.0f * lib3ds_io_read_byte(io) / 255.0f; map->tint_b[1] = 1.0f * lib3ds_io_read_byte(io) / 255.0f; map->tint_b[2] = 1.0f * lib3ds_io_read_byte(io) / 255.0f; break; } default: lib3ds_chunk_unknown(chunk,io); } } lib3ds_chunk_read_end(&c, io); }
static Lib3dsBool kfdata_read(Lib3dsFile *file, FILE *f) { Lib3dsChunk c; Lib3dsWord chunk; if (!lib3ds_chunk_read_start(&c, LIB3DS_KFDATA, f)) { return(LIB3DS_FALSE); } while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { switch (chunk) { case LIB3DS_KFHDR: { file->keyf_revision=lib3ds_word_read(f); if (!lib3ds_string_read(file->name, 12+1, f)) { return(LIB3DS_FALSE); } file->frames=lib3ds_intd_read(f); } break; case LIB3DS_KFSEG: { file->segment_from=lib3ds_intd_read(f); file->segment_to=lib3ds_intd_read(f); } break; case LIB3DS_KFCURTIME: { file->current_frame=lib3ds_intd_read(f); } break; case LIB3DS_VIEWPORT_LAYOUT: case LIB3DS_DEFAULT_VIEW: { lib3ds_chunk_read_reset(&c, f); if (!lib3ds_viewport_read(&file->viewport_keyf, f)) { return(LIB3DS_FALSE); } } break; case LIB3DS_AMBIENT_NODE_TAG: { Lib3dsNode *node; node=lib3ds_node_new_ambient(); if (!node) { return(LIB3DS_FALSE); } lib3ds_chunk_read_reset(&c, f); if (!lib3ds_node_read(node, file, f)) { return(LIB3DS_FALSE); } lib3ds_file_insert_node(file, node); } break; case LIB3DS_OBJECT_NODE_TAG: { Lib3dsNode *node; node=lib3ds_node_new_object(); if (!node) { return(LIB3DS_FALSE); } lib3ds_chunk_read_reset(&c, f); if (!lib3ds_node_read(node, file, f)) { return(LIB3DS_FALSE); } lib3ds_file_insert_node(file, node); } break; case LIB3DS_CAMERA_NODE_TAG: { Lib3dsNode *node; node=lib3ds_node_new_camera(); if (!node) { return(LIB3DS_FALSE); } lib3ds_chunk_read_reset(&c, f); if (!lib3ds_node_read(node, file, f)) { return(LIB3DS_FALSE); } lib3ds_file_insert_node(file, node); } break; case LIB3DS_TARGET_NODE_TAG: { Lib3dsNode *node; node=lib3ds_node_new_target(); if (!node) { return(LIB3DS_FALSE); } lib3ds_chunk_read_reset(&c, f); if (!lib3ds_node_read(node, file, f)) { return(LIB3DS_FALSE); } lib3ds_file_insert_node(file, node); } break; case LIB3DS_LIGHT_NODE_TAG: case LIB3DS_SPOTLIGHT_NODE_TAG: { Lib3dsNode *node; node=lib3ds_node_new_light(); if (!node) { return(LIB3DS_FALSE); } lib3ds_chunk_read_reset(&c, f); if (!lib3ds_node_read(node, file, f)) { return(LIB3DS_FALSE); } lib3ds_file_insert_node(file, node); } break; case LIB3DS_L_TARGET_NODE_TAG: { Lib3dsNode *node; node=lib3ds_node_new_spot(); if (!node) { return(LIB3DS_FALSE); } lib3ds_chunk_read_reset(&c, f); if (!lib3ds_node_read(node, file, f)) { return(LIB3DS_FALSE); } lib3ds_file_insert_node(file, node); } break; default: lib3ds_chunk_unknown(chunk); } } lib3ds_chunk_read_end(&c, f); return(LIB3DS_TRUE); }
static Lib3dsBool named_object_read(Lib3dsFile *file, FILE *f) { Lib3dsChunk c; char name[64]; Lib3dsWord chunk; if (!lib3ds_chunk_read_start(&c, LIB3DS_NAMED_OBJECT, f)) { return(LIB3DS_FALSE); } if (!lib3ds_string_read(name, 64, f)) { return(LIB3DS_FALSE); } lib3ds_chunk_dump_info(" NAME=%s", name); lib3ds_chunk_read_tell(&c, f); while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { switch (chunk) { case LIB3DS_N_TRI_OBJECT: { Lib3dsMesh *mesh; mesh=lib3ds_mesh_new(name); if (!mesh) { return(LIB3DS_FALSE); } lib3ds_chunk_read_reset(&c, f); if (!lib3ds_mesh_read(mesh, f)) { return(LIB3DS_FALSE); } lib3ds_file_insert_mesh(file, mesh); } break; case LIB3DS_N_CAMERA: { Lib3dsCamera *camera; camera=lib3ds_camera_new(name); if (!camera) { return(LIB3DS_FALSE); } lib3ds_chunk_read_reset(&c, f); if (!lib3ds_camera_read(camera, f)) { return(LIB3DS_FALSE); } lib3ds_file_insert_camera(file, camera); } break; case LIB3DS_N_DIRECT_LIGHT: { Lib3dsLight *light; light=lib3ds_light_new(name); if (!light) { return(LIB3DS_FALSE); } lib3ds_chunk_read_reset(&c, f); if (!lib3ds_light_read(light, f)) { return(LIB3DS_FALSE); } lib3ds_file_insert_light(file, light); } break; default: lib3ds_chunk_unknown(chunk); } } lib3ds_chunk_read_end(&c, f); return(LIB3DS_TRUE); }
static void named_object_read(Lib3dsFile *file, Lib3dsIo *io) { Lib3dsChunk c; char name[64]; uint16_t chunk; Lib3dsMesh *mesh = NULL; Lib3dsCamera *camera = NULL; Lib3dsLight *light = NULL; uint32_t object_flags; lib3ds_chunk_read_start(&c, CHK_NAMED_OBJECT, io); lib3ds_io_read_string(io, name, 64); lib3ds_io_log(io, LIB3DS_LOG_INFO, " NAME=%s", name); lib3ds_chunk_read_tell(&c, io); object_flags = 0; while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) { switch (chunk) { case CHK_N_TRI_OBJECT: { mesh = lib3ds_mesh_new(name); lib3ds_file_insert_mesh(file, mesh, -1); lib3ds_chunk_read_reset(&c, io); lib3ds_mesh_read(file, mesh, io); break; } case CHK_N_CAMERA: { camera = lib3ds_camera_new(name); lib3ds_file_insert_camera(file, camera, -1); lib3ds_chunk_read_reset(&c, io); lib3ds_camera_read(camera, io); break; } case CHK_N_DIRECT_LIGHT: { light = lib3ds_light_new(name); lib3ds_file_insert_light(file, light, -1); lib3ds_chunk_read_reset(&c, io); lib3ds_light_read(light, io); break; } case CHK_OBJ_HIDDEN: object_flags |= LIB3DS_OBJECT_HIDDEN; break; case CHK_OBJ_DOESNT_CAST: object_flags |= LIB3DS_OBJECT_DOESNT_CAST; break; case CHK_OBJ_VIS_LOFTER: object_flags |= LIB3DS_OBJECT_VIS_LOFTER; break; case CHK_OBJ_MATTE: object_flags |= LIB3DS_OBJECT_MATTE; break; case CHK_OBJ_DONT_RCVSHADOW: object_flags |= LIB3DS_OBJECT_DONT_RCVSHADOW; break; case CHK_OBJ_FAST: object_flags |= LIB3DS_OBJECT_FAST; break; case CHK_OBJ_FROZEN: object_flags |= LIB3DS_OBJECT_FROZEN; break; default: lib3ds_chunk_unknown(chunk, io); } } if (mesh) mesh->object_flags = object_flags; if (camera) camera->object_flags = object_flags; if (light) light->object_flags = object_flags; lib3ds_chunk_read_end(&c, io); }
/*! * \ingroup light */ Lib3dsBool lib3ds_light_read(Lib3dsLight *light, Lib3dsIo *io) { Lib3dsChunk c; Lib3dsWord chunk; if (!lib3ds_chunk_read_start(&c, LIB3DS_N_DIRECT_LIGHT, io)) { return(LIB3DS_FALSE); } { int i; for (i=0; i<3; ++i) { light->position[i]=lib3ds_io_read_float(io); } } lib3ds_chunk_read_tell(&c, io); while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) { switch (chunk) { case LIB3DS_COLOR_F: { int i; for (i=0; i<3; ++i) { light->color[i]=lib3ds_io_read_float(io); } } break; case LIB3DS_DL_OFF: { light->off=LIB3DS_TRUE; } break; case LIB3DS_DL_OUTER_RANGE: { light->outer_range=lib3ds_io_read_float(io); } break; case LIB3DS_DL_INNER_RANGE: { light->inner_range=lib3ds_io_read_float(io); } break; case LIB3DS_DL_MULTIPLIER: { light->multiplier=lib3ds_io_read_float(io); } break; case LIB3DS_DL_EXCLUDE: { /* FIXME: */ lib3ds_chunk_unknown(chunk); } case LIB3DS_DL_ATTENUATE: { light->attenuation=lib3ds_io_read_float(io); } break; case LIB3DS_DL_SPOTLIGHT: { lib3ds_chunk_read_reset(&c, io); if (!spotlight_read(light, io)) { return(LIB3DS_FALSE); } } break; default: lib3ds_chunk_unknown(chunk); } } lib3ds_chunk_read_end(&c, io); return(LIB3DS_TRUE); }
/*! * \ingroup light */ static Lib3dsBool spotlight_read(Lib3dsLight *light, Lib3dsIo *io) { Lib3dsChunk c; Lib3dsWord chunk; int i; if (!lib3ds_chunk_read_start(&c, LIB3DS_DL_SPOTLIGHT, io)) { return(LIB3DS_FALSE); } light->spot_light=LIB3DS_TRUE; for (i=0; i<3; ++i) { light->spot[i]=lib3ds_io_read_float(io); } light->hot_spot = lib3ds_io_read_float(io); light->fall_off = lib3ds_io_read_float(io); lib3ds_chunk_read_tell(&c, io); while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) { switch (chunk) { case LIB3DS_DL_SPOT_ROLL: { light->roll=lib3ds_io_read_float(io); } break; case LIB3DS_DL_SHADOWED: { light->shadowed=LIB3DS_TRUE; } break; case LIB3DS_DL_LOCAL_SHADOW2: { light->shadow_bias=lib3ds_io_read_float(io); light->shadow_filter=lib3ds_io_read_float(io); light->shadow_size=lib3ds_io_read_intw(io); } break; case LIB3DS_DL_SEE_CONE: { light->see_cone=LIB3DS_TRUE; } break; case LIB3DS_DL_SPOT_RECTANGULAR: { light->rectangular_spot=LIB3DS_TRUE; } break; case LIB3DS_DL_SPOT_ASPECT: { light->spot_aspect=lib3ds_io_read_float(io); } break; case LIB3DS_DL_SPOT_PROJECTOR: { light->use_projector=LIB3DS_TRUE; if (!lib3ds_io_read_string(io, light->projector, 64)) { return(LIB3DS_FALSE); } } case LIB3DS_DL_SPOT_OVERSHOOT: { light->spot_overshoot=LIB3DS_TRUE; } break; case LIB3DS_DL_RAY_BIAS: { light->ray_bias=lib3ds_io_read_float(io); } break; case LIB3DS_DL_RAYSHAD: { light->ray_shadows=LIB3DS_TRUE; } break; default: lib3ds_chunk_unknown(chunk); } } lib3ds_chunk_read_end(&c, io); return(LIB3DS_TRUE); }
/*! * \ingroup material */ Lib3dsBool lib3ds_material_read(Lib3dsMaterial *material, Lib3dsIo *io) { Lib3dsChunk c; Lib3dsWord chunk; ASSERT(material); if (!lib3ds_chunk_read_start(&c, LIB3DS_MAT_ENTRY, io)) { return(LIB3DS_FALSE); } while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) { switch (chunk) { case LIB3DS_MAT_NAME: { if (!lib3ds_io_read_string(io, material->name, 64)) { return(LIB3DS_FALSE); } lib3ds_chunk_dump_info(" NAME=%s", material->name); } break; case LIB3DS_MAT_AMBIENT: { lib3ds_chunk_read_reset(&c, io); if (!color_read(material->ambient, io)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_DIFFUSE: { lib3ds_chunk_read_reset(&c, io); if (!color_read(material->diffuse, io)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_SPECULAR: { lib3ds_chunk_read_reset(&c, io); if (!color_read(material->specular, io)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_SHININESS: { lib3ds_chunk_read_reset(&c, io); if (!int_percentage_read(&material->shininess, io)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_SHIN2PCT: { lib3ds_chunk_read_reset(&c, io); if (!int_percentage_read(&material->shin_strength, io)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_TRANSPARENCY: { lib3ds_chunk_read_reset(&c, io); if (!int_percentage_read(&material->transparency, io)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_XPFALL: { lib3ds_chunk_read_reset(&c, io); if (!int_percentage_read(&material->falloff, io)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_SELF_ILPCT: { lib3ds_chunk_read_reset(&c, io); if (!int_percentage_read(&material->self_ilpct, io)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_USE_XPFALL: { material->use_falloff=LIB3DS_TRUE; } break; case LIB3DS_MAT_REFBLUR: { lib3ds_chunk_read_reset(&c, io); if (!int_percentage_read(&material->blur, io)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_USE_REFBLUR: { material->use_blur=LIB3DS_TRUE; } break; case LIB3DS_MAT_SHADING: { material->shading=lib3ds_io_read_intw(io); } break; case LIB3DS_MAT_SELF_ILLUM: { material->self_illum=LIB3DS_TRUE; } break; case LIB3DS_MAT_TWO_SIDE: { material->two_sided=LIB3DS_TRUE; } break; case LIB3DS_MAT_DECAL: { material->map_decal=LIB3DS_TRUE; } break; case LIB3DS_MAT_ADDITIVE: { material->additive=LIB3DS_TRUE; } break; case LIB3DS_MAT_FACEMAP: { material->face_map=LIB3DS_TRUE; } break; case LIB3DS_MAT_PHONGSOFT: { material->soften=LIB3DS_TRUE; } break; case LIB3DS_MAT_WIRE: { material->use_wire=LIB3DS_TRUE; } break; case LIB3DS_MAT_WIREABS: { material->use_wire_abs=LIB3DS_TRUE; } break; case LIB3DS_MAT_WIRE_SIZE: { material->wire_size=lib3ds_io_read_float(io); } break; case LIB3DS_MAT_TEXMAP: { lib3ds_chunk_read_reset(&c, io); if (!texture_map_read(&material->texture1_map, io)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_TEXMASK: { lib3ds_chunk_read_reset(&c, io); if (!texture_map_read(&material->texture1_mask, io)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_TEX2MAP: { lib3ds_chunk_read_reset(&c, io); if (!texture_map_read(&material->texture2_map, io)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_TEX2MASK: { lib3ds_chunk_read_reset(&c, io); if (!texture_map_read(&material->texture2_mask, io)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_OPACMAP: { lib3ds_chunk_read_reset(&c, io); if (!texture_map_read(&material->opacity_map, io)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_OPACMASK: { lib3ds_chunk_read_reset(&c, io); if (!texture_map_read(&material->opacity_mask, io)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_BUMPMAP: { lib3ds_chunk_read_reset(&c, io); if (!texture_map_read(&material->bump_map, io)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_BUMPMASK: { lib3ds_chunk_read_reset(&c, io); if (!texture_map_read(&material->bump_mask, io)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_SPECMAP: { lib3ds_chunk_read_reset(&c, io); if (!texture_map_read(&material->specular_map, io)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_SPECMASK: { lib3ds_chunk_read_reset(&c, io); if (!texture_map_read(&material->specular_mask, io)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_SHINMAP: { lib3ds_chunk_read_reset(&c, io); if (!texture_map_read(&material->shininess_map, io)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_SHINMASK: { lib3ds_chunk_read_reset(&c, io); if (!texture_map_read(&material->shininess_mask, io)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_SELFIMAP: { lib3ds_chunk_read_reset(&c, io); if (!texture_map_read(&material->self_illum_map, io)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_SELFIMASK: { lib3ds_chunk_read_reset(&c, io); if (!texture_map_read(&material->self_illum_mask, io)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_REFLMAP: { lib3ds_chunk_read_reset(&c, io); if (!texture_map_read(&material->reflection_map, io)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_REFLMASK: { lib3ds_chunk_read_reset(&c, io); if (!texture_map_read(&material->reflection_mask, io)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_ACUBIC: { lib3ds_io_read_intb(io); material->autorefl_map.level=lib3ds_io_read_intb(io); material->autorefl_map.flags=lib3ds_io_read_intw(io); material->autorefl_map.size=lib3ds_io_read_intd(io); material->autorefl_map.frame_step=lib3ds_io_read_intd(io); } break; default: lib3ds_chunk_unknown(chunk); } } lib3ds_chunk_read_end(&c, io); return(LIB3DS_TRUE); }
static Lib3dsBool texture_map_read(Lib3dsTextureMap *map, Lib3dsIo *io) { Lib3dsChunk c; Lib3dsWord chunk; if (!lib3ds_chunk_read_start(&c, 0, io)) { return(LIB3DS_FALSE); } while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) { switch (chunk) { case LIB3DS_INT_PERCENTAGE: { map->percent=1.0f*lib3ds_io_read_intw(io)/100.0f; } break; case LIB3DS_MAT_MAPNAME: { if (!lib3ds_io_read_string(io, map->name, 64)) { return(LIB3DS_FALSE); } lib3ds_chunk_dump_info(" NAME=%s", map->name); } break; case LIB3DS_MAT_MAP_TILING: { map->flags=lib3ds_io_read_word(io); } break; case LIB3DS_MAT_MAP_TEXBLUR: { map->blur=lib3ds_io_read_float(io); } break; case LIB3DS_MAT_MAP_USCALE: { map->scale[0]=lib3ds_io_read_float(io); } break; case LIB3DS_MAT_MAP_VSCALE: { map->scale[1]=lib3ds_io_read_float(io); } break; case LIB3DS_MAT_MAP_UOFFSET: { map->offset[0]=lib3ds_io_read_float(io); } break; case LIB3DS_MAT_MAP_VOFFSET: { map->offset[1]=lib3ds_io_read_float(io); } break; case LIB3DS_MAT_MAP_ANG: { map->rotation=lib3ds_io_read_float(io); } break; case LIB3DS_MAT_MAP_COL1: { map->tint_1[0]=1.0f*lib3ds_io_read_byte(io)/255.0f; map->tint_1[1]=1.0f*lib3ds_io_read_byte(io)/255.0f; map->tint_1[2]=1.0f*lib3ds_io_read_byte(io)/255.0f; } break; case LIB3DS_MAT_MAP_COL2: { map->tint_2[0]=1.0f*lib3ds_io_read_byte(io)/255.0f; map->tint_2[1]=1.0f*lib3ds_io_read_byte(io)/255.0f; map->tint_2[2]=1.0f*lib3ds_io_read_byte(io)/255.0f; } break; case LIB3DS_MAT_MAP_RCOL: { map->tint_r[0]=1.0f*lib3ds_io_read_byte(io)/255.0f; map->tint_r[1]=1.0f*lib3ds_io_read_byte(io)/255.0f; map->tint_r[2]=1.0f*lib3ds_io_read_byte(io)/255.0f; } break; case LIB3DS_MAT_MAP_GCOL: { map->tint_g[0]=1.0f*lib3ds_io_read_byte(io)/255.0f; map->tint_g[1]=1.0f*lib3ds_io_read_byte(io)/255.0f; map->tint_g[2]=1.0f*lib3ds_io_read_byte(io)/255.0f; } break; case LIB3DS_MAT_MAP_BCOL: { map->tint_b[0]=1.0f*lib3ds_io_read_byte(io)/255.0f; map->tint_b[1]=1.0f*lib3ds_io_read_byte(io)/255.0f; map->tint_b[2]=1.0f*lib3ds_io_read_byte(io)/255.0f; } break; default: lib3ds_chunk_unknown(chunk); } } lib3ds_chunk_read_end(&c, io); return(LIB3DS_TRUE); }
/*! * \ingroup node */ Lib3dsBool lib3ds_node_read(Lib3dsNode *node, Lib3dsFile *file, Lib3dsIo *io) { Lib3dsChunk c; Lib3dsWord chunk; ASSERT(node); if (!lib3ds_chunk_read_start(&c, 0, io)) { return(LIB3DS_FALSE); } switch (c.chunk) { case LIB3DS_AMBIENT_NODE_TAG: case LIB3DS_OBJECT_NODE_TAG: case LIB3DS_CAMERA_NODE_TAG: case LIB3DS_TARGET_NODE_TAG: case LIB3DS_LIGHT_NODE_TAG: case LIB3DS_SPOTLIGHT_NODE_TAG: case LIB3DS_L_TARGET_NODE_TAG: break; default: return(LIB3DS_FALSE); } while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) { switch (chunk) { case LIB3DS_NODE_ID: { node->node_id=lib3ds_io_read_word(io); lib3ds_chunk_dump_info(" ID = %d", (short)node->node_id); } break; case LIB3DS_NODE_HDR: { if (!lib3ds_io_read_string(io, node->name, 64)) { return(LIB3DS_FALSE); } node->flags1=lib3ds_io_read_word(io); node->flags2=lib3ds_io_read_word(io); node->parent_id=lib3ds_io_read_word(io); lib3ds_chunk_dump_info(" NAME =%s", node->name); lib3ds_chunk_dump_info(" PARENT=%d", (short)node->parent_id); } break; case LIB3DS_PIVOT: { if (node->type==LIB3DS_OBJECT_NODE) { int i; for (i=0; i<3; ++i) { node->data.object.pivot[i]=lib3ds_io_read_float(io); } } else { lib3ds_chunk_unknown(chunk); } } break; case LIB3DS_INSTANCE_NAME: { if (node->type==LIB3DS_OBJECT_NODE) { if (!lib3ds_io_read_string(io, node->data.object.instance, 64)) { return(LIB3DS_FALSE); } } else { lib3ds_chunk_unknown(chunk); } } break; case LIB3DS_BOUNDBOX: { if (node->type==LIB3DS_OBJECT_NODE) { int i; for (i=0; i<3; ++i) { node->data.object.bbox_min[i]=lib3ds_io_read_float(io); } for (i=0; i<3; ++i) { node->data.object.bbox_max[i]=lib3ds_io_read_float(io); } } else { lib3ds_chunk_unknown(chunk); } } break; case LIB3DS_COL_TRACK_TAG: { Lib3dsBool result=LIB3DS_TRUE; switch (node->type) { case LIB3DS_AMBIENT_NODE: result=lib3ds_lin3_track_read(&node->data.ambient.col_track, io); break; case LIB3DS_LIGHT_NODE: result=lib3ds_lin3_track_read(&node->data.light.col_track, io); break; default: lib3ds_chunk_unknown(chunk); } if (!result) { return(LIB3DS_FALSE); } } break; case LIB3DS_POS_TRACK_TAG: { Lib3dsBool result=LIB3DS_TRUE; switch (node->type) { case LIB3DS_OBJECT_NODE: result=lib3ds_lin3_track_read(&node->data.object.pos_track, io); break; case LIB3DS_CAMERA_NODE: result=lib3ds_lin3_track_read(&node->data.camera.pos_track, io); break; case LIB3DS_TARGET_NODE: result=lib3ds_lin3_track_read(&node->data.target.pos_track, io); break; case LIB3DS_LIGHT_NODE: result=lib3ds_lin3_track_read(&node->data.light.pos_track, io); break; case LIB3DS_SPOT_NODE: result=lib3ds_lin3_track_read(&node->data.spot.pos_track, io); break; default: lib3ds_chunk_unknown(chunk); } if (!result) { return(LIB3DS_FALSE); } } break; case LIB3DS_ROT_TRACK_TAG: { if (node->type==LIB3DS_OBJECT_NODE) { if (!lib3ds_quat_track_read(&node->data.object.rot_track, io)) { return(LIB3DS_FALSE); } } else { lib3ds_chunk_unknown(chunk); } } break; case LIB3DS_SCL_TRACK_TAG: { if (node->type==LIB3DS_OBJECT_NODE) { if (!lib3ds_lin3_track_read(&node->data.object.scl_track, io)) { return(LIB3DS_FALSE); } } else { lib3ds_chunk_unknown(chunk); } } break; case LIB3DS_FOV_TRACK_TAG: { if (node->type==LIB3DS_CAMERA_NODE) { if (!lib3ds_lin1_track_read(&node->data.camera.fov_track, io)) { return(LIB3DS_FALSE); } } else { lib3ds_chunk_unknown(chunk); } } break; case LIB3DS_HOT_TRACK_TAG: { if (node->type==LIB3DS_LIGHT_NODE) { if (!lib3ds_lin1_track_read(&node->data.light.hotspot_track, io)) { return(LIB3DS_FALSE); } } else { lib3ds_chunk_unknown(chunk); } } break; case LIB3DS_FALL_TRACK_TAG: { if (node->type==LIB3DS_LIGHT_NODE) { if (!lib3ds_lin1_track_read(&node->data.light.falloff_track, io)) { return(LIB3DS_FALSE); } } else { lib3ds_chunk_unknown(chunk); } } break; case LIB3DS_ROLL_TRACK_TAG: { Lib3dsBool result=LIB3DS_TRUE; switch (node->type) { case LIB3DS_CAMERA_NODE: result=lib3ds_lin1_track_read(&node->data.camera.roll_track, io); break; case LIB3DS_LIGHT_NODE: result=lib3ds_lin1_track_read(&node->data.light.roll_track, io); break; default: lib3ds_chunk_unknown(chunk); } if (!result) { return(LIB3DS_FALSE); } } break; case LIB3DS_HIDE_TRACK_TAG: { if (node->type==LIB3DS_OBJECT_NODE) { if (!lib3ds_bool_track_read(&node->data.object.hide_track, io)) { return(LIB3DS_FALSE); } } else { lib3ds_chunk_unknown(chunk); } } break; case LIB3DS_MORPH_SMOOTH: { if (node->type==LIB3DS_OBJECT_NODE) { node->data.object.morph_smooth=lib3ds_io_read_float(io); } else { lib3ds_chunk_unknown(chunk); } } break; case LIB3DS_MORPH_TRACK_TAG: { if (node->type==LIB3DS_OBJECT_NODE) { if (!lib3ds_morph_track_read(&node->data.object.morph_track, io)) { return(LIB3DS_FALSE); } } else { lib3ds_chunk_unknown(chunk); } } break; default: lib3ds_chunk_unknown(chunk); } } lib3ds_chunk_read_end(&c, io); return(LIB3DS_TRUE); }
static void mdata_read(Lib3dsFile *file, Lib3dsIo *io) { Lib3dsChunk c; uint16_t chunk; lib3ds_chunk_read_start(&c, CHK_MDATA, io); while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) { switch (chunk) { case CHK_MESH_VERSION: { file->mesh_version = lib3ds_io_read_intd(io); break; } case CHK_MASTER_SCALE: { file->master_scale = lib3ds_io_read_float(io); break; } case CHK_SHADOW_MAP_SIZE: case CHK_LO_SHADOW_BIAS: case CHK_HI_SHADOW_BIAS: case CHK_SHADOW_SAMPLES: case CHK_SHADOW_RANGE: case CHK_SHADOW_FILTER: case CHK_RAY_BIAS: { lib3ds_chunk_read_reset(&c, io); lib3ds_shadow_read(&file->shadow, io); break; } case CHK_VIEWPORT_LAYOUT: case CHK_DEFAULT_VIEW: { lib3ds_chunk_read_reset(&c, io); lib3ds_viewport_read(&file->viewport, io); break; } case CHK_O_CONSTS: { int i; for (i = 0; i < 3; ++i) { file->construction_plane[i] = lib3ds_io_read_float(io); } break; } case CHK_AMBIENT_LIGHT: { lib3ds_chunk_read_reset(&c, io); ambient_read(file, io); break; } case CHK_BIT_MAP: case CHK_SOLID_BGND: case CHK_V_GRADIENT: case CHK_USE_BIT_MAP: case CHK_USE_SOLID_BGND: case CHK_USE_V_GRADIENT: { lib3ds_chunk_read_reset(&c, io); lib3ds_background_read(&file->background, io); break; } case CHK_FOG: case CHK_LAYER_FOG: case CHK_DISTANCE_CUE: case CHK_USE_FOG: case CHK_USE_LAYER_FOG: case CHK_USE_DISTANCE_CUE: { lib3ds_chunk_read_reset(&c, io); lib3ds_atmosphere_read(&file->atmosphere, io); break; } case CHK_MAT_ENTRY: { Lib3dsMaterial *material = lib3ds_material_new(NULL); lib3ds_file_insert_material(file, material, -1); lib3ds_chunk_read_reset(&c, io); lib3ds_material_read(material, io); break; } case CHK_NAMED_OBJECT: { lib3ds_chunk_read_reset(&c, io); named_object_read(file, io); break; } default: lib3ds_chunk_unknown(chunk, io); } } lib3ds_chunk_read_end(&c, io); }
static Lib3dsBool mdata_read(Lib3dsFile *file, FILE *f) { Lib3dsChunk c; Lib3dsWord chunk; if (!lib3ds_chunk_read_start(&c, LIB3DS_MDATA, f)) { return(LIB3DS_FALSE); } while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { switch (chunk) { case LIB3DS_MESH_VERSION: { file->mesh_version=lib3ds_intd_read(f); } break; case LIB3DS_MASTER_SCALE: { file->master_scale=lib3ds_float_read(f); } break; case LIB3DS_SHADOW_MAP_SIZE: case LIB3DS_LO_SHADOW_BIAS: case LIB3DS_HI_SHADOW_BIAS: case LIB3DS_SHADOW_SAMPLES: case LIB3DS_SHADOW_RANGE: case LIB3DS_SHADOW_FILTER: case LIB3DS_RAY_BIAS: { lib3ds_chunk_read_reset(&c, f); if (!lib3ds_shadow_read(&file->shadow, f)) { return(LIB3DS_FALSE); } } break; case LIB3DS_VIEWPORT_LAYOUT: case LIB3DS_DEFAULT_VIEW: { lib3ds_chunk_read_reset(&c, f); if (!lib3ds_viewport_read(&file->viewport, f)) { return(LIB3DS_FALSE); } } break; case LIB3DS_O_CONSTS: { int i; for (i=0; i<3; ++i) { file->construction_plane[i]=lib3ds_float_read(f); } } break; case LIB3DS_AMBIENT_LIGHT: { lib3ds_chunk_read_reset(&c, f); if (!ambient_read(file, f)) { return(LIB3DS_FALSE); } } break; case LIB3DS_BIT_MAP: case LIB3DS_SOLID_BGND: case LIB3DS_V_GRADIENT: case LIB3DS_USE_BIT_MAP: case LIB3DS_USE_SOLID_BGND: case LIB3DS_USE_V_GRADIENT: { lib3ds_chunk_read_reset(&c, f); if (!lib3ds_background_read(&file->background, f)) { return(LIB3DS_FALSE); } } break; case LIB3DS_FOG: case LIB3DS_LAYER_FOG: case LIB3DS_DISTANCE_CUE: case LIB3DS_USE_FOG: case LIB3DS_USE_LAYER_FOG: case LIB3DS_USE_DISTANCE_CUE: { lib3ds_chunk_read_reset(&c, f); if (!lib3ds_atmosphere_read(&file->atmosphere, f)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_ENTRY: { Lib3dsMaterial *material; material=lib3ds_material_new(); if (!material) { return(LIB3DS_FALSE); } lib3ds_chunk_read_reset(&c, f); if (!lib3ds_material_read(material, f)) { return(LIB3DS_FALSE); } lib3ds_file_insert_material(file, material); } break; case LIB3DS_NAMED_OBJECT: { lib3ds_chunk_read_reset(&c, f); if (!named_object_read(file, f)) { return(LIB3DS_FALSE); } } break; default: lib3ds_chunk_unknown(chunk); } } lib3ds_chunk_read_end(&c, f); return(LIB3DS_TRUE); }
static void kfdata_read(Lib3dsFile *file, Lib3dsIo *io) { Lib3dsChunk c; uint16_t chunk; unsigned num_nodes = 0; Lib3dsNode *last = NULL; lib3ds_chunk_read_start(&c, CHK_KFDATA, io); while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) { switch (chunk) { case CHK_KFHDR: { file->keyf_revision = lib3ds_io_read_word(io); lib3ds_io_read_string(io, file->name, 12 + 1); file->frames = lib3ds_io_read_intd(io); break; } case CHK_KFSEG: { file->segment_from = lib3ds_io_read_intd(io); file->segment_to = lib3ds_io_read_intd(io); break; } case CHK_KFCURTIME: { file->current_frame = lib3ds_io_read_intd(io); break; } case CHK_VIEWPORT_LAYOUT: case CHK_DEFAULT_VIEW: { lib3ds_chunk_read_reset(&c, io); lib3ds_viewport_read(&file->viewport_keyf, io); break; } case CHK_AMBIENT_NODE_TAG: case CHK_OBJECT_NODE_TAG: case CHK_CAMERA_NODE_TAG: case CHK_TARGET_NODE_TAG: case CHK_LIGHT_NODE_TAG: case CHK_SPOTLIGHT_NODE_TAG: case CHK_L_TARGET_NODE_TAG: { Lib3dsNodeType type = (Lib3dsNodeType)0; Lib3dsNode *node; switch (chunk) { case CHK_AMBIENT_NODE_TAG: type = LIB3DS_NODE_AMBIENT_COLOR; break; case CHK_OBJECT_NODE_TAG: type = LIB3DS_NODE_MESH_INSTANCE; break; case CHK_CAMERA_NODE_TAG: type = LIB3DS_NODE_CAMERA; break; case CHK_TARGET_NODE_TAG: type = LIB3DS_NODE_CAMERA_TARGET; break; case CHK_LIGHT_NODE_TAG: type = LIB3DS_NODE_OMNILIGHT; break; case CHK_SPOTLIGHT_NODE_TAG: type = LIB3DS_NODE_SPOTLIGHT; break; case CHK_L_TARGET_NODE_TAG: type = LIB3DS_NODE_SPOTLIGHT_TARGET; break; } node = lib3ds_node_new(type); node->node_id = (unsigned short)(num_nodes++); if (last) { last->next = node; } else { file->nodes = node; } node->user_ptr = last; last = node; lib3ds_chunk_read_reset(&c, io); lib3ds_node_read(node, io); break; } default: lib3ds_chunk_unknown(chunk, io); } } { Lib3dsNode **nodes = (Lib3dsNode**)malloc(num_nodes * sizeof(Lib3dsNode*)); unsigned i; Lib3dsNode *p, *q, *parent; p = file->nodes; for (i = 0; i < num_nodes; ++i) { nodes[i] = p; p = p->next; } qsort(nodes, num_nodes, sizeof(Lib3dsNode*), compare_node_id); p = last; while (p) { q = (Lib3dsNode*)p->user_ptr; if (p->user_id != 65535) { parent = *(Lib3dsNode**)bsearch(&p->user_id, nodes, num_nodes, sizeof(Lib3dsNode*), compare_node_id2); if (parent) { q->next = p->next; p->next = parent->childs; p->parent = parent; parent->childs = p; } else { /* TODO: warning */ } } p->user_id = 0; p->user_ptr = NULL; p = q; } free(nodes); } lib3ds_chunk_read_end(&c, io); }
/*! * Read 3ds file data into a Lib3dsFile object. * * \param file The Lib3dsFile object to be filled. * \param io A Lib3dsIo object previously set up by the caller. * * \return LIB3DS_TRUE on success, LIB3DS_FALSE on failure. */ int lib3ds_file_read(Lib3dsFile *file, Lib3dsIo *io) { Lib3dsChunk c; uint16_t chunk; Lib3dsIoImpl *impl; lib3ds_io_setup(io); impl = (Lib3dsIoImpl*)io->impl; if (setjmp(impl->jmpbuf) != 0) { lib3ds_io_cleanup(io); return FALSE; } lib3ds_chunk_read_start(&c, 0, io); switch (c.chunk) { case CHK_MDATA: { lib3ds_chunk_read_reset(&c, io); mdata_read(file, io); break; } case CHK_M3DMAGIC: case CHK_MLIBMAGIC: case CHK_CMAGIC: { while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) { switch (chunk) { case CHK_M3D_VERSION: { file->mesh_version = lib3ds_io_read_dword(io); break; } case CHK_MDATA: { lib3ds_chunk_read_reset(&c, io); mdata_read(file, io); break; } case CHK_KFDATA: { lib3ds_chunk_read_reset(&c, io); kfdata_read(file, io); break; } default: lib3ds_chunk_unknown(chunk, io); } } break; } default: lib3ds_chunk_unknown(c.chunk, io); return FALSE; } lib3ds_chunk_read_end(&c, io); memset(impl->jmpbuf, 0, sizeof(impl->jmpbuf)); lib3ds_io_cleanup(io); return TRUE; }
static void face_array_read(Lib3dsFile *file, Lib3dsMesh *mesh, Lib3dsIo *io) { Lib3dsChunk c; uint16_t chunk; int i; uint16_t nfaces; lib3ds_chunk_read_start(&c, CHK_FACE_ARRAY, io); lib3ds_mesh_resize_faces(mesh, 0); nfaces = lib3ds_io_read_word(io); if (nfaces) { lib3ds_mesh_resize_faces(mesh, nfaces); for (i = 0; i < nfaces; ++i) { mesh->faces[i].index[0] = lib3ds_io_read_word(io); mesh->faces[i].index[1] = lib3ds_io_read_word(io); mesh->faces[i].index[2] = lib3ds_io_read_word(io); mesh->faces[i].flags = lib3ds_io_read_word(io); } lib3ds_chunk_read_tell(&c, io); while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) { switch (chunk) { case CHK_MSH_MAT_GROUP: { char name[64]; unsigned n; unsigned i; int index; int material; lib3ds_io_read_string(io, name, 64); material = lib3ds_file_material_by_name(file, name); n = lib3ds_io_read_word(io); for (i = 0; i < n; ++i) { index = lib3ds_io_read_word(io); if (index < mesh->nfaces) { mesh->faces[index].material = material; } else { // TODO warning } } break; } case CHK_SMOOTH_GROUP: { int i; for (i = 0; i < mesh->nfaces; ++i) { mesh->faces[i].smoothing_group = lib3ds_io_read_dword(io); } break; } case CHK_MSH_BOXMAP: { lib3ds_io_read_string(io, mesh->box_front, 64); lib3ds_io_read_string(io, mesh->box_back, 64); lib3ds_io_read_string(io, mesh->box_left, 64); lib3ds_io_read_string(io, mesh->box_right, 64); lib3ds_io_read_string(io, mesh->box_top, 64); lib3ds_io_read_string(io, mesh->box_bottom, 64); break; } default: lib3ds_chunk_unknown(chunk,io); } } } lib3ds_chunk_read_end(&c, io); }
void lib3ds_material_read(Lib3dsMaterial *material, Lib3dsIo *io) { Lib3dsChunk c; uint16_t chunk; assert(material); lib3ds_chunk_read_start(&c, CHK_MAT_ENTRY, io); while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) { switch (chunk) { case CHK_MAT_NAME: { lib3ds_io_read_string(io, material->name, 64); lib3ds_io_log(io, LIB3DS_LOG_INFO, " NAME=%s", material->name); break; } case CHK_MAT_AMBIENT: { lib3ds_chunk_read_reset(&c, io); color_read(material->ambient, io); break; } case CHK_MAT_DIFFUSE: { lib3ds_chunk_read_reset(&c, io); color_read(material->diffuse, io); break; } case CHK_MAT_SPECULAR: { lib3ds_chunk_read_reset(&c, io); color_read(material->specular, io); break; } case CHK_MAT_SHININESS: { lib3ds_chunk_read_reset(&c, io); int_percentage_read(&material->shininess, io); break; } case CHK_MAT_SHIN2PCT: { lib3ds_chunk_read_reset(&c, io); int_percentage_read(&material->shin_strength, io); break; } case CHK_MAT_TRANSPARENCY: { lib3ds_chunk_read_reset(&c, io); int_percentage_read(&material->transparency, io); break; } case CHK_MAT_XPFALL: { lib3ds_chunk_read_reset(&c, io); int_percentage_read(&material->falloff, io); break; } case CHK_MAT_SELF_ILPCT: { lib3ds_chunk_read_reset(&c, io); int_percentage_read(&material->self_illum, io); break; } case CHK_MAT_USE_XPFALL: { material->use_falloff = TRUE; break; } case CHK_MAT_REFBLUR: { lib3ds_chunk_read_reset(&c, io); int_percentage_read(&material->blur, io); break; } case CHK_MAT_USE_REFBLUR: { material->use_blur = TRUE; break; } case CHK_MAT_SHADING: { material->shading = lib3ds_io_read_intw(io); break; } case CHK_MAT_SELF_ILLUM: { material->self_illum_flag = TRUE; break; } case CHK_MAT_TWO_SIDE: { material->two_sided = TRUE; break; } case CHK_MAT_DECAL: { material->map_decal = TRUE; break; } case CHK_MAT_ADDITIVE: { material->is_additive = TRUE; break; } case CHK_MAT_FACEMAP: { material->face_map = TRUE; break; } case CHK_MAT_PHONGSOFT: { material->soften = TRUE; break; } case CHK_MAT_WIRE: { material->use_wire = TRUE; break; } case CHK_MAT_WIREABS: { material->use_wire_abs = TRUE; break; } case CHK_MAT_WIRE_SIZE: { material->wire_size = lib3ds_io_read_float(io); break; } case CHK_MAT_TEXMAP: { lib3ds_chunk_read_reset(&c, io); texture_map_read(&material->texture1_map, io); break; } case CHK_MAT_TEXMASK: { lib3ds_chunk_read_reset(&c, io); texture_map_read(&material->texture1_mask, io); break; } case CHK_MAT_TEX2MAP: { lib3ds_chunk_read_reset(&c, io); texture_map_read(&material->texture2_map, io); break; } case CHK_MAT_TEX2MASK: { lib3ds_chunk_read_reset(&c, io); texture_map_read(&material->texture2_mask, io); break; } case CHK_MAT_OPACMAP: { lib3ds_chunk_read_reset(&c, io); texture_map_read(&material->opacity_map, io); break; } case CHK_MAT_OPACMASK: { lib3ds_chunk_read_reset(&c, io); texture_map_read(&material->opacity_mask, io); break; } case CHK_MAT_BUMPMAP: { lib3ds_chunk_read_reset(&c, io); texture_map_read(&material->bump_map, io); break; } case CHK_MAT_BUMPMASK: { lib3ds_chunk_read_reset(&c, io); texture_map_read(&material->bump_mask, io); break; } case CHK_MAT_SPECMAP: { lib3ds_chunk_read_reset(&c, io); texture_map_read(&material->specular_map, io); break; } case CHK_MAT_SPECMASK: { lib3ds_chunk_read_reset(&c, io); texture_map_read(&material->specular_mask, io); break; } case CHK_MAT_SHINMAP: { lib3ds_chunk_read_reset(&c, io); texture_map_read(&material->shininess_map, io); break; } case CHK_MAT_SHINMASK: { lib3ds_chunk_read_reset(&c, io); texture_map_read(&material->shininess_mask, io); break; } case CHK_MAT_SELFIMAP: { lib3ds_chunk_read_reset(&c, io); texture_map_read(&material->self_illum_map, io); break; } case CHK_MAT_SELFIMASK: { lib3ds_chunk_read_reset(&c, io); texture_map_read(&material->self_illum_mask, io); break; } case CHK_MAT_REFLMAP: { lib3ds_chunk_read_reset(&c, io); texture_map_read(&material->reflection_map, io); break; } case CHK_MAT_REFLMASK: { lib3ds_chunk_read_reset(&c, io); texture_map_read(&material->reflection_mask, io); break; } case CHK_MAT_ACUBIC: { lib3ds_io_read_intb(io); material->autorefl_map_anti_alias = lib3ds_io_read_intb(io); material->autorefl_map_flags = lib3ds_io_read_intw(io); material->autorefl_map_size = lib3ds_io_read_intd(io); material->autorefl_map_frame_step = lib3ds_io_read_intd(io); break; } default: lib3ds_chunk_unknown(chunk, io); } } lib3ds_chunk_read_end(&c, io); }
void lib3ds_light_read(Lib3dsLight *light, Lib3dsIo *io) { Lib3dsChunk c; uint16_t chunk; lib3ds_chunk_read_start(&c, CHK_N_DIRECT_LIGHT, io); { int i; for (i = 0; i < 3; ++i) { light->position[i] = lib3ds_io_read_float(io); } } lib3ds_chunk_read_tell(&c, io); while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) { switch (chunk) { case CHK_COLOR_F: { int i; for (i = 0; i < 3; ++i) { light->color[i] = lib3ds_io_read_float(io); } break; } case CHK_DL_OFF: light->off = TRUE; break; case CHK_DL_OUTER_RANGE: light->outer_range = lib3ds_io_read_float(io); break; case CHK_DL_INNER_RANGE: light->inner_range = lib3ds_io_read_float(io); break; case CHK_DL_MULTIPLIER: light->multiplier = lib3ds_io_read_float(io); break; case CHK_DL_EXCLUDE: { /* FIXME: */ lib3ds_chunk_unknown(chunk, io); break; } case CHK_DL_ATTENUATE: light->attenuation = lib3ds_io_read_float(io); break; case CHK_DL_SPOTLIGHT: { lib3ds_chunk_read_reset(&c, io); spotlight_read(light, io); break; } default: lib3ds_chunk_unknown(chunk, io); } } lib3ds_chunk_read_end(&c, io); }
/*! * \ingroup mesh */ Lib3dsBool lib3ds_mesh_read(Lib3dsMesh *mesh, Lib3dsIo *io) { Lib3dsChunk c; Lib3dsWord chunk; if (!lib3ds_chunk_read_start(&c, LIB3DS_N_TRI_OBJECT, io)) { return(LIB3DS_FALSE); } while ((chunk=lib3ds_chunk_read_next(&c, io))!=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_io_read_float(io); } } } break; case LIB3DS_MESH_COLOR: { mesh->color=lib3ds_io_read_byte(io); } break; case LIB3DS_POINT_ARRAY: { unsigned i,j; unsigned points; lib3ds_mesh_free_point_list(mesh); points=lib3ds_io_read_word(io); 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_io_read_float(io); } } 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_io_read_word(io); 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_io_read_word(io); } ASSERT((!mesh->points) || (mesh->flags==mesh->points)); ASSERT((!mesh->texels) || (mesh->flags==mesh->texels)); } } break; case LIB3DS_FACE_ARRAY: { lib3ds_chunk_read_reset(&c, io); if (!face_array_read(mesh, io)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MESH_TEXTURE_INFO: { int i,j; for (i=0; i<2; ++i) { mesh->map_data.tile[i]=lib3ds_io_read_float(io); } for (i=0; i<3; ++i) { mesh->map_data.pos[i]=lib3ds_io_read_float(io); } mesh->map_data.scale=lib3ds_io_read_float(io); 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_io_read_float(io); } } for (i=0; i<2; ++i) { mesh->map_data.planar_size[i]=lib3ds_io_read_float(io); } mesh->map_data.cylinder_height=lib3ds_io_read_float(io); } break; case LIB3DS_TEX_VERTS: { unsigned i; unsigned texels; lib3ds_mesh_free_texel_list(mesh); texels=lib3ds_io_read_word(io); 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_io_read_float(io); mesh->texelL[i][1]=lib3ds_io_read_float(io); } 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 ); } } if (lib3ds_matrix_det(mesh->matrix) < 0.0) { /* Flip X coordinate of vertices if mesh matrix has negative determinant */ Lib3dsMatrix inv_matrix, M; Lib3dsVector tmp; unsigned i; lib3ds_matrix_copy(inv_matrix, mesh->matrix); lib3ds_matrix_inv(inv_matrix); lib3ds_matrix_copy(M, mesh->matrix); lib3ds_matrix_scale_xyz(M, -1.0f, 1.0f, 1.0f); lib3ds_matrix_mult(M, inv_matrix); for (i=0; i<mesh->points; ++i) { lib3ds_vector_transform(tmp, M, mesh->pointL[i].pos); lib3ds_vector_copy(mesh->pointL[i].pos, tmp); } } lib3ds_chunk_read_end(&c, io); return(LIB3DS_TRUE); }
static void spotlight_read(Lib3dsLight *light, Lib3dsIo *io) { Lib3dsChunk c; uint16_t chunk; int i; lib3ds_chunk_read_start(&c, CHK_DL_SPOTLIGHT, io); light->spot_light = TRUE; for (i = 0; i < 3; ++i) { light->target[i] = lib3ds_io_read_float(io); } light->hotspot = lib3ds_io_read_float(io); light->falloff = lib3ds_io_read_float(io); lib3ds_chunk_read_tell(&c, io); while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) { switch (chunk) { case CHK_DL_SPOT_ROLL: light->roll = lib3ds_io_read_float(io); break; case CHK_DL_SHADOWED: { light->shadowed = TRUE; break; } case CHK_DL_LOCAL_SHADOW2: { light->shadow_bias = lib3ds_io_read_float(io); light->shadow_filter = lib3ds_io_read_float(io); light->shadow_size = lib3ds_io_read_intw(io); break; } case CHK_DL_SEE_CONE: { light->see_cone = TRUE; break; } case CHK_DL_SPOT_RECTANGULAR: { light->rectangular_spot = TRUE; break; } case CHK_DL_SPOT_ASPECT: { light->spot_aspect = lib3ds_io_read_float(io); break; } case CHK_DL_SPOT_PROJECTOR: { light->use_projector = TRUE; lib3ds_io_read_string(io, light->projector, 64); break; } case CHK_DL_SPOT_OVERSHOOT: { light->spot_overshoot = TRUE; break; } case CHK_DL_RAY_BIAS: { light->ray_bias = lib3ds_io_read_float(io); break; } case CHK_DL_RAYSHAD: { light->ray_shadows = TRUE; break; } default: lib3ds_chunk_unknown(chunk, io); } } lib3ds_chunk_read_end(&c, io); }
void lib3ds_mesh_read(Lib3dsFile *file, Lib3dsMesh *mesh, Lib3dsIo *io) { Lib3dsChunk c; uint16_t chunk; lib3ds_chunk_read_start(&c, CHK_N_TRI_OBJECT, io); while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) { switch (chunk) { case CHK_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_io_read_float(io); } } break; } case CHK_MESH_COLOR: { mesh->color = lib3ds_io_read_byte(io); break; } case CHK_POINT_ARRAY: { int i; uint16_t nvertices = lib3ds_io_read_word(io); lib3ds_mesh_resize_vertices(mesh, nvertices, mesh->texcos != NULL, mesh->vflags != NULL); for (i = 0; i < mesh->nvertices; ++i) { lib3ds_io_read_vector(io, mesh->vertices[i]); } break; } case CHK_POINT_FLAG_ARRAY: { int i; uint16_t nflags = lib3ds_io_read_word(io); uint16_t nvertices = (mesh->nvertices >= nflags)? mesh->nvertices : nflags; lib3ds_mesh_resize_vertices(mesh, nvertices, mesh->texcos != NULL, 1); for (i = 0; i < nflags; ++i) { mesh->vflags[i] = lib3ds_io_read_word(io); } break; } case CHK_FACE_ARRAY: { lib3ds_chunk_read_reset(&c, io); face_array_read(file, mesh, io); break; } case CHK_MESH_TEXTURE_INFO: { int i, j; //FIXME: mesh->map_type = lib3ds_io_read_word(io); for (i = 0; i < 2; ++i) { mesh->map_tile[i] = lib3ds_io_read_float(io); } for (i = 0; i < 3; ++i) { mesh->map_pos[i] = lib3ds_io_read_float(io); } mesh->map_scale = lib3ds_io_read_float(io); lib3ds_matrix_identity(mesh->map_matrix); for (i = 0; i < 4; i++) { for (j = 0; j < 3; j++) { mesh->map_matrix[i][j] = lib3ds_io_read_float(io); } } for (i = 0; i < 2; ++i) { mesh->map_planar_size[i] = lib3ds_io_read_float(io); } mesh->map_cylinder_height = lib3ds_io_read_float(io); break; } case CHK_TEX_VERTS: { int i; uint16_t ntexcos = lib3ds_io_read_word(io); uint16_t nvertices = (mesh->nvertices >= ntexcos)? mesh->nvertices : ntexcos;; if (!mesh->texcos) { lib3ds_mesh_resize_vertices(mesh, nvertices, 1, mesh->vflags != NULL); } for (i = 0; i < ntexcos; ++i) { mesh->texcos[i][0] = lib3ds_io_read_float(io); mesh->texcos[i][1] = lib3ds_io_read_float(io); } break; } default: lib3ds_chunk_unknown(chunk, io); } } if (lib3ds_matrix_det(mesh->matrix) < 0.0) { /* Flip X coordinate of vertices if mesh matrix has negative determinant */ float inv_matrix[4][4], M[4][4]; float tmp[3]; int i; lib3ds_matrix_copy(inv_matrix, mesh->matrix); lib3ds_matrix_inv(inv_matrix); lib3ds_matrix_copy(M, mesh->matrix); lib3ds_matrix_scale(M, -1.0f, 1.0f, 1.0f); lib3ds_matrix_mult(M, M, inv_matrix); for (i = 0; i < mesh->nvertices; ++i) { lib3ds_vector_transform(tmp, M, mesh->vertices[i]); lib3ds_vector_copy(mesh->vertices[i], tmp); } } lib3ds_chunk_read_end(&c, io); }
/*! * \ingroup viewport */ Lib3dsBool lib3ds_viewport_read(Lib3dsViewport *viewport, Lib3dsIo *io) { Lib3dsChunk c; Lib3dsWord chunk; if (!lib3ds_chunk_read_start(&c, 0, io)) { return(LIB3DS_FALSE); } switch (c.chunk) { case LIB3DS_VIEWPORT_LAYOUT: { int cur=0; viewport->layout.style=lib3ds_io_read_word(io); viewport->layout.active=lib3ds_io_read_intw(io); lib3ds_io_read_intw(io); viewport->layout.swap=lib3ds_io_read_intw(io); lib3ds_io_read_intw(io); viewport->layout.swap_prior=lib3ds_io_read_intw(io); viewport->layout.swap_view=lib3ds_io_read_intw(io); lib3ds_chunk_read_tell(&c, io); while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) { switch (chunk) { case LIB3DS_VIEWPORT_SIZE: { viewport->layout.position[0]=lib3ds_io_read_word(io); viewport->layout.position[1]=lib3ds_io_read_word(io); viewport->layout.size[0]=lib3ds_io_read_word(io); viewport->layout.size[1]=lib3ds_io_read_word(io); } break; case LIB3DS_VIEWPORT_DATA_3: { lib3ds_viewport_set_views(viewport,cur+1); lib3ds_io_read_intw(io); viewport->layout.viewL[cur].axis_lock=lib3ds_io_read_word(io); viewport->layout.viewL[cur].position[0]=lib3ds_io_read_intw(io); viewport->layout.viewL[cur].position[1]=lib3ds_io_read_intw(io); viewport->layout.viewL[cur].size[0]=lib3ds_io_read_intw(io); viewport->layout.viewL[cur].size[1]=lib3ds_io_read_intw(io); viewport->layout.viewL[cur].type=lib3ds_io_read_word(io); viewport->layout.viewL[cur].zoom=lib3ds_io_read_float(io); lib3ds_io_read_vector(io, viewport->layout.viewL[cur].center); viewport->layout.viewL[cur].horiz_angle=lib3ds_io_read_float(io); viewport->layout.viewL[cur].vert_angle=lib3ds_io_read_float(io); lib3ds_io_read(io, viewport->layout.viewL[cur].camera, 11); ++cur; } break; case LIB3DS_VIEWPORT_DATA: /* 3DS R2 & R3 chunk unsupported */ break; default: lib3ds_chunk_unknown(chunk); } } } break; case LIB3DS_DEFAULT_VIEW: { memset(&viewport->default_view,0,sizeof(Lib3dsDefaultView)); while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) { switch (chunk) { case LIB3DS_VIEW_TOP: { viewport->default_view.type=LIB3DS_VIEW_TYPE_TOP; lib3ds_io_read_vector(io, viewport->default_view.position); viewport->default_view.width=lib3ds_io_read_float(io); } break; case LIB3DS_VIEW_BOTTOM: { viewport->default_view.type=LIB3DS_VIEW_TYPE_BOTTOM; lib3ds_io_read_vector(io, viewport->default_view.position); viewport->default_view.width=lib3ds_io_read_float(io); } break; case LIB3DS_VIEW_LEFT: { viewport->default_view.type=LIB3DS_VIEW_TYPE_LEFT; lib3ds_io_read_vector(io, viewport->default_view.position); viewport->default_view.width=lib3ds_io_read_float(io); } break; case LIB3DS_VIEW_RIGHT: { viewport->default_view.type=LIB3DS_VIEW_TYPE_RIGHT; lib3ds_io_read_vector(io, viewport->default_view.position); viewport->default_view.width=lib3ds_io_read_float(io); } break; case LIB3DS_VIEW_FRONT: { viewport->default_view.type=LIB3DS_VIEW_TYPE_FRONT; lib3ds_io_read_vector(io, viewport->default_view.position); viewport->default_view.width=lib3ds_io_read_float(io); } break; case LIB3DS_VIEW_BACK: { viewport->default_view.type=LIB3DS_VIEW_TYPE_BACK; lib3ds_io_read_vector(io, viewport->default_view.position); viewport->default_view.width=lib3ds_io_read_float(io); } break; case LIB3DS_VIEW_USER: { viewport->default_view.type=LIB3DS_VIEW_TYPE_USER; lib3ds_io_read_vector(io, viewport->default_view.position); viewport->default_view.width=lib3ds_io_read_float(io); viewport->default_view.horiz_angle=lib3ds_io_read_float(io); viewport->default_view.vert_angle=lib3ds_io_read_float(io); viewport->default_view.roll_angle=lib3ds_io_read_float(io); } break; case LIB3DS_VIEW_CAMERA: { viewport->default_view.type=LIB3DS_VIEW_TYPE_CAMERA; lib3ds_io_read(io, viewport->default_view.camera, 11); } break; default: lib3ds_chunk_unknown(chunk); } } } break; } lib3ds_chunk_read_end(&c, io); return(LIB3DS_TRUE); }