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 *  tess.c
4032001f49Smrg *  This program demonstrates polygon tessellation.
4132001f49Smrg *  Two tesselated objects are drawn.  The first is a
4232001f49Smrg *  rectangle with a triangular hole.  The second is a
4332001f49Smrg *  smooth shaded, self-intersecting star.
4432001f49Smrg *
4532001f49Smrg *  Note the exterior rectangle is drawn with its vertices
4632001f49Smrg *  in counter-clockwise order, but its interior clockwise.
4732001f49Smrg *  Note the combineCallback is needed for the self-intersecting
4832001f49Smrg *  star.  Also note that removing the TessProperty for the
4932001f49Smrg *  star will make the interior unshaded (WINDING_ODD).
5032001f49Smrg */
5132001f49Smrg#include "glut_wrap.h"
5232001f49Smrg#include <stdlib.h>
5332001f49Smrg#include <stdio.h>
5432001f49Smrg
5532001f49Smrg#ifdef GLU_VERSION_1_2
5632001f49Smrg
5732001f49SmrgGLuint startList;
5832001f49Smrg
5932001f49Smrgstatic void display (void) {
6032001f49Smrg   glClear(GL_COLOR_BUFFER_BIT);
6132001f49Smrg   glColor3f(1.0, 1.0, 1.0);
6232001f49Smrg   glCallList(startList);
6332001f49Smrg   glCallList(startList + 1);
6432001f49Smrg   glFlush();
6532001f49Smrg}
6632001f49Smrg
6732001f49Smrgstatic void GLAPIENTRY beginCallback(GLenum which)
6832001f49Smrg{
6932001f49Smrg   glBegin(which);
7032001f49Smrg}
7132001f49Smrg
7232001f49Smrgstatic void GLAPIENTRY errorCallback(GLenum errorCode)
7332001f49Smrg{
7432001f49Smrg   const GLubyte *estring;
7532001f49Smrg
7632001f49Smrg   estring = gluErrorString(errorCode);
7732001f49Smrg   fprintf(stderr, "Tessellation Error: %s\n", (char *) estring);
7832001f49Smrg   exit(0);
7932001f49Smrg}
8032001f49Smrg
8132001f49Smrgstatic void GLAPIENTRY endCallback(void)
8232001f49Smrg{
8332001f49Smrg   glEnd();
8432001f49Smrg}
8532001f49Smrg
8632001f49Smrgstatic void GLAPIENTRY vertexCallback(GLvoid *vertex)
8732001f49Smrg{
8832001f49Smrg   const GLdouble *pointer;
8932001f49Smrg
9032001f49Smrg   pointer = (GLdouble *) vertex;
9132001f49Smrg   glColor3dv(pointer+3);
9232001f49Smrg   glVertex3dv(pointer);
9332001f49Smrg}
9432001f49Smrg
9532001f49Smrg/*  combineCallback is used to create a new vertex when edges
9632001f49Smrg *  intersect.  coordinate location is trivial to calculate,
9732001f49Smrg *  but weight[4] may be used to average color, normal, or texture
9832001f49Smrg *  coordinate data.  In this program, color is weighted.
9932001f49Smrg */
10032001f49Smrgstatic void GLAPIENTRY combineCallback(GLdouble coords[3],
10132001f49Smrg                     GLdouble *vertex_data[4],
10232001f49Smrg                     GLfloat weight[4], GLdouble **dataOut )
10332001f49Smrg{
10432001f49Smrg   GLdouble *vertex;
10532001f49Smrg   int i;
10632001f49Smrg
10732001f49Smrg   vertex = (GLdouble *) malloc(6 * sizeof(GLdouble));
10832001f49Smrg
10932001f49Smrg   vertex[0] = coords[0];
11032001f49Smrg   vertex[1] = coords[1];
11132001f49Smrg   vertex[2] = coords[2];
11232001f49Smrg   for (i = 3; i < 6; i++)
11332001f49Smrg      vertex[i] = weight[0] * vertex_data[0][i]
11432001f49Smrg                  + weight[1] * vertex_data[1][i]
11532001f49Smrg                  + weight[2] * vertex_data[2][i]
11632001f49Smrg                  + weight[3] * vertex_data[3][i];
11732001f49Smrg   *dataOut = vertex;
11832001f49Smrg}
11932001f49Smrg
12032001f49Smrgstatic void init (void)
12132001f49Smrg{
12232001f49Smrg   GLUtesselator *tobj;
12332001f49Smrg   GLdouble rect[4][3] = {{50.0, 50.0, 0.0},
12432001f49Smrg                          {200.0, 50.0, 0.0},
12532001f49Smrg                          {200.0, 200.0, 0.0},
12632001f49Smrg                          {50.0, 200.0, 0.0}};
12732001f49Smrg   GLdouble tri[3][3] = {{75.0, 75.0, 0.0},
12832001f49Smrg                         {125.0, 175.0, 0.0},
12932001f49Smrg                         {175.0, 75.0, 0.0}};
13032001f49Smrg   GLdouble star[5][6] = {{250.0, 50.0, 0.0, 1.0, 0.0, 1.0},
13132001f49Smrg                          {325.0, 200.0, 0.0, 1.0, 1.0, 0.0},
13232001f49Smrg                          {400.0, 50.0, 0.0, 0.0, 1.0, 1.0},
13332001f49Smrg                          {250.0, 150.0, 0.0, 1.0, 0.0, 0.0},
13432001f49Smrg                          {400.0, 150.0, 0.0, 0.0, 1.0, 0.0}};
13532001f49Smrg
13632001f49Smrg   glClearColor(0.0, 0.0, 0.0, 0.0);
13732001f49Smrg
13832001f49Smrg   startList = glGenLists(2);
13932001f49Smrg
14032001f49Smrg   tobj = gluNewTess();
14132001f49Smrg   gluTessCallback(tobj, GLU_TESS_VERTEX, &glVertex3dv);
14232001f49Smrg   gluTessCallback(tobj, GLU_TESS_BEGIN, &beginCallback);
14332001f49Smrg   gluTessCallback(tobj, GLU_TESS_END, &endCallback);
14432001f49Smrg   gluTessCallback(tobj, GLU_TESS_ERROR, &errorCallback);
14532001f49Smrg
14632001f49Smrg   /*  rectangle with triangular hole inside  */
14732001f49Smrg   glNewList(startList, GL_COMPILE);
14832001f49Smrg   glShadeModel(GL_FLAT);
14932001f49Smrg   gluTessBeginPolygon(tobj, NULL);
15032001f49Smrg      gluTessBeginContour(tobj);
15132001f49Smrg         gluTessVertex(tobj, rect[0], rect[0]);
15232001f49Smrg         gluTessVertex(tobj, rect[1], rect[1]);
15332001f49Smrg         gluTessVertex(tobj, rect[2], rect[2]);
15432001f49Smrg         gluTessVertex(tobj, rect[3], rect[3]);
15532001f49Smrg      gluTessEndContour(tobj);
15632001f49Smrg      gluTessBeginContour(tobj);
15732001f49Smrg         gluTessVertex(tobj, tri[0], tri[0]);
15832001f49Smrg         gluTessVertex(tobj, tri[1], tri[1]);
15932001f49Smrg         gluTessVertex(tobj, tri[2], tri[2]);
16032001f49Smrg      gluTessEndContour(tobj);
16132001f49Smrg   gluTessEndPolygon(tobj);
16232001f49Smrg   glEndList();
16332001f49Smrg
16432001f49Smrg   gluTessCallback(tobj, GLU_TESS_VERTEX, &vertexCallback);
16532001f49Smrg   gluTessCallback(tobj, GLU_TESS_BEGIN, &beginCallback);
16632001f49Smrg   gluTessCallback(tobj, GLU_TESS_END, &endCallback);
16732001f49Smrg   gluTessCallback(tobj, GLU_TESS_ERROR, &errorCallback);
16832001f49Smrg   gluTessCallback(tobj, GLU_TESS_COMBINE, &combineCallback);
16932001f49Smrg
17032001f49Smrg   /*  smooth shaded, self-intersecting star  */
17132001f49Smrg   glNewList(startList + 1, GL_COMPILE);
17232001f49Smrg   glShadeModel(GL_SMOOTH);
17332001f49Smrg   gluTessProperty(tobj, GLU_TESS_WINDING_RULE,
17432001f49Smrg                   GLU_TESS_WINDING_POSITIVE);
17532001f49Smrg   gluTessBeginPolygon(tobj, NULL);
17632001f49Smrg      gluTessBeginContour(tobj);
17732001f49Smrg         gluTessVertex(tobj, star[0], star[0]);
17832001f49Smrg         gluTessVertex(tobj, star[1], star[1]);
17932001f49Smrg         gluTessVertex(tobj, star[2], star[2]);
18032001f49Smrg         gluTessVertex(tobj, star[3], star[3]);
18132001f49Smrg         gluTessVertex(tobj, star[4], star[4]);
18232001f49Smrg      gluTessEndContour(tobj);
18332001f49Smrg   gluTessEndPolygon(tobj);
18432001f49Smrg   glEndList();
18532001f49Smrg   gluDeleteTess(tobj);
18632001f49Smrg}
18732001f49Smrg
18832001f49Smrgstatic void reshape (int w, int h)
18932001f49Smrg{
19032001f49Smrg   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
19132001f49Smrg   glMatrixMode(GL_PROJECTION);
19232001f49Smrg   glLoadIdentity();
19332001f49Smrg   gluOrtho2D(0.0, (GLdouble) w, 0.0, (GLdouble) h);
19432001f49Smrg}
19532001f49Smrg
19632001f49Smrg/* ARGSUSED1 */
19732001f49Smrgstatic void keyboard(unsigned char key, int x, int y)
19832001f49Smrg{
19932001f49Smrg   switch (key) {
20032001f49Smrg      case 27:
20132001f49Smrg         exit(0);
20232001f49Smrg         break;
20332001f49Smrg   }
20432001f49Smrg}
20532001f49Smrg
20632001f49Smrgint main(int argc, char** argv)
20732001f49Smrg{
20832001f49Smrg   glutInit(&argc, argv);
20932001f49Smrg   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
21032001f49Smrg   glutInitWindowSize(500, 500);
21132001f49Smrg   glutCreateWindow(argv[0]);
21232001f49Smrg   init();
21332001f49Smrg   glutDisplayFunc(display);
21432001f49Smrg   glutReshapeFunc(reshape);
21532001f49Smrg   glutKeyboardFunc(keyboard);
21632001f49Smrg   glutMainLoop();
21732001f49Smrg   return 0;
21832001f49Smrg}
21932001f49Smrg
22032001f49Smrg#else
22132001f49Smrgint main(int argc, char** argv)
22232001f49Smrg{
22332001f49Smrg    fprintf (stderr, "This program demonstrates the new tesselator API in GLU 1.2.\n");
22432001f49Smrg    fprintf (stderr, "Your GLU library does not support this new interface, sorry.\n");
22532001f49Smrg    return 0;
22632001f49Smrg}
22732001f49Smrg#endif
228