blob: 0bf8a8eb9867d31fc512e6b789d382d6e92601db [file] [log] [blame]
jtgafb833d1999-08-19 00:55:39 +00001/*
2 * Copyright (c) 1993-1997, Silicon Graphics, Inc.
Gareth Hughesdcf11bd1999-10-03 01:00:33 +00003 * ALL RIGHTS RESERVED
4 * Permission to use, copy, modify, and distribute this software for
jtgafb833d1999-08-19 00:55:39 +00005 * 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
Gareth Hughesdcf11bd1999-10-03 01:00:33 +00007 * and this permission notice appear in supporting documentation, and that
jtgafb833d1999-08-19 00:55:39 +00008 * the name of Silicon Graphics, Inc. not be used in advertising
9 * or publicity pertaining to distribution of the software without specific,
Gareth Hughesdcf11bd1999-10-03 01:00:33 +000010 * written prior permission.
jtgafb833d1999-08-19 00:55:39 +000011 *
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.
Gareth Hughesdcf11bd1999-10-03 01:00:33 +000024 *
25 * US Government Users Restricted Rights
jtgafb833d1999-08-19 00:55:39 +000026 * 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
Gareth Hughesdcf11bd1999-10-03 01:00:33 +000048 * star. Also note that removing the TessProperty for the
jtgafb833d1999-08-19 00:55:39 +000049 * 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
62GLuint startList;
63
Vinson Lee07b54fe2009-12-21 15:20:01 -080064static void display (void) {
jtgafb833d1999-08-19 00:55:39 +000065 glClear(GL_COLOR_BUFFER_BIT);
66 glColor3f(1.0, 1.0, 1.0);
67 glCallList(startList);
68 glCallList(startList + 1);
69 glFlush();
70}
71
Vinson Lee07b54fe2009-12-21 15:20:01 -080072static void CALLBACK beginCallback(GLenum which)
jtgafb833d1999-08-19 00:55:39 +000073{
74 glBegin(which);
75}
76
Vinson Lee07b54fe2009-12-21 15:20:01 -080077static void CALLBACK errorCallback(GLenum errorCode)
jtgafb833d1999-08-19 00:55:39 +000078{
79 const GLubyte *estring;
80
81 estring = gluErrorString(errorCode);
Brian Paulff389b02003-04-21 14:50:49 +000082 fprintf(stderr, "Tessellation Error: %s\n", (char *) estring);
jtgafb833d1999-08-19 00:55:39 +000083 exit(0);
84}
85
Vinson Lee07b54fe2009-12-21 15:20:01 -080086static void CALLBACK endCallback(void)
jtgafb833d1999-08-19 00:55:39 +000087{
88 glEnd();
89}
90
Vinson Lee07b54fe2009-12-21 15:20:01 -080091static void CALLBACK vertexCallback(GLvoid *vertex)
jtgafb833d1999-08-19 00:55:39 +000092{
93 const GLdouble *pointer;
94
95 pointer = (GLdouble *) vertex;
96 glColor3dv(pointer+3);
Brian Paulf02a5f62002-07-12 15:54:01 +000097 glVertex3dv(pointer);
jtgafb833d1999-08-19 00:55:39 +000098}
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 */
Vinson Lee07b54fe2009-12-21 15:20:01 -0800105static void CALLBACK combineCallback(GLdouble coords[3],
jtgafb833d1999-08-19 00:55:39 +0000106 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];
Gareth Hughesdcf11bd1999-10-03 01:00:33 +0000117 for (i = 3; i < 6; i++)
118 vertex[i] = weight[0] * vertex_data[0][i]
jtgafb833d1999-08-19 00:55:39 +0000119 + weight[1] * vertex_data[1][i]
Gareth Hughesdcf11bd1999-10-03 01:00:33 +0000120 + weight[2] * vertex_data[2][i]
jtgafb833d1999-08-19 00:55:39 +0000121 + weight[3] * vertex_data[3][i];
122 *dataOut = vertex;
123}
124
Vinson Lee07b54fe2009-12-21 15:20:01 -0800125static void init (void)
jtgafb833d1999-08-19 00:55:39 +0000126{
127 GLUtesselator *tobj;
Brian Paul37ff9431999-11-11 14:37:24 +0000128 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}};
jtgafb833d1999-08-19 00:55:39 +0000140
141 glClearColor(0.0, 0.0, 0.0, 0.0);
142
143 startList = glGenLists(2);
144
145 tobj = gluNewTess();
Gareth Hughesdcf11bd1999-10-03 01:00:33 +0000146 gluTessCallback(tobj, GLU_TESS_VERTEX,
jtgafb833d1999-08-19 00:55:39 +0000147 (GLvoid (CALLBACK*) ()) &glVertex3dv);
Gareth Hughesdcf11bd1999-10-03 01:00:33 +0000148 gluTessCallback(tobj, GLU_TESS_BEGIN,
jtgafb833d1999-08-19 00:55:39 +0000149 (GLvoid (CALLBACK*) ()) &beginCallback);
Gareth Hughesdcf11bd1999-10-03 01:00:33 +0000150 gluTessCallback(tobj, GLU_TESS_END,
jtgafb833d1999-08-19 00:55:39 +0000151 (GLvoid (CALLBACK*) ()) &endCallback);
Gareth Hughesdcf11bd1999-10-03 01:00:33 +0000152 gluTessCallback(tobj, GLU_TESS_ERROR,
jtgafb833d1999-08-19 00:55:39 +0000153 (GLvoid (CALLBACK*) ()) &errorCallback);
154
155 /* rectangle with triangular hole inside */
156 glNewList(startList, GL_COMPILE);
Gareth Hughesdcf11bd1999-10-03 01:00:33 +0000157 glShadeModel(GL_FLAT);
jtgafb833d1999-08-19 00:55:39 +0000158 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
Gareth Hughesdcf11bd1999-10-03 01:00:33 +0000173 gluTessCallback(tobj, GLU_TESS_VERTEX,
jtgafb833d1999-08-19 00:55:39 +0000174 (GLvoid (CALLBACK*) ()) &vertexCallback);
Gareth Hughesdcf11bd1999-10-03 01:00:33 +0000175 gluTessCallback(tobj, GLU_TESS_BEGIN,
jtgafb833d1999-08-19 00:55:39 +0000176 (GLvoid (CALLBACK*) ()) &beginCallback);
Gareth Hughesdcf11bd1999-10-03 01:00:33 +0000177 gluTessCallback(tobj, GLU_TESS_END,
jtgafb833d1999-08-19 00:55:39 +0000178 (GLvoid (CALLBACK*) ()) &endCallback);
Gareth Hughesdcf11bd1999-10-03 01:00:33 +0000179 gluTessCallback(tobj, GLU_TESS_ERROR,
jtgafb833d1999-08-19 00:55:39 +0000180 (GLvoid (CALLBACK*) ()) &errorCallback);
Gareth Hughesdcf11bd1999-10-03 01:00:33 +0000181 gluTessCallback(tobj, GLU_TESS_COMBINE,
jtgafb833d1999-08-19 00:55:39 +0000182 (GLvoid (CALLBACK*) ()) &combineCallback);
183
184 /* smooth shaded, self-intersecting star */
185 glNewList(startList + 1, GL_COMPILE);
Gareth Hughesdcf11bd1999-10-03 01:00:33 +0000186 glShadeModel(GL_SMOOTH);
jtgafb833d1999-08-19 00:55:39 +0000187 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
Vinson Lee07b54fe2009-12-21 15:20:01 -0800202static void reshape (int w, int h)
jtgafb833d1999-08-19 00:55:39 +0000203{
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 */
Vinson Lee07b54fe2009-12-21 15:20:01 -0800211static void keyboard(unsigned char key, int x, int y)
jtgafb833d1999-08-19 00:55:39 +0000212{
213 switch (key) {
214 case 27:
215 exit(0);
216 break;
217 }
218}
219
220int 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();
Gareth Hughesdcf11bd1999-10-03 01:00:33 +0000231 return 0;
jtgafb833d1999-08-19 00:55:39 +0000232}
233
234#else
235int 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