1 /* $Id: tessdemo.c,v 1.8 2000/07/11 14:11:58 brianp Exp $ */
4 * A demo of the GLU polygon tesselation functions written by Bogdan Sikorski.
13 #define MAX_POINTS 200
14 #define MAX_CONTOURS 50
18 { QUIT
, TESSELATE
, CLEAR
}
22 { DEFINE
, TESSELATED
}
27 GLint p
[MAX_POINTS
][2];
30 contours
[MAX_CONTOURS
];
32 static GLuint contour_cnt
;
33 static GLsizei width
, height
;
34 static mode_type mode
;
41 GLclampf p_color
[3][3];
46 static void GLCALLBACK
52 glColor3f(0.9, 0.9, 0.9);
54 str
= (const char *) gluErrorString(err
);
56 for (i
= 0; i
< len
; i
++)
57 glutBitmapCharacter(GLUT_BITMAP_9_BY_15
, str
[i
]);
61 static void GLCALLBACK
62 begin_callback(GLenum mode
)
68 static void GLCALLBACK
69 edge_callback(GLenum flag
)
71 if (flag
== GL_TRUE
) {
72 triangle
.color
[0] = 1.0;
73 triangle
.color
[1] = 1.0;
74 triangle
.color
[2] = 0.5;
77 triangle
.color
[0] = 1.0;
78 triangle
.color
[1] = 0.0;
79 triangle
.color
[2] = 0.0;
84 static void GLCALLBACK
88 glColor3f(triangle
.p_color
[0][0], triangle
.p_color
[0][1],
89 triangle
.p_color
[0][2]);
90 glVertex2i(triangle
.p
[0][0], triangle
.p
[0][1]);
91 glVertex2i(triangle
.p
[1][0], triangle
.p
[1][1]);
92 glColor3f(triangle
.p_color
[1][0], triangle
.p_color
[1][1],
93 triangle
.p_color
[1][2]);
94 glVertex2i(triangle
.p
[1][0], triangle
.p
[1][1]);
95 glVertex2i(triangle
.p
[2][0], triangle
.p
[2][1]);
96 glColor3f(triangle
.p_color
[2][0], triangle
.p_color
[2][1],
97 triangle
.p_color
[2][2]);
98 glVertex2i(triangle
.p
[2][0], triangle
.p
[2][1]);
99 glVertex2i(triangle
.p
[0][0], triangle
.p
[0][1]);
104 static void GLCALLBACK
105 vertex_callback(void *data
)
112 triangle
.p
[no
][0] = p
[0];
113 triangle
.p
[no
][1] = p
[1];
114 triangle
.p_color
[no
][0] = triangle
.color
[0];
115 triangle
.p_color
[no
][1] = triangle
.color
[1];
116 triangle
.p_color
[no
][2] = triangle
.color
[2];
122 set_screen_wh(GLsizei w
, GLsizei h
)
132 GLUtriangulatorObj
*tobj
;
134 GLuint i
, j
, point_cnt
;
138 glClear(GL_COLOR_BUFFER_BIT
);
139 glColor3f(0.7, 0.7, 0.0);
140 gluTessCallback(tobj
, GLU_BEGIN
, glBegin
);
141 gluTessCallback(tobj
, GLU_END
, glEnd
);
142 gluTessCallback(tobj
, GLU_ERROR
, my_error
);
143 gluTessCallback(tobj
, GLU_VERTEX
, glVertex2iv
);
144 gluBeginPolygon(tobj
);
145 for (j
= 0; j
<= contour_cnt
; j
++) {
146 point_cnt
= contours
[j
].point_cnt
;
147 gluNextContour(tobj
, GLU_UNKNOWN
);
148 for (i
= 0; i
< point_cnt
; i
++) {
149 data
[0] = (GLdouble
) (contours
[j
].p
[i
][0]);
150 data
[1] = (GLdouble
) (contours
[j
].p
[i
][1]);
152 gluTessVertex(tobj
, data
, contours
[j
].p
[i
]);
157 gluTessCallback(tobj
, GLU_BEGIN
, begin_callback
);
158 gluTessCallback(tobj
, GLU_END
, end_callback
);
159 gluTessCallback(tobj
, GLU_VERTEX
, vertex_callback
);
160 gluTessCallback(tobj
, GLU_EDGE_FLAG
, edge_callback
);
161 gluBeginPolygon(tobj
);
162 for (j
= 0; j
<= contour_cnt
; j
++) {
163 point_cnt
= contours
[j
].point_cnt
;
164 gluNextContour(tobj
, GLU_UNKNOWN
);
165 for (i
= 0; i
< point_cnt
; i
++) {
166 data
[0] = (GLdouble
) (contours
[j
].p
[i
][0]);
167 data
[1] = (GLdouble
) (contours
[j
].p
[i
][1]);
169 gluTessVertex(tobj
, data
, contours
[j
].p
[i
]);
175 glColor3f(1.0, 1.0, 0.0);
183 left_down(int x1
, int y1
)
188 /* translate GLUT into GL coordinates */
191 point_cnt
= contours
[contour_cnt
].point_cnt
;
192 contours
[contour_cnt
].p
[point_cnt
][0] = P
[0];
193 contours
[contour_cnt
].p
[point_cnt
][1] = P
[1];
196 glVertex2iv(contours
[contour_cnt
].p
[point_cnt
- 1]);
205 ++(contours
[contour_cnt
].point_cnt
);
210 middle_down(int x1
, int y1
)
214 point_cnt
= contours
[contour_cnt
].point_cnt
;
217 glVertex2iv(contours
[contour_cnt
].p
[0]);
218 glVertex2iv(contours
[contour_cnt
].p
[point_cnt
- 1]);
219 contours
[contour_cnt
].p
[point_cnt
][0] = -1;
223 contours
[contour_cnt
].point_cnt
= 0;
229 mouse_clicked(int button
, int state
, int x
, int y
)
234 case GLUT_LEFT_BUTTON
:
235 if (state
== GLUT_DOWN
)
238 case GLUT_MIDDLE_BUTTON
:
239 if (state
== GLUT_DOWN
)
252 glClear(GL_COLOR_BUFFER_BIT
);
256 glColor3f(0.6, 0.5, 0.5);
258 for (i
= 0; i
< width
; i
+= 10)
259 for (j
= 0; j
< height
; j
+= 10) {
261 glVertex2i(width
, j
);
262 glVertex2i(i
, height
);
266 glColor3f(1.0, 1.0, 0.0);
267 for (i
= 0; i
<= contour_cnt
; i
++) {
268 point_cnt
= contours
[i
].point_cnt
;
274 glVertex2iv(contours
[i
].p
[0]);
275 glVertex2iv(contours
[i
].p
[0]);
278 glVertex2iv(contours
[i
].p
[0]);
279 glVertex2iv(contours
[i
].p
[1]);
283 for (j
= 0; j
< point_cnt
; j
++) {
284 glVertex2iv(contours
[i
].p
[j
]);
285 glVertex2iv(contours
[i
].p
[j
+ 1]);
287 if (contours
[i
].p
[j
+ 1][0] == -1) {
288 glVertex2iv(contours
[i
].p
[0]);
289 glVertex2iv(contours
[i
].p
[j
]);
303 glColor3f(1.0, 1.0, 0.0);
311 contours
[0].point_cnt
= 0;
312 glutMouseFunc(mouse_clicked
);
326 menu_selected(int entry
)
343 key_pressed(unsigned char key
, int x
, int y
)
366 /* clear background to gray */
367 glClearColor(0.4, 0.4, 0.4, 0.0);
368 glShadeModel(GL_FLAT
);
370 menu
= glutCreateMenu(menu_selected
);
371 glutAddMenuEntry("clear", CLEAR
);
372 glutAddMenuEntry("tesselate", TESSELATE
);
373 glutAddMenuEntry("quit", QUIT
);
374 glutAttachMenu(GLUT_RIGHT_BUTTON
);
375 glutMouseFunc(mouse_clicked
);
376 glutKeyboardFunc(key_pressed
);
378 glPolygonMode(GL_FRONT
, GL_FILL
);
384 reshape(GLsizei w
, GLsizei h
)
386 glViewport(0, 0, w
, h
);
387 glMatrixMode(GL_PROJECTION
);
389 glOrtho(0.0, (GLdouble
) w
, 0.0, (GLdouble
) h
, -1.0, 1.0);
390 glMatrixMode(GL_MODELVIEW
);
399 printf("Use left mouse button to place vertices.\n");
400 printf("Press middle mouse button when done.\n");
401 printf("Select tesselate from the pop-up menu.\n");
406 * Open window with initial window size, title bar,
407 * RGBA display mode, and handle input events.
410 main(int argc
, char **argv
)
413 glutInit(&argc
, argv
);
414 glutInitDisplayMode(GLUT_SINGLE
| GLUT_RGB
);
415 glutInitWindowSize(400, 400);
416 glutCreateWindow(argv
[0]);
418 glutDisplayFunc(display
);
419 glutReshapeFunc(reshape
);