132001f49Smrg/*
232001f49Smrg * Copyright (c) 1993-1997, Silicon Graphics, Inc.
332001f49Smrg * ALL RIGHTS RESERVED
432001f49Smrg * Permission to use, copy, modify, and distribute this software for
532001f49Smrg * any purpose and without fee is hereby granted, provided that the above
632001f49Smrg * copyright notice appear in all copies and that both the copyright notice
732001f49Smrg * and this permission notice appear in supporting documentation, and that
832001f49Smrg * the name of Silicon Graphics, Inc. not be used in advertising
932001f49Smrg * or publicity pertaining to distribution of the software without specific,
1032001f49Smrg * written prior permission.
1132001f49Smrg *
1232001f49Smrg * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
1332001f49Smrg * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
1432001f49Smrg * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
1532001f49Smrg * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
1632001f49Smrg * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
1732001f49Smrg * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
1832001f49Smrg * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
1932001f49Smrg * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
2032001f49Smrg * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
2132001f49Smrg * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
2232001f49Smrg * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
2332001f49Smrg * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
2432001f49Smrg *
2532001f49Smrg * US Government Users Restricted Rights
2632001f49Smrg * Use, duplication, or disclosure by the Government is subject to
2732001f49Smrg * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
2832001f49Smrg * (c)(1)(ii) of the Rights in Technical Data and Computer Software
2932001f49Smrg * clause at DFARS 252.227-7013 and/or in similar or successor
3032001f49Smrg * clauses in the FAR or the DOD or NASA FAR Supplement.
3132001f49Smrg * Unpublished-- rights reserved under the copyright laws of the
3232001f49Smrg * United States.  Contractor/manufacturer is Silicon Graphics,
3332001f49Smrg * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
3432001f49Smrg *
3532001f49Smrg * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
3632001f49Smrg */
3732001f49Smrg
3832001f49Smrg/*
3932001f49Smrg *  tesswind.c
4032001f49Smrg *  This program demonstrates the winding rule polygon
4132001f49Smrg *  tessellation property.  Four tessellated objects are drawn,
4232001f49Smrg *  each with very different contours.  When the w key is pressed,
4332001f49Smrg *  the objects are drawn with a different winding rule.
4432001f49Smrg */
4532001f49Smrg#include "glut_wrap.h"
4632001f49Smrg#include <stdlib.h>
4732001f49Smrg#include <stdio.h>
4832001f49Smrg
4932001f49Smrg#ifdef GLU_VERSION_1_2
5032001f49Smrg
5132001f49SmrgGLdouble currentWinding = GLU_TESS_WINDING_ODD;
5232001f49Smrgint currentShape = 0;
5332001f49SmrgGLUtesselator *tobj;
5432001f49SmrgGLuint list;
5532001f49Smrg
5632001f49Smrg/*  Make four display lists,
5732001f49Smrg *  each with a different tessellated object.
5832001f49Smrg */
5932001f49Smrgstatic void makeNewLists (void) {
6032001f49Smrg   int i;
6132001f49Smrg   static GLdouble rects[12][3] =
6232001f49Smrg      {{ 50.0,  50.0, 0.0}, {300.0,  50.0, 0.0},
6332001f49Smrg       {300.0, 300.0, 0.0}, { 50.0, 300.0, 0.0},
6432001f49Smrg       {100.0, 100.0, 0.0}, {250.0, 100.0, 0.0},
6532001f49Smrg       {250.0, 250.0, 0.0}, {100.0, 250.0, 0.0},
6632001f49Smrg       {150.0, 150.0, 0.0}, {200.0, 150.0, 0.0},
6732001f49Smrg       {200.0, 200.0, 0.0}, {150.0, 200.0, 0.0}};
6832001f49Smrg   static GLdouble spiral[16][3] =
6932001f49Smrg      {{400.0, 250.0, 0.0}, {400.0,  50.0, 0.0},
7032001f49Smrg       { 50.0,  50.0, 0.0}, { 50.0, 400.0, 0.0},
7132001f49Smrg       {350.0, 400.0, 0.0}, {350.0, 100.0, 0.0},
7232001f49Smrg       {100.0, 100.0, 0.0}, {100.0, 350.0, 0.0},
7332001f49Smrg       {300.0, 350.0, 0.0}, {300.0, 150.0, 0.0},
7432001f49Smrg       {150.0, 150.0, 0.0}, {150.0, 300.0, 0.0},
7532001f49Smrg       {250.0, 300.0, 0.0}, {250.0, 200.0, 0.0},
7632001f49Smrg       {200.0, 200.0, 0.0}, {200.0, 250.0, 0.0}};
7732001f49Smrg   static GLdouble quad1[4][3] =
7832001f49Smrg      {{ 50.0, 150.0, 0.0}, {350.0, 150.0, 0.0},
7932001f49Smrg       {350.0, 200.0, 0.0}, { 50.0, 200.0, 0.0}};
8032001f49Smrg   static GLdouble quad2[4][3] =
8132001f49Smrg      {{100.0, 100.0, 0.0}, {300.0, 100.0, 0.0},
8232001f49Smrg       {300.0, 350.0, 0.0}, {100.0, 350.0, 0.0}};
8332001f49Smrg   static GLdouble tri[3][3] =
8432001f49Smrg      {{200.0, 50.0, 0.0}, {250.0, 300.0, 0.0},
8532001f49Smrg       {150.0, 300.0, 0.0}};
8632001f49Smrg
8732001f49Smrg   gluTessProperty(tobj, GLU_TESS_WINDING_RULE,
8832001f49Smrg                   currentWinding);
8932001f49Smrg
9032001f49Smrg   glNewList(list, GL_COMPILE);
9132001f49Smrg      gluTessBeginPolygon(tobj, NULL);
9232001f49Smrg         gluTessBeginContour(tobj);
9332001f49Smrg         for (i = 0; i < 4; i++)
9432001f49Smrg            gluTessVertex(tobj, rects[i], rects[i]);
9532001f49Smrg         gluTessEndContour(tobj);
9632001f49Smrg         gluTessBeginContour(tobj);
9732001f49Smrg         for (i = 4; i < 8; i++)
9832001f49Smrg            gluTessVertex(tobj, rects[i], rects[i]);
9932001f49Smrg         gluTessEndContour(tobj);
10032001f49Smrg         gluTessBeginContour(tobj);
10132001f49Smrg         for (i = 8; i < 12; i++)
10232001f49Smrg            gluTessVertex(tobj, rects[i], rects[i]);
10332001f49Smrg         gluTessEndContour(tobj);
10432001f49Smrg      gluTessEndPolygon(tobj);
10532001f49Smrg   glEndList();
10632001f49Smrg
10732001f49Smrg   glNewList(list+1, GL_COMPILE);
10832001f49Smrg      gluTessBeginPolygon(tobj, NULL);
10932001f49Smrg         gluTessBeginContour(tobj);
11032001f49Smrg         for (i = 0; i < 4; i++)
11132001f49Smrg            gluTessVertex(tobj, rects[i], rects[i]);
11232001f49Smrg         gluTessEndContour(tobj);
11332001f49Smrg         gluTessBeginContour(tobj);
11432001f49Smrg         for (i = 7; i >= 4; i--)
11532001f49Smrg            gluTessVertex(tobj, rects[i], rects[i]);
11632001f49Smrg         gluTessEndContour(tobj);
11732001f49Smrg         gluTessBeginContour(tobj);
11832001f49Smrg         for (i = 11; i >= 8; i--)
11932001f49Smrg            gluTessVertex(tobj, rects[i], rects[i]);
12032001f49Smrg         gluTessEndContour(tobj);
12132001f49Smrg      gluTessEndPolygon(tobj);
12232001f49Smrg   glEndList();
12332001f49Smrg
12432001f49Smrg   glNewList(list+2, GL_COMPILE);
12532001f49Smrg      gluTessBeginPolygon(tobj, NULL);
12632001f49Smrg         gluTessBeginContour(tobj);
12732001f49Smrg         for (i = 0; i < 16; i++)
12832001f49Smrg            gluTessVertex(tobj, spiral[i], spiral[i]);
12932001f49Smrg         gluTessEndContour(tobj);
13032001f49Smrg      gluTessEndPolygon(tobj);
13132001f49Smrg   glEndList();
13232001f49Smrg
13332001f49Smrg   glNewList(list+3, GL_COMPILE);
13432001f49Smrg      gluTessBeginPolygon(tobj, NULL);
13532001f49Smrg         gluTessBeginContour(tobj);
13632001f49Smrg         for (i = 0; i < 4; i++)
13732001f49Smrg            gluTessVertex(tobj, quad1[i], quad1[i]);
13832001f49Smrg         gluTessEndContour(tobj);
13932001f49Smrg         gluTessBeginContour(tobj);
14032001f49Smrg         for (i = 0; i < 4; i++)
14132001f49Smrg            gluTessVertex(tobj, quad2[i], quad2[i]);
14232001f49Smrg         gluTessEndContour(tobj);
14332001f49Smrg         gluTessBeginContour(tobj);
14432001f49Smrg         for (i = 0; i < 3; i++)
14532001f49Smrg            gluTessVertex(tobj, tri[i], tri[i]);
14632001f49Smrg         gluTessEndContour(tobj);
14732001f49Smrg      gluTessEndPolygon(tobj);
14832001f49Smrg   glEndList();
14932001f49Smrg}
15032001f49Smrg
15132001f49Smrgstatic void display (void) {
15232001f49Smrg   glClear(GL_COLOR_BUFFER_BIT);
15332001f49Smrg   glColor3f(1.0, 1.0, 1.0);
15432001f49Smrg   glPushMatrix();
15532001f49Smrg   glCallList(list);
15632001f49Smrg   glTranslatef(0.0, 500.0, 0.0);
15732001f49Smrg   glCallList(list+1);
15832001f49Smrg   glTranslatef(500.0, -500.0, 0.0);
15932001f49Smrg   glCallList(list+2);
16032001f49Smrg   glTranslatef(0.0, 500.0, 0.0);
16132001f49Smrg   glCallList(list+3);
16232001f49Smrg   glPopMatrix();
16332001f49Smrg   glFlush();
16432001f49Smrg}
16532001f49Smrg
16632001f49Smrgstatic void GLAPIENTRY beginCallback(GLenum which)
16732001f49Smrg{
16832001f49Smrg   glBegin(which);
16932001f49Smrg}
17032001f49Smrg
17132001f49Smrgstatic void GLAPIENTRY errorCallback(GLenum errorCode)
17232001f49Smrg{
17332001f49Smrg   const GLubyte *estring;
17432001f49Smrg
17532001f49Smrg   estring = gluErrorString(errorCode);
17632001f49Smrg   fprintf(stderr, "Tessellation Error: %s\n", (char *) estring);
17732001f49Smrg   exit(0);
17832001f49Smrg}
17932001f49Smrg
18032001f49Smrgstatic void GLAPIENTRY endCallback(void)
18132001f49Smrg{
18232001f49Smrg   glEnd();
18332001f49Smrg}
18432001f49Smrg
18532001f49Smrg/*  combineCallback is used to create a new vertex when edges
18632001f49Smrg *  intersect.  coordinate location is trivial to calculate,
18732001f49Smrg *  but weight[4] may be used to average color, normal, or texture
18832001f49Smrg *  coordinate data.
18932001f49Smrg */
19032001f49Smrg/* ARGSUSED */
19132001f49Smrgstatic void GLAPIENTRY combineCallback(GLdouble coords[3], GLdouble *data[4],
19232001f49Smrg                     GLfloat weight[4], GLdouble **dataOut )
19332001f49Smrg{
19432001f49Smrg   GLdouble *vertex;
19532001f49Smrg   vertex = (GLdouble *) malloc(3 * sizeof(GLdouble));
19632001f49Smrg
19732001f49Smrg   vertex[0] = coords[0];
19832001f49Smrg   vertex[1] = coords[1];
19932001f49Smrg   vertex[2] = coords[2];
20032001f49Smrg   *dataOut = vertex;
20132001f49Smrg}
20232001f49Smrg
20332001f49Smrgstatic void init(void)
20432001f49Smrg{
20532001f49Smrg   glClearColor(0.0, 0.0, 0.0, 0.0);
20632001f49Smrg   glShadeModel(GL_FLAT);
20732001f49Smrg
20832001f49Smrg   tobj = gluNewTess();
20932001f49Smrg   gluTessCallback(tobj, GLU_TESS_VERTEX, &glVertex3dv);
21032001f49Smrg   gluTessCallback(tobj, GLU_TESS_BEGIN, &beginCallback);
21132001f49Smrg   gluTessCallback(tobj, GLU_TESS_END, &endCallback);
21232001f49Smrg   gluTessCallback(tobj, GLU_TESS_ERROR, &errorCallback);
21332001f49Smrg   gluTessCallback(tobj, GLU_TESS_COMBINE, &combineCallback);
21432001f49Smrg
21532001f49Smrg   list = glGenLists(4);
21632001f49Smrg   makeNewLists();
21732001f49Smrg}
21832001f49Smrg
21932001f49Smrgstatic void reshape(int w, int h)
22032001f49Smrg{
22132001f49Smrg   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
22232001f49Smrg   glMatrixMode(GL_PROJECTION);
22332001f49Smrg   glLoadIdentity();
22432001f49Smrg   if (w <= h)
22532001f49Smrg      gluOrtho2D(0.0, 1000.0, 0.0, 1000.0 * (GLdouble)h/(GLdouble)w);
22632001f49Smrg   else
22732001f49Smrg      gluOrtho2D(0.0, 1000.0 * (GLdouble)w/(GLdouble)h, 0.0, 1000.0);
22832001f49Smrg   glMatrixMode(GL_MODELVIEW);
22932001f49Smrg   glLoadIdentity();
23032001f49Smrg}
23132001f49Smrg
23232001f49Smrg/* ARGSUSED1 */
23332001f49Smrgstatic void keyboard(unsigned char key, int x, int y)
23432001f49Smrg{
23532001f49Smrg   switch (key) {
23632001f49Smrg      case 'w':
23732001f49Smrg      case 'W':
23832001f49Smrg         if (currentWinding == GLU_TESS_WINDING_ODD)
23932001f49Smrg            currentWinding = GLU_TESS_WINDING_NONZERO;
24032001f49Smrg         else if (currentWinding == GLU_TESS_WINDING_NONZERO)
24132001f49Smrg            currentWinding = GLU_TESS_WINDING_POSITIVE;
24232001f49Smrg         else if (currentWinding == GLU_TESS_WINDING_POSITIVE)
24332001f49Smrg            currentWinding = GLU_TESS_WINDING_NEGATIVE;
24432001f49Smrg         else if (currentWinding == GLU_TESS_WINDING_NEGATIVE)
24532001f49Smrg            currentWinding = GLU_TESS_WINDING_ABS_GEQ_TWO;
24632001f49Smrg         else if (currentWinding == GLU_TESS_WINDING_ABS_GEQ_TWO)
24732001f49Smrg            currentWinding = GLU_TESS_WINDING_ODD;
24832001f49Smrg         makeNewLists();
24932001f49Smrg         glutPostRedisplay();
25032001f49Smrg         break;
25132001f49Smrg      case 27:
25232001f49Smrg         exit(0);
25332001f49Smrg         break;
25432001f49Smrg      default:
25532001f49Smrg         break;
25632001f49Smrg   }
25732001f49Smrg}
25832001f49Smrg
25932001f49Smrgint main(int argc, char** argv)
26032001f49Smrg{
26132001f49Smrg   glutInit(&argc, argv);
26232001f49Smrg   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
26332001f49Smrg   glutInitWindowSize(500, 500);
26432001f49Smrg   glutCreateWindow(argv[0]);
26532001f49Smrg   init();
26632001f49Smrg   glutDisplayFunc(display);
26732001f49Smrg   glutReshapeFunc(reshape);
26832001f49Smrg   glutKeyboardFunc(keyboard);
26932001f49Smrg   glutMainLoop();
27032001f49Smrg   return 0;
27132001f49Smrg}
27232001f49Smrg
27332001f49Smrg#else
27432001f49Smrgint main(int argc, char** argv)
27532001f49Smrg{
27632001f49Smrg    fprintf (stderr, "This program demonstrates the new tesselator API in GLU 1.2.\n");
27732001f49Smrg    fprintf (stderr, "Your GLU library does not support this new interface, sorry.\n");
27832001f49Smrg    return 0;
27932001f49Smrg}
28032001f49Smrg#endif
281