source: trunk/src/opengl/glu/tess/render.c

Last change on this file was 2689, checked in by jeroen, 26 years ago

* empty log message *

File size: 15.9 KB
Line 
1/* $Id: render.c,v 1.1 2000-02-09 08:47:36 jeroen Exp $ */
2/*
3** License Applicability. Except to the extent portions of this file are
4** made subject to an alternative license as permitted in the SGI Free
5** Software License B, Version 1.0 (the "License"), the contents of this
6** file are subject only to the provisions of the License. You may not use
7** this file except in compliance with the License. You may obtain a copy
8** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
9** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
10**
11** http://oss.sgi.com/projects/FreeB
12**
13** Note that, as provided in the License, the Software is distributed on an
14** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
15** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
16** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
17** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
18**
19** Original Code. The Original Code is: OpenGL Sample Implementation,
20** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
21** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
22** Copyright in any portions created by third parties is as indicated
23** elsewhere herein. All Rights Reserved.
24**
25** Additional Notice Provisions: The application programming interfaces
26** established by SGI in conjunction with the Original Code are The
27** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
28** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
29** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
30** Window System(R) (Version 1.3), released October 19, 1998. This software
31** was created using the OpenGL(R) version 1.2.1 Sample Implementation
32** published by SGI, but has not been independently verified as being
33** compliant with the OpenGL(R) version 1.2.1 Specification.
34**
35*/
36/*
37** Author: Eric Veach, July 1994.
38**
39** $Date: 2000-02-09 08:47:36 $ $Revision: 1.1 $
40** $Header: /home/ktk/tmp/odin/2007/netlabs.cvs/odin32/src/opengl/glu/tess/render.c,v 1.1 2000-02-09 08:47:36 jeroen Exp $
41*/
42
43#include "gluos.h"
44#include <assert.h>
45#include <stddef.h>
46#include "mesh.h"
47#include "tess.h"
48#include "render.h"
49
50#define TRUE 1
51#define FALSE 0
52
53/* This structure remembers the information we need about a primitive
54 * to be able to render it later, once we have determined which
55 * primitive is able to use the most triangles.
56 */
57struct FaceCount {
58 long size; /* number of triangles used */
59 GLUhalfEdge *eStart; /* edge where this primitive starts */
60 void (*render)(GLUtesselator *, GLUhalfEdge *, long);
61 /* routine to render this primitive */
62};
63
64static struct FaceCount MaximumFan( GLUhalfEdge *eOrig );
65static struct FaceCount MaximumStrip( GLUhalfEdge *eOrig );
66
67static void RenderFan( GLUtesselator *tess, GLUhalfEdge *eStart, long size );
68static void RenderStrip( GLUtesselator *tess, GLUhalfEdge *eStart, long size );
69static void RenderTriangle( GLUtesselator *tess, GLUhalfEdge *eStart,
70 long size );
71
72static void RenderMaximumFaceGroup( GLUtesselator *tess, GLUface *fOrig );
73static void RenderLonelyTriangles( GLUtesselator *tess, GLUface *head );
74
75
76
77/************************ Strips and Fans decomposition ******************/
78
79/* __gl_renderMesh( tess, mesh ) takes a mesh and breaks it into triangle
80 * fans, strips, and separate triangles. A substantial effort is made
81 * to use as few rendering primitives as possible (ie. to make the fans
82 * and strips as large as possible).
83 *
84 * The rendering output is provided as callbacks (see the api).
85 */
86void __gl_renderMesh( GLUtesselator *tess, GLUmesh *mesh )
87{
88 GLUface *f;
89
90 /* Make a list of separate triangles so we can render them all at once */
91 tess->lonelyTriList = NULL;
92
93 for( f = mesh->fHead.next; f != &mesh->fHead; f = f->next ) {
94 f->marked = FALSE;
95 }
96 for( f = mesh->fHead.next; f != &mesh->fHead; f = f->next ) {
97
98 /* We examine all faces in an arbitrary order. Whenever we find
99 * an unprocessed face F, we output a group of faces including F
100 * whose size is maximum.
101 */
102 if( f->inside && ! f->marked ) {
103 RenderMaximumFaceGroup( tess, f );
104 assert( f->marked );
105 }
106 }
107 if( tess->lonelyTriList != NULL ) {
108 RenderLonelyTriangles( tess, tess->lonelyTriList );
109 tess->lonelyTriList = NULL;
110 }
111}
112
113
114static void RenderMaximumFaceGroup( GLUtesselator *tess, GLUface *fOrig )
115{
116 /* We want to find the largest triangle fan or strip of unmarked faces
117 * which includes the given face fOrig. There are 3 possible fans
118 * passing through fOrig (one centered at each vertex), and 3 possible
119 * strips (one for each CCW permutation of the vertices). Our strategy
120 * is to try all of these, and take the primitive which uses the most
121 * triangles (a greedy approach).
122 */
123 GLUhalfEdge *e = fOrig->anEdge;
124 struct FaceCount max, newFace;
125
126 max.size = 1;
127 max.eStart = e;
128 max.render = &RenderTriangle;
129
130 if( ! tess->flagBoundary ) {
131 newFace = MaximumFan( e ); if( newFace.size > max.size ) { max = newFace; }
132 newFace = MaximumFan( e->Lnext ); if( newFace.size > max.size ) { max = newFace; }
133 newFace = MaximumFan( e->Lprev ); if( newFace.size > max.size ) { max = newFace; }
134
135 newFace = MaximumStrip( e ); if( newFace.size > max.size ) { max = newFace; }
136 newFace = MaximumStrip( e->Lnext ); if( newFace.size > max.size ) { max = newFace; }
137 newFace = MaximumStrip( e->Lprev ); if( newFace.size > max.size ) { max = newFace; }
138 }
139 (*(max.render))( tess, max.eStart, max.size );
140}
141
142
143/* Macros which keep track of faces we have marked temporarily, and allow
144 * us to backtrack when necessary. With triangle fans, this is not
145 * really necessary, since the only awkward case is a loop of triangles
146 * around a single origin vertex. However with strips the situation is
147 * more complicated, and we need a general tracking method like the
148 * one here.
149 */
150#define Marked(f) (! (f)->inside || (f)->marked)
151
152#define AddToTrail(f,t) ((f)->trail = (t), (t) = (f), (f)->marked = TRUE)
153
154#define FreeTrail(t) if( 1 ) { \
155 while( (t) != NULL ) { \
156 (t)->marked = FALSE; t = (t)->trail; \
157 } \
158 } else /* absorb trailing semicolon */
159
160
161
162static struct FaceCount MaximumFan( GLUhalfEdge *eOrig )
163{
164 /* eOrig->Lface is the face we want to render. We want to find the size
165 * of a maximal fan around eOrig->Org. To do this we just walk around
166 * the origin vertex as far as possible in both directions.
167 */
168 struct FaceCount newFace = { 0, NULL, &RenderFan };
169 GLUface *trail = NULL;
170 GLUhalfEdge *e;
171
172 for( e = eOrig; ! Marked( e->Lface ); e = e->Onext ) {
173 AddToTrail( e->Lface, trail );
174 ++newFace.size;
175 }
176 for( e = eOrig; ! Marked( e->Rface ); e = e->Oprev ) {
177 AddToTrail( e->Rface, trail );
178 ++newFace.size;
179 }
180 newFace.eStart = e;
181 /*LINTED*/
182 FreeTrail( trail );
183 return newFace;
184}
185
186
187#define IsEven(n) (((n) & 1) == 0)
188
189static struct FaceCount MaximumStrip( GLUhalfEdge *eOrig )
190{
191 /* Here we are looking for a maximal strip that contains the vertices
192 * eOrig->Org, eOrig->Dst, eOrig->Lnext->Dst (in that order or the
193 * reverse, such that all triangles are oriented CCW).
194 *
195 * Again we walk forward and backward as far as possible. However for
196 * strips there is a twist: to get CCW orientations, there must be
197 * an *even* number of triangles in the strip on one side of eOrig.
198 * We walk the strip starting on a side with an even number of triangles;
199 * if both side have an odd number, we are forced to shorten one side.
200 */
201 struct FaceCount newFace = { 0, NULL, &RenderStrip };
202 long headSize = 0, tailSize = 0;
203 GLUface *trail = NULL;
204 GLUhalfEdge *e, *eTail, *eHead;
205
206 for( e = eOrig; ! Marked( e->Lface ); ++tailSize, e = e->Onext ) {
207 AddToTrail( e->Lface, trail );
208 ++tailSize;
209 e = e->Dprev;
210 if( Marked( e->Lface )) break;
211 AddToTrail( e->Lface, trail );
212 }
213 eTail = e;
214
215 for( e = eOrig; ! Marked( e->Rface ); ++headSize, e = e->Dnext ) {
216 AddToTrail( e->Rface, trail );
217 ++headSize;
218 e = e->Oprev;
219 if( Marked( e->Rface )) break;
220 AddToTrail( e->Rface, trail );
221 }
222 eHead = e;
223
224 newFace.size = tailSize + headSize;
225 if( IsEven( tailSize )) {
226 newFace.eStart = eTail->Sym;
227 } else if( IsEven( headSize )) {
228 newFace.eStart = eHead;
229 } else {
230 /* Both sides have odd length, we must shorten one of them. In fact,
231 * we must start from eHead to guarantee inclusion of eOrig->Lface.
232 */
233 --newFace.size;
234 newFace.eStart = eHead->Onext;
235 }
236 /*LINTED*/
237 FreeTrail( trail );
238 return newFace;
239}
240
241
242static void RenderTriangle( GLUtesselator *tess, GLUhalfEdge *e, long size )
243{
244 /* Just add the triangle to a triangle list, so we can render all
245 * the separate triangles at once.
246 */
247 assert( size == 1 );
248 AddToTrail( e->Lface, tess->lonelyTriList );
249}
250
251
252static void RenderLonelyTriangles( GLUtesselator *tess, GLUface *f )
253{
254 /* Now we render all the separate triangles which could not be
255 * grouped into a triangle fan or strip.
256 */
257 GLUhalfEdge *e;
258 int newState;
259 int edgeState = -1; /* force edge state output for first vertex */
260
261 CALL_BEGIN_OR_BEGIN_DATA( GL_TRIANGLES );
262
263 for( ; f != NULL; f = f->trail ) {
264 /* Loop once for each edge (there will always be 3 edges) */
265
266 e = f->anEdge;
267 do {
268 if( tess->flagBoundary ) {
269 /* Set the "edge state" to TRUE just before we output the
270 * first vertex of each edge on the polygon boundary.
271 */
272 newState = ! e->Rface->inside;
273 if( edgeState != newState ) {
274 edgeState = newState;
275 CALL_EDGE_FLAG_OR_EDGE_FLAG_DATA( edgeState );
276 }
277 }
278 CALL_VERTEX_OR_VERTEX_DATA( e->Org->data );
279
280 e = e->Lnext;
281 } while( e != f->anEdge );
282 }
283 CALL_END_OR_END_DATA();
284}
285
286
287static void RenderFan( GLUtesselator *tess, GLUhalfEdge *e, long size )
288{
289 /* Render as many CCW triangles as possible in a fan starting from
290 * edge "e". The fan *should* contain exactly "size" triangles
291 * (otherwise we've goofed up somewhere).
292 */
293 CALL_BEGIN_OR_BEGIN_DATA( GL_TRIANGLE_FAN );
294 CALL_VERTEX_OR_VERTEX_DATA( e->Org->data );
295 CALL_VERTEX_OR_VERTEX_DATA( e->Dst->data );
296
297 while( ! Marked( e->Lface )) {
298 e->Lface->marked = TRUE;
299 --size;
300 e = e->Onext;
301 CALL_VERTEX_OR_VERTEX_DATA( e->Dst->data );
302 }
303
304 assert( size == 0 );
305 CALL_END_OR_END_DATA();
306}
307
308
309static void RenderStrip( GLUtesselator *tess, GLUhalfEdge *e, long size )
310{
311 /* Render as many CCW triangles as possible in a strip starting from
312 * edge "e". The strip *should* contain exactly "size" triangles
313 * (otherwise we've goofed up somewhere).
314 */
315 CALL_BEGIN_OR_BEGIN_DATA( GL_TRIANGLE_STRIP );
316 CALL_VERTEX_OR_VERTEX_DATA( e->Org->data );
317 CALL_VERTEX_OR_VERTEX_DATA( e->Dst->data );
318
319 while( ! Marked( e->Lface )) {
320 e->Lface->marked = TRUE;
321 --size;
322 e = e->Dprev;
323 CALL_VERTEX_OR_VERTEX_DATA( e->Org->data );
324 if( Marked( e->Lface )) break;
325
326 e->Lface->marked = TRUE;
327 --size;
328 e = e->Onext;
329 CALL_VERTEX_OR_VERTEX_DATA( e->Dst->data );
330 }
331
332 assert( size == 0 );
333 CALL_END_OR_END_DATA();
334}
335
336
337/************************ Boundary contour decomposition ******************/
338
339/* __gl_renderBoundary( tess, mesh ) takes a mesh, and outputs one
340 * contour for each face marked "inside". The rendering output is
341 * provided as callbacks (see the api).
342 */
343void __gl_renderBoundary( GLUtesselator *tess, GLUmesh *mesh )
344{
345 GLUface *f;
346 GLUhalfEdge *e;
347
348 for( f = mesh->fHead.next; f != &mesh->fHead; f = f->next ) {
349 if( f->inside ) {
350 CALL_BEGIN_OR_BEGIN_DATA( GL_LINE_LOOP );
351 e = f->anEdge;
352 do {
353 CALL_VERTEX_OR_VERTEX_DATA( e->Org->data );
354 e = e->Lnext;
355 } while( e != f->anEdge );
356 CALL_END_OR_END_DATA();
357 }
358 }
359}
360
361
362/************************ Quick-and-dirty decomposition ******************/
363
364#define SIGN_INCONSISTENT 2
365
366static int ComputeNormal( GLUtesselator *tess, GLdouble norm[3], int check )
367/*
368 * If check==FALSE, we compute the polygon normal and place it in norm[].
369 * If check==TRUE, we check that each triangle in the fan from v0 has a
370 * consistent orientation with respect to norm[]. If triangles are
371 * consistently oriented CCW, return 1; if CW, return -1; if all triangles
372 * are degenerate return 0; otherwise (no consistent orientation) return
373 * SIGN_INCONSISTENT.
374 */
375{
376 CachedVertex *v0 = tess->cache;
377 CachedVertex *vn = v0 + tess->cacheCount;
378 CachedVertex *vc;
379 GLdouble dot, xc, yc, zc, xp, yp, zp, n[3];
380 int sign = 0;
381
382 /* Find the polygon normal. It is important to get a reasonable
383 * normal even when the polygon is self-intersecting (eg. a bowtie).
384 * Otherwise, the computed normal could be very tiny, but perpendicular
385 * to the true plane of the polygon due to numerical noise. Then all
386 * the triangles would appear to be degenerate and we would incorrectly
387 * decompose the polygon as a fan (or simply not render it at all).
388 *
389 * We use a sum-of-triangles normal algorithm rather than the more
390 * efficient sum-of-trapezoids method (used in CheckOrientation()
391 * in normal.c). This lets us explicitly reverse the signed area
392 * of some triangles to get a reasonable normal in the self-intersecting
393 * case.
394 */
395 if( ! check ) {
396 norm[0] = norm[1] = norm[2] = 0.0;
397 }
398
399 vc = v0 + 1;
400 xc = vc->coords[0] - v0->coords[0];
401 yc = vc->coords[1] - v0->coords[1];
402 zc = vc->coords[2] - v0->coords[2];
403 while( ++vc < vn ) {
404 xp = xc; yp = yc; zp = zc;
405 xc = vc->coords[0] - v0->coords[0];
406 yc = vc->coords[1] - v0->coords[1];
407 zc = vc->coords[2] - v0->coords[2];
408
409 /* Compute (vp - v0) cross (vc - v0) */
410 n[0] = yp*zc - zp*yc;
411 n[1] = zp*xc - xp*zc;
412 n[2] = xp*yc - yp*xc;
413
414 dot = n[0]*norm[0] + n[1]*norm[1] + n[2]*norm[2];
415 if( ! check ) {
416 /* Reverse the contribution of back-facing triangles to get
417 * a reasonable normal for self-intersecting polygons (see above)
418 */
419 if( dot >= 0 ) {
420 norm[0] += n[0]; norm[1] += n[1]; norm[2] += n[2];
421 } else {
422 norm[0] -= n[0]; norm[1] -= n[1]; norm[2] -= n[2];
423 }
424 } else if( dot != 0 ) {
425 /* Check the new orientation for consistency with previous triangles */
426 if( dot > 0 ) {
427 if( sign < 0 ) return SIGN_INCONSISTENT;
428 sign = 1;
429 } else {
430 if( sign > 0 ) return SIGN_INCONSISTENT;
431 sign = -1;
432 }
433 }
434 }
435 return sign;
436}
437
438/* __gl_renderCache( tess ) takes a single contour and tries to render it
439 * as a triangle fan. This handles convex polygons, as well as some
440 * non-convex polygons if we get lucky.
441 *
442 * Returns TRUE if the polygon was successfully rendered. The rendering
443 * output is provided as callbacks (see the api).
444 */
445GLboolean __gl_renderCache( GLUtesselator *tess )
446{
447 CachedVertex *v0 = tess->cache;
448 CachedVertex *vn = v0 + tess->cacheCount;
449 CachedVertex *vc;
450 GLdouble norm[3];
451 int sign;
452
453 if( tess->cacheCount < 3 ) {
454 /* Degenerate contour -- no output */
455 return TRUE;
456 }
457
458 norm[0] = tess->normal[0];
459 norm[1] = tess->normal[1];
460 norm[2] = tess->normal[2];
461 if( norm[0] == 0 && norm[1] == 0 && norm[2] == 0 ) {
462 ComputeNormal( tess, norm, FALSE );
463 }
464
465 sign = ComputeNormal( tess, norm, TRUE );
466 if( sign == SIGN_INCONSISTENT ) {
467 /* Fan triangles did not have a consistent orientation */
468 return FALSE;
469 }
470 if( sign == 0 ) {
471 /* All triangles were degenerate */
472 return TRUE;
473 }
474
475 /* Make sure we do the right thing for each winding rule */
476 switch( tess->windingRule ) {
477 case GLU_TESS_WINDING_ODD:
478 case GLU_TESS_WINDING_NONZERO:
479 break;
480 case GLU_TESS_WINDING_POSITIVE:
481 if( sign < 0 ) return TRUE;
482 break;
483 case GLU_TESS_WINDING_NEGATIVE:
484 if( sign > 0 ) return TRUE;
485 break;
486 case GLU_TESS_WINDING_ABS_GEQ_TWO:
487 return TRUE;
488 }
489
490 CALL_BEGIN_OR_BEGIN_DATA( tess->boundaryOnly ? GL_LINE_LOOP
491 : (tess->cacheCount > 3) ? GL_TRIANGLE_FAN
492 : GL_TRIANGLES );
493
494 CALL_VERTEX_OR_VERTEX_DATA( v0->data );
495 if( sign > 0 ) {
496 for( vc = v0+1; vc < vn; ++vc ) {
497 CALL_VERTEX_OR_VERTEX_DATA( vc->data );
498 }
499 } else {
500 for( vc = vn-1; vc > v0; --vc ) {
501 CALL_VERTEX_OR_VERTEX_DATA( vc->data );
502 }
503 }
504 CALL_END_OR_END_DATA();
505 return TRUE;
506}
Note: See TracBrowser for help on using the repository browser.