demos: delete vertex array objects upon exit
[mesa.git] / progs / demos / isosurf.c
index 85fb7f215c71d56e3cffcd79b34a06d5edd376a3..e280d8f507c553d2d4201cf9176b6ef4d8325950 100644 (file)
@@ -1,4 +1,3 @@
-/* $Id: isosurf.c,v 1.11 2001/05/11 15:47:02 keithw Exp $ */
 
 /*
  * Display an isosurface of 3-D wind speed volume.
 #include <stdlib.h>
 #include <string.h>
 #include <math.h>
-#define GL_GLEXT_LEGACY
+#ifdef _WIN32
+#include <windows.h>
+#undef CLIP_MASK
+#endif
+#include <GL/glew.h>
 #include "GL/glut.h"
 
-#include "readtex.c"   /* I know, this is a hack.  KW: me too. */
+#include "readtex.h"
 #define TEXTURE_FILE "../images/reflect.rgb"
 
-#define LIT            0x1
-#define UNLIT          0x2
-#define REFLECT                0x10
-#define POINT_FILTER   0x40
-#define LINEAR_FILTER  0x80
-#define GLVERTEX       0x100
-#define DRAW_ELTS      0x4 
-#define DRAW_ARRAYS    0x200 
-#define ARRAY_ELT      0x400
-#define LOCKED         0x800
-#define UNLOCKED       0x8 
-#define IMMEDIATE      0x1000
-#define DISPLAYLIST    0x2000000
-#define SHADE_SMOOTH   0x2000
-#define SHADE_FLAT     0x4000
-#define TRIANGLES      0x8000
-#define STRIPS         0x10000
-#define POINTS         0x20000
-#define USER_CLIP      0x40000
-#define NO_USER_CLIP   0x80000
-#define MATERIALS      0x100000
-#define NO_MATERIALS   0x200000
-#define FOG            0x400000
-#define NO_FOG         0x800000
-#define QUIT           0x1000000
-#define GLINFO         0x4000000
-#define STIPPLE                0x8000000
-#define NO_STIPPLE     0x20
+#define LIT            0x00000001
+#define UNLIT          0x00000002
+#define REFLECT                0x00000004
+#define POINT_FILTER   0x00000008
+#define LINEAR_FILTER  0x00000010
+#define GLVERTEX       0x00000020
+#define DRAW_ELTS      0x00000040 
+#define DRAW_ARRAYS    0x00000080 
+#define ARRAY_ELT      0x00000100
+#define LOCKED         0x00000200
+#define UNLOCKED       0x00000400 
+#define IMMEDIATE      0x00000800
+#define DISPLAYLIST    0x00001000
+#define SHADE_SMOOTH   0x00002000
+#define SHADE_FLAT     0x00004000
+#define TRIANGLES      0x00008000
+#define STRIPS         0x00010000
+#define POINTS         0x00020000
+#define USER_CLIP      0x00040000
+#define NO_USER_CLIP   0x00080000
+#define MATERIALS      0x00100000
+#define NO_MATERIALS   0x00200000
+#define FOG            0x00400000
+#define NO_FOG         0x00800000
+#define QUIT           0x01000000
+#define GLINFO         0x02000000
+#define STIPPLE                0x04000000
+#define NO_STIPPLE     0x08000000
+#define POLYGON_FILL   0x10000000
+#define POLYGON_LINE   0x20000000
+#define POLYGON_POINT  0x40000000
 
 #define LIGHT_MASK             (LIT|UNLIT|REFLECT)
 #define FILTER_MASK            (POINT_FILTER|LINEAR_FILTER)
 #define SHADE_MASK             (SHADE_SMOOTH|SHADE_FLAT)
 #define FOG_MASK               (FOG|NO_FOG)
 #define STIPPLE_MASK           (STIPPLE|NO_STIPPLE)
+#define POLYGON_MASK           (POLYGON_FILL|POLYGON_LINE|POLYGON_POINT)
 
 #define MAXVERTS 10000
-static GLuint maxverts = MAXVERTS;
+static GLint maxverts = MAXVERTS;
 static float data[MAXVERTS][6];
 static float compressed_data[MAXVERTS][6];
 static float expanded_data[MAXVERTS*3][6];
@@ -141,7 +148,7 @@ static void read_surface( char *filename )
 static void print_flags( const char *msg, GLuint flags ) 
 {
    fprintf(stderr, 
-          "%s (0x%x): %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
+          "%s (0x%x): %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
           msg, flags,
           (flags & GLVERTEX) ? "glVertex, " : "",
           (flags & DRAW_ARRAYS) ? "glDrawArrays, " : "",
@@ -159,7 +166,9 @@ static void print_flags( const char *msg, GLuint flags )
           (flags & USER_CLIP) ? "user_clip, " : "",
           (flags & MATERIALS) ? "materials, " : "",
           (flags & FOG) ? "fog, " : "",
-          (flags & STIPPLE) ? "stipple, " : "");
+          (flags & STIPPLE) ? "stipple, " : "",
+          (flags & POLYGON_LINE) ? "polygon mode line, " : "",
+          (flags & POLYGON_POINT) ? "polygon mode point, " : "");
 }
 
 
