Gareth Hughes | 3b7a75a | 2000-01-23 21:25:39 +0000 | [diff] [blame^] | 1 | /* $Id: tessdemo.c,v 1.4 2000/01/23 21:25:39 gareth Exp $ */ |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2 | |
| 3 | /* |
| 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 |
| 9 | */ |
| 10 | |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 11 | /* |
| 12 | * Updated for GLU 1.3 tessellation by Gareth Hughes <garethh@bell-labs.com> |
| 13 | */ |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 14 | |
| 15 | /* |
| 16 | * $Log: tessdemo.c,v $ |
Gareth Hughes | 3b7a75a | 2000-01-23 21:25:39 +0000 | [diff] [blame^] | 17 | * Revision 1.4 2000/01/23 21:25:39 gareth |
| 18 | * Merged 3.2 updates, namely combine callback for intersecting |
| 19 | * contours. |
| 20 | * |
| 21 | * Revision 1.3.2.1 1999/11/16 11:09:09 gareth |
| 22 | * Added combine callback. Converted vertices from ints to floats. |
| 23 | * |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 24 | * Revision 1.3 1999/11/04 04:00:42 gareth |
| 25 | * Updated demo for new GLU 1.3 tessellation. Added optimized rendering |
| 26 | * by saving the output of the tessellation into display lists. |
| 27 | * |
tanner | bc34adf | 1999-09-19 20:09:00 +0000 | [diff] [blame] | 28 | * Revision 1.2 1999/09/19 20:09:00 tanner |
| 29 | * |
| 30 | * lots of autoconf updates |
| 31 | * |
| 32 | * Revision 1.1.1.1 1999/08/19 00:55:40 jtg |
| 33 | * Imported sources |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 34 | * |
| 35 | * Revision 3.5 1999/03/28 18:24:37 brianp |
| 36 | * minor clean-up |
| 37 | * |
| 38 | * Revision 3.4 1999/02/14 03:37:07 brianp |
| 39 | * fixed callback problem |
| 40 | * |
| 41 | * Revision 3.3 1998/07/26 01:25:26 brianp |
| 42 | * removed include of gl.h and glu.h |
| 43 | * |
| 44 | * Revision 3.2 1998/06/29 02:37:30 brianp |
| 45 | * minor changes for Windows (Ted Jump) |
| 46 | * |
| 47 | * Revision 3.1 1998/06/09 01:53:49 brianp |
| 48 | * main() should return an int |
| 49 | * |
| 50 | * Revision 3.0 1998/02/14 18:42:29 brianp |
| 51 | * initial rev |
| 52 | * |
| 53 | */ |
| 54 | |
| 55 | |
| 56 | #include <GL/glut.h> |
| 57 | #include <stdio.h> |
| 58 | #include <stdlib.h> |
| 59 | #include <string.h> |
| 60 | |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 61 | #define MAX_POINTS 256 |
| 62 | #define MAX_CONTOURS 32 |
| 63 | #define MAX_TRIANGLES 256 |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 64 | |
| 65 | #ifndef GLCALLBACK |
| 66 | #ifdef CALLBACK |
| 67 | #define GLCALLBACK CALLBACK |
| 68 | #else |
| 69 | #define GLCALLBACK |
| 70 | #endif |
| 71 | #endif |
| 72 | |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 73 | typedef enum{ QUIT, TESSELATE, CLEAR } menu_entries; |
| 74 | typedef enum{ DEFINE, TESSELATED } mode_type; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 75 | |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 76 | static GLsizei width, height; |
| 77 | static GLuint contour_cnt; |
| 78 | static GLuint triangle_cnt; |
| 79 | |
| 80 | static mode_type mode; |
| 81 | static int menu; |
| 82 | |
| 83 | static GLuint list_start; |
| 84 | |
| 85 | static GLfloat edge_color[3]; |
| 86 | |
| 87 | static struct |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 88 | { |
Gareth Hughes | 3b7a75a | 2000-01-23 21:25:39 +0000 | [diff] [blame^] | 89 | GLfloat p[MAX_POINTS][2]; |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 90 | GLuint point_cnt; |
| 91 | } contours[MAX_CONTOURS]; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 92 | |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 93 | static struct |
| 94 | { |
| 95 | GLsizei no; |
Gareth Hughes | 3b7a75a | 2000-01-23 21:25:39 +0000 | [diff] [blame^] | 96 | GLfloat p[3][2]; |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 97 | GLclampf color[3][3]; |
| 98 | } triangles[MAX_TRIANGLES]; |
| 99 | |
| 100 | |
| 101 | |
Gareth Hughes | 3b7a75a | 2000-01-23 21:25:39 +0000 | [diff] [blame^] | 102 | void GLCALLBACK error_callback( GLenum err ) |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 103 | { |
| 104 | int len, i; |
| 105 | char const *str; |
| 106 | |
| 107 | glColor3f( 0.9, 0.9, 0.9 ); |
| 108 | glRasterPos2i( 5, 5 ); |
| 109 | |
| 110 | str = (const char *) gluErrorString( err ); |
| 111 | len = strlen( str ); |
| 112 | |
| 113 | for ( i = 0 ; i < len ; i++ ) { |
| 114 | glutBitmapCharacter( GLUT_BITMAP_9_BY_15, str[i] ); |
| 115 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 118 | void GLCALLBACK begin_callback( GLenum mode ) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 119 | { |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 120 | /* Allow multiple triangles to be output inside the begin/end pair. */ |
| 121 | triangle_cnt = 0; |
| 122 | triangles[triangle_cnt].no = 0; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 125 | void GLCALLBACK edge_callback( GLenum flag ) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 126 | { |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 127 | /* Persist the edge flag across triangles. */ |
| 128 | if ( flag == GL_TRUE ) |
| 129 | { |
| 130 | edge_color[0] = 1.0; |
| 131 | edge_color[1] = 1.0; |
| 132 | edge_color[2] = 0.5; |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | edge_color[0] = 1.0; |
| 137 | edge_color[1] = 0.0; |
| 138 | edge_color[2] = 0.0; |
| 139 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void GLCALLBACK end_callback() |
| 143 | { |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 144 | GLint i; |
| 145 | |
| 146 | glBegin( GL_LINES ); |
| 147 | |
| 148 | /* Output the three edges of each triangle as lines colored |
| 149 | according to their edge flag. */ |
| 150 | for ( i = 0 ; i < triangle_cnt ; i++ ) |
| 151 | { |
| 152 | glColor3f( triangles[i].color[0][0], |
| 153 | triangles[i].color[0][1], |
| 154 | triangles[i].color[0][2] ); |
| 155 | |
Gareth Hughes | 3b7a75a | 2000-01-23 21:25:39 +0000 | [diff] [blame^] | 156 | glVertex2f( triangles[i].p[0][0], triangles[i].p[0][1] ); |
| 157 | glVertex2f( triangles[i].p[1][0], triangles[i].p[1][1] ); |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 158 | |
| 159 | glColor3f( triangles[i].color[1][0], |
| 160 | triangles[i].color[1][1], |
| 161 | triangles[i].color[1][2] ); |
| 162 | |
Gareth Hughes | 3b7a75a | 2000-01-23 21:25:39 +0000 | [diff] [blame^] | 163 | glVertex2f( triangles[i].p[1][0], triangles[i].p[1][1] ); |
| 164 | glVertex2f( triangles[i].p[2][0], triangles[i].p[2][1] ); |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 165 | |
| 166 | glColor3f( triangles[i].color[2][0], |
| 167 | triangles[i].color[2][1], |
| 168 | triangles[i].color[2][2] ); |
| 169 | |
Gareth Hughes | 3b7a75a | 2000-01-23 21:25:39 +0000 | [diff] [blame^] | 170 | glVertex2f( triangles[i].p[2][0], triangles[i].p[2][1] ); |
| 171 | glVertex2f( triangles[i].p[0][0], triangles[i].p[0][1] ); |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | glEnd(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 177 | void GLCALLBACK vertex_callback( void *data ) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 178 | { |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 179 | GLsizei no; |
Gareth Hughes | 3b7a75a | 2000-01-23 21:25:39 +0000 | [diff] [blame^] | 180 | GLfloat *p; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 181 | |
Gareth Hughes | 3b7a75a | 2000-01-23 21:25:39 +0000 | [diff] [blame^] | 182 | p = (GLfloat *) data; |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 183 | no = triangles[triangle_cnt].no; |
| 184 | |
| 185 | triangles[triangle_cnt].p[no][0] = p[0]; |
| 186 | triangles[triangle_cnt].p[no][1] = p[1]; |
| 187 | |
| 188 | triangles[triangle_cnt].color[no][0] = edge_color[0]; |
| 189 | triangles[triangle_cnt].color[no][1] = edge_color[1]; |
| 190 | triangles[triangle_cnt].color[no][2] = edge_color[2]; |
| 191 | |
| 192 | /* After every three vertices, initialize the next triangle. */ |
| 193 | if ( ++(triangles[triangle_cnt].no) == 3 ) |
| 194 | { |
| 195 | triangle_cnt++; |
| 196 | triangles[triangle_cnt].no = 0; |
| 197 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Gareth Hughes | 3b7a75a | 2000-01-23 21:25:39 +0000 | [diff] [blame^] | 200 | void GLCALLBACK combine_callback( GLdouble coords[3], |
| 201 | GLdouble *vertex_data[4], |
| 202 | GLfloat weight[4], void **data ) |
| 203 | { |
| 204 | GLfloat *vertex; |
| 205 | int i; |
| 206 | |
| 207 | vertex = (GLfloat *) malloc( 2 * sizeof(GLfloat) ); |
| 208 | |
| 209 | vertex[0] = (GLfloat) coords[0]; |
| 210 | vertex[1] = (GLfloat) coords[1]; |
| 211 | |
| 212 | *data = vertex; |
| 213 | } |
| 214 | |
| 215 | |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 216 | void set_screen_wh( GLsizei w, GLsizei h ) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 217 | { |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 218 | width = w; |
| 219 | height = h; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 222 | void tesse( void ) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 223 | { |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 224 | GLUtesselator *tobj; |
| 225 | GLdouble data[3]; |
| 226 | GLuint i, j, point_cnt; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 227 | |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 228 | list_start = glGenLists( 2 ); |
| 229 | |
| 230 | tobj = gluNewTess(); |
| 231 | |
| 232 | if ( tobj != NULL ) |
| 233 | { |
Gareth Hughes | 3b7a75a | 2000-01-23 21:25:39 +0000 | [diff] [blame^] | 234 | gluTessNormal( tobj, 0.0, 0.0, 1.0 ); |
| 235 | gluTessCallback( tobj, GLU_TESS_BEGIN, glBegin ); |
| 236 | gluTessCallback( tobj, GLU_TESS_VERTEX, glVertex2fv ); |
| 237 | gluTessCallback( tobj, GLU_TESS_END, glEnd ); |
| 238 | gluTessCallback( tobj, GLU_TESS_ERROR, error_callback ); |
| 239 | gluTessCallback( tobj, GLU_TESS_COMBINE, combine_callback ); |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 240 | |
| 241 | glNewList( list_start, GL_COMPILE ); |
| 242 | gluBeginPolygon( tobj ); |
| 243 | |
| 244 | for ( j = 0 ; j <= contour_cnt ; j++ ) |
| 245 | { |
| 246 | point_cnt = contours[j].point_cnt; |
| 247 | gluNextContour( tobj, GLU_UNKNOWN ); |
| 248 | |
| 249 | for ( i = 0 ; i < point_cnt ; i++ ) |
| 250 | { |
| 251 | data[0] = (GLdouble)( contours[j].p[i][0] ); |
| 252 | data[1] = (GLdouble)( contours[j].p[i][1] ); |
| 253 | data[2] = 0.0; |
| 254 | gluTessVertex( tobj, data, contours[j].p[i] ); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | gluEndPolygon( tobj ); |
| 259 | glEndList(); |
| 260 | |
Gareth Hughes | 3b7a75a | 2000-01-23 21:25:39 +0000 | [diff] [blame^] | 261 | #if 0 |
| 262 | gluTessCallback( tobj, GLU_TESS_BEGIN, begin_callback ); |
| 263 | gluTessCallback( tobj, GLU_TESS_VERTEX, vertex_callback ); |
| 264 | gluTessCallback( tobj, GLU_TESS_END, end_callback ); |
| 265 | gluTessCallback( tobj, GLU_TESS_EDGE_FLAG, edge_callback ); |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 266 | |
| 267 | glNewList( list_start + 1, GL_COMPILE ); |
| 268 | gluBeginPolygon( tobj ); |
| 269 | |
| 270 | for ( j = 0 ; j <= contour_cnt ; j++ ) |
| 271 | { |
| 272 | point_cnt = contours[j].point_cnt; |
| 273 | gluNextContour( tobj, GLU_UNKNOWN ); |
| 274 | |
| 275 | for ( i = 0 ; i < point_cnt ; i++ ) |
| 276 | { |
| 277 | data[0] = (GLdouble)( contours[j].p[i][0] ); |
| 278 | data[1] = (GLdouble)( contours[j].p[i][1] ); |
| 279 | data[2] = 0.0; |
| 280 | gluTessVertex( tobj, data, contours[j].p[i] ); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | gluEndPolygon( tobj ); |
| 285 | glEndList(); |
Gareth Hughes | 3b7a75a | 2000-01-23 21:25:39 +0000 | [diff] [blame^] | 286 | #endif |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 287 | |
| 288 | gluDeleteTess( tobj ); |
| 289 | |
| 290 | glutMouseFunc( NULL ); |
| 291 | mode = TESSELATED; |
| 292 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 295 | void left_down( int x1, int y1 ) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 296 | { |
Gareth Hughes | 3b7a75a | 2000-01-23 21:25:39 +0000 | [diff] [blame^] | 297 | GLfloat P[2]; |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 298 | GLuint point_cnt; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 299 | |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 300 | /* translate GLUT into GL coordinates */ |
| 301 | |
| 302 | P[0] = x1; |
| 303 | P[1] = height - y1; |
| 304 | |
| 305 | point_cnt = contours[contour_cnt].point_cnt; |
| 306 | |
| 307 | contours[contour_cnt].p[point_cnt][0] = P[0]; |
| 308 | contours[contour_cnt].p[point_cnt][1] = P[1]; |
| 309 | |
| 310 | glBegin( GL_LINES ); |
| 311 | |
| 312 | if ( point_cnt ) |
| 313 | { |
Gareth Hughes | 3b7a75a | 2000-01-23 21:25:39 +0000 | [diff] [blame^] | 314 | glVertex2fv( contours[contour_cnt].p[point_cnt-1] ); |
| 315 | glVertex2fv( P ); |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 316 | } |
| 317 | else |
| 318 | { |
Gareth Hughes | 3b7a75a | 2000-01-23 21:25:39 +0000 | [diff] [blame^] | 319 | glVertex2fv( P ); |
| 320 | glVertex2fv( P ); |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | glEnd(); |
| 324 | glFinish(); |
| 325 | |
| 326 | contours[contour_cnt].point_cnt++; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 329 | void middle_down( int x1, int y1 ) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 330 | { |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 331 | GLuint point_cnt; |
| 332 | (void) x1; |
| 333 | (void) y1; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 334 | |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 335 | point_cnt = contours[contour_cnt].point_cnt; |
| 336 | |
| 337 | if ( point_cnt > 2 ) |
| 338 | { |
| 339 | glBegin( GL_LINES ); |
| 340 | |
Gareth Hughes | 3b7a75a | 2000-01-23 21:25:39 +0000 | [diff] [blame^] | 341 | glVertex2fv( contours[contour_cnt].p[0] ); |
| 342 | glVertex2fv( contours[contour_cnt].p[point_cnt-1] ); |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 343 | |
| 344 | contours[contour_cnt].p[point_cnt][0] = -1; |
| 345 | |
| 346 | glEnd(); |
| 347 | glFinish(); |
| 348 | |
| 349 | contour_cnt++; |
| 350 | contours[contour_cnt].point_cnt = 0; |
| 351 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 354 | void mouse_clicked( int button, int state, int x, int y ) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 355 | { |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 356 | x -= x%10; |
| 357 | y -= y%10; |
| 358 | |
| 359 | switch ( button ) |
| 360 | { |
| 361 | case GLUT_LEFT_BUTTON: |
| 362 | if ( state == GLUT_DOWN ) { |
| 363 | left_down( x, y ); |
| 364 | } |
| 365 | break; |
| 366 | case GLUT_MIDDLE_BUTTON: |
| 367 | if ( state == GLUT_DOWN ) { |
| 368 | middle_down( x, y ); |
| 369 | } |
| 370 | break; |
| 371 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 374 | void display( void ) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 375 | { |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 376 | GLuint i,j; |
| 377 | GLuint point_cnt; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 378 | |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 379 | glClear( GL_COLOR_BUFFER_BIT ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 380 | |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 381 | switch ( mode ) |
| 382 | { |
| 383 | case DEFINE: |
| 384 | /* draw grid */ |
| 385 | glColor3f( 0.6, 0.5, 0.5 ); |
| 386 | |
| 387 | glBegin( GL_LINES ); |
| 388 | |
| 389 | for ( i = 0 ; i < width ; i += 10 ) |
| 390 | { |
| 391 | for ( j = 0 ; j < height ; j += 10 ) |
| 392 | { |
| 393 | glVertex2i( 0, j ); |
| 394 | glVertex2i( width, j ); |
| 395 | glVertex2i( i, height ); |
| 396 | glVertex2i( i, 0 ); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | glColor3f( 1.0, 1.0, 0.0 ); |
| 401 | |
| 402 | for ( i = 0 ; i <= contour_cnt ; i++ ) |
| 403 | { |
| 404 | point_cnt = contours[i].point_cnt; |
| 405 | |
| 406 | glBegin( GL_LINES ); |
| 407 | |
| 408 | switch ( point_cnt ) |
| 409 | { |
| 410 | case 0: |
| 411 | break; |
| 412 | case 1: |
Gareth Hughes | 3b7a75a | 2000-01-23 21:25:39 +0000 | [diff] [blame^] | 413 | glVertex2fv( contours[i].p[0] ); |
| 414 | glVertex2fv( contours[i].p[0] ); |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 415 | break; |
| 416 | case 2: |
Gareth Hughes | 3b7a75a | 2000-01-23 21:25:39 +0000 | [diff] [blame^] | 417 | glVertex2fv( contours[i].p[0] ); |
| 418 | glVertex2fv( contours[i].p[1] ); |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 419 | break; |
| 420 | default: |
| 421 | --point_cnt; |
| 422 | for ( j = 0 ; j < point_cnt ; j++ ) |
| 423 | { |
Gareth Hughes | 3b7a75a | 2000-01-23 21:25:39 +0000 | [diff] [blame^] | 424 | glVertex2fv( contours[i].p[j] ); |
| 425 | glVertex2fv( contours[i].p[j+1] ); |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 426 | } |
| 427 | if ( contours[i].p[j+1][0] == -1 ) |
| 428 | { |
Gareth Hughes | 3b7a75a | 2000-01-23 21:25:39 +0000 | [diff] [blame^] | 429 | glVertex2fv( contours[i].p[0] ); |
| 430 | glVertex2fv( contours[i].p[j] ); |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 431 | } |
| 432 | break; |
| 433 | } |
| 434 | |
| 435 | glEnd(); |
| 436 | } |
| 437 | |
| 438 | glFinish(); |
| 439 | break; |
| 440 | |
| 441 | case TESSELATED: |
| 442 | /* draw triangles */ |
| 443 | glColor3f( 0.7, 0.7, 0.0 ); |
| 444 | glCallList( list_start ); |
| 445 | |
| 446 | glLineWidth( 2.0 ); |
| 447 | glCallList( list_start + 1 ); |
| 448 | glLineWidth( 1.0 ); |
| 449 | |
| 450 | glFlush(); |
| 451 | break; |
| 452 | } |
| 453 | |
| 454 | glColor3f( 1.0, 1.0, 0.0 ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | void clear( void ) |
| 458 | { |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 459 | contour_cnt = 0; |
| 460 | contours[0].point_cnt = 0; |
| 461 | triangle_cnt = 0; |
| 462 | |
| 463 | glutMouseFunc( mouse_clicked ); |
| 464 | |
| 465 | mode = DEFINE; |
| 466 | |
| 467 | glDeleteLists( list_start, 2 ); |
| 468 | list_start = 0; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | void quit( void ) |
| 472 | { |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 473 | exit( 0 ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 476 | void menu_selected( int entry ) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 477 | { |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 478 | switch ( entry ) |
| 479 | { |
| 480 | case CLEAR: |
| 481 | clear(); |
| 482 | break; |
| 483 | case TESSELATE: |
| 484 | tesse(); |
| 485 | break; |
| 486 | case QUIT: |
| 487 | quit(); |
| 488 | break; |
| 489 | } |
| 490 | |
| 491 | glutPostRedisplay(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 494 | void key_pressed( unsigned char key, int x, int y ) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 495 | { |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 496 | (void) x; |
| 497 | (void) y; |
| 498 | |
| 499 | switch ( key ) |
| 500 | { |
| 501 | case 'c': |
| 502 | case 'C': |
| 503 | clear(); |
| 504 | break; |
| 505 | case 't': |
| 506 | case 'T': |
| 507 | tesse(); |
| 508 | break; |
| 509 | case 'q': |
| 510 | case 'Q': |
| 511 | quit(); |
| 512 | break; |
| 513 | } |
| 514 | |
| 515 | glutPostRedisplay(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 518 | void myinit( void ) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 519 | { |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 520 | /* clear background to gray */ |
| 521 | glClearColor( 0.4, 0.4, 0.4, 0.0 ); |
| 522 | glShadeModel( GL_FLAT ); |
| 523 | glPolygonMode( GL_FRONT, GL_FILL ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 524 | |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 525 | menu = glutCreateMenu( menu_selected ); |
| 526 | |
| 527 | glutAddMenuEntry( "clear", CLEAR ); |
| 528 | glutAddMenuEntry( "tesselate", TESSELATE ); |
| 529 | glutAddMenuEntry( "quit", QUIT ); |
| 530 | |
| 531 | glutAttachMenu( GLUT_RIGHT_BUTTON ); |
| 532 | |
| 533 | glutMouseFunc( mouse_clicked ); |
| 534 | glutKeyboardFunc( key_pressed ); |
| 535 | |
| 536 | contour_cnt = 0; |
| 537 | mode = DEFINE; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 540 | static void reshape( GLsizei w, GLsizei h ) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 541 | { |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 542 | glViewport( 0, 0, w, h ); |
| 543 | |
| 544 | glMatrixMode( GL_PROJECTION ); |
| 545 | glLoadIdentity(); |
| 546 | glOrtho( 0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0 ); |
| 547 | |
| 548 | glMatrixMode( GL_MODELVIEW ); |
| 549 | glLoadIdentity(); |
| 550 | |
| 551 | set_screen_wh( w, h ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | |
| 555 | static void usage( void ) |
| 556 | { |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 557 | printf( "Use left mouse button to place vertices.\n" ); |
| 558 | printf( "Press middle mouse button when done.\n" ); |
| 559 | printf( "Select tesselate from the pop-up menu.\n" ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 563 | /* |
| 564 | * Main Loop |
| 565 | * Open window with initial window size, title bar, |
| 566 | * RGBA display mode, and handle input events. |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 567 | */ |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 568 | int main( int argc, char **argv ) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 569 | { |
Gareth Hughes | eb459c6 | 1999-11-04 04:00:42 +0000 | [diff] [blame] | 570 | usage(); |
| 571 | |
| 572 | glutInit( &argc, argv ); |
| 573 | glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB ); |
| 574 | glutInitWindowSize( 400, 400 ); |
| 575 | glutCreateWindow( argv[0] ); |
| 576 | |
| 577 | myinit(); |
| 578 | |
| 579 | glutDisplayFunc( display ); |
| 580 | glutReshapeFunc( reshape ); |
| 581 | |
| 582 | glutMainLoop(); |
| 583 | |
| 584 | return 0; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 585 | } |