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