aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortaw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5>2008-08-10 19:14:39 +0000
committertaw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5>2008-08-10 19:14:39 +0000
commit98315c0a86e2d8157e3aec73d532bb4c87860ed2 (patch)
tree6e8ce575413bd54a4e0b804b55edac6c702b5672
parent61879e0d54fc9410a484eb02cb66835712dc3a0b (diff)
obj2model: parse .mtl files
git-svn-id: svn://cook.msm.cam.ac.uk:745/thrust3d/thrust3d@245 84d2e878-0bd5-11dd-ad15-13eda11d74c5
-rw-r--r--src/obj2model.c109
1 files changed, 96 insertions, 13 deletions
diff --git a/src/obj2model.c b/src/obj2model.c
index d58786e..a720273 100644
--- a/src/obj2model.c
+++ b/src/obj2model.c
@@ -16,11 +16,78 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <libgen.h>
#include "utils.c"
#define MAX_VERTICES 65536
+static void do_material(const char *mtllib, const char *mtlname, FILE *out, char *infilename) {
+
+ FILE *fh;
+ char tmp[1024];
+ char *infilename_dup;
+
+ infilename_dup = strdup(infilename);
+ snprintf(tmp, 1023, "%s/%s", dirname(infilename_dup), mtllib);
+ free(infilename_dup);
+ fh = fopen(tmp, "r");
+ if ( fh == NULL ) {
+ fprintf(stderr, "Couldn't open material library '%s'\n", tmp);
+ return;
+ }
+
+ while ( !feof(fh) ) {
+
+ char line[1024];
+ char **bits;
+ int n, i;
+
+ fgets(line, 1023, fh);
+ n = assplode(line, " \t\r\n", &bits, ASSPLODE_NONE);
+
+ if ( n > 1 ) {
+ if ( (strcmp(bits[0], "newmtl") == 0) && (strcmp(bits[1], mtlname) == 0) ) {
+ /* Found the right material */
+ while ( !feof(fh) ) {
+
+ char line[1024];
+ char **bits;
+ int n, i;
+
+ fgets(line, 1023, fh);
+ n = assplode(line, " \t\r\n", &bits, ASSPLODE_NONE);
+
+ if ( n > 1 ) {
+
+ if ( strcmp(bits[0], "newmtl") == 0 ) {
+ return;
+ }
+ if ( strcmp(bits[0], "Kd") == 0 ) {
+ fprintf(out, "colour %s %s %s\n", bits[1], bits[2], bits[3]);
+ }
+ if ( strcmp(bits[0], "Ns") == 0 ) {
+ fprintf(out, "shiny %s\n", bits[1]);
+ }
+ }
+
+ for ( i=0; i<n; i++ ) free(bits[i]);
+ free(bits);
+
+ }
+
+ }
+ }
+
+ for ( i=0; i<n; i++ ) free(bits[i]);
+ free(bits);
+
+ }
+
+ fclose(fh);
+
+}
+
int main(int argc, char *argv[]) {
FILE *fh;
@@ -32,8 +99,10 @@ int main(int argc, char *argv[]) {
int vn_used[MAX_VERTICES];
int tex_used[MAX_VERTICES];
int n_vtmp, n_vntmp, n_textmp;
- int nprev;
+ int new_prim, n_prev;
int i, v_unused, vn_unused, tex_unused;
+ char mtllib[64];
+ char *cur_material = NULL;
fh = fopen(argv[1], "r");
if ( fh == NULL ) {
@@ -92,6 +161,11 @@ int main(int argc, char *argv[]) {
n_textmp++;
continue;
}
+
+ if ( sscanf(line+s, "mtllib %63s\n", mtllib) == 1 ) {
+ printf("Material library '%s'\n", mtllib);
+ continue;
+ }
}
@@ -100,7 +174,8 @@ int main(int argc, char *argv[]) {
out = fopen(argv[2], "w");
fprintf(out, "# %s\n", argv[1]);
fprintf(out, "\n");
- nprev = 0;
+ new_prim = 1;
+ n_prev = 0;
while ( !feof(fh) ) {
char line[1024];
@@ -112,23 +187,24 @@ int main(int argc, char *argv[]) {
/* Read in a face */
if ( strcmp(bits[0], "f") == 0 ) {
-
- int nthis;
- /* Change primitive type if this face has a different number of vertices */
- nthis = n-1;
- if ( nthis != nprev ) {
- if ( nprev != 0 ) fprintf(out, "\n");
- if ( nthis == 4 ) {
+ int n_this = n-1;
+ if ( new_prim || (n_this != n_prev) ) {
+ if ( n_this != n_prev ) {
+ fprintf(out, "\n");
+ }
+ if ( n_this == 4 ) {
fprintf(out, "QUADS\n");
- } else if ( nthis == 3 ) {
+ } else if ( n_this == 3 ) {
fprintf(out, "TRIANGLES\n");
} else {
fprintf(out, "POLYGONS\n");
}
- nprev = nthis;
- } else {
- //fprintf(out, "#\n");
+ new_prim = 0;
+ n_prev = n_this;
+ if ( cur_material != NULL ) {
+ do_material(mtllib, cur_material, out, argv[1]);
+ }
}
/* For each vertex... */
@@ -186,6 +262,13 @@ int main(int argc, char *argv[]) {
}
+ } else if ( strcmp(bits[0], "usemtl") == 0 ) {
+ cur_material = strdup(bits[1]);
+ } else if ( strcmp(bits[0], "v") == 0 ) {
+ if ( !new_prim ) {
+ fprintf(out, "\n");
+ }
+ new_prim = 1;
}
for ( i=0; i<n; i++ ) free(bits[i]);