2 * Copyright (c) 1993-1997, Silicon Graphics, Inc.
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.
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.
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.
35 * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
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.
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).
55 #ifdef GLU_VERSION_1_2
57 /* Win32 calling conventions. */
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);
72 static void CALLBACK
beginCallback(GLenum which
)
77 static void CALLBACK
errorCallback(GLenum errorCode
)
79 const GLubyte
*estring
;
81 estring
= gluErrorString(errorCode
);
82 fprintf(stderr
, "Tessellation Error: %s\n", (char *) estring
);
86 static void CALLBACK
endCallback(void)
91 static void CALLBACK
vertexCallback(GLvoid
*vertex
)
93 const GLdouble
*pointer
;
95 pointer
= (GLdouble
*) vertex
;
96 glColor3dv(pointer
+3);
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.
105 static void CALLBACK
combineCallback(GLdouble coords
[3],
106 GLdouble
*vertex_data
[4],
107 GLfloat weight
[4], GLdouble
**dataOut
)
112 vertex
= (GLdouble
*) malloc(6 * sizeof(GLdouble
));
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
];
125 static void init (void)
128 GLdouble rect
[4][3] = {{50.0, 50.0, 0.0},
132 GLdouble tri
[3][3] = {{75.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}};
141 glClearColor(0.0, 0.0, 0.0, 0.0);
143 startList
= glGenLists(2);
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
);
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
);
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
);
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
);
202 static void reshape (int w
, int h
)
204 glViewport(0, 0, (GLsizei
) w
, (GLsizei
) h
);
205 glMatrixMode(GL_PROJECTION
);
207 gluOrtho2D(0.0, (GLdouble
) w
, 0.0, (GLdouble
) h
);
211 static void keyboard(unsigned char key
, int x
, int y
)
220 int main(int argc
, char** argv
)
222 glutInit(&argc
, argv
);
223 glutInitDisplayMode(GLUT_SINGLE
| GLUT_RGB
);
224 glutInitWindowSize(500, 500);
225 glutCreateWindow(argv
[0]);
227 glutDisplayFunc(display
);
228 glutReshapeFunc(reshape
);
229 glutKeyboardFunc(keyboard
);
235 int main(int argc
, char** argv
)
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");