Merge branch '7.8'
[mesa.git] / progs / redbook / tess.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 * tess.c
40 * This program demonstrates polygon tessellation.
41 * Two tesselated objects are drawn. The first is a
42 * rectangle with a triangular hole. The second is a
43 * smooth shaded, self-intersecting star.
44 *
45 * Note the exterior rectangle is drawn with its vertices
46 * in counter-clockwise order, but its interior clockwise.
47 * Note the combineCallback is needed for the self-intersecting
48 * star. Also note that removing the TessProperty for the
49 * star will make the interior unshaded (WINDING_ODD).
50 */
51 #include <GL/glut.h>
52 #include <stdlib.h>
53 #include <stdio.h>
54
55 #ifdef GLU_VERSION_1_2
56
57 /* Win32 calling conventions. */
58 #ifndef CALLBACK
59 #define CALLBACK
60 #endif
61
62 GLuint startList;
63
64 static void display (void) {
65 glClear(GL_COLOR_BUFFER_BIT);
66 glColor3f(1.0, 1.0, 1.0);
67 glCallList(startList);
68 glCallList(startList + 1);
69 glFlush();
70 }
71
72 static void CALLBACK beginCallback(GLenum which)
73 {
74 glBegin(which);
75 }
76
77 static void CALLBACK errorCallback(GLenum errorCode)
78 {
79 const GLubyte *estring;
80
81 estring = gluErrorString(errorCode);
82 fprintf(stderr, "Tessellation Error: %s\n", (char *) estring);
83 exit(0);
84 }
85
86 static void CALLBACK endCallback(void)
87 {
88 glEnd();
89 }
90
91 static void CALLBACK vertexCallback(GLvoid *vertex)
92 {
93 const GLdouble *pointer;
94
95 pointer = (GLdouble *) vertex;
96 glColor3dv(pointer+3);
97 glVertex3dv(pointer);
98 }
99
100 /* combineCallback is used to create a new vertex when edges
101 * intersect. coordinate location is trivial to calculate,
102 * but weight[4] may be used to average color, normal, or texture
103 * coordinate data. In this program, color is weighted.
104 */
105 static void CALLBACK combineCallback(GLdouble coords[3],
106 GLdouble *vertex_data[4],
107 GLfloat weight[4], GLdouble **dataOut )
108 {
109 GLdouble *vertex;
110 int i;
111
112 vertex = (GLdouble *) malloc(6 * sizeof(GLdouble));
113
114 vertex[0] = coords[0];
115 vertex[1] = coords[1];
116 vertex[2] = coords[2];
117 for (i = 3; i < 6; i++)
118 vertex[i] = weight[0] * vertex_data[0][i]
119 + weight[1] * vertex_data[1][i]
120 + weight[2] * vertex_data[2][i]
121 + weight[3] * vertex_data[3][i];
122 *dataOut = vertex;
123 }
124
125 static void init (void)
126 {
127 GLUtesselator *tobj;
128 GLdouble rect[4][3] = {{50.0, 50.0, 0.0},
129 {200.0, 50.0, 0.0},
130 {200.0, 200.0, 0.0},
131 {50.0, 200.0, 0.0}};
132 GLdouble tri[3][3] = {{75.0, 75.0, 0.0},
133 {125.0, 175.0, 0.0},
134 {175.0, 75.0, 0.0}};
135 GLdouble star[5][6] = {{250.0, 50.0, 0.0, 1.0, 0.0, 1.0},
136 {325.0, 200.0, 0.0, 1.0, 1.0, 0.0},
137 {400.0, 50.0, 0.0, 0.0, 1.0, 1.0},
138 {250.0, 150.0, 0.0, 1.0, 0.0, 0.0},
139 {400.0, 150.0, 0.0, 0.0, 1.0, 0.0}};
140
141 glClearColor(0.0, 0.0, 0.0, 0.0);
142
143 startList = glGenLists(2);
144
145 tobj = gluNewTess();
146 gluTessCallback(tobj, GLU_TESS_VERTEX,
147 (GLvoid (CALLBACK*) ()) &glVertex3dv);
148 gluTessCallback(tobj, GLU_TESS_BEGIN,
149 (GLvoid (CALLBACK*) ()) &beginCallback);
150 gluTessCallback(tobj, GLU_TESS_END,
151 (GLvoid (CALLBACK*) ()) &endCallback);
152 gluTessCallback(tobj, GLU_TESS_ERROR,
153 (GLvoid (CALLBACK*) ()) &errorCallback);
154
155 /* rectangle with triangular hole inside */
156 glNewList(startList, GL_COMPILE);
157 glShadeModel(GL_FLAT);
158 gluTessBeginPolygon(tobj, NULL);
159 gluTessBeginContour(tobj);
160 gluTessVertex(tobj, rect[0], rect[0]);
161 gluTessVertex(tobj, rect[1], rect[1]);
162 gluTessVertex(tobj, rect[2], rect[2]);
163 gluTessVertex(tobj, rect[3], rect[3]);
164 gluTessEndContour(tobj);
165 gluTessBeginContour(tobj);
166 gluTessVertex(tobj, tri[0], tri[0]);
167 gluTessVertex(tobj, tri[1], tri[1]);
168 gluTessVertex(tobj, tri[2], tri[2]);
169 gluTessEndContour(tobj);
170 gluTessEndPolygon(tobj);
171 glEndList();
172
173 gluTessCallback(tobj, GLU_TESS_VERTEX,
174 (GLvoid (CALLBACK*) ()) &vertexCallback);
175 gluTessCallback(tobj, GLU_TESS_BEGIN,
176 (GLvoid (CALLBACK*) ()) &beginCallback);
177 gluTessCallback(tobj, GLU_TESS_END,
178 (GLvoid (CALLBACK*) ()) &endCallback);
179 gluTessCallback(tobj, GLU_TESS_ERROR,
180 (GLvoid (CALLBACK*) ()) &errorCallback);
181 gluTessCallback(tobj, GLU_TESS_COMBINE,
182 (GLvoid (CALLBACK*) ()) &combineCallback);
183
184 /* smooth shaded, self-intersecting star */
185 glNewList(startList + 1, GL_COMPILE);
186 glShadeModel(GL_SMOOTH);
187 gluTessProperty(tobj, GLU_TESS_WINDING_RULE,
188 GLU_TESS_WINDING_POSITIVE);
189 gluTessBeginPolygon(tobj, NULL);
190 gluTessBeginContour(tobj);
191 gluTessVertex(tobj, star[0], star[0]);
192 gluTessVertex(tobj, star[1], star[1]);
193 gluTessVertex(tobj, star[2], star[2]);
194 gluTessVertex(tobj, star[3], star[3]);
195 gluTessVertex(tobj, star[4], star[4]);
196 gluTessEndContour(tobj);
197 gluTessEndPolygon(tobj);
198 glEndList();
199 gluDeleteTess(tobj);
200 }
201
202 static void reshape (int w, int h)
203 {
204 glViewport(0, 0, (GLsizei) w, (GLsizei) h);
205 glMatrixMode(GL_PROJECTION);
206 glLoadIdentity();
207 gluOrtho2D(0.0, (GLdouble) w, 0.0, (GLdouble) h);
208 }
209
210 /* ARGSUSED1 */
211 static void keyboard(unsigned char key, int x, int y)
212 {
213 switch (key) {
214 case 27:
215 exit(0);
216 break;
217 }
218 }
219
220 int main(int argc, char** argv)
221 {
222 glutInit(&argc, argv);
223 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
224 glutInitWindowSize(500, 500);
225 glutCreateWindow(argv[0]);
226 init();
227 glutDisplayFunc(display);
228 glutReshapeFunc(reshape);
229 glutKeyboardFunc(keyboard);
230 glutMainLoop();
231 return 0;
232 }
233
234 #else
235 int main(int argc, char** argv)
236 {
237 fprintf (stderr, "This program demonstrates the new tesselator API in GLU 1.2.\n");
238 fprintf (stderr, "Your GLU library does not support this new interface, sorry.\n");
239 return 0;
240 }
241 #endif