Merge commit 'origin/master' into gallium-0.2
[mesa.git] / progs / redbook / tesswind.c
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 <GL/glut.h>
46 #include <stdlib.h>
47 #include <stdio.h>
48
49 #ifdef GLU_VERSION_1_2
50
51 /* Win32 calling conventions. */
52 #ifndef CALLBACK
53 #define CALLBACK
54 #endif
55
56 GLdouble currentWinding = GLU_TESS_WINDING_ODD;
57 int currentShape = 0;
58 GLUtesselator *tobj;
59 GLuint list;
60
61 /* Make four display lists,
62 * each with a different tessellated object.
63 */
64 void makeNewLists (void) {
65 int i;
66 static GLdouble rects[12][3] =
67 {{ 50.0, 50.0, 0.0}, {300.0, 50.0, 0.0},
68 {300.0, 300.0, 0.0}, { 50.0, 300.0, 0.0},
69 {100.0, 100.0, 0.0}, {250.0, 100.0, 0.0},
70 {250.0, 250.0, 0.0}, {100.0, 250.0, 0.0},
71 {150.0, 150.0, 0.0}, {200.0, 150.0, 0.0},
72 {200.0, 200.0, 0.0}, {150.0, 200.0, 0.0}};
73 static GLdouble spiral[16][3] =
74 {{400.0, 250.0, 0.0}, {400.0, 50.0, 0.0},
75 { 50.0, 50.0, 0.0}, { 50.0, 400.0, 0.0},
76 {350.0, 400.0, 0.0}, {350.0, 100.0, 0.0},
77 {100.0, 100.0, 0.0}, {100.0, 350.0, 0.0},
78 {300.0, 350.0, 0.0}, {300.0, 150.0, 0.0},
79 {150.0, 150.0, 0.0}, {150.0, 300.0, 0.0},
80 {250.0, 300.0, 0.0}, {250.0, 200.0, 0.0},
81 {200.0, 200.0, 0.0}, {200.0, 250.0, 0.0}};
82 static GLdouble quad1[4][3] =
83 {{ 50.0, 150.0, 0.0}, {350.0, 150.0, 0.0},
84 {350.0, 200.0, 0.0}, { 50.0, 200.0, 0.0}};
85 static GLdouble quad2[4][3] =
86 {{100.0, 100.0, 0.0}, {300.0, 100.0, 0.0},
87 {300.0, 350.0, 0.0}, {100.0, 350.0, 0.0}};
88 static GLdouble tri[3][3] =
89 {{200.0, 50.0, 0.0}, {250.0, 300.0, 0.0},
90 {150.0, 300.0, 0.0}};
91
92 gluTessProperty(tobj, GLU_TESS_WINDING_RULE,
93 currentWinding);
94
95 glNewList(list, GL_COMPILE);
96 gluTessBeginPolygon(tobj, NULL);
97 gluTessBeginContour(tobj);
98 for (i = 0; i < 4; i++)
99 gluTessVertex(tobj, rects[i], rects[i]);
100 gluTessEndContour(tobj);
101 gluTessBeginContour(tobj);
102 for (i = 4; i < 8; i++)
103 gluTessVertex(tobj, rects[i], rects[i]);
104 gluTessEndContour(tobj);
105 gluTessBeginContour(tobj);
106 for (i = 8; i < 12; i++)
107 gluTessVertex(tobj, rects[i], rects[i]);
108 gluTessEndContour(tobj);
109 gluTessEndPolygon(tobj);
110 glEndList();
111
112 glNewList(list+1, GL_COMPILE);
113 gluTessBeginPolygon(tobj, NULL);
114 gluTessBeginContour(tobj);
115 for (i = 0; i < 4; i++)
116 gluTessVertex(tobj, rects[i], rects[i]);
117 gluTessEndContour(tobj);
118 gluTessBeginContour(tobj);
119 for (i = 7; i >= 4; i--)
120 gluTessVertex(tobj, rects[i], rects[i]);
121 gluTessEndContour(tobj);
122 gluTessBeginContour(tobj);
123 for (i = 11; i >= 8; i--)
124 gluTessVertex(tobj, rects[i], rects[i]);
125 gluTessEndContour(tobj);
126 gluTessEndPolygon(tobj);
127 glEndList();
128
129 glNewList(list+2, GL_COMPILE);
130 gluTessBeginPolygon(tobj, NULL);
131 gluTessBeginContour(tobj);
132 for (i = 0; i < 16; i++)
133 gluTessVertex(tobj, spiral[i], spiral[i]);
134 gluTessEndContour(tobj);
135 gluTessEndPolygon(tobj);
136 glEndList();
137
138 glNewList(list+3, GL_COMPILE);
139 gluTessBeginPolygon(tobj, NULL);
140 gluTessBeginContour(tobj);
141 for (i = 0; i < 4; i++)
142 gluTessVertex(tobj, quad1[i], quad1[i]);
143 gluTessEndContour(tobj);
144 gluTessBeginContour(tobj);
145 for (i = 0; i < 4; i++)
146 gluTessVertex(tobj, quad2[i], quad2[i]);
147 gluTessEndContour(tobj);
148 gluTessBeginContour(tobj);
149 for (i = 0; i < 3; i++)
150 gluTessVertex(tobj, tri[i], tri[i]);
151 gluTessEndContour(tobj);
152 gluTessEndPolygon(tobj);
153 glEndList();
154 }
155
156 void display (void) {
157 glClear(GL_COLOR_BUFFER_BIT);
158 glColor3f(1.0, 1.0, 1.0);
159 glPushMatrix();
160 glCallList(list);
161 glTranslatef(0.0, 500.0, 0.0);
162 glCallList(list+1);
163 glTranslatef(500.0, -500.0, 0.0);
164 glCallList(list+2);
165 glTranslatef(0.0, 500.0, 0.0);
166 glCallList(list+3);
167 glPopMatrix();
168 glFlush();
169 }
170
171 void CALLBACK beginCallback(GLenum which)
172 {
173 glBegin(which);
174 }
175
176 void CALLBACK errorCallback(GLenum errorCode)
177 {
178 const GLubyte *estring;
179
180 estring = gluErrorString(errorCode);
181 fprintf(stderr, "Tessellation Error: %s\n", (char *) estring);
182 exit(0);
183 }
184
185 void CALLBACK endCallback(void)
186 {
187 glEnd();
188 }
189
190 /* combineCallback is used to create a new vertex when edges
191 * intersect. coordinate location is trivial to calculate,
192 * but weight[4] may be used to average color, normal, or texture
193 * coordinate data.
194 */
195 /* ARGSUSED */
196 void CALLBACK combineCallback(GLdouble coords[3], GLdouble *data[4],
197 GLfloat weight[4], GLdouble **dataOut )
198 {
199 GLdouble *vertex;
200 vertex = (GLdouble *) malloc(3 * sizeof(GLdouble));
201
202 vertex[0] = coords[0];
203 vertex[1] = coords[1];
204 vertex[2] = coords[2];
205 *dataOut = vertex;
206 }
207
208 void init(void)
209 {
210 glClearColor(0.0, 0.0, 0.0, 0.0);
211 glShadeModel(GL_FLAT);
212
213 tobj = gluNewTess();
214 gluTessCallback(tobj, GLU_TESS_VERTEX,
215 (GLvoid (CALLBACK*) ()) &glVertex3dv);
216 gluTessCallback(tobj, GLU_TESS_BEGIN,
217 (GLvoid (CALLBACK*) ()) &beginCallback);
218 gluTessCallback(tobj, GLU_TESS_END,
219 (GLvoid (CALLBACK*) ()) &endCallback);
220 gluTessCallback(tobj, GLU_TESS_ERROR,
221 (GLvoid (CALLBACK*) ()) &errorCallback);
222 gluTessCallback(tobj, GLU_TESS_COMBINE,
223 (GLvoid (CALLBACK*) ()) &combineCallback);
224
225 list = glGenLists(4);
226 makeNewLists();
227 }
228
229 void reshape(int w, int h)
230 {
231 glViewport(0, 0, (GLsizei) w, (GLsizei) h);
232 glMatrixMode(GL_PROJECTION);
233 glLoadIdentity();
234 if (w <= h)
235 gluOrtho2D(0.0, 1000.0, 0.0, 1000.0 * (GLdouble)h/(GLdouble)w);
236 else
237 gluOrtho2D(0.0, 1000.0 * (GLdouble)w/(GLdouble)h, 0.0, 1000.0);
238 glMatrixMode(GL_MODELVIEW);
239 glLoadIdentity();
240 }
241
242 /* ARGSUSED1 */
243 void keyboard(unsigned char key, int x, int y)
244 {
245 switch (key) {
246 case 'w':
247 case 'W':
248 if (currentWinding == GLU_TESS_WINDING_ODD)
249 currentWinding = GLU_TESS_WINDING_NONZERO;
250 else if (currentWinding == GLU_TESS_WINDING_NONZERO)
251 currentWinding = GLU_TESS_WINDING_POSITIVE;
252 else if (currentWinding == GLU_TESS_WINDING_POSITIVE)
253 currentWinding = GLU_TESS_WINDING_NEGATIVE;
254 else if (currentWinding == GLU_TESS_WINDING_NEGATIVE)
255 currentWinding = GLU_TESS_WINDING_ABS_GEQ_TWO;
256 else if (currentWinding == GLU_TESS_WINDING_ABS_GEQ_TWO)
257 currentWinding = GLU_TESS_WINDING_ODD;
258 makeNewLists();
259 glutPostRedisplay();
260 break;
261 case 27:
262 exit(0);
263 break;
264 default:
265 break;
266 }
267 }
268
269 int main(int argc, char** argv)
270 {
271 glutInit(&argc, argv);
272 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
273 glutInitWindowSize(500, 500);
274 glutCreateWindow(argv[0]);
275 init();
276 glutDisplayFunc(display);
277 glutReshapeFunc(reshape);
278 glutKeyboardFunc(keyboard);
279 glutMainLoop();
280 return 0;
281 }
282
283 #else
284 int main(int argc, char** argv)
285 {
286 fprintf (stderr, "This program demonstrates the new tesselator API in GLU 1.2.\n");
287 fprintf (stderr, "Your GLU library does not support this new interface, sorry.\n");
288 return 0;
289 }
290 #endif