ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/rglfile.c
Revision: 3.4
Committed: Sat Feb 22 02:07:22 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.3: +62 -11 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

# User Rev Content
1 gwlarson 3.1 #ifndef lint
2 greg 3.4 static const char RCSid[] = "$Id$";
3 gwlarson 3.1 #endif
4     /*
5     * Load Radiance object(s) and create OpenGL display lists
6     */
7    
8 greg 3.4 /* ====================================================================
9     * The Radiance Software License, Version 1.0
10     *
11     * Copyright (c) 1990 - 2002 The Regents of the University of California,
12     * through Lawrence Berkeley National Laboratory. All rights reserved.
13     *
14     * Redistribution and use in source and binary forms, with or without
15     * modification, are permitted provided that the following conditions
16     * are met:
17     *
18     * 1. Redistributions of source code must retain the above copyright
19     * notice, this list of conditions and the following disclaimer.
20     *
21     * 2. Redistributions in binary form must reproduce the above copyright
22     * notice, this list of conditions and the following disclaimer in
23     * the documentation and/or other materials provided with the
24     * distribution.
25     *
26     * 3. The end-user documentation included with the redistribution,
27     * if any, must include the following acknowledgment:
28     * "This product includes Radiance software
29     * (http://radsite.lbl.gov/)
30     * developed by the Lawrence Berkeley National Laboratory
31     * (http://www.lbl.gov/)."
32     * Alternately, this acknowledgment may appear in the software itself,
33     * if and wherever such third-party acknowledgments normally appear.
34     *
35     * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
36     * and "The Regents of the University of California" must
37     * not be used to endorse or promote products derived from this
38     * software without prior written permission. For written
39     * permission, please contact [email protected].
40     *
41     * 5. Products derived from this software may not be called "Radiance",
42     * nor may "Radiance" appear in their name, without prior written
43     * permission of Lawrence Berkeley National Laboratory.
44     *
45     * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
46     * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
48     * DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
49     * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50     * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51     * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
52     * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
53     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
54     * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
55     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56     * SUCH DAMAGE.
57     * ====================================================================
58     *
59     * This software consists of voluntary contributions made by many
60     * individuals on behalf of Lawrence Berkeley National Laboratory. For more
61     * information on Lawrence Berkeley National Laboratory, please see
62     * <http://www.lbl.gov/>.
63     */
64    
65 gwlarson 3.1 #include "radogl.h"
66    
67     #ifndef NLIST2ALLOC
68     #define NLIST2ALLOC 16 /* batch of display lists to get */
69     #endif
70    
71     FUN ofun[NUMOTYPE] = INIT_OTYPE;
72    
73 gwlarson 3.3 static int nextlist, nlistleft = 0;
74 gwlarson 3.1
75 gwlarson 3.3
76 greg 3.4 static void
77 gwlarson 3.1 initotypes() /* initialize ofun array */
78     {
79     if (ofun[OBJ_SPHERE].funp == o_sphere)
80     return; /* already done */
81     /* assign surface types */
82     ofun[OBJ_SPHERE].funp =
83     ofun[OBJ_BUBBLE].funp = o_sphere;
84     ofun[OBJ_FACE].funp = o_face;
85     ofun[OBJ_CONE].funp =
86     ofun[OBJ_CUP].funp =
87     ofun[OBJ_CYLINDER].funp =
88     ofun[OBJ_TUBE].funp = o_cone;
89     ofun[OBJ_RING].funp = o_ring;
90     ofun[OBJ_SOURCE].funp = o_source;
91     ofun[OBJ_INSTANCE].funp = o_instance;
92     /* assign material types */
93     ofun[MAT_TRANS].funp =
94     ofun[MAT_PLASTIC].funp =
95     ofun[MAT_METAL].funp = m_normal;
96     ofun[MAT_GLASS].funp =
97     ofun[MAT_DIELECTRIC].funp =
98     ofun[MAT_INTERFACE].funp = m_glass;
99     ofun[MAT_PLASTIC2].funp =
100     ofun[MAT_METAL2].funp =
101     ofun[MAT_TRANS2].funp = m_aniso;
102     ofun[MAT_TDATA].funp =
103     ofun[MAT_PDATA].funp =
104     ofun[MAT_MDATA].funp =
105     ofun[MAT_TFUNC].funp =
106     ofun[MAT_PFUNC].funp =
107     ofun[MAT_MFUNC].funp = m_brdf;
108     ofun[MAT_BRTDF].funp = m_brdf2;
109     ofun[MAT_GLOW].funp =
110     ofun[MAT_LIGHT].funp =
111     ofun[MAT_SPOT].funp =
112     ofun[MAT_ILLUM].funp = m_light;
113     ofun[MAT_MIRROR].funp = m_mirror;
114     ofun[MAT_DIRECT1].funp =
115     ofun[MAT_DIRECT2].funp = m_prism;
116     }
117    
118    
119     int
120     newglist() /* allocate an OGL list id */
121     {
122 gwlarson 3.3 if (!nlistleft--) {
123 gwlarson 3.1 nextlist = glGenLists(NLIST2ALLOC);
124 gwlarson 3.3 if (!nextlist)
125     error(SYSTEM, "no list space left in newglist");
126     nlistleft = NLIST2ALLOC-1;
127 gwlarson 3.1 }
128     return(nextlist++);
129     }
130    
131    
132 greg 3.4 void
133 gwlarson 3.1 rgl_checkerr(where) /* check for GL or GLU error */
134     char *where;
135     {
136     register GLenum errcode;
137    
138     while ((errcode = glGetError()) != GL_NO_ERROR) {
139     sprintf(errmsg, "OpenGL error %s: %s",
140     where, gluErrorString(errcode));
141     error(WARNING, errmsg);
142     }
143     }
144    
145    
146     int
147 gwlarson 3.3 rgl_filelist(ic, inp, nl) /* load scene files into display list */
148 gwlarson 3.1 int ic;
149     char **inp;
150 gwlarson 3.3 int *nl; /* returned number of lists (optional) */
151 gwlarson 3.1 {
152     int listid;
153    
154     initotypes(); /* prepare */
155     listid = newglist();
156     glNewList(listid, GL_COMPILE);
157     lightinit(); /* start light source list */
158     while (ic--) /* load each file */
159     rgl_load(*inp++);
160     surfclean(); /* clean up first pass */
161     lightclean(); /* clean up light sources also */
162     glEndList(); /* end of top display list */
163     lightdefs(); /* define light sources */
164     loadoctrees(); /* load octrees (sublists) for instances */
165 gwlarson 3.3 if (nl != NULL) /* return total number of lists allocated */
166     *nl = nextlist - listid;
167 gwlarson 3.1 return(listid); /* all done -- return list id */
168     }
169    
170    
171     int
172 gwlarson 3.3 rgl_octlist(fname, cent, radp, nl) /* load scen into display list */
173 gwlarson 3.1 char *fname;
174     FVECT cent; /* returned octree center (optional) */
175 gwlarson 3.2 FLOAT *radp; /* returned octree size (optional) */
176 gwlarson 3.3 int *nl; /* returned number of lists (optional) */
177 gwlarson 3.1 {
178     double r;
179     int listid;
180     /* modeled after rgl_filelist() */
181     initotypes();
182     /* check the octree and get its size */
183     r = checkoct(fname, cent);
184     if (radp != NULL) *radp = r;
185     /* start the display list */
186     listid = newglist();
187     glNewList(listid, GL_COMPILE);
188     lightinit(); /* start light source list */
189     loadoct(fname); /* load octree objects into display list */
190     surfclean(); /* clean up and close top list */
191     lightclean(); /* clean up light sources also */
192     glEndList(); /* close top list */
193     lightdefs(); /* define light sources */
194     loadoctrees(); /* load referenced octrees into sublists */
195 gwlarson 3.3 if (nl != NULL) /* return total number of lists allocated */
196     *nl = nextlist - listid;
197 gwlarson 3.1 return(listid);
198     }
199    
200    
201 greg 3.4 void
202 gwlarson 3.1 rgl_load(inpspec) /* convert scene description into OGL calls */
203     char *inpspec;
204     {
205     FILE *popen();
206     char *fgetline();
207     FILE *infp;
208     char buf[1024];
209     register int c;
210    
211     if (inpspec == NULL) {
212     infp = stdin;
213     inpspec = "standard input";
214     } else if (inpspec[0] == '!') {
215     if ((infp = popen(inpspec+1, "r")) == NULL) {
216     sprintf(errmsg, "cannot execute \"%s\"", inpspec);
217     error(SYSTEM, errmsg);
218     }
219     } else if ((infp = fopen(inpspec, "r")) == NULL) {
220     sprintf(errmsg, "cannot open scene file \"%s\"", inpspec);
221     error(SYSTEM, errmsg);
222     }
223     while ((c = getc(infp)) != EOF) {
224     if (isspace(c))
225     continue;
226     if (c == '#') { /* comment */
227     fgets(buf, sizeof(buf), infp);
228     } else if (c == '!') { /* command */
229     ungetc(c, infp);
230     fgetline(buf, sizeof(buf), infp);
231     rgl_load(buf);
232     } else { /* object */
233     ungetc(c, infp);
234     rgl_object(inpspec, infp);
235     }
236     }
237     if (inpspec[0] == '!')
238     pclose(infp);
239     else
240     fclose(infp);
241     }
242    
243    
244 greg 3.4 void
245 gwlarson 3.1 rgl_object(name, fp) /* read the next object */
246     char *name;
247     FILE *fp;
248     {
249     static OBJREC ob;
250     char sbuf[MAXSTR];
251     int rval;
252     /* get modifier */
253     strcpy(sbuf, "EOF");
254     fgetword(sbuf, MAXSTR, fp);
255     ob.omod = 0; /* use ob.os for pointer to material */
256     if (!strcmp(sbuf, VOIDID))
257     ob.os = NULL;
258     else
259     ob.os = (char *)getmatp(sbuf);
260     /* get type */
261     strcpy(sbuf, "EOF");
262     fgetword(sbuf, MAXSTR, fp);
263     if (!strcmp(sbuf, ALIASID))
264     ob.otype = -1;
265     else if ((ob.otype = otype(sbuf)) < 0) {
266     sprintf(errmsg, "(%s): unknown type \"%s\"", name, sbuf);
267     error(USER, errmsg);
268     }
269     /* get identifier */
270     sbuf[0] = '\0';
271     fgetword(sbuf, MAXSTR, fp);
272     ob.oname = sbuf;
273     /* get arguments */
274     if (ob.otype == -1) {
275     char sbuf2[MAXSTR]; /* get alias */
276     strcpy(sbuf2, "EOF");
277     fgetword(sbuf2, MAXSTR, fp);
278     if (ob.os == NULL)
279     ob.os = (char *)getmatp(sbuf2);
280     o_default(&ob); /* fake reference */
281     return;
282     }
283     if ((rval = readfargs(&ob.oargs, fp)) == 0) {
284     sprintf(errmsg, "(%s): bad arguments", name);
285     objerror(&ob, USER, errmsg);
286     } else if (rval < 0) {
287     sprintf(errmsg, "(%s): error reading scene", name);
288     error(SYSTEM, errmsg);
289     }
290     /* execute */
291     (*ofun[ob.otype].funp)(&ob);
292     /* free arguments */
293     freefargs(&ob.oargs);
294     }