Removed all RCS / CVS tags (Id, Header, Date, etc.) from everything.
[mesa.git] / progs / demos / tessdemo.c
index 7b3cf9e7025bc5aa535e97e92e0e66f744fe5e7d..66fdc6370c8d9de04f92c8b3505755b9da965e88 100644 (file)
@@ -1,51 +1,9 @@
-/* $Id: tessdemo.c,v 1.3 1999/11/04 04:00:42 gareth Exp $ */
 
 /*
  * A demo of the GLU polygon tesselation functions written by Bogdan Sikorski.
- * This demo isn't built by the Makefile because it needs GLUT.  After you've
- * installed GLUT you can try this demo.
- * Here's the command for IRIX, for example:
-   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
+ * Updated for GLU 1.3 tessellation by Gareth Hughes <gareth@valinux.com>
  */
 
-/*
- * Updated for GLU 1.3 tessellation by Gareth Hughes <garethh@bell-labs.com>
- */
-
-/*
- * $Log: tessdemo.c,v $
- * Revision 1.3  1999/11/04 04:00:42  gareth
- * Updated demo for new GLU 1.3 tessellation.  Added optimized rendering
- * by saving the output of the tessellation into display lists.
- *
- * Revision 1.2  1999/09/19 20:09:00  tanner
- *
- * lots of autoconf updates
- *
- * Revision 1.1.1.1  1999/08/19 00:55:40  jtg
- * Imported sources
- *
- * Revision 3.5  1999/03/28 18:24:37  brianp
- * minor clean-up
- *
- * Revision 3.4  1999/02/14 03:37:07  brianp
- * fixed callback problem
- *
- * Revision 3.3  1998/07/26 01:25:26  brianp
- * removed include of gl.h and glu.h
- *
- * Revision 3.2  1998/06/29 02:37:30  brianp
- * minor changes for Windows (Ted Jump)
- *
- * Revision 3.1  1998/06/09 01:53:49  brianp
- * main() should return an int
- *
- * Revision 3.0  1998/02/14 18:42:29  brianp
- * initial rev
- *
- */
-
-
 #include <GL/glut.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -63,6 +21,8 @@
 #endif
 #endif
 
+#ifdef GLU_VERSION_1_2
+
 typedef enum{ QUIT, TESSELATE, CLEAR } menu_entries;
 typedef enum{ DEFINE, TESSELATED } mode_type;
 
@@ -77,22 +37,20 @@ static GLuint               list_start;
 
 static GLfloat         edge_color[3];
 
