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