#include #include typedef struct rwheader { unsigned int type; unsigned int size; unsigned int version; } rwheader; typedef struct split { unsigned int indexcount; unsigned int mat; float **vertices; float **uv; unsigned char **vc; unsigned char **normals; } split; typedef struct geometry { split *splt; int nextgeo; int facecnt, vertexcnt, splitcount, indexcount; short flags; } geometry; int main(int argc, char *argv[]) { int i, j, k, faceinc; rwheader rwh; int subsecthdr; geometry *geo; int geocnt; FILE *obj, *dff; int blocksize, nextblock, unk; short comp_vert[4]; float vertex[3]; if ((dff = fopen(argv[1], "rb")) == NULL) { printf("error\n"); exit(1); } if ((obj = fopen(argv[2], "wb")) == NULL) { printf("error\n"); exit(1); } k = 0; i = 0; fread(&rwh, 4, 3, dff); /* clump */ fread(&rwh, 4, 3, dff); /* struct */ fseek(dff, rwh.size, SEEK_CUR); fread(&rwh, 4, 3, dff); /* Frame list */ fseek(dff, rwh.size, SEEK_CUR); fread(&rwh, 4, 3, dff); /* geometry list */ fread(&rwh, 4, 3, dff); /* Struct */ fread(&geocnt, 4, 1, dff); geo = (geometry *) malloc(sizeof(geometry)*geocnt); fread(&rwh, 4, 3, dff); /* geometry */ geo[k].nextgeo = rwh.size + ftell(dff); fread(&rwh, 4, 3, dff); /* struct */ fread(&geo[k].flags, 2, 1, dff); fseek(dff, 2, SEEK_CUR); fread(&geo[k].facecnt, 4, 1, dff); fread(&geo[k].vertexcnt, 4, 1, dff); fseek(dff, rwh.size-12, SEEK_CUR); fread(&rwh, 4, 3, dff); /* Material list */ fseek(dff, rwh.size, SEEK_CUR); fread(&rwh, 4, 3, dff); /* Extension */ fread(&rwh, 4, 3, dff); /* Bin Mesh PLG */ fseek(dff, 4, SEEK_CUR); fread(&geo[k].splitcount, 4, 1, dff); fread(&geo[k].indexcount, 4, 1, dff); geo[k].splt = (split *) malloc(sizeof(split)*geo[k].splitcount); for (i = 0; i < geo[k].splitcount; i++) { fread(&geo[k].splt[i].indexcount, 4, 1, dff); fread(&geo[k].splt[i].mat, 4, 1, dff); } fread(&rwh, 4, 3, dff); /* Native Data PLG */ fread(&rwh, 4, 3, dff); /* Struct - broken header */ /* Native Data PLG Struct */ fseek(dff, 4, SEEK_CUR); /* Skip 40 00 00 00 */ fread(&blocksize, 4, 1, dff); /* splitsize */ nextblock = blocksize + ftell(dff) + 4; fseek(dff, 20, SEEK_CUR); /* skip unknowns, not necessarily 32 bytes, to be researched */ /* */ /* Vertices */ /* */ /* get vertexcount in format: 00 80 vertexcount 68 */ fseek(dff, 12, SEEK_CUR); fread(&subsecthdr, 4, 1, dff); if ((subsecthdr & 0xff00ffff) != 0x68008000) { printf("Vertexsection expected, exiting\n"); goto bla; //exit(1); } /* Note: indexcount is a confusing name: vertexcount is better */ geo[0].splt[0].indexcount = (subsecthdr & 0x00ff0000) >> 16; /* read Vertices */ geo[0].splt[0].vertices = (float **) malloc(geo[0].splt[0].indexcount*4); for (j = 0; j < geo[0].splt[0].indexcount; j++) { geo[0].splt[0].vertices[j] = (float *) malloc(12); fread(&geo[0].splt[0].vertices[j] [0], 4, 1, dff); fread(&geo[0].splt[0].vertices[j] [1], 4, 1, dff); fread(&geo[0].splt[0].vertices[j] [2], 4, 1, dff); } if ((geo[0].indexcount % 2) != 0) /* skip padding */ fseek(dff, 12, SEEK_CUR); /* */ /* UV Coordinates */ /* */ /* get vertexcount in format: 01 80 vertexcount 64 */ fseek(dff, 12, SEEK_CUR); fread(&subsecthdr, 4, 1, dff); if ((subsecthdr & 0xff00ffff) != 0x64008001) { printf("UVsection expected, exiting\n"); goto bla; //exit(1); } geo[0].splt[0].indexcount = (subsecthdr & 0x00ff0000) >> 16; /* read UV Coordinates - possibly incorrect*/ geo[0].splt[0].uv = (float **) malloc(geo[0].splt[0].indexcount*4); for (j = 0; j < geo[0].splt[0].indexcount; j++) { geo[0].splt[0].uv[j] = (float *) malloc(8); fread(&geo[0].splt[0].uv[j] [0], 4, 1, dff); fread(&geo[0].splt[0].uv[j] [1], 4, 1, dff); } if ((geo[0].indexcount % 2) != 0) /* skip padding */ fseek(dff, 8, SEEK_CUR); /* */ /* Vertexcolors */ /* */ /* get vertexcount in format: 02 C0 vertexcount 6E */ fseek(dff, 12, SEEK_CUR); fread(&subsecthdr, 4, 1, dff); if ((subsecthdr & 0xff00ffff) != 0x6E00C002) { printf("Vertexcolorsection expected, exiting\n"); goto bla; //exit(1); } geo[0].splt[0].indexcount = (subsecthdr & 0x00ff0000) >> 16; /* read Vertexcolors */ geo[0].splt[0].vc = (unsigned char **) malloc(geo[0].splt[0].indexcount*4); for (j = 0; j < geo[0].splt[0].indexcount; j++) { geo[0].splt[0].vc[j] = (unsigned char *) malloc(4); fread(&geo[0].splt[0].uv[j] [0], 1, 4, dff); } if ((geo[0].indexcount % 2) != 0) /* skip padding */ fseek(dff, 4, SEEK_CUR); /* */ /* Normals */ /* */ /* get vertexcount in format: 03 80 vertexcount 6A */ fseek(dff, 12, SEEK_CUR); fread(&subsecthdr, 4, 1, dff); if ((subsecthdr & 0xff00ffff) != 0x6A008003) { printf("Normalsection expected, exiting\n"); //exit(1); goto bla; } geo[0].splt[0].indexcount = (subsecthdr & 0x00ff0000) >> 16; /* read Normals */ geo[0].splt[0].normals = (unsigned char **) malloc(geo[0].splt[0].indexcount*4); for (j = 0; j < geo[0].splt[0].indexcount; j++) { geo[0].splt[0].normals[j] = (unsigned char *) malloc(3); fread(&geo[0].splt[0].normals[j] [0], 1, 3, dff); } if ((geo[0].indexcount % 2) != 0) /* skip padding */ fseek(dff, 3, SEEK_CUR); fseek(dff, nextblock, 0); printf("%X\n", ftell(dff)); //fseek(dff, geo[0].nextgeo, 0); bla: faceinc = 0; //for (k = 0; k < geocnt; k++) { //for (i = 0; i < geo[k].splitcount; i++) { for (j= 0; j < geo[0].splt[0].indexcount; j++) { fprintf(obj, "v %f %f %f\n", geo[0].splt[0].vertices[j] [0], geo[0].splt[0].vertices[j] [1], geo[0].splt[0].vertices[j] [2]); } /* for (j= 0; j < geo[0].splt[0].indexcount; j++) { fprintf(obj, "vt %f %f\n", geo[0].splt[0].uv[j] [0], geo[0].splt[0].uv[j] [1]); } */ for (j = 2; j < geo[0].splt[0].indexcount; j++) { //if (geo[0].splt[0].flags[j] == 0x8000) /* 0x8000 - interrupt vertex strip */ // continue; fprintf(obj, "f %d %d %d\n", j+1+faceinc, j+faceinc, j-1+faceinc); } printf("%X\n", ftell(dff)); //faceinc += geo[0].splt[0].indexcount; //} //} return 0; }