/* * obj2model.c * * Turn Wavefront OBJ files into Thrust3D models * * Copyright (c) 2008 Thomas White * * This file is part of Thrust3D - a silly game * * Thrust3D is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Thrust3D is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Thrust3D. If not, see . * */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include "utils.c" #define MAX_VERTICES 65536 static char *texname(const char *filename) { int i; char *rval; rval = strdup(filename); for ( i=0; i 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], "Ks") == 0 ) { fprintf(out, "colspec %s %s %s\n", bits[1], bits[2], bits[3]); } if ( strcmp(bits[0], "map_Kd") == 0 ) { char *tex; tex = texname(bits[1]); fprintf(out, "texture %s\n", tex); free(tex); } if ( strcmp(bits[0], "Ns") == 0 ) { fprintf(out, "shiny %s\n", bits[1]); } } for ( i=0; i= 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 %8.3f %8.3f %+8.3f %+8.3f %+8.3f\n", vtmp[3*vnum+0], vtmp[3*vnum+1], vtmp[3*vnum+2], textmp[2*tnum+0], textmp[2*tnum+1], vntmp[3*nnum+0], vntmp[3*nnum+1], vntmp[3*nnum+2]); v_used[vnum] = 1; vn_used[nnum] = 1; tex_used[tnum] = 1; free(sp[0]); free(sp[1]); free(sp[2]); free(sp); } else if ( nslash == 0 ) { int vnum; vnum = atoi(bits[i])-1; fprintf(out, "%+8.3f %+8.3f %+8.3f %8.3f %8.3f\n", vtmp[3*vnum+0], vtmp[3*vnum+1], vtmp[3*vnum+2], 0.0, 0.0); v_used[vnum] = 1; } } } 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