1 |
gwlarson |
3.1 |
#ifndef lint |
2 |
greg |
3.4 |
static const char RCSid[] = "$Id$"; |
3 |
gwlarson |
3.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* Convert Radiance -> OpenGL surfaces. |
6 |
|
|
*/ |
7 |
|
|
|
8 |
greg |
3.5 |
#include "copyright.h" |
9 |
greg |
3.4 |
|
10 |
gwlarson |
3.1 |
#include "radogl.h" |
11 |
|
|
|
12 |
|
|
#ifndef NSLICES |
13 |
|
|
#define NSLICES 18 /* number of quadric slices */ |
14 |
|
|
#endif |
15 |
|
|
#ifndef NSTACKS |
16 |
|
|
#define NSTACKS 10 /* number of quadric stacks */ |
17 |
|
|
#endif |
18 |
|
|
|
19 |
|
|
MATREC *curmat = NULL; /* current material */ |
20 |
|
|
|
21 |
|
|
static int curpolysize = 0; /* outputting triangles/quads */ |
22 |
|
|
|
23 |
|
|
static GLUquadricObj *gluqo; /* shared quadric object */ |
24 |
|
|
static GLUtesselator *gluto; /* shared tessallation object */ |
25 |
|
|
|
26 |
|
|
static char *glu_rout = "unk"; /* active GLU routine */ |
27 |
|
|
|
28 |
|
|
#define NOPOLY() if (curpolysize) {glEnd(); curpolysize = 0;} else |
29 |
|
|
|
30 |
|
|
|
31 |
greg |
3.4 |
void |
32 |
gwlarson |
3.1 |
setmaterial(mp, cent, ispoly) /* prepare for new material */ |
33 |
|
|
register MATREC *mp; |
34 |
|
|
FVECT cent; |
35 |
|
|
int ispoly; |
36 |
|
|
{ |
37 |
|
|
if (mp != curmat && domats) { |
38 |
|
|
NOPOLY(); |
39 |
|
|
domatobj(curmat = mp, cent); |
40 |
|
|
} else if (!ispoly) |
41 |
|
|
NOPOLY(); |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
|
45 |
|
|
double |
46 |
|
|
polyarea(cent, norm, n, v) /* compute polygon area & normal */ |
47 |
|
|
FVECT cent, norm; /* returned center and normal */ |
48 |
|
|
int n; /* number of vertices */ |
49 |
|
|
register FVECT v[]; /* vertex list */ |
50 |
|
|
{ |
51 |
|
|
FVECT v1, v2, v3; |
52 |
|
|
double d; |
53 |
|
|
register int i; |
54 |
|
|
|
55 |
|
|
norm[0] = norm[1] = norm[2] = 0.; |
56 |
|
|
v1[0] = v[1][0] - v[0][0]; |
57 |
|
|
v1[1] = v[1][1] - v[0][1]; |
58 |
|
|
v1[2] = v[1][2] - v[0][2]; |
59 |
|
|
for (i = 2; i < n; i++) { |
60 |
|
|
v2[0] = v[i][0] - v[0][0]; |
61 |
|
|
v2[1] = v[i][1] - v[0][1]; |
62 |
|
|
v2[2] = v[i][2] - v[0][2]; |
63 |
|
|
fcross(v3, v1, v2); |
64 |
|
|
norm[0] += v3[0]; |
65 |
|
|
norm[1] += v3[1]; |
66 |
|
|
norm[2] += v3[2]; |
67 |
|
|
VCOPY(v1, v2); |
68 |
|
|
} |
69 |
|
|
if (cent != NULL) { /* compute center also */ |
70 |
|
|
cent[0] = cent[1] = cent[2] = 0.; |
71 |
|
|
for (i = n; i--; ) { |
72 |
|
|
cent[0] += v[i][0]; |
73 |
|
|
cent[1] += v[i][1]; |
74 |
|
|
cent[2] += v[i][2]; |
75 |
|
|
} |
76 |
|
|
d = 1./n; |
77 |
|
|
cent[0] *= d; cent[1] *= d; cent[2] *= d; |
78 |
|
|
} |
79 |
|
|
return(normalize(norm)*.5); |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
|
83 |
greg |
3.4 |
static void |
84 |
gwlarson |
3.2 |
glu_error(en) /* report an error as a warning */ |
85 |
gwlarson |
3.1 |
GLenum en; |
86 |
|
|
{ |
87 |
|
|
sprintf(errmsg, "GLU error %s: %s", glu_rout, gluErrorString(en)); |
88 |
|
|
error(WARNING, errmsg); |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
|
92 |
gwlarson |
3.3 |
static void |
93 |
|
|
myCombine(coords, vertex_data, weight, dataOut) |
94 |
|
|
register GLdouble coords[3]; |
95 |
|
|
GLdouble *vertex_data[4]; |
96 |
|
|
GLfloat weight[4]; |
97 |
|
|
GLdouble **dataOut; |
98 |
|
|
{ |
99 |
|
|
register GLdouble *newvert; |
100 |
|
|
|
101 |
|
|
newvert = (GLdouble *)malloc(3*sizeof(GLdouble)); |
102 |
|
|
if (newvert == NULL) |
103 |
|
|
error(SYSTEM, "out of memory in myCombine"); |
104 |
|
|
VCOPY(newvert, coords); /* no data, just coordinates */ |
105 |
|
|
*dataOut = newvert; |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
|
109 |
gwlarson |
3.1 |
static |
110 |
|
|
newtess() /* allocate GLU tessellation object */ |
111 |
|
|
{ |
112 |
|
|
if ((gluto = gluNewTess()) == NULL) |
113 |
|
|
error(INTERNAL, "gluNewTess failed"); |
114 |
greg |
3.4 |
gluTessCallback(gluto, GLU_TESS_BEGIN, (_GLUfuncptr)glBegin); |
115 |
|
|
gluTessCallback(gluto, GLU_TESS_VERTEX, (_GLUfuncptr)glVertex3dv); |
116 |
gwlarson |
3.1 |
gluTessCallback(gluto, GLU_TESS_END, glEnd); |
117 |
gwlarson |
3.3 |
gluTessCallback(gluto, GLU_TESS_COMBINE, myCombine); |
118 |
gwlarson |
3.1 |
gluTessCallback(gluto, GLU_TESS_ERROR, glu_error); |
119 |
|
|
gluTessProperty(gluto, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_NONZERO); |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
|
123 |
|
|
static |
124 |
|
|
newquadric() /* allocate GLU quadric structure */ |
125 |
|
|
{ |
126 |
|
|
if ((gluqo = gluNewQuadric()) == NULL) |
127 |
|
|
error(INTERNAL, "gluNewQuadric failed"); |
128 |
|
|
gluQuadricDrawStyle(gluqo, GLU_FILL); |
129 |
|
|
gluQuadricCallback(gluqo, GLU_ERROR, glu_error); |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
|
133 |
greg |
3.4 |
int |
134 |
gwlarson |
3.1 |
o_face(o) /* convert a face */ |
135 |
|
|
register OBJREC *o; |
136 |
|
|
{ |
137 |
|
|
double area; |
138 |
|
|
FVECT norm, cent; |
139 |
|
|
register int i; |
140 |
|
|
|
141 |
|
|
if (o->oargs.nfargs < 9 | o->oargs.nfargs % 3) |
142 |
|
|
objerror(o, USER, "bad # real arguments"); |
143 |
|
|
area = polyarea(cent, norm, o->oargs.nfargs/3, (FVECT *)o->oargs.farg); |
144 |
|
|
if (area <= FTINY) |
145 |
|
|
return; |
146 |
|
|
if (dolights) /* check for source */ |
147 |
greg |
3.4 |
doflatsrc((MATREC *)o->os, cent, norm, area); |
148 |
|
|
setmaterial((MATREC *)o->os, cent, 1); /* set material */ |
149 |
gwlarson |
3.1 |
if (o->oargs.nfargs/3 != curpolysize) { |
150 |
|
|
if (curpolysize) glEnd(); |
151 |
|
|
curpolysize = o->oargs.nfargs/3; |
152 |
|
|
if (curpolysize == 3) |
153 |
|
|
glBegin(GL_TRIANGLES); |
154 |
|
|
else if (curpolysize == 4) |
155 |
|
|
glBegin(GL_QUADS); |
156 |
|
|
} |
157 |
|
|
glNormal3d((GLdouble)norm[0], (GLdouble)norm[1], (GLdouble)norm[2]); |
158 |
|
|
if (curpolysize > 4) { |
159 |
|
|
if (gluto == NULL) newtess(); |
160 |
|
|
glu_rout = "tessellating polygon"; |
161 |
|
|
gluTessNormal(gluto, (GLdouble)norm[0], |
162 |
|
|
(GLdouble)norm[1], (GLdouble)norm[2]); |
163 |
|
|
gluTessBeginPolygon(gluto, NULL); |
164 |
|
|
gluTessBeginContour(gluto); |
165 |
|
|
#ifdef SMLFLT |
166 |
|
|
error(INTERNAL, "bad code segment in o_face"); |
167 |
|
|
#endif |
168 |
|
|
for (i = 0; i < curpolysize; i++) |
169 |
|
|
gluTessVertex(gluto, (GLdouble *)(o->oargs.farg+3*i), |
170 |
|
|
(void *)(o->oargs.farg+3*i)); |
171 |
|
|
gluTessEndContour(gluto); |
172 |
|
|
gluTessEndPolygon(gluto); |
173 |
|
|
curpolysize = 0; |
174 |
|
|
} else { |
175 |
|
|
for (i = 0; i < curpolysize; i++) |
176 |
|
|
glVertex3d((GLdouble)o->oargs.farg[3*i], |
177 |
|
|
(GLdouble)o->oargs.farg[3*i+1], |
178 |
|
|
(GLdouble)o->oargs.farg[3*i+2]); |
179 |
|
|
} |
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
|
183 |
greg |
3.4 |
void |
184 |
gwlarson |
3.1 |
surfclean() /* clean up surface routines */ |
185 |
|
|
{ |
186 |
|
|
setmaterial(NULL, NULL, 0); |
187 |
|
|
if (gluqo != NULL) { |
188 |
|
|
gluDeleteQuadric(gluqo); |
189 |
|
|
gluqo = NULL; |
190 |
|
|
} |
191 |
|
|
if (gluto != NULL) { |
192 |
|
|
gluDeleteTess(gluto); |
193 |
|
|
gluto = NULL; |
194 |
|
|
} |
195 |
|
|
rgl_checkerr("in surfclean"); |
196 |
|
|
} |
197 |
|
|
|
198 |
|
|
|
199 |
greg |
3.4 |
int |
200 |
gwlarson |
3.1 |
o_sphere(o) /* convert a sphere */ |
201 |
|
|
register OBJREC *o; |
202 |
|
|
{ |
203 |
|
|
/* check arguments */ |
204 |
|
|
if (o->oargs.nfargs != 4) |
205 |
|
|
objerror(o, USER, "bad # real arguments"); |
206 |
|
|
if (o->oargs.farg[3] < -FTINY) { |
207 |
|
|
o->otype = o->otype==OBJ_SPHERE ? OBJ_BUBBLE : OBJ_SPHERE; |
208 |
|
|
o->oargs.farg[3] = -o->oargs.farg[3]; |
209 |
|
|
} else if (o->oargs.farg[3] <= FTINY) |
210 |
|
|
return; |
211 |
|
|
if (dolights) |
212 |
greg |
3.4 |
dosphsrc((MATREC *)o->os, o->oargs.farg, |
213 |
gwlarson |
3.1 |
PI*o->oargs.farg[3]*o->oargs.farg[3]); |
214 |
greg |
3.4 |
setmaterial((MATREC *)o->os, o->oargs.farg, 0); |
215 |
gwlarson |
3.1 |
if (gluqo == NULL) newquadric(); |
216 |
|
|
glu_rout = "making sphere"; |
217 |
|
|
gluQuadricOrientation(gluqo, |
218 |
|
|
o->otype==OBJ_BUBBLE ? GLU_INSIDE : GLU_OUTSIDE); |
219 |
|
|
gluQuadricNormals(gluqo, GLU_SMOOTH); |
220 |
|
|
glMatrixMode(GL_MODELVIEW); |
221 |
|
|
glPushMatrix(); |
222 |
|
|
glTranslated((GLdouble)o->oargs.farg[0], (GLdouble)o->oargs.farg[1], |
223 |
|
|
(GLdouble)o->oargs.farg[2]); |
224 |
|
|
gluSphere(gluqo, (GLdouble)o->oargs.farg[3], NSLICES, NSTACKS); |
225 |
|
|
glPopMatrix(); |
226 |
|
|
} |
227 |
|
|
|
228 |
|
|
|
229 |
greg |
3.4 |
int |
230 |
gwlarson |
3.1 |
o_cone(o) /* convert a cone or cylinder */ |
231 |
|
|
register OBJREC *o; |
232 |
|
|
{ |
233 |
|
|
double x1, y1, h, d; |
234 |
|
|
FVECT cent; |
235 |
|
|
register int iscyl; |
236 |
|
|
|
237 |
|
|
iscyl = o->otype==OBJ_CYLINDER | o->otype==OBJ_TUBE; |
238 |
|
|
if (o->oargs.nfargs != (iscyl ? 7 : 8)) |
239 |
greg |
3.4 |
objerror(o, USER, "bad # real arguments"); |
240 |
gwlarson |
3.1 |
if (o->oargs.farg[6] < -FTINY) { |
241 |
|
|
o->oargs.farg[6] = -o->oargs.farg[6]; |
242 |
|
|
if (iscyl) |
243 |
|
|
o->otype = o->otype==OBJ_CYLINDER ? |
244 |
|
|
OBJ_TUBE : OBJ_CYLINDER; |
245 |
|
|
else { |
246 |
|
|
if ((o->oargs.farg[7] = -o->oargs.farg[7]) < -FTINY) |
247 |
|
|
objerror(o, USER, "illegal radii"); |
248 |
|
|
o->otype = o->otype==OBJ_CONE ? OBJ_CUP : OBJ_CONE; |
249 |
|
|
} |
250 |
|
|
} else if (!iscyl && o->oargs.farg[7] < -FTINY) |
251 |
|
|
objerror(o, USER, "illegal radii"); |
252 |
|
|
if (o->oargs.farg[6] <= FTINY && (iscyl || o->oargs.farg[7] <= FTINY)) |
253 |
|
|
return; |
254 |
gwlarson |
3.3 |
if (!iscyl) { |
255 |
|
|
if (o->oargs.farg[6] < 0.) /* complains for tiny neg's */ |
256 |
|
|
o->oargs.farg[6] = 0.; |
257 |
|
|
if (o->oargs.farg[7] < 0.) |
258 |
|
|
o->oargs.farg[7] = 0.; |
259 |
|
|
} |
260 |
gwlarson |
3.1 |
cent[0] = .5*(o->oargs.farg[0] + o->oargs.farg[3]); |
261 |
|
|
cent[1] = .5*(o->oargs.farg[1] + o->oargs.farg[4]); |
262 |
|
|
cent[2] = .5*(o->oargs.farg[2] + o->oargs.farg[5]); |
263 |
greg |
3.4 |
setmaterial((MATREC *)o->os, cent, 0); |
264 |
gwlarson |
3.1 |
if (gluqo == NULL) newquadric(); |
265 |
|
|
glu_rout = "making cylinder"; |
266 |
|
|
gluQuadricOrientation(gluqo, o->otype==OBJ_CUP | o->otype==OBJ_TUBE ? |
267 |
|
|
GLU_INSIDE : GLU_OUTSIDE); |
268 |
|
|
gluQuadricNormals(gluqo, GLU_SMOOTH); |
269 |
|
|
glMatrixMode(GL_MODELVIEW); |
270 |
|
|
glPushMatrix(); |
271 |
|
|
/* do base translation */ |
272 |
|
|
glTranslated((GLdouble)o->oargs.farg[0], (GLdouble)o->oargs.farg[1], |
273 |
|
|
(GLdouble)o->oargs.farg[2]); |
274 |
|
|
/* compute height & rotation angle */ |
275 |
|
|
h = sqrt(dist2(o->oargs.farg,o->oargs.farg+3)); |
276 |
|
|
if (h <= FTINY) |
277 |
|
|
return; |
278 |
|
|
x1 = o->oargs.farg[1] - o->oargs.farg[4]; |
279 |
|
|
y1 = o->oargs.farg[3] - o->oargs.farg[0]; |
280 |
|
|
/* z1 = 0; */ |
281 |
|
|
d = 180./PI * asin(sqrt(x1*x1 + y1*y1) / h); |
282 |
|
|
if (o->oargs.farg[5] < o->oargs.farg[2]) |
283 |
|
|
d = 180. - d; |
284 |
|
|
if (d > FTINY) |
285 |
|
|
glRotated(d, (GLdouble)x1, (GLdouble)y1, 0.); |
286 |
|
|
gluCylinder(gluqo, o->oargs.farg[6], o->oargs.farg[iscyl ? 6 : 7], |
287 |
|
|
h, NSLICES, 1); |
288 |
|
|
glPopMatrix(); |
289 |
|
|
} |
290 |
|
|
|
291 |
|
|
|
292 |
greg |
3.4 |
int |
293 |
gwlarson |
3.1 |
o_ring(o) /* convert a ring */ |
294 |
|
|
register OBJREC *o; |
295 |
|
|
{ |
296 |
|
|
double x1, y1, d; |
297 |
|
|
|
298 |
|
|
if (o->oargs.nfargs != 8) |
299 |
greg |
3.4 |
objerror(o, USER, "bad # real arguments"); |
300 |
gwlarson |
3.1 |
if (o->oargs.farg[7] < o->oargs.farg[6]) { |
301 |
|
|
register double d = o->oargs.farg[7]; |
302 |
|
|
o->oargs.farg[7] = o->oargs.farg[6]; |
303 |
|
|
o->oargs.farg[6] = d; |
304 |
|
|
} |
305 |
|
|
if (o->oargs.farg[6] < -FTINY) |
306 |
|
|
objerror(o, USER, "negative radius"); |
307 |
|
|
if (o->oargs.farg[6] < 0.) /* complains for tiny neg's */ |
308 |
|
|
o->oargs.farg[6] = 0.; |
309 |
|
|
if (o->oargs.farg[7] - o->oargs.farg[6] <= FTINY) |
310 |
|
|
return; |
311 |
|
|
if (dolights) |
312 |
greg |
3.4 |
doflatsrc((MATREC *)o->os, o->oargs.farg, o->oargs.farg+3, |
313 |
gwlarson |
3.1 |
PI*(o->oargs.farg[7]*o->oargs.farg[7] - |
314 |
|
|
o->oargs.farg[6]*o->oargs.farg[6])); |
315 |
greg |
3.4 |
setmaterial((MATREC *)o->os, o->oargs.farg, 0); |
316 |
gwlarson |
3.1 |
if (gluqo == NULL) newquadric(); |
317 |
|
|
glu_rout = "making disk"; |
318 |
|
|
gluQuadricOrientation(gluqo, GLU_OUTSIDE); |
319 |
|
|
gluQuadricNormals(gluqo, GLU_FLAT); |
320 |
|
|
glMatrixMode(GL_MODELVIEW); |
321 |
|
|
glPushMatrix(); |
322 |
|
|
glTranslated((GLdouble)o->oargs.farg[0], (GLdouble)o->oargs.farg[1], |
323 |
|
|
(GLdouble)o->oargs.farg[2]); |
324 |
|
|
/* compute rotation angle */ |
325 |
|
|
d = VLEN(o->oargs.farg+3); |
326 |
|
|
if (d <= FTINY) |
327 |
|
|
return; |
328 |
|
|
x1 = -o->oargs.farg[4]; |
329 |
|
|
y1 = o->oargs.farg[3]; |
330 |
|
|
/* z1 = 0; */ |
331 |
|
|
d = 180./PI * asin(sqrt(x1*x1 + y1*y1) / d); |
332 |
|
|
if (o->oargs.farg[5] < 0.) |
333 |
|
|
d = 180. - d; |
334 |
|
|
if (d > FTINY) |
335 |
|
|
glRotated(d, (GLdouble)x1, (GLdouble)y1, 0.); |
336 |
|
|
gluDisk(gluqo, o->oargs.farg[6], o->oargs.farg[7], NSLICES, 1); |
337 |
|
|
glPopMatrix(); |
338 |
|
|
} |