summaryrefslogtreecommitdiffstats
path: root/progs/demos/tessdemo.c
blob: 2e02b9027e9cb286505f3d8c10d780c7d3cce534 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/* $Id: tessdemo.c,v 1.8 2000/07/11 14:11:58 brianp Exp $ */

/*
 * A demo of the GLU polygon tesselation functions written by Bogdan Sikorski.
 */


#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_POINTS 200
#define MAX_CONTOURS 50

static int menu;
typedef enum
{ QUIT, TESSELATE, CLEAR }
menu_entries;

typedef enum
{ DEFINE, TESSELATED }
mode_type;

struct
{
   GLint p[MAX_POINTS][2];
   GLuint point_cnt;
}
contours[MAX_CONTOURS];

static GLuint contour_cnt;
static GLsizei width, height;
static mode_type mode;

struct
{
   GLsizei no;
   GLfloat color[3];
   GLint p[3][2];
   GLclampf p_color[3][3];
}
triangle;


static void GLCALLBACK
my_error(GLenum err)
{
   int len, i;
   char const *str;

   glColor3f(0.9, 0.9, 0.9);
   glRasterPos2i(5, 5);
   str = (const char *) gluErrorString(err);
   len = strlen(str);
   for (i = 0; i < len; i++)
      glutBitmapCharacter(GLUT_BITMAP_9_BY_15, str[i]);
}


static void GLCALLBACK
begin_callback(GLenum mode)
{
   triangle.no = 0;
}


static void GLCALLBACK
edge_callback(GLenum flag)
{
   if (flag == GL_TRUE) {
      triangle.color[0] = 1.0;
      triangle.color[1] = 1.0;
      triangle.color[2] = 0.5;
   }
   else {
      triangle.color[0] = 1.0;
      triangle.color[1] = 0.0;
      triangle.color[2] = 0.0;
   }
}


static void GLCALLBACK
end_callback()
{
   glBegin(GL_LINES);
   glColor3f(triangle.p_color[0][0], triangle.p_color[0][1],
	     triangle.p_color[0][2]);
   glVertex2i(triangle.p[0][0], triangle.p[0][1]);
   glVertex2i(triangle.p[1][0], triangle.p[1][1]);
   glColor3f(triangle.p_color[1][0], triangle.p_color[1][1],
	     triangle.p_color[1][2]);
   glVertex2i(triangle.p[1][0], triangle.p[1][1]);
   glVertex2i(triangle.p[2][0], triangle.p[2][1]);
   glColor3f(triangle.p_color[2][0], triangle.p_color[2][1],
	     triangle.p_color[2][2]);
   glVertex2i(triangle.p[2][0], triangle.p[2][1]);
   glVertex2i(triangle.p[0][0], triangle.p[0][1]);
   glEnd();
}


static void GLCALLBACK
vertex_callback(void *data)
{
   GLsizei no;
   GLint *p;

   p = (GLint *) data;
   no = triangle.no;
   triangle.p[no][0] = p[0];
   triangle.p[no][1] = p[1];
   triangle.p_color[no][0] = triangle.color[0];
   triangle.p_color[no][1] = triangle.color[1];
   triangle.p_color[no][2] = triangle.color[2];
   ++(triangle.no);
}


static void
set_screen_wh(GLsizei w, GLsizei h)
{
   width = w;
   height = h;
}


static void
tesse(void)
{
   GLUtriangulatorObj *tobj;
   GLdouble data[3];
   GLuint i, j, point_cnt;

   tobj = gluNewTess();
   if (tobj != NULL) {
      glClear(GL_COLOR_BUFFER_BIT);
      glColor3f(0.7, 0.7, 0.0);
      gluTessCallback(tobj, GLU_BEGIN, glBegin);
      gluTessCallback(tobj, GLU_END, glEnd);
      gluTessCallback(tobj, GLU_ERROR, my_error);
      gluTessCallback(tobj, GLU_VERTEX, glVertex2iv);
      gluBeginPolygon(tobj);
      for (j = 0; j <= contour_cnt; j++) {
	 point_cnt = contours[j].point_cnt;
	 gluNextContour(tobj, GLU_UNKNOWN);
	 for (i = 0; i < point_cnt; i++) {
	    data[0] = (GLdouble) (contours[j].p[i][0]);
	    data[1] = (GLdouble) (contours[j].p[i][1]);
	    data[2] = 0.0;
	    gluTessVertex(tobj, data, contours[j].p[i]);
	 }
      }
      gluEndPolygon(tobj);
      glLineWidth(2.0);
      gluTessCallback(tobj, GLU_BEGIN, begin_callback);
      gluTessCallback(tobj, GLU_END, end_callback);
      gluTessCallback(tobj, GLU_VERTEX, vertex_callback);
      gluTessCallback(tobj, GLU_EDGE_FLAG, edge_callback);
      gluBeginPolygon(tobj);
      for (j = 0; j <= contour_cnt; j++) {
	 point_cnt = contours[j].point_cnt;
	 gluNextContour(tobj, GLU_UNKNOWN);
	 for (i = 0; i < point_cnt; i++) {
	    data[0] = (GLdouble) (contours[j].p[i][0]);
	    data[1] = (GLdouble) (contours[j].p[i][1]);
	    data[2] = 0.0;
	    gluTessVertex(tobj, data, contours[j].p[i]);
	 }
      }
      gluEndPolygon(tobj);
      gluDeleteTess(tobj);
      glutMouseFunc(NULL);
      glColor3f(1.0, 1.0, 0.0);
      glLineWidth(1.0);
      mode = TESSELATED;
   }
}


