aboutsummaryrefslogtreecommitdiff
path: root/src/obj2model.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/obj2model.c')
-rw-r--r--src/obj2model.c145
1 files changed, 145 insertions, 0 deletions
diff --git a/src/obj2model.c b/src/obj2model.c
index a3ac831..1e160ff 100644
--- a/src/obj2model.c
+++ b/src/obj2model.c
@@ -14,9 +14,154 @@
#endif
#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "utils.c"
+
+#define MAX_VERTICES 65536
int main(int argc, char *argv[]) {
+ FILE *fh;
+ FILE *out;
+ float vtmp[3*MAX_VERTICES];
+ float vntmp[3*MAX_VERTICES];
+ int n_vtmp, n_vntmp;
+ int nprev;
+
+ fh = fopen(argv[1], "r");
+ if ( fh == NULL ) {
+ fprintf(stderr, "Couldn't open '%s'\n", argv[1]);
+ return 1;
+ }
+
+ /* Zip through and find all the vertices */
+ n_vtmp = 0;
+ n_vntmp = 0;
+ while ( !feof(fh) ) {
+
+ char line[1024];
+ float x, y, z;
+ size_t s;
+
+ fgets(line, 1023, fh);
+ s = 0;
+ for ( ; s<strlen(line); s++ ) {
+ if ( line[s] != ' ' ) break;
+ }
+
+ if ( line[s] == '#' ) {
+ continue;
+ }
+
+ if ( sscanf(line+s, "v %f %f %f\n", &x, &y, &z) == 3 ) {
+ vtmp[3*n_vtmp+0] = x;
+ vtmp[3*n_vtmp+1] = y;
+ vtmp[3*n_vtmp+2] = z;
+ n_vtmp++;
+ continue;
+ }
+
+ if ( sscanf(line+s, "vn %f %f %f\n", &x, &y, &z) == 3 ) {
+ vntmp[3*n_vntmp+0] = x;
+ vntmp[3*n_vntmp+1] = y;
+ vntmp[3*n_vntmp+2] = z;
+ n_vntmp++;
+ continue;
+ }
+
+ }
+
+ /* Go through again and look for faces */
+ rewind(fh);
+ out = fopen(argv[2], "w");
+ fprintf(out, "# %s\n", argv[1]);
+ fprintf(out, "\n");
+ nprev = 0;
+ while ( !feof(fh) ) {
+
+ char line[1024];
+ char **bits;
+ int n, i;
+
+ fgets(line, 1023, fh);
+ n = assplode(line, " \t\r\n", &bits, ASSPLODE_NONE);
+
+ /* Read in a face */
+ if ( strcmp(bits[0], "f") == 0 ) {
+
+ int nthis;
+
+ nthis = n-1;
+ if ( nthis != nprev ) {
+ if ( nprev != 0 ) fprintf(out, "\n");
+ if ( nthis == 4 ) {
+ fprintf(out, "QUADS\n");
+ } else {
+ fprintf(out, "POLYGONS\n");
+ }
+ nprev = nthis;
+ } else {
+ fprintf(out, "#\n");
+ }
+
+ for ( i=1; i<n; i++ ) {
+
+ char **sp;
+ int np, nnum, j, nslash;
+
+ nslash = 0;
+ for ( j=0; j<strlen(bits[i]); j++ ) {
+ if ( bits[i][j] == '/' ) nslash++;
+ }
+ if ( nslash == 2 ) {
+
+ int vnum;
+
+ np = assplode(bits[i], "/", &sp, ASSPLODE_DUPS);
+ if ( np != 3 ) {
+ continue;
+ }
+ vnum = atoi(sp[0])-1;
+ nnum = atoi(sp[2])-1;
+ if ( vnum >= n_vtmp ) {
+ fprintf(stderr, "Vertex index is too high (%i/%i)\n", vnum, n_vtmp);
+ continue;
+ }
+ if ( nnum >= n_vntmp ) {
+ fprintf(stderr, "Normal index is too high (%i/%i)\n", nnum, n_vntmp);
+ continue;
+ }
+ fprintf(out, "%+8.3f %+8.3f %+8.3f\n", vtmp[3*vnum+0], vtmp[3*vnum+1],
+ vtmp[3*vnum+2]);
+ free(sp[0]);
+ free(sp[1]);
+ free(sp[2]);
+ free(sp);
+
+ } else if ( nslash == 0 ) {
+
+ int vnum;
+ vnum = atoi(bits[i]);
+ fprintf(out, "%+8.3f %+8.3f %+8.3f\n", vtmp[3*vnum+0], vtmp[3*vnum+1],
+ vtmp[3*vnum+2]);
+
+ }
+
+ }
+
+ }
+
+ for ( i=0; i<n; i++ ) free(bits[i]);
+ free(bits);
+
+ }
+ fprintf(out, "\n");
+
+ fclose(fh);
+ fclose(out);
+
return 0;
}