ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/rglfile.c
Revision: 3.1
Committed: Tue Jun 9 11:18:35 1998 UTC (25 years, 10 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Log Message:
Initial revision

File Contents

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