ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/rglinst.c
Revision: 3.7
Committed: Wed Apr 23 02:28:06 2003 UTC (21 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.6: +1 -0 lines
Log Message:
Added check for mesh primitive and warning when encountered

File Contents

# User Rev Content
1 gwlarson 3.1 #ifndef lint
2 greg 3.5 static const char RCSid[] = "$Id$";
3 gwlarson 3.1 #endif
4     /*
5     * Routines for reading instances and converting to OpenGL.
6     */
7    
8 greg 3.6 #include "copyright.h"
9 greg 3.5
10 gwlarson 3.1 #include "radogl.h"
11     #include "octree.h"
12    
13 gwlarson 3.2 #define MAXLEVEL 16 /* maximum instance hierarchy level */
14    
15 gwlarson 3.1 typedef struct {
16     int listid; /* our list id */
17     short localmatl; /* uses local material only */
18     FVECT cent; /* center of octree cube */
19     char octfile[256]; /* octree file path */
20     } OCTINST; /* octree to instantiate */
21    
22     static double ogetflt();
23     static long ogetint();
24     static char *ogetstr();
25 greg 3.5 static int loadobj();
26     static void skiptree();
27     static void octerror();
28 gwlarson 3.1 static OCTINST *getoct();
29    
30     static char *infn; /* input file name */
31     static FILE *infp; /* input file stream */
32     static int objsize; /* size of stored OBJECT's */
33     static short otypmap[NUMOTYPE+8]; /* object type map */
34    
35 gwlarson 3.4 static unsigned long imhash(mod) char *mod; {return((unsigned long)mod);}
36 gwlarson 3.1 static LUTAB imtab = {imhash,NULL,NULL,NULL,0,NULL,0};
37    
38     static LUTAB ottab = LU_SINIT(free,free);
39    
40    
41 greg 3.5 int
42 gwlarson 3.1 o_instance(o) /* convert instance to list call */
43     register OBJREC *o;
44     {
45     XF xfs;
46     register OCTINST *ot;
47     /* set up */
48     if (o->oargs.nsargs < 1)
49     objerror(o, USER, "missing octree");
50     setmaterial(NULL, NULL, 0);
51     /* put out transform (if any) */
52     if (o->oargs.nsargs > 1) {
53     if (xf(&xfs, o->oargs.nsargs-1, o->oargs.sarg+1) !=
54     o->oargs.nsargs-1)
55     objerror(o, USER, "bad transform");
56     glPushAttrib(GL_TRANSFORM_BIT);
57     if (xfs.sca < 1.-FTINY | xfs.sca > 1.+FTINY)
58     glEnable(GL_NORMALIZE);
59     glMatrixMode(GL_MODELVIEW);
60     glPushMatrix();
61     /* matrix order works out to same */
62     #ifdef SMLFLT
63     glMultMatrixf((GLfloat *)xfs.xfm);
64     #else
65     glMultMatrixd((GLdouble *)xfs.xfm);
66     #endif
67     }
68     ot = getoct(o->oargs.sarg[0]); /* get octree reference */
69     if (ot->localmatl &= o->os != NULL) /* set material */
70 greg 3.5 setmaterial((MATREC *)o->os, ot->cent, 0);
71 gwlarson 3.1 /* call the assigned list */
72     glCallList(ot->listid);
73    
74     if (o->oargs.nsargs > 1) { /* end transform */
75 gwlarson 3.3 glMatrixMode(GL_MODELVIEW);
76 gwlarson 3.1 glPopMatrix();
77     glPopAttrib();
78     }
79     rgl_checkerr("creating instance");
80 greg 3.7 return(0);
81 gwlarson 3.1 }
82    
83    
84     static int
85     buildoctlist(lp) /* build octree list */
86     LUENT *lp;
87     {
88     int old_dolights = dolights, old_domats = domats;
89     register OCTINST *op = (OCTINST *)lp->data;
90    
91     domats = !op->localmatl; /* do materials only if needed */
92     dolights = 0; /* never do light sources */
93     glNewList(op->listid, GL_COMPILE);
94     loadoct(op->octfile); /* load objects into display list */
95     surfclean(); /* clean up */
96     glEndList();
97     dolights = old_dolights; /* restore */
98     domats = old_domats;
99     return(1); /* return success */
100     }
101    
102    
103     int
104     loadoctrees() /* load octrees we've saved up */
105     {
106 gwlarson 3.2 int levelsleft = MAXLEVEL;
107 gwlarson 3.1 int nocts = 0;
108     LUTAB looptab;
109     /* loop through new octree references */
110     while (ottab.tsiz) {
111 gwlarson 3.2 if (!levelsleft--)
112     error(USER, "too many octree levels -- instance loop?");
113 gwlarson 3.1 copystruct(&looptab, &ottab);
114     ottab.tsiz = 0;
115     nocts += lu_doall(&looptab, buildoctlist);
116     lu_done(&looptab);
117     }
118     return(nocts);
119     }
120    
121    
122     static OCTINST *
123     getoct(name) /* get/assign octree list id */
124     char *name;
125     {
126     extern char *getpath(), *getlibpath();
127     char *path;
128     register LUENT *lp;
129     register OCTINST *op;
130    
131     if ((lp = lu_find(&ottab, name)) == NULL)
132     goto memerr;
133     if (lp->key == NULL) {
134     lp->key = (char *)malloc(strlen(name)+1);
135     if (lp->key == NULL)
136     goto memerr;
137     strcpy(lp->key, name);
138     }
139     if ((op = (OCTINST *)lp->data) == NULL) {
140     path = getpath(name, getlibpath(), R_OK);
141     if (path == NULL) {
142     sprintf(errmsg, "cannot find octree \"%s\"", name);
143     error(USER, errmsg);
144     }
145     op = (OCTINST *)(lp->data = (char *)malloc(sizeof(OCTINST)));
146     strcpy(op->octfile, path);
147     checkoct(op->octfile, op->cent);
148     op->listid = newglist();
149     op->localmatl = ~0;
150     }
151     return(op);
152     memerr:
153     error(SYSTEM, "out of memory in getoct");
154     }
155    
156    
157     double
158     checkoct(fname, cent) /* check octree file for validity */
159     char *fname;
160     FVECT cent;
161     {
162     char sbuf[64];
163     FILE *fp = infp;
164     char *fn = infn;
165     double siz = 0.;
166     register int i;
167    
168     if ((infp = fopen(infn=fname, "r")) == NULL) {
169     sprintf(errmsg, "cannot open octree file \"%s\"", fname);
170     error(SYSTEM, errmsg);
171     }
172     #ifdef MSDOS
173     setmode(fileno(infp), O_BINARY);
174     #endif
175     /* get header */
176     if (checkheader(infp, OCTFMT, NULL) < 0)
177     octerror(USER, "not an octree");
178     /* check format */
179     if ((objsize = ogetint(2)-OCTMAGIC) <= 0 ||
180     objsize > MAXOBJSIZ || objsize > sizeof(long))
181     octerror("incompatible octree format");
182     if (cent != NULL) { /* get boundaries (compute center) */
183     for (i = 0; i < 3; i++)
184     cent[i] = atof(ogetstr(sbuf));
185     siz = atof(ogetstr(sbuf))*.5;
186     cent[0] += siz; cent[1] += siz; cent[2] += siz;
187     } else { /* get size (radius) only */
188     for (i = 0; i < 3; i++)
189     ogetstr(sbuf);
190     siz = atof(ogetstr(sbuf))*.5;
191     }
192     fclose(infp);
193     infp = fp;
194     infn = fn;
195     return(siz);
196     }
197    
198    
199 greg 3.5 int
200 gwlarson 3.1 loadoct(fname) /* read in objects from octree */
201     char *fname;
202     {
203     OBJECT fnobjects;
204     char sbuf[256];
205     int nf;
206     register int i;
207     long m;
208    
209     infn = fname;
210     infp = fopen(fname, "r"); /* assume already checked */
211     #ifdef MSDOS
212     setmode(fileno(infp), O_BINARY);
213     #endif
214     /* skip header */
215     getheader(infp, NULL, NULL);
216     /* get format */
217     objsize = ogetint(2)-OCTMAGIC;
218     /* skip boundaries */
219     for (i = 0; i < 4; i++)
220     ogetstr(sbuf);
221     nf = 0; /* load object files */
222     while (*ogetstr(sbuf)) {
223     rgl_load(sbuf);
224     nf++;
225     }
226     /* get number of objects */
227     fnobjects = m = ogetint(objsize);
228     if (fnobjects != m)
229     octerror(USER, "too many objects");
230    
231     if (nf == 0) {
232     skiptree();
233     for (i = 0; *ogetstr(sbuf); i++)
234     if ((otypmap[i] = otype(sbuf)) < 0) {
235     sprintf(errmsg, "unknown type \"%s\"", sbuf);
236     octerror(WARNING, errmsg);
237     }
238     lu_init(&imtab, 1000); nobjects = 0;
239     while (loadobj() != OVOID)
240     ;
241     lu_done(&imtab);
242     if (nobjects != fnobjects)
243     octerror(USER, "inconsistent object count");
244     }
245     fclose(infp);
246     return(nf);
247     }
248    
249    
250     static char *
251     ogetstr(s) /* get null-terminated string */
252     char *s;
253     {
254     extern char *getstr();
255    
256     if (getstr(s, infp) == NULL)
257     octerror(USER, "truncated octree");
258     return(s);
259     }
260    
261    
262     static long
263     ogetint(siz) /* get a siz-byte integer */
264     int siz;
265     {
266     extern long getint();
267     register long r;
268    
269     r = getint(siz, infp);
270     if (feof(infp))
271     octerror(USER, "truncated octree");
272     return(r);
273     }
274    
275    
276     static double
277     ogetflt() /* get a floating point number */
278     {
279     extern double getflt();
280     double r;
281    
282     r = getflt(infp);
283     if (feof(infp))
284     octerror(USER, "truncated octree");
285     return(r);
286     }
287    
288    
289 greg 3.5 static void
290 gwlarson 3.1 skiptree() /* skip octree on input */
291     {
292     register int i;
293    
294     switch (getc(infp)) {
295     case OT_EMPTY:
296     return;
297     case OT_FULL:
298     for (i = ogetint(objsize)*objsize; i-- > 0; )
299     if (getc(infp) == EOF)
300     octerror(USER, "truncated octree");
301     return;
302     case OT_TREE:
303     for (i = 0; i < 8; i++)
304     skiptree();
305     return;
306     case EOF:
307     octerror(USER, "truncated octree");
308     default:
309     octerror(USER, "damaged octree");
310     }
311     }
312    
313    
314 greg 3.5 static int
315 gwlarson 3.1 loadobj() /* get next object */
316     {
317     static OBJREC ob;
318     char idbuf[MAXSTR], sbuf[MAXSTR];
319     register LUENT *lep;
320     register int i;
321     register long m;
322     /* get type */
323     i = ogetint(1);
324     if (i == -1)
325     return(OVOID); /* terminator */
326     if ((ob.otype = otypmap[i]) < 0)
327     octerror(USER, "reference to unknown type");
328     /* get modifier */
329     if ((m = ogetint(objsize)) != OVOID && (OBJECT)m != m)
330     octerror(USER, "too many objects");
331     if ((ob.omod = m) != OVOID && domats) {
332     if ((lep = lu_find(&imtab, (char *)m)) == NULL)
333     goto memerr;
334     ob.os = lep->data;
335     } else
336     ob.os = NULL;
337     /* get name id */
338     ob.oname = ogetstr(idbuf);
339     /* get string arguments */
340     if (ob.oargs.nsargs = ogetint(2)) {
341     ob.oargs.sarg = (char **)malloc
342     (ob.oargs.nsargs*sizeof(char *));
343     if (ob.oargs.sarg == NULL)
344     goto memerr;
345     for (i = 0; i < ob.oargs.nsargs; i++)
346     ob.oargs.sarg[i] = savestr(ogetstr(sbuf));
347     } else
348     ob.oargs.sarg = NULL;
349     /* get integer arguments */
350     #ifdef IARGS
351     if (ob.oargs.niargs = ogetint(2)) {
352     ob.oargs.iarg = (long *)malloc
353     (ob.oargs.niargs*sizeof(long));
354     if (ob.oargs.iarg == NULL)
355     goto memerr;
356     for (i = 0; i < ob.oargs.niargs; i++)
357     ob.oargs.iarg[i] = ogetint(4);
358     } else
359     ob.oargs.iarg = NULL;
360     #endif
361     /* get real arguments */
362     if (ob.oargs.nfargs = ogetint(2)) {
363     ob.oargs.farg = (FLOAT *)malloc
364     (ob.oargs.nfargs*sizeof(FLOAT));
365     if (ob.oargs.farg == NULL)
366     goto memerr;
367     for (i = 0; i < ob.oargs.nfargs; i++)
368     ob.oargs.farg[i] = ogetflt();
369     } else
370     ob.oargs.farg = NULL;
371     /* process object */
372     (*ofun[ob.otype].funp)(&ob);
373     /* record material if modifier */
374     if (ismodifier(ob.otype)) {
375     if ((lep = lu_find(&imtab, (char *)nobjects)) == NULL)
376     goto memerr;
377     lep->key = (char *)nobjects;
378     lep->data = (char *)getmatp(ob.oname);
379     }
380     freefargs(&ob.oargs); /* free arguments */
381     return(nobjects++); /* return object id */
382     memerr:
383     error(SYSTEM, "out of memory in loadobj");
384     }
385    
386    
387 greg 3.5 static void
388 gwlarson 3.1 octerror(etyp, msg) /* octree error */
389     int etyp;
390     char *msg;
391     {
392     char msgbuf[128];
393    
394     sprintf(msgbuf, "(%s): %s", infn, msg);
395     error(etyp, msgbuf);
396     }