From 98315c0a86e2d8157e3aec73d532bb4c87860ed2 Mon Sep 17 00:00:00 2001 From: taw27 Date: Sun, 10 Aug 2008 19:14:39 +0000 Subject: obj2model: parse .mtl files git-svn-id: svn://cook.msm.cam.ac.uk:745/thrust3d/thrust3d@245 84d2e878-0bd5-11dd-ad15-13eda11d74c5 --- src/obj2model.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file 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 #include #include +#include #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