1 |
greg |
2.1 |
#ifndef lint |
2 |
greg |
2.6 |
static const char RCSid[] = "$Id: robjutil.c,v 2.5 2023/02/09 21:54:10 greg Exp $"; |
3 |
greg |
2.1 |
#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 |
greg |
2.4 |
int do_triangulate = 0; |
32 |
greg |
2.2 |
char *xfm = NULL; |
33 |
greg |
2.1 |
char cbuf[256]; |
34 |
|
|
double verteps = -1.; |
35 |
|
|
int i, n; |
36 |
|
|
|
37 |
|
|
progname = argv[0]; |
38 |
|
|
/* process options */ |
39 |
|
|
for (i = 1; i < argc && (argv[i][0] == '-' || argv[i][0] == '+'); i++) |
40 |
|
|
switch (argv[i][1]) { |
41 |
|
|
case 'v': /* verbose mode */ |
42 |
|
|
verbose = (argv[i][0] == '+'); |
43 |
|
|
break; |
44 |
|
|
case 'g': /* save/delete group */ |
45 |
|
|
if (save_grps != (argv[i][0] == '+')) { |
46 |
|
|
if (ngrps) { |
47 |
|
|
fprintf(stderr, |
48 |
|
|
"%s: -g and +g are mutually exclusive\n", |
49 |
|
|
argv[0]); |
50 |
|
|
exit(1); |
51 |
|
|
} |
52 |
|
|
save_grps = !save_grps; |
53 |
|
|
} |
54 |
|
|
if (ngrps >= 64) { |
55 |
|
|
fprintf(stderr, "%s: too many groups\n", |
56 |
|
|
argv[0]); |
57 |
|
|
exit(1); |
58 |
|
|
} |
59 |
|
|
grp[ngrps++] = argv[++i]; |
60 |
|
|
break; |
61 |
|
|
case 'm': /* save/delete material */ |
62 |
|
|
if (save_mats != (argv[i][0] == '+')) { |
63 |
|
|
if (nmats) { |
64 |
|
|
fprintf(stderr, |
65 |
|
|
"%s: -m and +m are mutually exclusive\n", |
66 |
|
|
argv[0]); |
67 |
|
|
exit(1); |
68 |
|
|
} |
69 |
|
|
save_mats = !save_mats; |
70 |
|
|
} |
71 |
|
|
if (nmats >= 64) { |
72 |
|
|
fprintf(stderr, "%s: too many materials\n", |
73 |
|
|
argv[0]); |
74 |
|
|
exit(1); |
75 |
|
|
} |
76 |
|
|
mat[nmats++] = argv[++i]; |
77 |
|
|
break; |
78 |
|
|
case 't': /* do texture coord's */ |
79 |
|
|
do_tex = (argv[i][0] == '+') ? 1 : -1; |
80 |
|
|
break; |
81 |
|
|
case 'n': /* do normals */ |
82 |
|
|
do_norm = (argv[i][0] == '+') ? 1 : -1; |
83 |
|
|
break; |
84 |
|
|
case 'c': /* coalesce vertices */ |
85 |
|
|
verteps = atof(argv[++i]); |
86 |
|
|
break; |
87 |
|
|
case 'r': /* output to Radiance file? */ |
88 |
|
|
radout = (argv[i][0] == '+'); |
89 |
|
|
break; |
90 |
greg |
2.4 |
case 'T': /* triangulate faces? */ |
91 |
|
|
do_triangulate = (argv[i][0] == '+'); |
92 |
|
|
break; |
93 |
greg |
2.2 |
case 'x': /* apply a transform */ |
94 |
|
|
if (xfm != NULL) { |
95 |
|
|
fprintf(stderr, "%s: only one '-x' option allowed\n", |
96 |
|
|
argv[0]); |
97 |
|
|
exit(1); |
98 |
|
|
} |
99 |
|
|
xfm = argv[++i]; |
100 |
|
|
break; |
101 |
greg |
2.1 |
default: |
102 |
|
|
fprintf(stderr, "%s: unknown option: %s\n", |
103 |
|
|
argv[0], argv[i]); |
104 |
|
|
goto userr; |
105 |
|
|
} |
106 |
|
|
if (i == argc) { |
107 |
|
|
if (verbose) |
108 |
|
|
fputs("Loading scene from standard input...\n", |
109 |
|
|
stderr); |
110 |
|
|
myScene = loadOBJ(NULL, NULL); |
111 |
|
|
} else { |
112 |
|
|
myScene = newScene(); |
113 |
|
|
for ( ; i < argc; i++) { |
114 |
|
|
if (verbose) |
115 |
|
|
fprintf(stderr, |
116 |
|
|
"Loading scene from \"%s\"...\n", |
117 |
|
|
argv[i]); |
118 |
|
|
if (loadOBJ(myScene, argv[i]) == NULL) |
119 |
|
|
exit(1); |
120 |
|
|
} |
121 |
|
|
} |
122 |
|
|
if (ngrps) { |
123 |
|
|
if (verbose) |
124 |
|
|
fputs("Deleting unwanted groups...\n", stderr); |
125 |
|
|
clearSelection(myScene, 0); |
126 |
|
|
sprintf(cbuf, "%s the following groups:", |
127 |
|
|
save_grps ? "Extracting" : "Deleting"); |
128 |
|
|
addComment(myScene, cbuf); |
129 |
|
|
for (i = 0; i < ngrps; i++) { |
130 |
|
|
sprintf(cbuf, "\t%s", grp[i]); |
131 |
|
|
addComment(myScene, cbuf); |
132 |
|
|
selectGroup(myScene, grp[i], 0); |
133 |
|
|
} |
134 |
|
|
if (save_grps) |
135 |
|
|
n = deleteFaces(myScene, 0, FACE_SELECTED); |
136 |
|
|
else |
137 |
|
|
n = deleteFaces(myScene, FACE_SELECTED, 0); |
138 |
|
|
sprintf(cbuf, "\t(%d faces removed)", n); |
139 |
|
|
addComment(myScene, cbuf); |
140 |
|
|
} |
141 |
|
|
if (nmats) { |
142 |
|
|
if (verbose) |
143 |
|
|
fputs("Deleting unwanted materials...\n", stderr); |
144 |
|
|
clearSelection(myScene, 0); |
145 |
|
|
sprintf(cbuf, "%s the following materials:", |
146 |
|
|
save_mats ? "Extracting" : "Deleting"); |
147 |
|
|
addComment(myScene, cbuf); |
148 |
|
|
for (i = 0; i < nmats; i++) { |
149 |
|
|
sprintf(cbuf, "\t%s", mat[i]); |
150 |
|
|
addComment(myScene, cbuf); |
151 |
|
|
selectMaterial(myScene, mat[i], 0); |
152 |
|
|
} |
153 |
|
|
if (save_mats) |
154 |
|
|
n = deleteFaces(myScene, 0, FACE_SELECTED); |
155 |
|
|
else |
156 |
|
|
n = deleteFaces(myScene, FACE_SELECTED, 0); |
157 |
|
|
sprintf(cbuf, "\t(%d faces removed)", n); |
158 |
|
|
addComment(myScene, cbuf); |
159 |
|
|
} |
160 |
|
|
if (verbose && do_tex < 0) |
161 |
|
|
fputs("Removing texture coordinates...\n", stderr); |
162 |
|
|
if (do_tex < 0 && (n = removeTexture(myScene, 0, 0))) { |
163 |
|
|
sprintf(cbuf, "Removed texture coordinates from %d faces", n); |
164 |
|
|
addComment(myScene, cbuf); |
165 |
|
|
} |
166 |
|
|
if (verbose && do_norm < 0) |
167 |
|
|
fputs("Removing surface normals...\n", stderr); |
168 |
|
|
if (do_norm < 0 && (n = removeNormals(myScene, 0, 0))) { |
169 |
|
|
sprintf(cbuf, "Removed surface normals from %d faces", n); |
170 |
|
|
addComment(myScene, cbuf); |
171 |
|
|
} |
172 |
|
|
if (verbose && verteps >= 0) |
173 |
|
|
fputs("Coalescing vertices...\n", stderr); |
174 |
|
|
if (verteps >= 0 && (n = coalesceVertices(myScene, verteps))) { |
175 |
|
|
sprintf(cbuf, "Coalesced %d vertices with epsilon of %g", |
176 |
|
|
n, verteps); |
177 |
|
|
addComment(myScene, cbuf); |
178 |
|
|
} |
179 |
|
|
if (verbose) |
180 |
|
|
fputs("Deleting degenerate faces...\n", stderr); |
181 |
|
|
n = deleteFaces(myScene, FACE_DEGENERATE, 0); |
182 |
|
|
if (n) { |
183 |
|
|
sprintf(cbuf, "Removed %d degenerate faces", n); |
184 |
|
|
addComment(myScene, cbuf); |
185 |
|
|
} |
186 |
|
|
if (verbose) |
187 |
|
|
fputs("Checking for duplicate faces...\n", stderr); |
188 |
greg |
2.6 |
if (findDuplicateFaces(myScene)) { |
189 |
greg |
2.1 |
n = deleteFaces(myScene, FACE_DUPLICATE, 0); |
190 |
greg |
2.6 |
if (n) { |
191 |
|
|
sprintf(cbuf, "Removed %d duplicate faces", n); |
192 |
|
|
addComment(myScene, cbuf); |
193 |
|
|
} |
194 |
greg |
2.1 |
} |
195 |
greg |
2.4 |
if (do_triangulate) { |
196 |
|
|
if (verbose) |
197 |
|
|
fputs("Making sure all faces are triangles...\n", stderr); |
198 |
|
|
n = triangulateScene(myScene); |
199 |
|
|
if (n > 0) { |
200 |
|
|
sprintf(cbuf, "Added %d faces during triangulation", n); |
201 |
|
|
addComment(myScene, cbuf); |
202 |
|
|
} |
203 |
|
|
} |
204 |
greg |
2.2 |
if (xfm != NULL) { |
205 |
|
|
if (verbose) |
206 |
|
|
fputs("Applying transform...\n", stderr); |
207 |
|
|
if (!xfmScene(myScene, xfm)) { |
208 |
|
|
fprintf(stderr, "%s: transform error\n", argv[0]); |
209 |
|
|
exit(1); |
210 |
|
|
} |
211 |
|
|
} |
212 |
greg |
2.1 |
if (verbose) |
213 |
|
|
fputs("Writing out scene...\n", stderr); |
214 |
|
|
|
215 |
|
|
fputs("# File processed by: ", stdout); |
216 |
|
|
printargs(argc, argv, stdout); |
217 |
|
|
if (radout) |
218 |
|
|
toRadiance(myScene, stdout, 0, 0); |
219 |
|
|
else |
220 |
|
|
toOBJ(myScene, stdout); |
221 |
|
|
/* freeScene(myScene); */ |
222 |
|
|
if (verbose) |
223 |
|
|
fputs("Done. \n", stderr); |
224 |
|
|
return(0); |
225 |
|
|
userr: |
226 |
|
|
fprintf(stderr, "Usage: %s [options] [input.obj ..]\n", argv[0]); |
227 |
|
|
fprintf(stderr, "Available options:\n"); |
228 |
|
|
fprintf(stderr, "\t+/-r\t\t\t# Radiance scene output on/off\n"); |
229 |
|
|
fprintf(stderr, "\t+/-v\t\t\t# on/off verbosity (progress reports)\n"); |
230 |
|
|
fprintf(stderr, "\t+/-g name\t\t# save/delete group\n"); |
231 |
|
|
fprintf(stderr, "\t+/-m name\t\t# save/delete faces with material\n"); |
232 |
|
|
fprintf(stderr, "\t+/-t\t\t\t# keep/remove texture coordinates\n"); |
233 |
|
|
fprintf(stderr, "\t+/-n\t\t\t# keep/remove vertex normals\n"); |
234 |
|
|
fprintf(stderr, "\t-c epsilon\t\t# coalesce vertices within epsilon\n"); |
235 |
greg |
2.4 |
fprintf(stderr, "\t+T\t\t\t# turn all faces into triangles\n"); |
236 |
greg |
2.2 |
fprintf(stderr, "\t-x 'xf spec'\t# apply the quoted transform\n"); |
237 |
greg |
2.1 |
return(1); |
238 |
|
|
} |
239 |
|
|
|
240 |
|
|
void |
241 |
greg |
2.5 |
eputs(const char *s) /* put string to stderr */ |
242 |
greg |
2.1 |
{ |
243 |
|
|
static int midline = 0; |
244 |
|
|
|
245 |
|
|
if (!*s) |
246 |
|
|
return; |
247 |
|
|
if (!midline++) { |
248 |
|
|
fputs(progname, stderr); |
249 |
|
|
fputs(": ", stderr); |
250 |
|
|
} |
251 |
|
|
fputs(s, stderr); |
252 |
|
|
if (s[strlen(s)-1] == '\n') |
253 |
|
|
midline = 0; |
254 |
|
|
} |