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