1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id$"; |
3 |
#endif |
4 |
/* |
5 |
* Load Radiance object(s) and create OpenGL display lists |
6 |
*/ |
7 |
|
8 |
#include "copyright.h" |
9 |
|
10 |
#include "radogl.h" |
11 |
|
12 |
#ifndef NLIST2ALLOC |
13 |
#define NLIST2ALLOC 16 /* batch of display lists to get */ |
14 |
#endif |
15 |
|
16 |
FUN ofun[NUMOTYPE] = INIT_OTYPE; |
17 |
|
18 |
static int nextlist, nlistleft = 0; |
19 |
|
20 |
|
21 |
static void |
22 |
initotypes() /* initialize ofun array */ |
23 |
{ |
24 |
if (ofun[OBJ_SPHERE].funp == o_sphere) |
25 |
return; /* already done */ |
26 |
/* assign surface types */ |
27 |
ofun[OBJ_SPHERE].funp = |
28 |
ofun[OBJ_BUBBLE].funp = o_sphere; |
29 |
ofun[OBJ_FACE].funp = o_face; |
30 |
ofun[OBJ_CONE].funp = |
31 |
ofun[OBJ_CUP].funp = |
32 |
ofun[OBJ_CYLINDER].funp = |
33 |
ofun[OBJ_TUBE].funp = o_cone; |
34 |
ofun[OBJ_RING].funp = o_ring; |
35 |
ofun[OBJ_SOURCE].funp = o_source; |
36 |
ofun[OBJ_INSTANCE].funp = o_instance; |
37 |
ofun[OBJ_MESH].funp = o_unsupported; |
38 |
/* assign material types */ |
39 |
ofun[MAT_TRANS].funp = |
40 |
ofun[MAT_PLASTIC].funp = |
41 |
ofun[MAT_METAL].funp = m_normal; |
42 |
ofun[MAT_GLASS].funp = |
43 |
ofun[MAT_DIELECTRIC].funp = |
44 |
ofun[MAT_INTERFACE].funp = m_glass; |
45 |
ofun[MAT_PLASTIC2].funp = |
46 |
ofun[MAT_METAL2].funp = |
47 |
ofun[MAT_TRANS2].funp = m_aniso; |
48 |
ofun[MAT_TDATA].funp = |
49 |
ofun[MAT_PDATA].funp = |
50 |
ofun[MAT_MDATA].funp = |
51 |
ofun[MAT_TFUNC].funp = |
52 |
ofun[MAT_PFUNC].funp = |
53 |
ofun[MAT_MFUNC].funp = m_brdf; |
54 |
ofun[MAT_BRTDF].funp = m_brdf2; |
55 |
ofun[MAT_GLOW].funp = |
56 |
ofun[MAT_LIGHT].funp = |
57 |
ofun[MAT_SPOT].funp = |
58 |
ofun[MAT_ILLUM].funp = m_light; |
59 |
ofun[MAT_MIRROR].funp = m_mirror; |
60 |
ofun[MAT_DIRECT1].funp = |
61 |
ofun[MAT_DIRECT2].funp = m_prism; |
62 |
} |
63 |
|
64 |
|
65 |
int |
66 |
newglist() /* allocate an OGL list id */ |
67 |
{ |
68 |
if (!nlistleft--) { |
69 |
nextlist = glGenLists(NLIST2ALLOC); |
70 |
if (!nextlist) |
71 |
error(SYSTEM, "no list space left in newglist"); |
72 |
nlistleft = NLIST2ALLOC-1; |
73 |
} |
74 |
return(nextlist++); |
75 |
} |
76 |
|
77 |
|
78 |
void |
79 |
rgl_checkerr(where) /* check for GL or GLU error */ |
80 |
char *where; |
81 |
{ |
82 |
register GLenum errcode; |
83 |
|
84 |
while ((errcode = glGetError()) != GL_NO_ERROR) { |
85 |
sprintf(errmsg, "OpenGL error %s: %s", |
86 |
where, gluErrorString(errcode)); |
87 |
error(WARNING, errmsg); |
88 |
} |
89 |
} |
90 |
|
91 |
|
92 |
int |
93 |
rgl_filelist(ic, inp, nl) /* load scene files into display list */ |
94 |
int ic; |
95 |
char **inp; |
96 |
int *nl; /* returned number of lists (optional) */ |
97 |
{ |
98 |
int listid; |
99 |
|
100 |
initotypes(); /* prepare */ |
101 |
listid = newglist(); |
102 |
glNewList(listid, GL_COMPILE); |
103 |
lightinit(); /* start light source list */ |
104 |
while (ic--) /* load each file */ |
105 |
rgl_load(*inp++); |
106 |
surfclean(); /* clean up first pass */ |
107 |
lightclean(); /* clean up light sources also */ |
108 |
glEndList(); /* end of top display list */ |
109 |
lightdefs(); /* define light sources */ |
110 |
loadoctrees(); /* load octrees (sublists) for instances */ |
111 |
if (nl != NULL) /* return total number of lists allocated */ |
112 |
*nl = nextlist - listid; |
113 |
return(listid); /* all done -- return list id */ |
114 |
} |
115 |
|
116 |
|
117 |
int |
118 |
rgl_octlist(fname, cent, radp, nl) /* load scen into display list */ |
119 |
char *fname; |
120 |
FVECT cent; /* returned octree center (optional) */ |
121 |
FLOAT *radp; /* returned octree size (optional) */ |
122 |
int *nl; /* returned number of lists (optional) */ |
123 |
{ |
124 |
double r; |
125 |
int listid; |
126 |
/* modeled after rgl_filelist() */ |
127 |
initotypes(); |
128 |
/* check the octree and get its size */ |
129 |
r = checkoct(fname, cent); |
130 |
if (radp != NULL) *radp = r; |
131 |
/* start the display list */ |
132 |
listid = newglist(); |
133 |
glNewList(listid, GL_COMPILE); |
134 |
lightinit(); /* start light source list */ |
135 |
loadoct(fname); /* load octree objects into display list */ |
136 |
surfclean(); /* clean up and close top list */ |
137 |
lightclean(); /* clean up light sources also */ |
138 |
glEndList(); /* close top list */ |
139 |
lightdefs(); /* define light sources */ |
140 |
loadoctrees(); /* load referenced octrees into sublists */ |
141 |
if (nl != NULL) /* return total number of lists allocated */ |
142 |
*nl = nextlist - listid; |
143 |
return(listid); |
144 |
} |
145 |
|
146 |
|
147 |
void |
148 |
rgl_load(inpspec) /* convert scene description into OGL calls */ |
149 |
char *inpspec; |
150 |
{ |
151 |
FILE *popen(); |
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 |
void |
191 |
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 |
if (!strcmp(sbuf, VOIDID) || !strcmp(sbuf, ALIASMOD)) |
203 |
ob.os = NULL; |
204 |
else |
205 |
ob.os = (char *)getmatp(sbuf); |
206 |
/* get type */ |
207 |
strcpy(sbuf, "EOF"); |
208 |
fgetword(sbuf, MAXSTR, fp); |
209 |
if ((ob.otype = otype(sbuf)) < 0) { |
210 |
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 |
if (ob.otype == MOD_ALIAS) { |
219 |
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 |
} |