Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
render.c
Go to the documentation of this file.
1/*
2 * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
3 * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice including the dates of first publication and
13 * either this permission notice or a reference to
14 * http://oss.sgi.com/projects/FreeB/
15 * shall be included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
22 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Except as contained in this notice, the name of Silicon Graphics, Inc.
26 * shall not be used in advertising or otherwise to promote the sale, use or
27 * other dealings in this Software without prior written authorization from
28 * Silicon Graphics, Inc.
29 */
30/*
31** Author: Eric Veach, July 1994.
32**
33*/
34
35#include "gluos.h"
36#include <assert.h>
37#include <stddef.h>
38#include "mesh.h"
39#include "tess.h"
40#include "render.h"
41
42#ifndef TRUE
43#define TRUE 1
44#endif
45#ifndef FALSE
46#define FALSE 0
47#endif
48
49/* This structure remembers the information we need about a primitive
50 * to be able to render it later, once we have determined which
51 * primitive is able to use the most triangles.
52 */
53struct FaceCount {
54 long size; /* number of triangles used */
55 GLUhalfEdge *eStart; /* edge where this primitive starts */
56 void (*render)(GLUtesselator *, GLUhalfEdge *, long);
57 /* routine to render this primitive */
58};
59
60static struct FaceCount MaximumFan( GLUhalfEdge *eOrig );
61static struct FaceCount MaximumStrip( GLUhalfEdge *eOrig );
62
63static void RenderFan( GLUtesselator *tess, GLUhalfEdge *eStart, long size );
64static void RenderStrip( GLUtesselator *tess, GLUhalfEdge *eStart, long size );
66 long size );
67
68static void RenderMaximumFaceGroup( GLUtesselator *tess, GLUface *fOrig );
69static void RenderLonelyTriangles( GLUtesselator *tess, GLUface *head );
70
71
72
73/************************ Strips and Fans decomposition ******************/
74
75/* __gl_renderMesh( tess, mesh ) takes a mesh and breaks it into triangle
76 * fans, strips, and separate triangles. A substantial effort is made
77 * to use as few rendering primitives as possible (ie. to make the fans
78 * and strips as large as possible).
79 *
80 * The rendering output is provided as callbacks (see the api).
81 */
83{
84 GLUface *f;
85
86 /* Make a list of separate triangles so we can render them all at once */
87 tess->lonelyTriList = NULL;
88
89 for( f = mesh->fHead.next; f != &mesh->fHead; f = f->next ) {
90 f->marked = FALSE;
91 }
92 for( f = mesh->fHead.next; f != &mesh->fHead; f = f->next ) {
93
94 /* We examine all faces in an arbitrary order. Whenever we find
95 * an unprocessed face F, we output a group of faces including F
96 * whose size is maximum.
97 */
98 if( f->inside && ! f->marked ) {
100 assert( f->marked );
101 }
102 }
103 if( tess->lonelyTriList != NULL ) {
105 tess->lonelyTriList = NULL;
106 }
107}
108
109
110static void RenderMaximumFaceGroup( GLUtesselator *tess, GLUface *fOrig )
111{
112 /* We want to find the largest triangle fan or strip of unmarked faces
113 * which includes the given face fOrig. There are 3 possible fans
114 * passing through fOrig (one centered at each vertex), and 3 possible
115 * strips (one for each CCW permutation of the vertices). Our strategy
116 * is to try all of these, and take the primitive which uses the most
117 * triangles (a greedy approach).
118 */
119 GLUhalfEdge *e = fOrig->anEdge;
120 struct FaceCount max, newFace;
121
122 max.size = 1;
123 max.eStart = e;
124 max.render = &RenderTriangle;
125
126 if( ! tess->flagBoundary ) {
127 newFace = MaximumFan( e ); if( newFace.size > max.size ) { max = newFace; }
128 newFace = MaximumFan( e->Lnext ); if( newFace.size > max.size ) { max = newFace; }
129 newFace = MaximumFan( e->Lprev ); if( newFace.size > max.size ) { max = newFace; }
130
131 newFace = MaximumStrip( e ); if( newFace.size > max.size ) { max = newFace; }
132 newFace = MaximumStrip( e->Lnext ); if( newFace.size > max.size ) { max = newFace; }
133 newFace = MaximumStrip( e->Lprev ); if( newFace.size > max.size ) { max = newFace; }
134 }
135 (*(max.render))( tess, max.eStart, max.size );
136}
137
138
139/* Macros which keep track of faces we have marked temporarily, and allow
140 * us to backtrack when necessary. With triangle fans, this is not
141 * really necessary, since the only awkward case is a loop of triangles
142 * around a single origin vertex. However with strips the situation is
143 * more complicated, and we need a general tracking method like the
144 * one here.
145 */
146#define Marked(f) (! (f)->inside || (f)->marked)
147
148#define AddToTrail(f,t) ((f)->trail = (t), (t) = (f), (f)->marked = TRUE)
149
150#define FreeTrail(t) do { \
151 while( (t) != NULL ) { \
152 (t)->marked = FALSE; t = (t)->trail; \
153 } \
154 } while(0) /* absorb trailing semicolon */
155
156
157
158static struct FaceCount MaximumFan( GLUhalfEdge *eOrig )
159{
160 /* eOrig->Lface is the face we want to render. We want to find the size
161 * of a maximal fan around eOrig->Org. To do this we just walk around
162 * the origin vertex as far as possible in both directions.
163 */
164 struct FaceCount newFace = { 0, NULL, &RenderFan };
165 GLUface *trail = NULL;
166 GLUhalfEdge *e;
167
168 for( e = eOrig; ! Marked( e->Lface ); e = e->Onext ) {
169 AddToTrail( e->Lface, trail );
170 ++newFace.size;
171 }
172 for( e = eOrig; ! Marked( e->Rface ); e = e->Oprev ) {
173 AddToTrail( e->Rface, trail );
174 ++newFace.size;
175 }
176 newFace.eStart = e;
177 /*LINTED*/
178 FreeTrail( trail );
179 return newFace;
180}
181
182
183#define IsEven(n) (((n) & 1) == 0)
184
185static struct FaceCount MaximumStrip( GLUhalfEdge *eOrig )
186{
187 /* Here we are looking for a maximal strip that contains the vertices
188 * eOrig->Org, eOrig->Dst, eOrig->Lnext->Dst (in that order or the
189 * reverse, such that all triangles are oriented CCW).
190 *
191 * Again we walk forward and backward as far as possible. However for
192 * strips there is a twist: to get CCW orientations, there must be
193 * an *even* number of triangles in the strip on one side of eOrig.
194 * We walk the strip starting on a side with an even number of triangles;
195 * if both side have an odd number, we are forced to shorten one side.
196 */
197 struct FaceCount newFace = { 0, NULL, &RenderStrip };
198 long headSize = 0, tailSize = 0;
199 GLUface *trail = NULL;
200 GLUhalfEdge *e, *eTail, *eHead;
201
202 for( e = eOrig; ! Marked( e->Lface ); ++tailSize, e = e->Onext ) {
203 AddToTrail( e->Lface, trail );
204 ++tailSize;
205 e = e->Dprev;
206 if( Marked( e->Lface )) break;
207 AddToTrail( e->Lface, trail );
208 }
209 eTail = e;
210
211 for( e = eOrig; ! Marked( e->Rface ); ++headSize, e = e->Dnext ) {
212 AddToTrail( e->Rface, trail );
213 ++headSize;
214 e = e->Oprev;
215 if( Marked( e->Rface )) break;
216 AddToTrail( e->Rface, trail );
217 }
218 eHead = e;
219
220 newFace.size = tailSize + headSize;
221 if( IsEven( tailSize )) {
222 newFace.eStart = eTail->Sym;
223 } else if( IsEven( headSize )) {
224 newFace.eStart = eHead;
225 } else {
226 /* Both sides have odd length, we must shorten one of them. In fact,
227 * we must start from eHead to guarantee inclusion of eOrig->Lface.
228 */
229 --newFace.size;
230 newFace.eStart = eHead->Onext;
231 }
232 /*LINTED*/
233 FreeTrail( trail );
234 return newFace;
235}
236
237
238static void RenderTriangle( GLUtesselator *tess, GLUhalfEdge *e, long size )
239{
240 /* Just add the triangle to a triangle list, so we can render all
241 * the separate triangles at once.
242 */
243 (void) size;
244 assert( size == 1 );
245 AddToTrail( e->Lface, tess->lonelyTriList );
246}
247
248
250{
251 /* Now we render all the separate triangles which could not be
252 * grouped into a triangle fan or strip.
253 */
254 GLUhalfEdge *e;
255 int newState;
256 int edgeState = -1; /* force edge state output for first vertex */
257
259
260 for( ; f != NULL; f = f->trail ) {
261 /* Loop once for each edge (there will always be 3 edges) */
262
263 e = f->anEdge;
264 do {
265 if( tess->flagBoundary ) {
266 /* Set the "edge state" to TRUE just before we output the
267 * first vertex of each edge on the polygon boundary.
268 */
269 newState = ! e->Rface->inside;
270 if( edgeState != newState ) {
271 edgeState = newState;
273 }
274 }
275 CALL_VERTEX_OR_VERTEX_DATA( e->Org->data );
276
277 e = e->Lnext;
278 } while( e != f->anEdge );
279 }
281}
282
283
284static void RenderFan( GLUtesselator *tess, GLUhalfEdge *e, long size )
285{
286 /* Render as many CCW triangles as possible in a fan starting from
287 * edge "e". The fan *should* contain exactly "size" triangles
288 * (otherwise we've goofed up somewhere).
289 */
291 CALL_VERTEX_OR_VERTEX_DATA( e->Org->data );
292 CALL_VERTEX_OR_VERTEX_DATA( e->Dst->data );
293
294 while( ! Marked( e->Lface )) {
295 e->Lface->marked = TRUE;
296 --size;
297 e = e->Onext;
298 CALL_VERTEX_OR_VERTEX_DATA( e->Dst->data );
299 }
300
301 assert( size == 0 );
302 (void)size;
304}
305
306
307static void RenderStrip( GLUtesselator *tess, GLUhalfEdge *e, long size )
308{
309 /* Render as many CCW triangles as possible in a strip starting from
310 * edge "e". The strip *should* contain exactly "size" triangles
311 * (otherwise we've goofed up somewhere).
312 */
314 CALL_VERTEX_OR_VERTEX_DATA( e->Org->data );
315 CALL_VERTEX_OR_VERTEX_DATA( e->Dst->data );
316
317 while( ! Marked( e->Lface )) {
318 e->Lface->marked = TRUE;
319 --size;
320 e = e->Dprev;
321 CALL_VERTEX_OR_VERTEX_DATA( e->Org->data );
322 if( Marked( e->Lface )) break;
323
324 e->Lface->marked = TRUE;
325 --size;
326 e = e->Onext;
327 CALL_VERTEX_OR_VERTEX_DATA( e->Dst->data );
328 }
329
330 assert( size == 0 );
331 (void)size;
333}
334
335
336/************************ Boundary contour decomposition ******************/
337
338/* __gl_renderBoundary( tess, mesh ) takes a mesh, and outputs one
339 * contour for each face marked "inside". The rendering output is
340 * provided as callbacks (see the api).
341 */
343{
344 GLUface *f;
345 GLUhalfEdge *e;
346
347 for( f = mesh->fHead.next; f != &mesh->fHead; f = f->next ) {
348 if( f->inside ) {
350 e = f->anEdge;
351 do {
352 CALL_VERTEX_OR_VERTEX_DATA( e->Org->data );
353 e = e->Lnext;
354 } while( e != f->anEdge );
356 }
357 }
358}
359
360
361/************************ Quick-and-dirty decomposition ******************/
362
363#define SIGN_INCONSISTENT 2
364
365static int ComputeNormal( GLUtesselator *tess, GLdouble norm[3], int check )
366/*
367 * If check==FALSE, we compute the polygon normal and place it in norm[].
368 * If check==TRUE, we check that each triangle in the fan from v0 has a
369 * consistent orientation with respect to norm[]. If triangles are
370 * consistently oriented CCW, return 1; if CW, return -1; if all triangles
371 * are degenerate return 0; otherwise (no consistent orientation) return
372 * SIGN_INCONSISTENT.
373 */
374{
375 CachedVertex *v0 = tess->cache;
376 CachedVertex *vn = v0 + tess->cacheCount;
377 CachedVertex *vc;
378 GLdouble dot, xc, yc, zc, xp, yp, zp, n[3];
379 int sign = 0;
380
381 /* Find the polygon normal. It is important to get a reasonable
382 * normal even when the polygon is self-intersecting (eg. a bowtie).
383 * Otherwise, the computed normal could be very tiny, but perpendicular
384 * to the true plane of the polygon due to numerical noise. Then all
385 * the triangles would appear to be degenerate and we would incorrectly
386 * decompose the polygon as a fan (or simply not render it at all).
387 *
388 * We use a sum-of-triangles normal algorithm rather than the more
389 * efficient sum-of-trapezoids method (used in CheckOrientation()
390 * in normal.c). This lets us explicitly reverse the signed area
391 * of some triangles to get a reasonable normal in the self-intersecting
392 * case.
393 */
394 if( ! check ) {
395 norm[0] = norm[1] = norm[2] = 0.0;
396 }
397
398 vc = v0 + 1;
399 xc = vc->coords[0] - v0->coords[0];
400 yc = vc->coords[1] - v0->coords[1];
401 zc = vc->coords[2] - v0->coords[2];
402 while( ++vc < vn ) {
403 xp = xc; yp = yc; zp = zc;
404 xc = vc->coords[0] - v0->coords[0];
405 yc = vc->coords[1] - v0->coords[1];
406 zc = vc->coords[2] - v0->coords[2];
407
408 /* Compute (vp - v0) cross (vc - v0) */
409 n[0] = yp*zc - zp*yc;
410 n[1] = zp*xc - xp*zc;
411 n[2] = xp*yc - yp*xc;
412
413 dot = n[0]*norm[0] + n[1]*norm[1] + n[2]*norm[2];
414 if( ! check ) {
415 /* Reverse the contribution of back-facing triangles to get
416 * a reasonable normal for self-intersecting polygons (see above)
417 */
418 if( dot >= 0 ) {
419 norm[0] += n[0]; norm[1] += n[1]; norm[2] += n[2];
420 } else {
421 norm[0] -= n[0]; norm[1] -= n[1]; norm[2] -= n[2];
422 }
423 } else if( dot != 0 ) {
424 /* Check the new orientation for consistency with previous triangles */
425 if( dot > 0 ) {
426 if( sign < 0 ) return SIGN_INCONSISTENT;
427 sign = 1;
428 } else {
429 if( sign > 0 ) return SIGN_INCONSISTENT;
430 sign = -1;
431 }
432 }
433 }
434 return sign;
435}
436
437/* __gl_renderCache( tess ) takes a single contour and tries to render it
438 * as a triangle fan. This handles convex polygons, as well as some
439 * non-convex polygons if we get lucky.
440 *
441 * Returns TRUE if the polygon was successfully rendered. The rendering
442 * output is provided as callbacks (see the api).
443 */
445{
446 CachedVertex *v0 = tess->cache;
447 CachedVertex *vn = v0 + tess->cacheCount;
448 CachedVertex *vc;
449 GLdouble norm[3];
450 int sign;
451
452 if( tess->cacheCount < 3 ) {
453 /* Degenerate contour -- no output */
454 return TRUE;
455 }
456
457 norm[0] = tess->normal[0];
458 norm[1] = tess->normal[1];
459 norm[2] = tess->normal[2];
460 if( norm[0] == 0 && norm[1] == 0 && norm[2] == 0 ) {
461 ComputeNormal( tess, norm, FALSE );
462 }
463
464 sign = ComputeNormal( tess, norm, TRUE );
465 if( sign == SIGN_INCONSISTENT ) {
466 /* Fan triangles did not have a consistent orientation */
467 return FALSE;
468 }
469 if( sign == 0 ) {
470 /* All triangles were degenerate */
471 return TRUE;
472 }
473
474 /* Make sure we do the right thing for each winding rule */
475 switch( tess->windingRule ) {
478 break;
480 if( sign < 0 ) return TRUE;
481 break;
483 if( sign > 0 ) return TRUE;
484 break;
486 return TRUE;
487 }
488
490 : (tess->cacheCount > 3) ? GL_TRIANGLE_FAN
491 : GL_TRIANGLES );
492
494 if( sign > 0 ) {
495 for( vc = v0+1; vc < vn; ++vc ) {
497 }
498 } else {
499 for( vc = vn-1; vc > v0; --vc ) {
501 }
502 }
504 return TRUE;
505}
#define GLU_TESS_WINDING_NONZERO
Definition GL_glu.h:254
#define GLU_TESS_WINDING_POSITIVE
Definition GL_glu.h:255
#define GL_TRIANGLE_FAN
Definition GL_glu.h:289
#define GLU_TESS_WINDING_NEGATIVE
Definition GL_glu.h:256
double GLdouble
Definition GL_glu.h:279
#define GL_TRIANGLES
Definition GL_glu.h:287
unsigned char GLboolean
Definition GL_glu.h:267
#define GLU_TESS_WINDING_ODD
Definition GL_glu.h:253
#define GL_LINE_LOOP
Definition GL_glu.h:285
#define GL_TRIANGLE_STRIP
Definition GL_glu.h:288
#define GLU_TESS_WINDING_ABS_GEQ_TWO
Definition GL_glu.h:257
#define f(i)
Definition RSha256.hxx:104
#define e(i)
Definition RSha256.hxx:103
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
const Int_t n
Definition legend1.C:16
static struct FaceCount MaximumFan(GLUhalfEdge *eOrig)
Definition render.c:158
static void RenderStrip(GLUtesselator *tess, GLUhalfEdge *eStart, long size)
Definition render.c:307
#define SIGN_INCONSISTENT
Definition render.c:363
static void RenderMaximumFaceGroup(GLUtesselator *tess, GLUface *fOrig)
Definition render.c:110
static struct FaceCount MaximumStrip(GLUhalfEdge *eOrig)
Definition render.c:185
#define FreeTrail(t)
Definition render.c:150
GLboolean __gl_renderCache(GLUtesselator *tess)
Definition render.c:444
static void RenderLonelyTriangles(GLUtesselator *tess, GLUface *head)
Definition render.c:249
static void RenderTriangle(GLUtesselator *tess, GLUhalfEdge *eStart, long size)
Definition render.c:238
#define IsEven(n)
Definition render.c:183
#define TRUE
Definition render.c:43
#define FALSE
Definition render.c:46
void __gl_renderMesh(GLUtesselator *tess, GLUmesh *mesh)
Definition render.c:82
void __gl_renderBoundary(GLUtesselator *tess, GLUmesh *mesh)
Definition render.c:342
static void RenderFan(GLUtesselator *tess, GLUhalfEdge *eStart, long size)
Definition render.c:284
#define AddToTrail(f, t)
Definition render.c:148
static int ComputeNormal(GLUtesselator *tess, GLdouble norm[3], int check)
Definition render.c:365
#define Marked(f)
Definition render.c:146
void * data
Definition tess.h:56
GLdouble coords[3]
Definition tess.h:55
long size
Definition render.c:54
void(* render)(GLUtesselator *, GLUhalfEdge *, long)
Definition render.c:56
GLUhalfEdge * eStart
Definition render.c:55
GLboolean marked
Definition mesh.h:134
GLUhalfEdge * anEdge
Definition mesh.h:129
GLUface * next
Definition mesh.h:127
GLUhalfEdge * Onext
Definition mesh.h:141
GLUhalfEdge * Sym
Definition mesh.h:140
GLUface fHead
Definition mesh.h:170
GLboolean flagBoundary
Definition tess.h:92
int cacheCount
Definition tess.h:107
GLdouble normal[3]
Definition tess.h:73
GLboolean boundaryOnly
Definition tess.h:93
GLenum windingRule
Definition tess.h:80
GLUface * lonelyTriList
Definition tess.h:94
CachedVertex cache[100]
Definition tess.h:108
#define CALL_EDGE_FLAG_OR_EDGE_FLAG_DATA(a)
Definition tess.h:145
#define CALL_BEGIN_OR_BEGIN_DATA(a)
Definition tess.h:135
#define CALL_VERTEX_OR_VERTEX_DATA(a)
Definition tess.h:140
#define CALL_END_OR_END_DATA()
Definition tess.h:150