1 |
greg |
2.1 |
#ifndef lint |
2 |
greg |
2.2 |
static const char RCSid[] = "$Id: convertobj.c,v 2.1 2020/03/30 18:28:35 greg Exp $"; |
3 |
greg |
2.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* convertobj.c |
6 |
|
|
* |
7 |
|
|
* Convert .OBJ scene to Radiance |
8 |
|
|
* |
9 |
|
|
* Created by Greg Ward on Thu Apr 22 2004. |
10 |
|
|
*/ |
11 |
|
|
|
12 |
greg |
2.2 |
#include "paths.h" |
13 |
greg |
2.1 |
#include "rterror.h" |
14 |
|
|
#include "objutil.h" |
15 |
|
|
|
16 |
|
|
/* Callback to convert face to Radiance */ |
17 |
|
|
static int |
18 |
|
|
radface(Scene *sc, Face *f, void *ptr) |
19 |
|
|
{ |
20 |
|
|
static int fcnt = 0; |
21 |
|
|
FILE *fp = (FILE *)ptr; |
22 |
|
|
int i; |
23 |
|
|
|
24 |
|
|
if (f->flags & FACE_DEGENERATE) |
25 |
|
|
return(0); |
26 |
|
|
fprintf(fp, "\n%s polygon %s.%d\n0\n0\n%d\n", sc->matname[f->mat], |
27 |
|
|
sc->grpname[f->grp], ++fcnt, 3*f->nv); |
28 |
|
|
for (i = 0; i < f->nv; i++) { |
29 |
|
|
Vertex *vp = sc->vert + f->v[i].vid; |
30 |
|
|
fprintf(fp, "\t%18.12g %18.12g %18.12g\n", |
31 |
|
|
vp->p[0], vp->p[1], vp->p[2]); |
32 |
|
|
} |
33 |
|
|
return(1); |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
/* Convert indicated faces to Radiance, return # written or -1 on error */ |
37 |
|
|
int |
38 |
|
|
toRadiance(Scene *sc, FILE *fp, int flreq, int flexc) |
39 |
|
|
{ |
40 |
|
|
int n; |
41 |
|
|
|
42 |
|
|
if (sc == NULL || sc->nfaces <= 0 || fp == NULL) |
43 |
|
|
return(0); |
44 |
|
|
/* write comments */ |
45 |
|
|
for (n = 0; n < sc->ndescr; n++) |
46 |
|
|
fprintf(fp, "# %s\n", sc->descr[n]); |
47 |
|
|
/* write faces */ |
48 |
|
|
n = foreachFace(sc, radface, flreq, flexc, (void *)fp); |
49 |
|
|
if (fflush(fp) < 0) { |
50 |
|
|
error(SYSTEM, "Error flushing Radiance scene data"); |
51 |
|
|
return(-1); |
52 |
|
|
} |
53 |
|
|
return(n); |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
/* Convert faces to Radiance file, return # written or -1 on error */ |
57 |
|
|
int |
58 |
|
|
writeRadiance(Scene *sc, const char *fspec, int flreq, int flexc) |
59 |
|
|
{ |
60 |
|
|
extern char *progname; |
61 |
|
|
FILE *fp; |
62 |
|
|
int n; |
63 |
|
|
|
64 |
|
|
if (sc == NULL || sc->nfaces <= 0 || fspec == NULL || !*fspec) |
65 |
|
|
return(0); |
66 |
|
|
#if POPEN_SUPPORT |
67 |
|
|
if (fspec[0] == '!') { |
68 |
|
|
if ((fp = popen(fspec+1, "w")) == NULL) { |
69 |
|
|
sprintf(errmsg, "%s: cannot execute", fspec); |
70 |
|
|
error(SYSTEM, errmsg); |
71 |
|
|
return(0); |
72 |
|
|
} |
73 |
|
|
} else |
74 |
|
|
#endif |
75 |
|
|
if ((fp = fopen(fspec, "w")) == NULL) { |
76 |
|
|
sprintf(errmsg, "%s: cannot open for writing", fspec); |
77 |
|
|
error(SYSTEM, errmsg); |
78 |
|
|
return(0); |
79 |
|
|
} |
80 |
|
|
/* start off header */ |
81 |
|
|
fprintf(fp, "# Radiance scene file converted from .OBJ by %s\n#\n", |
82 |
|
|
progname); |
83 |
|
|
n = toRadiance(sc, fp, flreq, flexc); /* write file */ |
84 |
|
|
#if POPEN_SUPPORT |
85 |
|
|
if (fspec[0] == '!') |
86 |
|
|
pclose(fp); |
87 |
|
|
else |
88 |
|
|
#endif |
89 |
|
|
fclose(fp); /* close file */ |
90 |
|
|
return(n); |
91 |
|
|
} |