1 /*
2  * Copyright (c) 1993-1997, Silicon Graphics, Inc.
3  * ALL RIGHTS RESERVED
4  * Permission to use, copy, modify, and distribute this software for
5  * any purpose and without fee is hereby granted, provided that the above
6  * copyright notice appear in all copies and that both the copyright notice
7  * and this permission notice appear in supporting documentation, and that
8  * the name of Silicon Graphics, Inc. not be used in advertising
9  * or publicity pertaining to distribution of the software without specific,
10  * written prior permission.
11  *
12  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
13  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
14  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
15  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
16  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
17  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
18  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
19  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
20  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
21  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
22  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
23  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
24  *
25  * US Government Users Restricted Rights
26  * Use, duplication, or disclosure by the Government is subject to
27  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
28  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
29  * clause at DFARS 252.227-7013 and/or in similar or successor
30  * clauses in the FAR or the DOD or NASA FAR Supplement.
31  * Unpublished-- rights reserved under the copyright laws of the
32  * United States.  Contractor/manufacturer is Silicon Graphics,
33  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
34  *
35  * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
36  */
37 
38 /*
39  *  tesswind.c
40  *  This program demonstrates the winding rule polygon
41  *  tessellation property.  Four tessellated objects are drawn,
42  *  each with very different contours.  When the w key is pressed,
43  *  the objects are drawn with a different winding rule.
44  */
45 #include "glut_wrap.h"
46 #include <stdlib.h>
47 #include <stdio.h>
48 
49 #ifdef GLU_VERSION_1_2
50 
51 GLdouble currentWinding = GLU_TESS_WINDING_ODD;
52 int currentShape = 0;
53 GLUtesselator *tobj;
54 GLuint list;
55 
56 /*  Make four display lists,
57  *  each with a different tessellated object.
58  */
makeNewLists(void)59 static void makeNewLists (void) {
60    int i;
61    static GLdouble rects[12][3] =
62       {{ 50.0,  50.0, 0.0}, {300.0,  50.0, 0.0},
63        {300.0, 300.0, 0.0}, { 50.0, 300.0, 0.0},
64        {100.0, 100.0, 0.0}, {250.0, 100.0, 0.0},
65        {250.0, 250.0, 0.0}, {100.0, 250.0, 0.0},
66        {150.0, 150.0, 0.0}, {200.0, 150.0, 0.0},
67        {200.0, 200.0, 0.0}, {150.0, 200.0, 0.0}};
68    static GLdouble spiral[16][3] =
69       {{400.0, 250.0, 0.0}, {400.0,  50.0, 0.0},
70        { 50.0,  50.0, 0.0}, { 50.0, 400.0, 0.0},
71        {350.0, 400.0, 0.0}, {350.0, 100.0, 0.0},
72        {100.0, 100.0, 0.0}, {100.0, 350.0, 0.0},
73        {300.0, 350.0, 0.0}, {300.0, 150.0, 0.0},
74        {150.0, 150.0, 0.0}, {150.0, 300.0, 0.0},
75        {250.0, 300.0, 0.0}, {250.0, 200.0, 0.0},
76        {200.0, 200.0, 0.0}, {200.0, 250.0, 0.0}};
77    static GLdouble quad1[4][3] =
78       {{ 50.0, 150.0, 0.0}, {350.0, 150.0, 0.0},
79        {350.0, 200.0, 0.0}, { 50.0, 200.0, 0.0}};
80    static GLdouble quad2[4][3] =
81       {{100.0, 100.0, 0.0}, {300.0, 100.0, 0.0},
82        {300.0, 350.0, 0.0}, {100.0, 350.0, 0.0}};
83    static GLdouble tri[3][3] =
84       {{200.0, 50.0, 0.0}, {250.0, 300.0, 0.0},
85        {150.0, 300.0, 0.0}};
86 
87    gluTessProperty(tobj, GLU_TESS_WINDING_RULE,
88                    currentWinding);
89 
90    glNewList(list, GL_COMPILE);
91       gluTessBeginPolygon(tobj, NULL);
92          gluTessBeginContour(tobj);
93          for (i = 0; i < 4; i++)
94             gluTessVertex(tobj, rects[i], rects[i]);
95          gluTessEndContour(tobj);
96          gluTessBeginContour(tobj);
97          for (i = 4; i < 8; i++)
98             gluTessVertex(tobj, rects[i], rects[i]);
99          gluTessEndContour(tobj);
100          gluTessBeginContour(tobj);
101          for (i = 8; i < 12; i++)
102             gluTessVertex(tobj, rects[i], rects[i]);
103          gluTessEndContour(tobj);
104       gluTessEndPolygon(tobj);
105    glEndList();
106 
107    glNewList(list+1, GL_COMPILE);
108       gluTessBeginPolygon(tobj, NULL);
109          gluTessBeginContour(tobj);
110          for (i = 0; i < 4; i++)
111             gluTessVertex(tobj, rects[i], rects[i]);
112          gluTessEndContour(tobj);
113          gluTessBeginContour(tobj);
114          for (i = 7; i >= 4; i--)
115             gluTessVertex(tobj, rects[i], rects[i]);
116          gluTessEndContour(tobj);
117          gluTessBeginContour(tobj);
118          for (i = 11; i >= 8; i--)
119             gluTessVertex(tobj, rects[i], rects[i]);
120          gluTessEndContour(tobj);
121       gluTessEndPolygon(tobj);
122    glEndList();
123 
124    glNewList(list+2, GL_COMPILE);
125       gluTessBeginPolygon(tobj, NULL);
126          gluTessBeginContour(tobj);
127          for (i = 0; i < 16; i++)
128             gluTessVertex(tobj, spiral[i], spiral[i]);
129          gluTessEndContour(tobj);
130       gluTessEndPolygon(tobj);
131    glEndList();
132 
133    glNewList(list+3, GL_COMPILE);
134       gluTessBeginPolygon(tobj, NULL);
135          gluTessBeginContour(tobj);
136          for (i = 0; i < 4; i++)
137             gluTessVertex(tobj, quad1[i], quad1[i]);
138          gluTessEndContour(tobj);
139          gluTessBeginContour(tobj);
140          for (i = 0; i < 4; i++)
141             gluTessVertex(tobj, quad2[i], quad2[i]);
142          gluTessEndContour(tobj);
143          gluTessBeginContour(tobj);
144          for (i = 0; i < 3; i++)
145             gluTessVertex(tobj, tri[i], tri[i]);
146          gluTessEndContour(tobj);
147       gluTessEndPolygon(tobj);
148    glEndList();
149 }
150 
display(void)151 static void display (void) {
152    glClear(GL_COLOR_BUFFER_BIT);
153    glColor3f(1.0, 1.0, 1.0);
154    glPushMatrix();
155    glCallList(list);
156    glTranslatef(0.0, 500.0, 0.0);
157    glCallList(list+1);
158    glTranslatef(500.0, -500.0, 0.0);
159    glCallList(list+2);
160    glTranslatef(0.0, 500.0, 0.0);
161    glCallList(list+3);
162    glPopMatrix();
163    glFlush();
164 }
165 
beginCallback(GLenum which)166 static void GLAPIENTRY beginCallback(GLenum which)
167 {
168    glBegin(which);
169 }
170 
errorCallback(GLenum errorCode)171 static void GLAPIENTRY errorCallback(GLenum errorCode)
172 {
173    const GLubyte *estring;
174 
175    estring = gluErrorString(errorCode);
176    fprintf(stderr, "Tessellation Error: %s\n", (char *) estring);
177    exit(0);
178 }
179 
endCallback(void)180 static void GLAPIENTRY endCallback(void)
181 {
182    glEnd();
183 }
184 
185 /*  combineCallback is used to create a new vertex when edges
186  *  intersect.  coordinate location is trivial to calculate,
187  *  but weight[4] may be used to average color, normal, or texture
188  *  coordinate data.
189  */
190 /* ARGSUSED */
combineCallback(GLdouble coords[3],GLdouble * data[4],GLfloat weight[4],GLdouble ** dataOut)191 static void GLAPIENTRY combineCallback(GLdouble coords[3], GLdouble *data[4],
192                      GLfloat weight[4], GLdouble **dataOut )
193 {
194    GLdouble *vertex;
195    vertex = (GLdouble *) malloc(3 * sizeof(GLdouble));
196 
197    vertex[0] = coords[0];
198    vertex[1] = coords[1];
199    vertex[2] = coords[2];
200    *dataOut = vertex;
201 }
202 
init(void)203 static void init(void)
204 {
205    glClearColor(0.0, 0.0, 0.0, 0.0);
206    glShadeModel(GL_FLAT);
207 
208    tobj = gluNewTess();
209    gluTessCallback(tobj, GLU_TESS_VERTEX, &glVertex3dv);
210    gluTessCallback(tobj, GLU_TESS_BEGIN, &beginCallback);
211    gluTessCallback(tobj, GLU_TESS_END, &endCallback);
212    gluTessCallback(tobj, GLU_TESS_ERROR, &errorCallback);
213    gluTessCallback(tobj, GLU_TESS_COMBINE, &combineCallback);
214 
215    list = glGenLists(4);
216    makeNewLists();
217 }
218 
reshape(int w,int h)219 static void reshape(int w, int h)
220 {
221    glViewport(0, 0, (GLsizei) w, (GLsizei) h);
222    glMatrixMode(GL_PROJECTION);
223    glLoadIdentity();
224    if (w <= h)
225       gluOrtho2D(0.0, 1000.0, 0.0, 1000.0 * (GLdouble)h/(GLdouble)w);
226    else
227       gluOrtho2D(0.0, 1000.0 * (GLdouble)w/(GLdouble)h, 0.0, 1000.0);
228    glMatrixMode(GL_MODELVIEW);
229    glLoadIdentity();
230 }
231 
232 /* ARGSUSED1 */
keyboard(unsigned char key,int x,int y)233 static void keyboard(unsigned char key, int x, int y)
234 {
235    switch (key) {
236       case 'w':
237       case 'W':
238          if (currentWinding == GLU_TESS_WINDING_ODD)
239             currentWinding = GLU_TESS_WINDING_NONZERO;
240          else if (currentWinding == GLU_TESS_WINDING_NONZERO)
241             currentWinding = GLU_TESS_WINDING_POSITIVE;
242          else if (currentWinding == GLU_TESS_WINDING_POSITIVE)
243             currentWinding = GLU_TESS_WINDING_NEGATIVE;
244          else if (currentWinding == GLU_TESS_WINDING_NEGATIVE)
245             currentWinding = GLU_TESS_WINDING_ABS_GEQ_TWO;
246          else if (currentWinding == GLU_TESS_WINDING_ABS_GEQ_TWO)
247             currentWinding = GLU_TESS_WINDING_ODD;
248          makeNewLists();
249          glutPostRedisplay();
250          break;
251       case 27:
252          exit(0);
253          break;
254       default:
255          break;
256    }
257 }
258 
main(int argc,char ** argv)259 int main(int argc, char** argv)
260 {
261    glutInit(&argc, argv);
262    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
263    glutInitWindowSize(500, 500);
264    glutCreateWindow(argv[0]);
265    init();
266    glutDisplayFunc(display);
267    glutReshapeFunc(reshape);
268    glutKeyboardFunc(keyboard);
269    glutMainLoop();
270    return 0;
271 }
272 
273 #else
main(int argc,char ** argv)274 int main(int argc, char** argv)
275 {
276     fprintf (stderr, "This program demonstrates the new tesselator API in GLU 1.2.\n");
277     fprintf (stderr, "Your GLU library does not support this new interface, sorry.\n");
278     return 0;
279 }
280 #endif
281