12 |
|
#include "rtio.h" |
13 |
|
#include "rterror.h" |
14 |
|
#include "fvect.h" |
15 |
< |
#include <stdlib.h> |
15 |
> |
#include "paths.h" |
16 |
|
#include <ctype.h> |
17 |
|
#include "objutil.h" |
18 |
|
|
19 |
– |
typedef int VNDX[3]; /* vertex index (point,map,normal) */ |
20 |
– |
|
19 |
|
#define MAXARG 512 /* maximum # arguments in a statement */ |
20 |
|
|
21 |
|
static int lineno; /* current line number */ |
113 |
|
error(USER, errmsg); |
114 |
|
} |
115 |
|
|
118 |
– |
/* Add a vertex to our scene */ |
119 |
– |
static void |
120 |
– |
add_vertex(Scene *sc, double x, double y, double z) |
121 |
– |
{ |
122 |
– |
sc->vert = chunk_alloc(Vertex, sc->vert, sc->nverts); |
123 |
– |
sc->vert[sc->nverts].p[0] = x; |
124 |
– |
sc->vert[sc->nverts].p[1] = y; |
125 |
– |
sc->vert[sc->nverts].p[2] = z; |
126 |
– |
sc->vert[sc->nverts++].vflist = NULL; |
127 |
– |
} |
128 |
– |
|
129 |
– |
/* Add a texture coordinate to our scene */ |
130 |
– |
static void |
131 |
– |
add_texture(Scene *sc, double u, double v) |
132 |
– |
{ |
133 |
– |
sc->tex = chunk_alloc(TexCoord, sc->tex, sc->ntex); |
134 |
– |
sc->tex[sc->ntex].u = u; |
135 |
– |
sc->tex[sc->ntex].v = v; |
136 |
– |
sc->ntex++; |
137 |
– |
} |
138 |
– |
|
139 |
– |
/* Add a surface normal to our scene */ |
140 |
– |
static int |
141 |
– |
add_normal(Scene *sc, double xn, double yn, double zn) |
142 |
– |
{ |
143 |
– |
FVECT nrm; |
144 |
– |
|
145 |
– |
nrm[0] = xn; nrm[1] = yn; nrm[2] = zn; |
146 |
– |
if (normalize(nrm) == .0) |
147 |
– |
return(0); |
148 |
– |
sc->norm = chunk_alloc(Normal, sc->norm, sc->nnorms); |
149 |
– |
VCOPY(sc->norm[sc->nnorms], nrm); |
150 |
– |
sc->nnorms++; |
151 |
– |
return(1); |
152 |
– |
} |
153 |
– |
|
116 |
|
/* combine multi-group name into single identifier w/o spaces */ |
117 |
|
static char * |
118 |
|
group_name(int ac, char **av) |
141 |
|
return(nambuf); |
142 |
|
} |
143 |
|
|
144 |
< |
/* set current group */ |
183 |
< |
static void |
184 |
< |
set_group(Scene *sc, const char *nm) |
185 |
< |
{ |
186 |
< |
sc->lastgrp = findName(nm, (const char **)sc->grpname, sc->ngrps); |
187 |
< |
if (sc->lastgrp >= 0) |
188 |
< |
return; |
189 |
< |
sc->grpname = chunk_alloc(char *, sc->grpname, sc->ngrps); |
190 |
< |
sc->grpname[sc->lastgrp=sc->ngrps++] = savqstr((char *)nm); |
191 |
< |
} |
192 |
< |
|
193 |
< |
/* set current material */ |
194 |
< |
static void |
195 |
< |
set_material(Scene *sc, const char *nm) |
196 |
< |
{ |
197 |
< |
sc->lastmat = findName(nm, (const char **)sc->matname, sc->nmats); |
198 |
< |
if (sc->lastmat >= 0) |
199 |
< |
return; |
200 |
< |
sc->matname = chunk_alloc(char *, sc->matname, sc->nmats); |
201 |
< |
sc->matname[sc->lastmat=sc->nmats++] = savqstr((char *)nm); |
202 |
< |
} |
203 |
< |
|
204 |
< |
/* Add a new face to scene */ |
144 |
> |
/* add a new face to scene */ |
145 |
|
static int |
146 |
|
add_face(Scene *sc, const VNDX ondx, int ac, char *av[]) |
147 |
|
{ |
148 |
< |
Face *f; |
148 |
> |
VNDX vdef[4]; |
149 |
> |
VNDX *varr = vdef; |
150 |
> |
Face *f = NULL; |
151 |
|
int i; |
152 |
|
|
153 |
< |
if (ac < 3) |
153 |
> |
if (ac < 3) /* legal polygon? */ |
154 |
|
return(0); |
155 |
< |
f = (Face *)emalloc(sizeof(Face)+sizeof(VertEnt)*(ac-3)); |
156 |
< |
f->flags = 0; |
157 |
< |
f->nv = ac; |
158 |
< |
f->grp = sc->lastgrp; |
159 |
< |
f->mat = sc->lastmat; |
160 |
< |
for (i = 0; i < ac; i++) { /* add each vertex */ |
161 |
< |
VNDX vin; |
162 |
< |
int j; |
163 |
< |
if (!cvtndx(vin, sc, ondx, av[i])) { |
164 |
< |
efree((char *)f); |
223 |
< |
return(0); |
224 |
< |
} |
225 |
< |
f->v[i].vid = vin[0]; |
226 |
< |
f->v[i].tid = vin[1]; |
227 |
< |
f->v[i].nid = vin[2]; |
228 |
< |
f->v[i].fnext = NULL; |
229 |
< |
for (j = i; j-- > 0; ) |
230 |
< |
if (f->v[j].vid == vin[0]) |
231 |
< |
break; |
232 |
< |
if (j < 0) { /* first occurrence? */ |
233 |
< |
f->v[i].fnext = sc->vert[vin[0]].vflist; |
234 |
< |
sc->vert[vin[0]].vflist = f; |
235 |
< |
} else if (ac == 3) /* degenerate triangle? */ |
236 |
< |
f->flags |= FACE_DEGENERATE; |
237 |
< |
} |
238 |
< |
f->next = sc->flist; /* push onto face list */ |
239 |
< |
sc->flist = f; |
240 |
< |
sc->nfaces++; |
241 |
< |
/* check face area */ |
242 |
< |
if (!(f->flags & FACE_DEGENERATE) && faceArea(sc, f, NULL) <= FTINY) |
243 |
< |
f->flags |= FACE_DEGENERATE; |
244 |
< |
return(1); |
155 |
> |
if (ac > 4) /* need to allocate array? */ |
156 |
> |
varr = (VNDX *)emalloc(ac*sizeof(VNDX)); |
157 |
> |
for (i = ac; i--; ) /* index each vertex */ |
158 |
> |
if (!cvtndx(varr[i], sc, ondx, av[i])) |
159 |
> |
break; |
160 |
> |
if (i < 0) /* create face if indices OK */ |
161 |
> |
f = addFace(sc, varr, ac); |
162 |
> |
if (varr != vdef) |
163 |
> |
efree((char *)varr); |
164 |
> |
return(f != NULL); |
165 |
|
} |
166 |
|
|
167 |
|
/* Load a .OBJ file */ |
208 |
|
syntax(fspec, "bad vertex"); |
209 |
|
goto failure; |
210 |
|
} |
211 |
< |
add_vertex(sc, atof(argv[1]), |
211 |
> |
addVertex(sc, atof(argv[1]), |
212 |
|
atof(argv[2]), |
213 |
|
atof(argv[3])); |
214 |
|
break; |
217 |
|
goto unknown; |
218 |
|
if (badarg(argc-1,argv+1,"ff")) |
219 |
|
goto unknown; |
220 |
< |
add_texture(sc, atof(argv[1]), atof(argv[2])); |
220 |
> |
addTexture(sc, atof(argv[1]), atof(argv[2])); |
221 |
|
break; |
222 |
|
case 'n': /* normal */ |
223 |
|
if (argv[0][2]) |
226 |
|
syntax(fspec, "bad normal"); |
227 |
|
goto failure; |
228 |
|
} |
229 |
< |
if (!add_normal(sc, atof(argv[1]), |
229 |
> |
if (addNormal(sc, atof(argv[1]), |
230 |
|
atof(argv[2]), |
231 |
< |
atof(argv[3]))) { |
231 |
> |
atof(argv[3])) < 0) { |
232 |
|
syntax(fspec, "zero normal"); |
233 |
|
goto failure; |
234 |
|
} |
254 |
|
syntax(fspec, "bad # arguments"); |
255 |
|
goto failure; |
256 |
|
} |
257 |
< |
set_material(sc, argv[1]); |
257 |
> |
setMaterial(sc, argv[1]); |
258 |
|
break; |
259 |
|
case 'o': /* object name */ |
260 |
|
case 'g': /* group name */ |
262 |
|
syntax(fspec, "missing argument"); |
263 |
|
goto failure; |
264 |
|
} |
265 |
< |
set_group(sc, group_name(argc-1, argv+1)); |
265 |
> |
setGroup(sc, group_name(argc-1, argv+1)); |
266 |
|
break; |
267 |
|
case '#': /* comment */ |
268 |
|
continue; |
276 |
|
fprintf(stderr, " %8d statements\r", nstats); |
277 |
|
} |
278 |
|
#if POPEN_SUPPORT |
279 |
< |
if (fspec[0] == '!') |
280 |
< |
pclose(fp); |
281 |
< |
else |
279 |
> |
if (fspec[0] == '!') { |
280 |
> |
if (pclose(fp) != 0) { |
281 |
> |
sprintf(errmsg, "Bad return status from: %s", fspec+1); |
282 |
> |
error(USER, errmsg); |
283 |
> |
freeScene(sc); |
284 |
> |
return(NULL); |
285 |
> |
} |
286 |
> |
} else |
287 |
|
#endif |
288 |
|
if (fp != stdin) |
289 |
|
fclose(fp); |
290 |
+ |
if (verbose) |
291 |
+ |
fprintf(stderr, "Read %d statements\n", nstats); |
292 |
|
sprintf(buf, "%d statements read from \"%s\"", nstats, fspec); |
293 |
|
addComment(sc, buf); |
294 |
|
if (nunknown) { |