ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/rglsurf.c
Revision: 3.3
Committed: Fri Dec 11 13:53:32 1998 UTC (25 years, 4 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.2: +24 -4 lines
Log Message:
added combine callback for polygon tessellator

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 * Convert Radiance -> OpenGL surfaces.
9 */
10
11 #include "radogl.h"
12
13 #ifndef NSLICES
14 #define NSLICES 18 /* number of quadric slices */
15 #endif
16 #ifndef NSTACKS
17 #define NSTACKS 10 /* number of quadric stacks */
18 #endif
19
20 MATREC *curmat = NULL; /* current material */
21
22 static int curpolysize = 0; /* outputting triangles/quads */
23
24 static GLUquadricObj *gluqo; /* shared quadric object */
25 static GLUtesselator *gluto; /* shared tessallation object */
26
27 static char *glu_rout = "unk"; /* active GLU routine */
28
29 #define NOPOLY() if (curpolysize) {glEnd(); curpolysize = 0;} else
30
31
32 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 static
84 glu_error(en) /* report an error as a warning */
85 GLenum en;
86 {
87 sprintf(errmsg, "GLU error %s: %s", glu_rout, gluErrorString(en));
88 error(WARNING, errmsg);
89 }
90
91
92 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 static
110 newtess() /* allocate GLU tessellation object */
111 {
112 if ((gluto = gluNewTess()) == NULL)
113 error(INTERNAL, "gluNewTess failed");
114 gluTessCallback(gluto, GLU_TESS_BEGIN, glBegin);
115 gluTessCallback(gluto, GLU_TESS_VERTEX, glVertex3dv);
116 gluTessCallback(gluto, GLU_TESS_END, glEnd);
117 gluTessCallback(gluto, GLU_TESS_COMBINE, myCombine);
118 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 o_face(o) /* convert a face */
134 register OBJREC *o;
135 {
136 double area;
137 FVECT norm, cent;
138 register int i;
139
140 if (o->oargs.nfargs < 9 | o->oargs.nfargs % 3)
141 objerror(o, USER, "bad # real arguments");
142 area = polyarea(cent, norm, o->oargs.nfargs/3, (FVECT *)o->oargs.farg);
143 if (area <= FTINY)
144 return;
145 if (dolights) /* check for source */
146 doflatsrc(o->os, cent, norm, area);
147 setmaterial(o->os, cent, 1); /* set material */
148 if (o->oargs.nfargs/3 != curpolysize) {
149 if (curpolysize) glEnd();
150 curpolysize = o->oargs.nfargs/3;
151 if (curpolysize == 3)
152 glBegin(GL_TRIANGLES);
153 else if (curpolysize == 4)
154 glBegin(GL_QUADS);
155 }
156 glNormal3d((GLdouble)norm[0], (GLdouble)norm[1], (GLdouble)norm[2]);
157 if (curpolysize > 4) {
158 if (gluto == NULL) newtess();
159 glu_rout = "tessellating polygon";
160 gluTessNormal(gluto, (GLdouble)norm[0],
161 (GLdouble)norm[1], (GLdouble)norm[2]);
162 gluTessBeginPolygon(gluto, NULL);
163 gluTessBeginContour(gluto);
164 #ifdef SMLFLT
165 error(INTERNAL, "bad code segment in o_face");
166 #endif
167 for (i = 0; i < curpolysize; i++)
168 gluTessVertex(gluto, (GLdouble *)(o->oargs.farg+3*i),
169 (void *)(o->oargs.farg+3*i));
170 gluTessEndContour(gluto);
171 gluTessEndPolygon(gluto);
172 curpolysize = 0;
173 } else {
174 for (i = 0; i < curpolysize; i++)
175 glVertex3d((GLdouble)o->oargs.farg[3*i],
176 (GLdouble)o->oargs.farg[3*i+1],
177 (GLdouble)o->oargs.farg[3*i+2]);
178 }
179 }
180
181
182 surfclean() /* clean up surface routines */
183 {
184 setmaterial(NULL, NULL, 0);
185 if (gluqo != NULL) {
186 gluDeleteQuadric(gluqo);
187 gluqo = NULL;
188 }
189 if (gluto != NULL) {
190 gluDeleteTess(gluto);
191 gluto = NULL;
192 }
193 rgl_checkerr("in surfclean");
194 }
195
196
197 o_sphere(o) /* convert a sphere */
198 register OBJREC *o;
199 {
200 /* check arguments */
201 if (o->oargs.nfargs != 4)
202 objerror(o, USER, "bad # real arguments");
203 if (o->oargs.farg[3] < -FTINY) {
204 o->otype = o->otype==OBJ_SPHERE ? OBJ_BUBBLE : OBJ_SPHERE;
205 o->oargs.farg[3] = -o->oargs.farg[3];
206 } else if (o->oargs.farg[3] <= FTINY)
207 return;
208 if (dolights)
209 dosphsrc(o->os, o->oargs.farg,
210 PI*o->oargs.farg[3]*o->oargs.farg[3]);
211 setmaterial(o->os, o->oargs.farg, 0);
212 if (gluqo == NULL) newquadric();
213 glu_rout = "making sphere";
214 gluQuadricOrientation(gluqo,
215 o->otype==OBJ_BUBBLE ? GLU_INSIDE : GLU_OUTSIDE);
216 gluQuadricNormals(gluqo, GLU_SMOOTH);
217 glMatrixMode(GL_MODELVIEW);
218 glPushMatrix();
219 glTranslated((GLdouble)o->oargs.farg[0], (GLdouble)o->oargs.farg[1],
220 (GLdouble)o->oargs.farg[2]);
221 gluSphere(gluqo, (GLdouble)o->oargs.farg[3], NSLICES, NSTACKS);
222 glPopMatrix();
223 }
224
225
226 o_cone(o) /* convert a cone or cylinder */
227 register OBJREC *o;
228 {
229 double x1, y1, h, d;
230 FVECT cent;
231 register int iscyl;
232
233 iscyl = o->otype==OBJ_CYLINDER | o->otype==OBJ_TUBE;
234 if (o->oargs.nfargs != (iscyl ? 7 : 8))
235 objerror(o, "bad # real arguments");
236 if (o->oargs.farg[6] < -FTINY) {
237 o->oargs.farg[6] = -o->oargs.farg[6];
238 if (iscyl)
239 o->otype = o->otype==OBJ_CYLINDER ?
240 OBJ_TUBE : OBJ_CYLINDER;
241 else {
242 if ((o->oargs.farg[7] = -o->oargs.farg[7]) < -FTINY)
243 objerror(o, USER, "illegal radii");
244 o->otype = o->otype==OBJ_CONE ? OBJ_CUP : OBJ_CONE;
245 }
246 } else if (!iscyl && o->oargs.farg[7] < -FTINY)
247 objerror(o, USER, "illegal radii");
248 if (o->oargs.farg[6] <= FTINY && (iscyl || o->oargs.farg[7] <= FTINY))
249 return;
250 if (!iscyl) {
251 if (o->oargs.farg[6] < 0.) /* complains for tiny neg's */
252 o->oargs.farg[6] = 0.;
253 if (o->oargs.farg[7] < 0.)
254 o->oargs.farg[7] = 0.;
255 }
256 cent[0] = .5*(o->oargs.farg[0] + o->oargs.farg[3]);
257 cent[1] = .5*(o->oargs.farg[1] + o->oargs.farg[4]);
258 cent[2] = .5*(o->oargs.farg[2] + o->oargs.farg[5]);
259 setmaterial(o->os, cent, 0);
260 if (gluqo == NULL) newquadric();
261 glu_rout = "making cylinder";
262 gluQuadricOrientation(gluqo, o->otype==OBJ_CUP | o->otype==OBJ_TUBE ?
263 GLU_INSIDE : GLU_OUTSIDE);
264 gluQuadricNormals(gluqo, GLU_SMOOTH);
265 glMatrixMode(GL_MODELVIEW);
266 glPushMatrix();
267 /* do base translation */
268 glTranslated((GLdouble)o->oargs.farg[0], (GLdouble)o->oargs.farg[1],
269 (GLdouble)o->oargs.farg[2]);
270 /* compute height & rotation angle */
271 h = sqrt(dist2(o->oargs.farg,o->oargs.farg+3));
272 if (h <= FTINY)
273 return;
274 x1 = o->oargs.farg[1] - o->oargs.farg[4];
275 y1 = o->oargs.farg[3] - o->oargs.farg[0];
276 /* z1 = 0; */
277 d = 180./PI * asin(sqrt(x1*x1 + y1*y1) / h);
278 if (o->oargs.farg[5] < o->oargs.farg[2])
279 d = 180. - d;
280 if (d > FTINY)
281 glRotated(d, (GLdouble)x1, (GLdouble)y1, 0.);
282 gluCylinder(gluqo, o->oargs.farg[6], o->oargs.farg[iscyl ? 6 : 7],
283 h, NSLICES, 1);
284 glPopMatrix();
285 }
286
287
288 o_ring(o) /* convert a ring */
289 register OBJREC *o;
290 {
291 double x1, y1, d;
292
293 if (o->oargs.nfargs != 8)
294 objerror(o, "bad # real arguments");
295 if (o->oargs.farg[7] < o->oargs.farg[6]) {
296 register double d = o->oargs.farg[7];
297 o->oargs.farg[7] = o->oargs.farg[6];
298 o->oargs.farg[6] = d;
299 }
300 if (o->oargs.farg[6] < -FTINY)
301 objerror(o, USER, "negative radius");
302 if (o->oargs.farg[6] < 0.) /* complains for tiny neg's */
303 o->oargs.farg[6] = 0.;
304 if (o->oargs.farg[7] - o->oargs.farg[6] <= FTINY)
305 return;
306 if (dolights)
307 doflatsrc(o->os, o->oargs.farg, o->oargs.farg+3,
308 PI*(o->oargs.farg[7]*o->oargs.farg[7] -
309 o->oargs.farg[6]*o->oargs.farg[6]));
310 setmaterial(o->os, o->oargs.farg, 0);
311 if (gluqo == NULL) newquadric();
312 glu_rout = "making disk";
313 gluQuadricOrientation(gluqo, GLU_OUTSIDE);
314 gluQuadricNormals(gluqo, GLU_FLAT);
315 glMatrixMode(GL_MODELVIEW);
316 glPushMatrix();
317 glTranslated((GLdouble)o->oargs.farg[0], (GLdouble)o->oargs.farg[1],
318 (GLdouble)o->oargs.farg[2]);
319 /* compute rotation angle */
320 d = VLEN(o->oargs.farg+3);
321 if (d <= FTINY)
322 return;
323 x1 = -o->oargs.farg[4];
324 y1 = o->oargs.farg[3];
325 /* z1 = 0; */
326 d = 180./PI * asin(sqrt(x1*x1 + y1*y1) / d);
327 if (o->oargs.farg[5] < 0.)
328 d = 180. - d;
329 if (d > FTINY)
330 glRotated(d, (GLdouble)x1, (GLdouble)y1, 0.);
331 gluDisk(gluqo, o->oargs.farg[6], o->oargs.farg[7], NSLICES, 1);
332 glPopMatrix();
333 }