-static struct
-{
-   GLint       p[MAX_POINTS][2];
+static struct {
+   GLfloat     p[MAX_POINTS][2];
    GLuint      point_cnt;
 } contours[MAX_CONTOURS];
 
-static struct
-{
+static struct {
    GLsizei     no;
-   GLint       p[3][2];
+   GLfloat     p[3][2];
    GLclampf    color[3][3];
 } triangles[MAX_TRIANGLES];
 
 
 
-void GLCALLBACK my_error( GLenum err )
+static void GLCALLBACK error_callback( GLenum err )
 {
    int         len, i;
    char const  *str;
@@ -108,71 +66,67 @@ void GLCALLBACK my_error( GLenum err )
    }
 }
 
-void GLCALLBACK begin_callback( GLenum mode )
+static void GLCALLBACK begin_callback( GLenum mode )
 {
    /* Allow multiple triangles to be output inside the begin/end pair. */
    triangle_cnt = 0;
    triangles[triangle_cnt].no = 0;
 }
 
-void GLCALLBACK edge_callback( GLenum flag )
+static void GLCALLBACK edge_callback( GLenum flag )
 {
    /* Persist the edge flag across triangles. */
-   if ( flag == GL_TRUE )
-   {
+   if ( flag == GL_TRUE ) {
       edge_color[0] = 1.0;
       edge_color[1] = 1.0;
       edge_color[2] = 0.5;
-   }
-   else
-   {
+   } else {
       edge_color[0] = 1.0;
       edge_color[1] = 0.0;
       edge_color[2] = 0.0;
    }
 }
 
-void GLCALLBACK end_callback()
+static void GLCALLBACK end_callback()
 {
-   GLint       i;
+   GLuint      i;
 
    glBegin( GL_LINES );
 
    /* Output the three edges of each triangle as lines colored
       according to their edge flag. */
-   for ( i = 0 ; i < triangle_cnt ; i++ )
-   {
+   for ( i = 0 ; i < triangle_cnt ; i++ ) {
       glColor3f( triangles[i].color[0][0],
                 triangles[i].color[0][1],
                 triangles[i].color[0][2] );
 
-      glVertex2i( triangles[i].p[0][0], triangles[i].p[0][1] );
-      glVertex2i( triangles[i].p[1][0], triangles[i].p[1][1] );
+      glVertex2f( triangles[i].p[0][0], triangles[i].p[0][1] );
+      glVertex2f( triangles[i].p[1][0], triangles[i].p[1][1] );
 
       glColor3f( triangles[i].color[1][0],
                 triangles[i].color[1][1],
                 triangles[i].color[1][2] );
 
-      glVertex2i( triangles[i].p[1][0], triangles[i].p[1][1] );
-      glVertex2i( triangles[i].p[2][0], triangles[i].p[2][1] );
+      glVertex2f( triangles[i].p[1][0], triangles[i].p[1][1] );
+      glVertex2f( triangles[i].p[2][0], triangles[i].p[2][1] );
 
       glColor3f( triangles[i].color[2][0],
                 triangles[i].color[2][1],
                 triangles[i].color[2][2] );
 
-      glVertex2i( triangles[i].p[2][0], triangles[i].p[2][1] );
-      glVertex2i( triangles[i].p[0][0], triangles[i].p[0][1] );
+      glVertex2f( triangles[i].p[2][0], triangles[i].p[2][1] );
+      glVertex2f( triangles[i].p[0][0], triangles[i].p[0][1] );
    }
 
    glEnd();
 }
 
-void GLCALLBACK vertex_callback( void *data )
+static void GLCALLBACK vertex_callback( void *data )
 {
    GLsizei     no;
-   GLint       *p;
+   GLfloat     *p;
 
-   p = (GLint *) data;
+   p = (GLfloat *) data;
    no = triangles[triangle_cnt].no;
 
    triangles[triangle_cnt].p[no][0] = p[0];
@@ -183,20 +137,36 @@ void GLCALLBACK vertex_callback( void *data )
    triangles[triangle_cnt].color[no][2] = edge_color[2];
 
    /* After every three vertices, initialize the next triangle. */
-   if ( ++(triangles[triangle_cnt].no) == 3 )
-   {
+   if ( ++(triangles[triangle_cnt].no) == 3 ) {
       triangle_cnt++;
       triangles[triangle_cnt].no = 0;
    }
 }
 
-void set_screen_wh( GLsizei w, GLsizei h )
+static void GLCALLBACK combine_callback( GLdouble coords[3],
+                                        GLdouble *vertex_data[4],
+                                        GLfloat weight[4], void **data )
+{
+   GLfloat     *vertex;
+
+   vertex = (GLfloat *) malloc( 2 * sizeof(GLfloat) );
+
+   vertex[0] = (GLfloat) coords[0];
+   vertex[1] = (GLfloat) coords[1];
+
+   *data = vertex;
+}
+
+
+static void set_screen_wh( GLsizei w, GLsizei h )
 {
    width = w;
    height = h;
 }
 
-void tesse( void )
+typedef void (GLAPIENTRY *callback_t)();
+
+static void tesse( void )
 {
    GLUtesselator       *tobj;
    GLdouble            data[3];
@@ -206,23 +176,22 @@ void tesse( void )
 
    tobj = gluNewTess();
 
-   if ( tobj != NULL )
-   {
-      gluTessCallback( tobj, GLU_BEGIN, glBegin );
-      gluTessCallback( tobj, GLU_VERTEX, glVertex2iv );
-      gluTessCallback( tobj, GLU_END, glEnd );
-      gluTessCallback( tobj, GLU_ERROR, my_error );
+   if ( tobj != NULL ) {
+      gluTessNormal( tobj, 0.0, 0.0, 1.0 );
+      gluTessCallback( tobj, GLU_TESS_BEGIN, (callback_t) glBegin );
+      gluTessCallback( tobj, GLU_TESS_VERTEX, (callback_t) glVertex2fv );
+      gluTessCallback( tobj, GLU_TESS_END, (callback_t) glEnd );
+      gluTessCallback( tobj, GLU_TESS_ERROR, (callback_t) error_callback );
+      gluTessCallback( tobj, GLU_TESS_COMBINE, (callback_t) combine_callback );
 
       glNewList( list_start, GL_COMPILE );
       gluBeginPolygon( tobj );
 
-      for ( j = 0 ; j <= contour_cnt ; j++ )
-      {
+      for ( j = 0 ; j <= contour_cnt ; j++ ) {
         point_cnt = contours[j].point_cnt;
         gluNextContour( tobj, GLU_UNKNOWN );
 
-        for ( i = 0 ; i < point_cnt ; i++ )
-        {
+        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;
@@ -233,21 +202,19 @@ void tesse( void )
       gluEndPolygon( tobj );
       glEndList();
 
-      gluTessCallback( tobj, GLU_BEGIN, begin_callback );
-      gluTessCallback( tobj, GLU_VERTEX, vertex_callback );
-      gluTessCallback( tobj, GLU_END, end_callback );
-      gluTessCallback( tobj, GLU_EDGE_FLAG, edge_callback );
+      gluTessCallback( tobj, GLU_TESS_BEGIN, (callback_t) begin_callback );
+      gluTessCallback( tobj, GLU_TESS_VERTEX, (callback_t) vertex_callback );
+      gluTessCallback( tobj, GLU_TESS_END, (callback_t) end_callback );
+      gluTessCallback( tobj, GLU_TESS_EDGE_FLAG, (callback_t) edge_callback );
 
       glNewList( list_start + 1, GL_COMPILE );
       gluBeginPolygon( tobj );
 
-      for ( j = 0 ; j <= contour_cnt ; j++ )
-      {
+      for ( j = 0 ; j <= contour_cnt ; j++ ) {
         point_cnt = contours[j].point_cnt;
         gluNextContour( tobj, GLU_UNKNOWN );
 
-        for ( i = 0 ; i < point_cnt ; i++ )
-        {
+        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;
@@ -265,9 +232,9 @@ void tesse( void )
    }
 }
 
-void left_down( int x1, int y1 )
+static void left_down( int x1, int y1 )
 {
-   GLint       P[2];
+   GLfloat     P[2];
    GLuint      point_cnt;
 
    /* translate GLUT into GL coordinates */
@@ -282,15 +249,12 @@ void left_down( int x1, int y1 )
 
    glBegin( GL_LINES );
 
-   if ( point_cnt )
-   {
-      glVertex2iv( contours[contour_cnt].p[point_cnt-1] );
-      glVertex2iv( P );
-   }
-   else
-   {
-      glVertex2iv( P );
-      glVertex2iv( P );
+   if ( point_cnt ) {
+      glVertex2fv( contours[contour_cnt].p[point_cnt-1] );
+      glVertex2fv( P );
+   } else {
+      glVertex2fv( P );
+      glVertex2fv( P );
    }
 
    glEnd();
@@ -299,7 +263,7 @@ void left_down( int x1, int y1 )
    contours[contour_cnt].point_cnt++;
 }
 
-void middle_down( int x1, int y1 )
+static void middle_down( int x1, int y1 )
 {
    GLuint      point_cnt;
    (void) x1;
@@ -307,12 +271,11 @@ void middle_down( int x1, int y1 )
 
    point_cnt = contours[contour_cnt].point_cnt;
 
-   if ( point_cnt > 2 )
-   {
+   if ( point_cnt > 2 ) {
       glBegin( GL_LINES );
 
-      glVertex2iv( contours[contour_cnt].p[0] );
-      glVertex2iv( contours[contour_cnt].p[point_cnt-1] );
+      glVertex2fv( contours[contour_cnt].p[0] );
+      glVertex2fv( contours[contour_cnt].p[point_cnt-1] );
 
       contours[contour_cnt].p[point_cnt][0] = -1;
 
@@ -324,13 +287,12 @@ void middle_down( int x1, int y1 )
    }
 }
 
-void mouse_clicked( int button, int state, int x, int y )
+static void mouse_clicked( int button, int state, int x, int y )
 {
    x -= x%10;
    y -= y%10;
 
-   switch ( button )
-   {
+   switch ( button ) {
    case GLUT_LEFT_BUTTON:
       if ( state == GLUT_DOWN ) {
         left_down( x, y );
@@ -344,63 +306,59 @@ void mouse_clicked( int button, int state, int x, int y )
    }
 }
 
-void display( void )
+static void display( void )
 {
    GLuint i,j;
+   GLsizei ii, jj;
    GLuint point_cnt;
 
    glClear( GL_COLOR_BUFFER_BIT );
 
-   switch ( mode )
-   {
+   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 );
+      for ( ii = 0 ; ii < width ; ii += 10 ) {
+        for ( jj = 0 ; jj < height ; jj += 10 ) {
+           glVertex2i( 0, jj );
+           glVertex2i( width, jj );
+           glVertex2i( ii, height );
+           glVertex2i( ii, 0 );
         }
       }
 
+      glEnd();
+
       glColor3f( 1.0, 1.0, 0.0 );
 
-      for ( i = 0 ; i <= contour_cnt ; i++ )
-      {
+      for ( i = 0 ; i <= contour_cnt ; i++ ) {
         point_cnt = contours[i].point_cnt;
 
         glBegin( GL_LINES );
 
-        switch ( point_cnt )
-        {
+        switch ( point_cnt ) {
         case 0:
            break;
         case 1:
-           glVertex2iv( contours[i].p[0] );
-           glVertex2iv( contours[i].p[0] );
+           glVertex2fv( contours[i].p[0] );
+           glVertex2fv( contours[i].p[0] );
            break;
         case 2:
-           glVertex2iv( contours[i].p[0] );
-           glVertex2iv( contours[i].p[1] );
+           glVertex2fv( contours[i].p[0] );
+           glVertex2fv( 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] );
+           for ( j = 0 ; j < point_cnt ; j++ ) {
+              glVertex2fv( contours[i].p[j] );
+              glVertex2fv( contours[i].p[j+1] );
            }
-           if ( contours[i].p[j+1][0] == -1 )
-           {
-              glVertex2iv( contours[i].p[0] );
-              glVertex2iv( contours[i].p[j] );
+           if ( contours[i].p[j+1][0] == -1 ) {
+              glVertex2fv( contours[i].p[0] );
+              glVertex2fv( contours[i].p[j] );
            }
            break;
         }
@@ -427,7 +385,7 @@ void display( void )
    glColor3f( 1.0, 1.0, 0.0 );
 }
 
-void clear( void )
+static void clear( void )
 {
    contour_cnt = 0;
    contours[0].point_cnt = 0;
@@ -441,15 +399,14 @@ void clear( void )
    list_start = 0;
 }
 
-void quit( void )
+static void quit( void )
 {
    exit( 0 );
 }
 
-void menu_selected( int entry )
+static void menu_selected( int entry )
 {
-   switch ( entry )
-   {
+   switch ( entry ) {
    case CLEAR:
       clear();
       break;
@@ -464,13 +421,12 @@ void menu_selected( int entry )
    glutPostRedisplay();
 }
 
-void key_pressed( unsigned char key, int x, int y )
+static void key_pressed( unsigned char key, int x, int y )
 {
    (void) x;
    (void) y;
 
-   switch ( key )
-   {
+   switch ( key ) {
    case 'c':
    case 'C':
       clear();
@@ -479,6 +435,7 @@ void key_pressed( unsigned char key, int x, int y )
    case 'T':
       tesse();
       break;
+   case 27:
    case 'q':
    case 'Q':
       quit();
@@ -488,7 +445,7 @@ void key_pressed( unsigned char key, int x, int y )
    glutPostRedisplay();
 }
 
-void myinit( void )
+static void myinit( void )
 {
    /* clear background to gray */
    glClearColor( 0.4, 0.4, 0.4, 0.0 );
@@ -524,6 +481,8 @@ static void reshape( GLsizei w, GLsizei h )
    set_screen_wh( w, h );
 }
 
+#endif
+
 
 static void usage( void )
 {
@@ -533,26 +492,32 @@ static void usage( void )
 }
 
 
-/*
- * Main Loop
- * Open window with initial window size, title bar,
- * RGBA display mode, and handle input events.
- */
 int main( int argc, char **argv )
 {
-    usage();
+   const char *version = (const char *) gluGetString( GLU_VERSION );
+   printf( "GLU version string: %s\n", version );
+   if ( strstr( version, "1.0" ) || strstr( version, "1.1" ) ) {
+      fprintf( stderr, "Sorry, this demo reqiures GLU 1.2 or later.\n" );
+      exit( 1 );
+   }
+
+   usage();
 
-    glutInit( &argc, argv );
-    glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
-    glutInitWindowSize( 400, 400 );
-    glutCreateWindow( argv[0] );
+   glutInit( &argc, argv );
+   glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
+   glutInitWindowSize( 400, 400 );
+   glutCreateWindow( argv[0] );
 
-    myinit();
+   /* GH: Bit of a hack...
+    */
+#ifdef GLU_VERSION_1_2
+   myinit();
 
-    glutDisplayFunc( display );
-    glutReshapeFunc( reshape );
+   glutDisplayFunc( display );
+   glutReshapeFunc( reshape );
 
-    glutMainLoop();
+   glutMainLoop();
+#endif
 
-    return 0;
+   return 0;
 }