@@ -330,7 +339,7 @@ static void make_tri_indices( void )
 {
    unsigned int *v = tri_indices;
    unsigned int parity = 0;
-   unsigned int i, j;
+   int i, j;
 
    for (j=2;j<numverts;j++,parity^=1) {
       if (parity) {
@@ -360,9 +369,9 @@ static void make_tri_indices( void )
 
 #define MIN(x,y) (x < y) ? x : y
 
-static void draw_surface( int with_state )
+static void draw_surface( unsigned int with_state )
 {
-   GLuint i, j;
+   GLint i, j;
    
    if (with_state & DISPLAYLIST) {
       if ((with_state & (RENDER_STYLE_MASK|PRIMITIVE_MASK|MATERIAL_MASK)) != 
@@ -449,15 +458,20 @@ static void draw_surface( int with_state )
       break;
 
    case (DRAW_ARRAYS|POINTS):
-      glDrawArraysEXT( GL_POINTS, 0, numverts );
+      glDrawArraysEXT( GL_POINTS, 0, numuniq );
       break;
    case (DRAW_ELTS|POINTS):
-      glDrawElements( GL_POINTS, numverts, GL_UNSIGNED_INT, indices );
+      /* can use numuniq with strip_indices as strip_indices[i] == i.
+       */
+      glDrawElements( GL_POINTS, numuniq, 
+                     GL_UNSIGNED_INT, strip_indices );
       break;
    case (ARRAY_ELT|POINTS):
+      /* just emit each unique element once:
+       */
       glBegin( GL_POINTS );
-      for (i = 0 ; i < numverts ; i++)
-        glArrayElement( indices[i] );
+      for (i = 0 ; i < numuniq ; i++)
+        glArrayElement( i );
       glEnd();
       break;
 #endif
@@ -487,10 +501,14 @@ static void draw_surface( int with_state )
       break;
 
    case (GLVERTEX|POINTS):
+      /* Renders all points, but not in strip order...  Shouldn't be a
+       * problem, but people may be confused as to why points are so
+       * much faster in this demo...  And why cva doesn't help them...
+       */
       glBegin( GL_POINTS );
-      for ( i = 0 ; i < numverts ; i++ ) {
-         glNormal3fv( &data[i][3] );
-         glVertex3fv( &data[i][0] );
+      for ( i = 0 ; i < numuniq ; i++ ) {
+         glNormal3fv( &compressed_data[i][3] );
+         glVertex3fv( &compressed_data[i][0] );
       }
       glEnd();
       break;
@@ -672,28 +690,37 @@ static void ModeMenu(int m)
 
    if (CHANGED(state, m, FOG_MASK)) {
       UPDATE(state, m, FOG_MASK);
-      if (m & FOG)
-      {
+      if (m & FOG) {
         glEnable(GL_FOG);
       }
-      else
-      {
+      else {
         glDisable(GL_FOG);
       }
    }
 
    if (CHANGED(state, m, STIPPLE_MASK)) {
       UPDATE(state, m, STIPPLE_MASK);
-      if (m & STIPPLE)
-      {
+      if (m & STIPPLE) {
         glEnable(GL_POLYGON_STIPPLE);
       }
-      else
-      {
+      else {
         glDisable(GL_POLYGON_STIPPLE);
       }
    }
 
+   if (CHANGED(state, m, POLYGON_MASK)) {
+      UPDATE(state, m, POLYGON_MASK);
+      if (m & POLYGON_FILL) {
+        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
+      }
+      else if (m & POLYGON_LINE) {
+        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+      }
+      else {
+        glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
+      }
+   }
+
 #ifdef GL_EXT_vertex_array
    if (CHANGED(state, m, (LOCK_MASK|RENDER_STYLE_MASK|PRIMITIVE_MASK)))
    {
@@ -713,24 +740,16 @@ static void ModeMenu(int m)
       print_flags("primitive", state & PRIMITIVE_MASK);
       print_flags("render style", state & RENDER_STYLE_MASK);
 
-      if ((state & PRIMITIVE_MASK) != TRIANGLES)
-      {
-        fprintf(stderr, "enabling normal arrays\n");
-        glVertexPointerEXT( 3, GL_FLOAT, sizeof(data[0]), numverts, data );
-        glNormalPointerEXT( GL_FLOAT, sizeof(data[0]), numverts, &data[0][3]);
-#ifdef GL_EXT_compiled_vertex_array
-        if (allowed & LOCKED) {
-           if (state & LOCKED) {
-              glLockArraysEXT( 0, numverts );
-           } else {
-              glUnlockArraysEXT();
-           }
-        }
-#endif
-      }
-      else if ((state & RENDER_STYLE_MASK) != DRAW_ARRAYS) 
+      if ((state & PRIMITIVE_MASK) != STRIPS &&
+         ((state & RENDER_STYLE_MASK) == DRAW_ELTS ||
+          (state & RENDER_STYLE_MASK) == ARRAY_ELT || 
+          (state & PRIMITIVE_MASK) == POINTS))
       {
         fprintf(stderr, "enabling small arrays\n");
+        /* Rendering any primitive with draw-element/array-element
+         *  --> Can't do strips here as ordering has been lost in
+         *  compaction process...
+         */
         glVertexPointerEXT( 3, GL_FLOAT, sizeof(data[0]), numuniq,
                             compressed_data );
         glNormalPointerEXT( GL_FLOAT, sizeof(data[0]), numuniq,
@@ -745,9 +764,10 @@ static void ModeMenu(int m)
         }
 #endif
       }
-      else {
+      else if ((state & PRIMITIVE_MASK) == TRIANGLES &&
+              (state & RENDER_STYLE_MASK) == DRAW_ARRAYS) {
         fprintf(stderr, "enabling big arrays\n");
-        /* Only get here for DRAW_ARRAYS + TRIANGLES
+        /* Only get here for TRIANGLES and drawarrays
          */
         glVertexPointerEXT( 3, GL_FLOAT, sizeof(data[0]), (numverts-2) * 3,
                             expanded_data );
@@ -762,6 +782,20 @@ static void ModeMenu(int m)
               glUnlockArraysEXT();
            }
         }
+#endif
+      }
+      else {
+        fprintf(stderr, "enabling normal arrays\n");
+        glVertexPointerEXT( 3, GL_FLOAT, sizeof(data[0]), numverts, data );
+        glNormalPointerEXT( GL_FLOAT, sizeof(data[0]), numverts, &data[0][3]);
+#ifdef GL_EXT_compiled_vertex_array
+        if (allowed & LOCKED) {
+           if (state & LOCKED) {
+              glLockArraysEXT( 0, numverts );
+           } else {
+              glUnlockArraysEXT();
+           }
+        }
 #endif
       }
 
@@ -1004,7 +1038,6 @@ static GLint Args(int argc, char **argv)
 int main(int argc, char **argv)
 {
    GLenum type;
-   char *extensions;
 
    GLuint arg_mode = Args(argc, argv);
 
@@ -1013,6 +1046,7 @@ int main(int argc, char **argv)
 
    read_surface( "isosurf.dat" );
 
+   glutInit( &argc, argv);
    glutInitWindowPosition(0, 0);
    glutInitWindowSize(400, 400);
 
@@ -1025,15 +1059,15 @@ int main(int argc, char **argv)
       exit(0);
    }
 
-   /* Make sure server supports the vertex array extension */
-   extensions = (char *) glGetString( GL_EXTENSIONS );
+   glewInit();
 
-   if (!strstr( extensions, "GL_EXT_vertex_array" ))
+   /* Make sure server supports the vertex array extension */
+   if (!GLEW_EXT_vertex_array)
    {
       printf("Vertex arrays not supported by this renderer\n");
       allowed &= ~(LOCKED|DRAW_ARRAYS|DRAW_ELTS|ARRAY_ELT);
    }
-   else if (!strstr( extensions, "GL_EXT_compiled_vertex_array" ))
+   else if (!GLEW_EXT_compiled_vertex_array)
    {
       printf("Compiled vertex arrays not supported by this renderer\n");
       allowed &= ~LOCKED;
@@ -1058,6 +1092,10 @@ int main(int argc, char **argv)
    glutAddMenuEntry("Stipple",               STIPPLE);
    glutAddMenuEntry("No Stipple",            NO_STIPPLE);
    glutAddMenuEntry("", 0);
+   glutAddMenuEntry("Polygon Mode Fill",     POLYGON_FILL);
+   glutAddMenuEntry("Polygon Mode Line",     POLYGON_LINE);
+   glutAddMenuEntry("Polygon Mode Points",   POLYGON_POINT);
+   glutAddMenuEntry("", 0);
    glutAddMenuEntry("Point Filtered",        POINT_FILTER);
    glutAddMenuEntry("Linear Filtered",       LINEAR_FILTER);
    glutAddMenuEntry("", 0);