static void
left_down(int x1, int y1)
{
   GLint P[2];
   GLuint point_cnt;

   /* translate GLUT into GL coordinates */
   P[0] = x1;
   P[1] = height - y1;
   point_cnt = contours[contour_cnt].point_cnt;
   contours[contour_cnt].p[point_cnt][0] = P[0];
   contours[contour_cnt].p[point_cnt][1] = P[1];
   glBegin(GL_LINES);
   if (point_cnt) {
      glVertex2iv(contours[contour_cnt].p[point_cnt - 1]);
      glVertex2iv(P);
   }
   else {
      glVertex2iv(P);
      glVertex2iv(P);
   }
   glEnd();
   glFinish();
   ++(contours[contour_cnt].point_cnt);
}


static void
middle_down(int x1, int y1)
{
   GLuint point_cnt;

   point_cnt = contours[contour_cnt].point_cnt;
   if (point_cnt > 2) {
      glBegin(GL_LINES);
      glVertex2iv(contours[contour_cnt].p[0]);
      glVertex2iv(contours[contour_cnt].p[point_cnt - 1]);
      contours[contour_cnt].p[point_cnt][0] = -1;
      glEnd();
      glFinish();
      contour_cnt++;
      contours[contour_cnt].point_cnt = 0;
   }
}


static void
mouse_clicked(int button, int state, int x, int y)
{
   x -= x % 10;
   y -= y % 10;
   switch (button) {
   case GLUT_LEFT_BUTTON:
      if (state == GLUT_DOWN)
	 left_down(x, y);
      break;
   case GLUT_MIDDLE_BUTTON:
      if (state == GLUT_DOWN)
	 middle_down(x, y);
      break;
   }
}


static void
display(void)
{
   GLuint i, j;
   GLuint point_cnt;

   glClear(GL_COLOR_BUFFER_BIT);
   switch (mode) {
   case DEFINE:
      /* draw grid */
      glColor3f(0.6, 0.5, 0.5);
      glBegin(GL_LINES);
      for (i = 0; i < width; i += 10)
	 for (j = 0; j < height; j += 10) {
	    glVertex2i(0, j);
	    glVertex2i(width, j);
	    glVertex2i(i, height);
	    glVertex2i(i, 0);
	 }
      glEnd();
      glColor3f(1.0, 1.0, 0.0);
      for (i = 0; i <= contour_cnt; i++) {
	 point_cnt = contours[i].point_cnt;
	 glBegin(GL_LINES);
	 switch (point_cnt) {
	 case 0:
	    break;
	 case 1:
	    glVertex2iv(contours[i].p[0]);
	    glVertex2iv(contours[i].p[0]);
	    break;
	 case 2:
	    glVertex2iv(contours[i].p[0]);
	    glVertex2iv(contours[i].p[1]);
	    break;
	 default:
	    --point_cnt;
	    for (j = 0; j < point_cnt; j++) {
	       glVertex2iv(contours[i].p[j]);
	       glVertex2iv(contours[i].p[j + 1]);
	    }
	    if (contours[i].p[j + 1][0] == -1) {
	       glVertex2iv(contours[i].p[0]);
	       glVertex2iv(contours[i].p[j]);
	    }
	    break;
	 }
	 glEnd();
      }
      glFinish();
      break;
   case TESSELATED:
      /* draw lines */
      tesse();
      break;
   }

   glColor3f(1.0, 1.0, 0.0);
}


static void
clear(void)
{
   contour_cnt = 0;
   contours[0].point_cnt = 0;
   glutMouseFunc(mouse_clicked);
   mode = DEFINE;
   display();
}


static void
quit(void)
{
   exit(0);
}


static void
menu_selected(int entry)
{
   switch (entry) {
   case CLEAR:
      clear();
      break;
   case TESSELATE:
      tesse();
      break;
   case QUIT:
      quit();
      break;
   }
}


static void
key_pressed(unsigned char key, int x, int y)
{
   switch (key) {
   case 't':
   case 'T':
      tesse();
      glFinish();
      break;
   case 'q':
   case 'Q':
      quit();
      break;
   case 'c':
   case 'C':
      clear();
      break;
   }
}


static void
myinit(void)
{
/*  clear background to gray	*/
   glClearColor(0.4, 0.4, 0.4, 0.0);
   glShadeModel(GL_FLAT);

   menu = glutCreateMenu(menu_selected);
   glutAddMenuEntry("clear", CLEAR);
   glutAddMenuEntry("tesselate", TESSELATE);
   glutAddMenuEntry("quit", QUIT);
   glutAttachMenu(GLUT_RIGHT_BUTTON);
   glutMouseFunc(mouse_clicked);
   glutKeyboardFunc(key_pressed);
   contour_cnt = 0;
   glPolygonMode(GL_FRONT, GL_FILL);
   mode = DEFINE;
}


static void
reshape(GLsizei w, GLsizei h)
{
   glViewport(0, 0, w, h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(0.0, (GLdouble) w, 0.0, (GLdouble) h, -1.0, 1.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   set_screen_wh(w, h);
}


static void
usage(void)
{
   printf("Use left mouse button to place vertices.\n");
   printf("Press middle mouse button when done.\n");
   printf("Select tesselate from the pop-up menu.\n");
}


/*  Main Loop
 *  Open window with initial window size, title bar, 
 *  RGBA display mode, and handle input events.
 */
int
main(int argc, char **argv)
{
   usage();
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
   glutInitWindowSize(400, 400);
   glutCreateWindow(argv[0]);
   myinit();
   glutDisplayFunc(display);
   glutReshapeFunc(reshape);
   glutMainLoop();
   return 0;
}