/* * JoeyLib 3D * Copyright (C) 2019 Scott Duensing * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages * arising from the use of this software. * * Permission is granted to anyone to use this software for any purpose, * including commercial applications, and to alter it and redistribute it * freely, subject to the following restrictions: * * 1. The origin of this software must not be misrepresented; you must not * claim that you wrote the original software. If you use this software * in a product, an acknowledgment in the product documentation would be * appreciated but is not required. * 2. Altered source versions must be plainly marked as such, and must not be * misrepresented as being the original software. * 3. This notice may not be removed or altered from any source distribution. */ // OBJ format: http://paulbourke.net/dataformats/obj/ #include #include #include "stretchy_buffer.h" #include "joey.h" #include "j3d.h" int main(int argc, char *argv[]) { char *nameIn; char *nameOut; jint16 r; juint16 x; juint16 y; char token[1024]; char *c; FILE *f; j3CoordinatesT v; j3ObjectT o; j3TriangleT t; j3WorldT world; if (argc < 3) { printf("Usage: %s [infile] [outfile]\n", argv[0]); return 1; } nameIn = argv[1]; nameOut = argv[2]; f = fopen(nameIn, "rt"); // Did we find the file? if (f == NULL) { // Nope. printf("Unable to open %s\n", nameIn); return 1; } world.objectCount = 0; world.objects = NULL; o.vertexCount = 0; o.triangleCount = 0; o.verticies = NULL; o.triangles = NULL; while (true) { // Read next token r = (jint16)fscanf(f, "%s", token); // End of file? if (r == EOF) { break; } // Vertex? if (strcmp(token, "v" ) == 0) { r = (jint16)fscanf(f, "%f %f %f\n", &v.local.x, &v.local.y, &v.local.z); sb_push(o.verticies, v); o.vertexCount++; } // Face? if (strcmp(token, "f" ) == 0) { for (x=0; x<3; x++) { // Fetch 'x'th vertex index r = (jint16)fscanf(f, "%s", token); c = strstr(token, "/"); if (c) c[0] = 0; r = (jint16)atoi(token); t.index[x] = (juint16)r - 1; // obj indicies start at 1 } fscanf(f, "\n"); sb_push(o.triangles, t); o.triangleCount++; } } //***TODO*** support multiple objects - not sure how I want to do this yet sb_push(world.objects, o); world.objectCount++; // Finished reading fclose(f); printf("Objects: %d\n", world.objectCount); for (x=0; x