#include #include #include #pragma pack(1) struct chunk_hdr { unsigned short chunk_id; unsigned long chunk_offset; }; struct point { float p[3]; }; struct face { unsigned short v[3]; unsigned short flags; }; struct mesh_matrix { float matrix[4][3]; }; void print_zone(FILE *f, int length) { unsigned char *buf = (unsigned char *)malloc(length + 1); fread(buf, length, 1, f); // Print HEX version for (int i = 0; i < length; i++) { printf(" %02x", buf[i]); if ((i+1) % 25 == 0) printf("\n"); } printf("\n"); // Print ASCII version for (int i = 0; i < length; i++) { printf("%c", (buf[i] == '\n')?' ':buf[i]); if ((i+1) % 25 == 0) printf("\n"); } printf("\n"); free(buf); } short read_short(FILE *f) { unsigned short x; fread(&x, sizeof(unsigned short), 1, f); return x; } long read_long(FILE *f) { unsigned long x; fread(&x, sizeof(unsigned long), 1, f); return x; } float read_float(FILE *f) { float x; fread(&x, sizeof(float), 1, f); return x; } unsigned char *read_string(FILE *f) { unsigned char s[201], *r; for (int i = 0; i < 200; i++) { fread(&s[i], 1, 1, f); if (s[i] == '\0') break; } r = (unsigned char *)calloc(strlen((char *)s) + 1, 1); strcpy((char *)r, (char *)s); return r; } struct chunk_hdr read_chunk_hdr(FILE *f) { struct chunk_hdr hdr; fread(&hdr, 6, 1, f); printf("CHUNK: %04x - %ld\n", hdr.chunk_id, hdr.chunk_offset); return hdr; } void unread_chunk_hdr(FILE *f) { fseek(f, -6, SEEK_CUR); } void skip_chunk(FILE *f, struct chunk_hdr hdr) { fseek(f, hdr.chunk_offset, SEEK_CUR); } int main (int argc, char **argv) { struct chunk_hdr hdr; FILE *f = fopen("sphere04.3ds", "rb"); // Read MAGIC header (offset = file size) hdr = read_chunk_hdr(f); // Read VERSION hdr = read_chunk_hdr(f); printf("Version: %u\n", read_long(f)); // Read MESH DATA header hdr = read_chunk_hdr(f); // Read MESH VERSION header hdr = read_chunk_hdr(f); printf("Version: %u\n", read_long(f)); // Read Master Scale hdr = read_chunk_hdr(f); printf("Scale: %f\n", read_float(f)); // Read Name hdr = read_chunk_hdr(f); unsigned char *s = read_string(f); printf("Name: %s\n", s); free(s); // Read TRIANGLE OBJECT header hdr = read_chunk_hdr(f); // Read POINT ARRAY hdr = read_chunk_hdr(f); unsigned short num_pts = read_short(f); printf("Size: %u\n", num_pts); struct point *pts = (struct point *)calloc(num_pts, sizeof(struct point)); fread(pts, sizeof(struct point), num_pts, f); // Read MESH MATRIX hdr = read_chunk_hdr(f); struct mesh_matrix mm; fread(&mm, sizeof(struct mesh_matrix), 1, f); for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) printf("%.2f ", mm.matrix[j][i]); printf("\n"); } // Read FACE ARRAY hdr = read_chunk_hdr(f); unsigned short num_face = read_short(f); printf("Size: %u\n", num_face); struct face *face = (struct face *)calloc(num_face, sizeof(struct face)); fread(face, sizeof(struct face), num_face, f); // Read SMOOTH GROUP hdr = read_chunk_hdr(f); unread_chunk_hdr(f); skip_chunk(f, hdr); // Good Enough // Print next section print_zone(f, 500); fclose(f); char junk; scanf("%c", &junk); return 0; }