| 1 | #ifndef lint | 
| 2 | static const char RCSid[] = "$Id$"; | 
| 3 | #endif | 
| 4 | /* | 
| 5 | * Utility program for fixing up Wavefront .OBJ files. | 
| 6 | * | 
| 7 | */ | 
| 8 |  | 
| 9 | #include <stdio.h> | 
| 10 | #include <stdlib.h> | 
| 11 | #include <string.h> | 
| 12 | #include "rtio.h" | 
| 13 | #include "objutil.h" | 
| 14 |  | 
| 15 | const char      *progname; | 
| 16 | int             verbose = 0; | 
| 17 |  | 
| 18 | int | 
| 19 | main(int argc, char *argv[]) | 
| 20 | { | 
| 21 | Scene           *myScene; | 
| 22 | int             radout = 0; | 
| 23 | const char      *grp[64]; | 
| 24 | int             ngrps = 0; | 
| 25 | int             save_grps = 1; | 
| 26 | const char      *mat[64]; | 
| 27 | int             nmats = 0; | 
| 28 | int             save_mats = 1; | 
| 29 | int             do_tex = 0; | 
| 30 | int             do_norm = 0; | 
| 31 | char            cbuf[256]; | 
| 32 | double          verteps = -1.; | 
| 33 | int     i, n; | 
| 34 |  | 
| 35 | progname = argv[0]; | 
| 36 | /* process options */ | 
| 37 | for (i = 1; i < argc && (argv[i][0] == '-' || argv[i][0] == '+'); i++) | 
| 38 | switch (argv[i][1]) { | 
| 39 | case 'v':                       /* verbose mode */ | 
| 40 | verbose = (argv[i][0] == '+'); | 
| 41 | break; | 
| 42 | case 'g':                       /* save/delete group */ | 
| 43 | if (save_grps != (argv[i][0] == '+')) { | 
| 44 | if (ngrps) { | 
| 45 | fprintf(stderr, | 
| 46 | "%s: -g and +g are mutually exclusive\n", | 
| 47 | argv[0]); | 
| 48 | exit(1); | 
| 49 | } | 
| 50 | save_grps = !save_grps; | 
| 51 | } | 
| 52 | if (ngrps >= 64) { | 
| 53 | fprintf(stderr, "%s: too many groups\n", | 
| 54 | argv[0]); | 
| 55 | exit(1); | 
| 56 | } | 
| 57 | grp[ngrps++] = argv[++i]; | 
| 58 | break; | 
| 59 | case 'm':                       /* save/delete material */ | 
| 60 | if (save_mats != (argv[i][0] == '+')) { | 
| 61 | if (nmats) { | 
| 62 | fprintf(stderr, | 
| 63 | "%s: -m and +m are mutually exclusive\n", | 
| 64 | argv[0]); | 
| 65 | exit(1); | 
| 66 | } | 
| 67 | save_mats = !save_mats; | 
| 68 | } | 
| 69 | if (nmats >= 64) { | 
| 70 | fprintf(stderr, "%s: too many materials\n", | 
| 71 | argv[0]); | 
| 72 | exit(1); | 
| 73 | } | 
| 74 | mat[nmats++] = argv[++i]; | 
| 75 | break; | 
| 76 | case 't':                       /* do texture coord's */ | 
| 77 | do_tex = (argv[i][0] == '+') ? 1 : -1; | 
| 78 | break; | 
| 79 | case 'n':                       /* do normals */ | 
| 80 | do_norm = (argv[i][0] == '+') ? 1 : -1; | 
| 81 | break; | 
| 82 | case 'c':                       /* coalesce vertices */ | 
| 83 | verteps = atof(argv[++i]); | 
| 84 | break; | 
| 85 | case 'r':                       /* output to Radiance file? */ | 
| 86 | radout = (argv[i][0] == '+'); | 
| 87 | break; | 
| 88 | default: | 
| 89 | fprintf(stderr, "%s: unknown option: %s\n", | 
| 90 | argv[0], argv[i]); | 
| 91 | goto userr; | 
| 92 | } | 
| 93 | if (i == argc) { | 
| 94 | if (verbose) | 
| 95 | fputs("Loading scene from standard input...\n", | 
| 96 | stderr); | 
| 97 | myScene = loadOBJ(NULL, NULL); | 
| 98 | } else { | 
| 99 | myScene = newScene(); | 
| 100 | for ( ; i < argc; i++) { | 
| 101 | if (verbose) | 
| 102 | fprintf(stderr, | 
| 103 | "Loading scene from \"%s\"...\n", | 
| 104 | argv[i]); | 
| 105 | if (loadOBJ(myScene, argv[i]) == NULL) | 
| 106 | exit(1); | 
| 107 | } | 
| 108 | } | 
| 109 | if (ngrps) { | 
| 110 | if (verbose) | 
| 111 | fputs("Deleting unwanted groups...\n", stderr); | 
| 112 | clearSelection(myScene, 0); | 
| 113 | sprintf(cbuf, "%s the following groups:", | 
| 114 | save_grps ? "Extracting" : "Deleting"); | 
| 115 | addComment(myScene, cbuf); | 
| 116 | for (i = 0; i < ngrps; i++) { | 
| 117 | sprintf(cbuf, "\t%s", grp[i]); | 
| 118 | addComment(myScene, cbuf); | 
| 119 | selectGroup(myScene, grp[i], 0); | 
| 120 | } | 
| 121 | if (save_grps) | 
| 122 | n = deleteFaces(myScene, 0, FACE_SELECTED); | 
| 123 | else | 
| 124 | n = deleteFaces(myScene, FACE_SELECTED, 0); | 
| 125 | sprintf(cbuf, "\t(%d faces removed)", n); | 
| 126 | addComment(myScene, cbuf); | 
| 127 | } | 
| 128 | if (nmats) { | 
| 129 | if (verbose) | 
| 130 | fputs("Deleting unwanted materials...\n", stderr); | 
| 131 | clearSelection(myScene, 0); | 
| 132 | sprintf(cbuf, "%s the following materials:", | 
| 133 | save_mats ? "Extracting" : "Deleting"); | 
| 134 | addComment(myScene, cbuf); | 
| 135 | for (i = 0; i < nmats; i++) { | 
| 136 | sprintf(cbuf, "\t%s", mat[i]); | 
| 137 | addComment(myScene, cbuf); | 
| 138 | selectMaterial(myScene, mat[i], 0); | 
| 139 | } | 
| 140 | if (save_mats) | 
| 141 | n = deleteFaces(myScene, 0, FACE_SELECTED); | 
| 142 | else | 
| 143 | n = deleteFaces(myScene, FACE_SELECTED, 0); | 
| 144 | sprintf(cbuf, "\t(%d faces removed)", n); | 
| 145 | addComment(myScene, cbuf); | 
| 146 | } | 
| 147 | if (verbose && do_tex < 0) | 
| 148 | fputs("Removing texture coordinates...\n", stderr); | 
| 149 | if (do_tex < 0 && (n = removeTexture(myScene, 0, 0))) { | 
| 150 | sprintf(cbuf, "Removed texture coordinates from %d faces", n); | 
| 151 | addComment(myScene, cbuf); | 
| 152 | } | 
| 153 | if (verbose && do_norm < 0) | 
| 154 | fputs("Removing surface normals...\n", stderr); | 
| 155 | if (do_norm < 0 && (n = removeNormals(myScene, 0, 0))) { | 
| 156 | sprintf(cbuf, "Removed surface normals from %d faces", n); | 
| 157 | addComment(myScene, cbuf); | 
| 158 | } | 
| 159 | if (verbose && verteps >= 0) | 
| 160 | fputs("Coalescing vertices...\n", stderr); | 
| 161 | if (verteps >= 0 && (n = coalesceVertices(myScene, verteps))) { | 
| 162 | sprintf(cbuf, "Coalesced %d vertices with epsilon of %g", | 
| 163 | n, verteps); | 
| 164 | addComment(myScene, cbuf); | 
| 165 | } | 
| 166 | if (verbose) | 
| 167 | fputs("Deleting degenerate faces...\n", stderr); | 
| 168 | n = deleteFaces(myScene, FACE_DEGENERATE, 0); | 
| 169 | if (n) { | 
| 170 | sprintf(cbuf, "Removed %d degenerate faces", n); | 
| 171 | addComment(myScene, cbuf); | 
| 172 | } | 
| 173 | if (verbose) | 
| 174 | fputs("Checking for duplicate faces...\n", stderr); | 
| 175 | if (findDuplicateFaces(myScene)) | 
| 176 | n = deleteFaces(myScene, FACE_DUPLICATE, 0); | 
| 177 | if (n) { | 
| 178 | sprintf(cbuf, "Removed %d duplicate faces", n); | 
| 179 | addComment(myScene, cbuf); | 
| 180 | } | 
| 181 | if (verbose) | 
| 182 | fputs("Writing out scene...\n", stderr); | 
| 183 |  | 
| 184 | fputs("# File processed by: ", stdout); | 
| 185 | printargs(argc, argv, stdout); | 
| 186 | if (radout) | 
| 187 | toRadiance(myScene, stdout, 0, 0); | 
| 188 | else | 
| 189 | toOBJ(myScene, stdout); | 
| 190 | /* freeScene(myScene); */ | 
| 191 | if (verbose) | 
| 192 | fputs("Done.                                    \n", stderr); | 
| 193 | return(0); | 
| 194 | userr: | 
| 195 | fprintf(stderr, "Usage: %s [options] [input.obj ..]\n", argv[0]); | 
| 196 | fprintf(stderr, "Available options:\n"); | 
| 197 | fprintf(stderr, "\t+/-r\t\t\t# Radiance scene output on/off\n"); | 
| 198 | fprintf(stderr, "\t+/-v\t\t\t# on/off verbosity (progress reports)\n"); | 
| 199 | fprintf(stderr, "\t+/-g name\t\t# save/delete group\n"); | 
| 200 | fprintf(stderr, "\t+/-m name\t\t# save/delete faces with material\n"); | 
| 201 | fprintf(stderr, "\t+/-t\t\t\t# keep/remove texture coordinates\n"); | 
| 202 | fprintf(stderr, "\t+/-n\t\t\t# keep/remove vertex normals\n"); | 
| 203 | fprintf(stderr, "\t-c epsilon\t\t# coalesce vertices within epsilon\n"); | 
| 204 | return(1); | 
| 205 | } | 
| 206 |  | 
| 207 | void | 
| 208 | eputs(char *s)                          /* put string to stderr */ | 
| 209 | { | 
| 210 | static int  midline = 0; | 
| 211 |  | 
| 212 | if (!*s) | 
| 213 | return; | 
| 214 | if (!midline++) { | 
| 215 | fputs(progname, stderr); | 
| 216 | fputs(": ", stderr); | 
| 217 | } | 
| 218 | fputs(s, stderr); | 
| 219 | if (s[strlen(s)-1] == '\n') | 
| 220 | midline = 0; | 
| 221 | } |