ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/rglfile.c
Revision: 3.9
Committed: Mon Oct 27 10:19:31 2003 UTC (20 years, 6 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 3.8: +2 -2 lines
Log Message:
Added gethomedir.c and various compatibility fixes.

File Contents

# User Rev Content
1 gwlarson 3.1 #ifndef lint
2 schorsch 3.9 static const char RCSid[] = "$Id: rglfile.c,v 3.8 2003/06/26 00:58:09 schorsch Exp $";
3 gwlarson 3.1 #endif
4     /*
5     * Load Radiance object(s) and create OpenGL display lists
6     */
7    
8 greg 3.5 #include "copyright.h"
9 greg 3.4
10 schorsch 3.9 #include "rtprocess.h"
11 gwlarson 3.1 #include "radogl.h"
12    
13     #ifndef NLIST2ALLOC
14     #define NLIST2ALLOC 16 /* batch of display lists to get */
15     #endif
16    
17     FUN ofun[NUMOTYPE] = INIT_OTYPE;
18    
19 gwlarson 3.3 static int nextlist, nlistleft = 0;
20 gwlarson 3.1
21 gwlarson 3.3
22 greg 3.4 static void
23 gwlarson 3.1 initotypes() /* initialize ofun array */
24     {
25     if (ofun[OBJ_SPHERE].funp == o_sphere)
26     return; /* already done */
27     /* assign surface types */
28     ofun[OBJ_SPHERE].funp =
29     ofun[OBJ_BUBBLE].funp = o_sphere;
30     ofun[OBJ_FACE].funp = o_face;
31     ofun[OBJ_CONE].funp =
32     ofun[OBJ_CUP].funp =
33     ofun[OBJ_CYLINDER].funp =
34     ofun[OBJ_TUBE].funp = o_cone;
35     ofun[OBJ_RING].funp = o_ring;
36     ofun[OBJ_SOURCE].funp = o_source;
37     ofun[OBJ_INSTANCE].funp = o_instance;
38 greg 3.7 ofun[OBJ_MESH].funp = o_unsupported;
39 gwlarson 3.1 /* assign material types */
40     ofun[MAT_TRANS].funp =
41     ofun[MAT_PLASTIC].funp =
42     ofun[MAT_METAL].funp = m_normal;
43     ofun[MAT_GLASS].funp =
44     ofun[MAT_DIELECTRIC].funp =
45     ofun[MAT_INTERFACE].funp = m_glass;
46     ofun[MAT_PLASTIC2].funp =
47     ofun[MAT_METAL2].funp =
48     ofun[MAT_TRANS2].funp = m_aniso;
49     ofun[MAT_TDATA].funp =
50     ofun[MAT_PDATA].funp =
51     ofun[MAT_MDATA].funp =
52     ofun[MAT_TFUNC].funp =
53     ofun[MAT_PFUNC].funp =
54     ofun[MAT_MFUNC].funp = m_brdf;
55     ofun[MAT_BRTDF].funp = m_brdf2;
56     ofun[MAT_GLOW].funp =
57     ofun[MAT_LIGHT].funp =
58     ofun[MAT_SPOT].funp =
59     ofun[MAT_ILLUM].funp = m_light;
60     ofun[MAT_MIRROR].funp = m_mirror;
61     ofun[MAT_DIRECT1].funp =
62     ofun[MAT_DIRECT2].funp = m_prism;
63     }
64    
65    
66     int
67     newglist() /* allocate an OGL list id */
68     {
69 gwlarson 3.3 if (!nlistleft--) {
70 gwlarson 3.1 nextlist = glGenLists(NLIST2ALLOC);
71 gwlarson 3.3 if (!nextlist)
72     error(SYSTEM, "no list space left in newglist");
73     nlistleft = NLIST2ALLOC-1;
74 gwlarson 3.1 }
75     return(nextlist++);
76     }
77    
78    
79 greg 3.4 void
80 gwlarson 3.1 rgl_checkerr(where) /* check for GL or GLU error */
81     char *where;
82     {
83     register GLenum errcode;
84    
85     while ((errcode = glGetError()) != GL_NO_ERROR) {
86     sprintf(errmsg, "OpenGL error %s: %s",
87     where, gluErrorString(errcode));
88     error(WARNING, errmsg);
89     }
90     }
91    
92    
93     int
94 gwlarson 3.3 rgl_filelist(ic, inp, nl) /* load scene files into display list */
95 gwlarson 3.1 int ic;
96     char **inp;
97 gwlarson 3.3 int *nl; /* returned number of lists (optional) */
98 gwlarson 3.1 {
99     int listid;
100    
101     initotypes(); /* prepare */
102     listid = newglist();
103     glNewList(listid, GL_COMPILE);
104     lightinit(); /* start light source list */
105     while (ic--) /* load each file */
106     rgl_load(*inp++);
107     surfclean(); /* clean up first pass */
108     lightclean(); /* clean up light sources also */
109     glEndList(); /* end of top display list */
110     lightdefs(); /* define light sources */
111     loadoctrees(); /* load octrees (sublists) for instances */
112 gwlarson 3.3 if (nl != NULL) /* return total number of lists allocated */
113     *nl = nextlist - listid;
114 gwlarson 3.1 return(listid); /* all done -- return list id */
115     }
116    
117    
118     int
119 gwlarson 3.3 rgl_octlist(fname, cent, radp, nl) /* load scen into display list */
120 gwlarson 3.1 char *fname;
121     FVECT cent; /* returned octree center (optional) */
122 schorsch 3.8 RREAL *radp; /* returned octree size (optional) */
123 gwlarson 3.3 int *nl; /* returned number of lists (optional) */
124 gwlarson 3.1 {
125     double r;
126     int listid;
127     /* modeled after rgl_filelist() */
128     initotypes();
129     /* check the octree and get its size */
130     r = checkoct(fname, cent);
131     if (radp != NULL) *radp = r;
132     /* start the display list */
133     listid = newglist();
134     glNewList(listid, GL_COMPILE);
135     lightinit(); /* start light source list */
136     loadoct(fname); /* load octree objects into display list */
137     surfclean(); /* clean up and close top list */
138     lightclean(); /* clean up light sources also */
139     glEndList(); /* close top list */
140     lightdefs(); /* define light sources */
141     loadoctrees(); /* load referenced octrees into sublists */
142 gwlarson 3.3 if (nl != NULL) /* return total number of lists allocated */
143     *nl = nextlist - listid;
144 gwlarson 3.1 return(listid);
145     }
146    
147    
148 greg 3.4 void
149 gwlarson 3.1 rgl_load(inpspec) /* convert scene description into OGL calls */
150     char *inpspec;
151     {
152     char *fgetline();
153     FILE *infp;
154     char buf[1024];
155     register int c;
156    
157     if (inpspec == NULL) {
158     infp = stdin;
159     inpspec = "standard input";
160     } else if (inpspec[0] == '!') {
161     if ((infp = popen(inpspec+1, "r")) == NULL) {
162     sprintf(errmsg, "cannot execute \"%s\"", inpspec);
163     error(SYSTEM, errmsg);
164     }
165     } else if ((infp = fopen(inpspec, "r")) == NULL) {
166     sprintf(errmsg, "cannot open scene file \"%s\"", inpspec);
167     error(SYSTEM, errmsg);
168     }
169     while ((c = getc(infp)) != EOF) {
170     if (isspace(c))
171     continue;
172     if (c == '#') { /* comment */
173     fgets(buf, sizeof(buf), infp);
174     } else if (c == '!') { /* command */
175     ungetc(c, infp);
176     fgetline(buf, sizeof(buf), infp);
177     rgl_load(buf);
178     } else { /* object */
179     ungetc(c, infp);
180     rgl_object(inpspec, infp);
181     }
182     }
183     if (inpspec[0] == '!')
184     pclose(infp);
185     else
186     fclose(infp);
187     }
188    
189    
190 greg 3.4 void
191 gwlarson 3.1 rgl_object(name, fp) /* read the next object */
192     char *name;
193     FILE *fp;
194     {
195     static OBJREC ob;
196     char sbuf[MAXSTR];
197     int rval;
198     /* get modifier */
199     strcpy(sbuf, "EOF");
200     fgetword(sbuf, MAXSTR, fp);
201     ob.omod = 0; /* use ob.os for pointer to material */
202 greg 3.6 if (!strcmp(sbuf, VOIDID) || !strcmp(sbuf, ALIASMOD))
203 gwlarson 3.1 ob.os = NULL;
204     else
205     ob.os = (char *)getmatp(sbuf);
206     /* get type */
207     strcpy(sbuf, "EOF");
208     fgetword(sbuf, MAXSTR, fp);
209 greg 3.6 if ((ob.otype = otype(sbuf)) < 0) {
210 gwlarson 3.1 sprintf(errmsg, "(%s): unknown type \"%s\"", name, sbuf);
211     error(USER, errmsg);
212     }
213     /* get identifier */
214     sbuf[0] = '\0';
215     fgetword(sbuf, MAXSTR, fp);
216     ob.oname = sbuf;
217     /* get arguments */
218 greg 3.6 if (ob.otype == MOD_ALIAS) {
219 gwlarson 3.1 char sbuf2[MAXSTR]; /* get alias */
220     strcpy(sbuf2, "EOF");
221     fgetword(sbuf2, MAXSTR, fp);
222     if (ob.os == NULL)
223     ob.os = (char *)getmatp(sbuf2);
224     o_default(&ob); /* fake reference */
225     return;
226     }
227     if ((rval = readfargs(&ob.oargs, fp)) == 0) {
228     sprintf(errmsg, "(%s): bad arguments", name);
229     objerror(&ob, USER, errmsg);
230     } else if (rval < 0) {
231     sprintf(errmsg, "(%s): error reading scene", name);
232     error(SYSTEM, errmsg);
233     }
234     /* execute */
235     (*ofun[ob.otype].funp)(&ob);
236     /* free arguments */
237     freefargs(&ob.oargs);
238     }