1 /* $Id: tessdemo.c,v 1.1 1999/08/19 00:55:40 jtg Exp $ */
4 * A demo of the GLU polygon tesselation functions written by Bogdan Sikorski.
5 * This demo isn't built by the Makefile because it needs GLUT. After you've
6 * installed GLUT you can try this demo.
7 * Here's the command for IRIX, for example:
8 cc -g -ansi -prototypes -fullwarn -float -I../include -DSHM tess_demo.c -L../lib -lglut -lMesaGLU -lMesaGL -lm -lX11 -lXext -lXmu -lfpe -lXext -o tess_demo
13 * $Log: tessdemo.c,v $
14 * Revision 1.1 1999/08/19 00:55:40 jtg
17 * Revision 3.5 1999/03/28 18:24:37 brianp
20 * Revision 3.4 1999/02/14 03:37:07 brianp
21 * fixed callback problem
23 * Revision 3.3 1998/07/26 01:25:26 brianp
24 * removed include of gl.h and glu.h
26 * Revision 3.2 1998/06/29 02:37:30 brianp
27 * minor changes for Windows (Ted Jump)
29 * Revision 3.1 1998/06/09 01:53:49 brianp
30 * main() should return an int
32 * Revision 3.0 1998/02/14 18:42:29 brianp
43 #define MAX_POINTS 200
44 #define MAX_CONTOURS 50
47 typedef enum{ QUIT
, TESSELATE
, CLEAR
} menu_entries
;
48 typedef enum{ DEFINE
, TESSELATED
} mode_type
;
51 GLint p
[MAX_POINTS
][2];
53 } contours
[MAX_CONTOURS
];
63 GLclampf p_color
[3][3];
69 #define GLCALLBACK CALLBACK
76 void GLCALLBACK
my_error(GLenum err
)
81 glColor3f(0.9,0.9,0.9);
83 str
=(const char *)gluErrorString(err
);
86 glutBitmapCharacter(GLUT_BITMAP_9_BY_15
,str
[i
]);
89 void GLCALLBACK
begin_callback(GLenum mode
)
94 void GLCALLBACK
edge_callback(GLenum flag
)
98 triangle
.color
[0]=1.0;
99 triangle
.color
[1]=1.0;
100 triangle
.color
[2]=0.5;
104 triangle
.color
[0]=1.0;
105 triangle
.color
[1]=0.0;
106 triangle
.color
[2]=0.0;
110 void GLCALLBACK
end_callback()
113 glColor3f(triangle
.p_color
[0][0],triangle
.p_color
[0][1],
114 triangle
.p_color
[0][2]);
115 glVertex2i(triangle
.p
[0][0],triangle
.p
[0][1]);
116 glVertex2i(triangle
.p
[1][0],triangle
.p
[1][1]);
117 glColor3f(triangle
.p_color
[1][0],triangle
.p_color
[1][1],
118 triangle
.p_color
[1][2]);
119 glVertex2i(triangle
.p
[1][0],triangle
.p
[1][1]);
120 glVertex2i(triangle
.p
[2][0],triangle
.p
[2][1]);
121 glColor3f(triangle
.p_color
[2][0],triangle
.p_color
[2][1],
122 triangle
.p_color
[2][2]);
123 glVertex2i(triangle
.p
[2][0],triangle
.p
[2][1]);
124 glVertex2i(triangle
.p
[0][0],triangle
.p
[0][1]);
128 void GLCALLBACK
vertex_callback(void *data
)
135 triangle
.p
[no
][0]=p
[0];
136 triangle
.p
[no
][1]=p
[1];
137 triangle
.p_color
[no
][0]=triangle
.color
[0];
138 triangle
.p_color
[no
][1]=triangle
.color
[1];
139 triangle
.p_color
[no
][2]=triangle
.color
[2];
143 void set_screen_wh(GLsizei w
, GLsizei h
)
151 GLUtriangulatorObj
*tobj
;
153 GLuint i
,j
,point_cnt
;
158 glClear(GL_COLOR_BUFFER_BIT
);
159 glColor3f (0.7, 0.7, 0.0);
160 gluTessCallback(tobj
,GLU_BEGIN
,glBegin
);
161 gluTessCallback(tobj
,GLU_END
,glEnd
);
162 gluTessCallback(tobj
,GLU_ERROR
,my_error
);
163 gluTessCallback(tobj
,GLU_VERTEX
,glVertex2iv
);
164 gluBeginPolygon(tobj
);
165 for(j
=0;j
<=contour_cnt
;j
++)
167 point_cnt
=contours
[j
].point_cnt
;
168 gluNextContour(tobj
,GLU_UNKNOWN
);
169 for(i
=0;i
<point_cnt
;i
++)
171 data
[0]=(GLdouble
)(contours
[j
].p
[i
][0]);
172 data
[1]=(GLdouble
)(contours
[j
].p
[i
][1]);
174 gluTessVertex(tobj
,data
,contours
[j
].p
[i
]);
179 gluTessCallback(tobj
,GLU_BEGIN
,begin_callback
);
180 gluTessCallback(tobj
,GLU_END
,end_callback
);
181 gluTessCallback(tobj
,GLU_VERTEX
,vertex_callback
);
182 gluTessCallback(tobj
,GLU_EDGE_FLAG
,edge_callback
);
183 gluBeginPolygon(tobj
);
184 for(j
=0;j
<=contour_cnt
;j
++)
186 point_cnt
=contours
[j
].point_cnt
;
187 gluNextContour(tobj
,GLU_UNKNOWN
);
188 for(i
=0;i
<point_cnt
;i
++)
190 data
[0]=(GLdouble
)(contours
[j
].p
[i
][0]);
191 data
[1]=(GLdouble
)(contours
[j
].p
[i
][1]);
193 gluTessVertex(tobj
,data
,contours
[j
].p
[i
]);
199 glColor3f (1.0, 1.0, 0.0);
205 void left_down(int x1
,int y1
)
210 /* translate GLUT into GL coordinates */
213 point_cnt
=contours
[contour_cnt
].point_cnt
;
214 contours
[contour_cnt
].p
[point_cnt
][0]=P
[0];
215 contours
[contour_cnt
].p
[point_cnt
][1]=P
[1];
219 glVertex2iv(contours
[contour_cnt
].p
[point_cnt
-1]);
229 ++(contours
[contour_cnt
].point_cnt
);
232 void middle_down(int x1
, int y1
)
238 point_cnt
=contours
[contour_cnt
].point_cnt
;
242 glVertex2iv(contours
[contour_cnt
].p
[0]);
243 glVertex2iv(contours
[contour_cnt
].p
[point_cnt
-1]);
244 contours
[contour_cnt
].p
[point_cnt
][0]= -1;
248 contours
[contour_cnt
].point_cnt
=0;
252 void mouse_clicked(int button
,int state
,int x
,int y
)
258 case GLUT_LEFT_BUTTON
:
262 case GLUT_MIDDLE_BUTTON
:
274 glClear(GL_COLOR_BUFFER_BIT
);
279 glColor3f (0.6,0.5,0.5);
281 for(i
=0;i
<width
;i
+=10)
282 for(j
=0;j
<height
;j
+=10)
286 glVertex2i(i
,height
);
289 glColor3f (1.0, 1.0, 0.0);
290 for(i
=0;i
<=contour_cnt
;i
++)
292 point_cnt
=contours
[i
].point_cnt
;
299 glVertex2iv(contours
[i
].p
[0]);
300 glVertex2iv(contours
[i
].p
[0]);
303 glVertex2iv(contours
[i
].p
[0]);
304 glVertex2iv(contours
[i
].p
[1]);
308 for(j
=0;j
<point_cnt
;j
++)
310 glVertex2iv(contours
[i
].p
[j
]);
311 glVertex2iv(contours
[i
].p
[j
+1]);
313 if(contours
[i
].p
[j
+1][0]== -1)
315 glVertex2iv(contours
[i
].p
[0]);
316 glVertex2iv(contours
[i
].p
[j
]);
330 glColor3f (1.0, 1.0, 0.0);
336 contours
[0].point_cnt
=0;
337 glutMouseFunc(mouse_clicked
);
347 void menu_selected(int entry
)
363 void key_pressed(unsigned char key
,int x
,int y
)
387 /* clear background to gray */
388 glClearColor (0.4, 0.4, 0.4, 0.0);
389 glShadeModel (GL_FLAT
);
391 menu
=glutCreateMenu(menu_selected
);
392 glutAddMenuEntry("clear",CLEAR
);
393 glutAddMenuEntry("tesselate",TESSELATE
);
394 glutAddMenuEntry("quit",QUIT
);
395 glutAttachMenu(GLUT_RIGHT_BUTTON
);
396 glutMouseFunc(mouse_clicked
);
397 glutKeyboardFunc(key_pressed
);
399 glPolygonMode(GL_FRONT
,GL_FILL
);
403 static void reshape(GLsizei w
, GLsizei h
)
405 glViewport(0, 0, w
, h
);
406 glMatrixMode(GL_PROJECTION
);
408 glOrtho(0.0, (GLdouble
)w
, 0.0, (GLdouble
)h
, -1.0, 1.0);
409 glMatrixMode(GL_MODELVIEW
);
415 static void usage( void )
417 printf("Use left mouse button to place vertices.\n");
418 printf("Press middle mouse button when done.\n");
419 printf("Select tesselate from the pop-up menu.\n");
424 * Open window with initial window size, title bar,
425 * RGBA display mode, and handle input events.
427 int main(int argc
, char** argv
)
430 glutInit(&argc
, argv
);
431 glutInitDisplayMode (GLUT_SINGLE
| GLUT_RGB
);
432 glutInitWindowSize (400, 400);
433 glutCreateWindow (argv
[0]);
435 glutDisplayFunc(display
);
436 glutReshapeFunc(reshape
);