Merge branch 'mesa_7_7_branch'
[mesa.git] / progs / demos / isosurf.c
index 0ae69d101043ea1c88367ad7ba46f10d26ae4f68..a5b21ffb5c3f9430fd563f2ce060dd8582cd4d62 100644 (file)
@@ -1,7 +1,6 @@
-/* $Id: isosurf.c,v 1.5 2000/03/30 17:58:56 keithw Exp $ */
 
 /*
- * Display an isosurface of 3-D wind speed volume.  
+ * Display an isosurface of 3-D wind speed volume.
  *
  * Command line options:
  *    -info      print GL implementation information
  * Other options are available via the popup menu.
  */
 
-/*
- * $Log: isosurf.c,v $
- * Revision 1.5  2000/03/30 17:58:56  keithw
- * Added stipple mode
- *
- * Revision 1.4  1999/10/21 16:39:06  brianp
- * added -info command line option
- *
- * Revision 1.3  1999/09/08 22:14:31  brianp
- * minor changes. always call compactify_arrays()
- *
- * Revision 1.2  1999/09/03 14:56:40  keithw
- * Fog, displaylist and zoom operations
- *
- * Revision 3.4  1999/04/24 01:10:47  keithw
- * clip planes, materials
- *
- * Revision 3.3  1999/03/31 19:42:14  keithw
- * support for cva
- *
- * Revision 3.1  1998/11/01 20:30:20  brianp
- * added benchmark feature (b key)
- *
- * Revision 3.0  1998/02/14 18:42:29  brianp
- * initial rev
- *
- */
-
-
-
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
-#include <string.h>
 #include <math.h>
+#ifdef _WIN32
+#include <windows.h>
+#undef CLIP_MASK
+#endif
+#include <GL/glew.h>
 #include "GL/glut.h"
 
-#include "../util/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 TEXTURE             0x4
-#define NO_TEXTURE          0x8
-#define REFLECT             0x10
-#define NO_REFLECT          0x20
-#define POINT_FILTER        0x40
-#define LINEAR_FILTER       0x80
-#define GLVERTEX           0x100
-#define DRAW_ARRAYS         0x200 /* or draw_elts, if compiled */
-#define ARRAY_ELT           0x400
-#define COMPILED            0x800
-#define IMMEDIATE           0x1000
-#define SHADE_SMOOTH        0x2000
-#define SHADE_FLAT          0x4000
-#define TRIANGLES           0x8000
-#define STRIPS              0x10000
-#define USER_CLIP           0x20000
-#define NO_USER_CLIP        0x40000
-#define MATERIALS           0x80000
-#define NO_MATERIALS        0x100000
-#define FOG                 0x200000
-#define NO_FOG              0x400000
-#define QUIT                0x800000
-#define DISPLAYLIST         0x1000000
-#define GLINFO              0x2000000
-#define STIPPLE             0x4000000
-#define NO_STIPPLE          0x8000000
-
-#define LIGHT_MASK  (LIT|UNLIT)
-#define TEXTURE_MASK (TEXTURE|NO_TEXTURE)
-#define REFLECT_MASK (REFLECT|NO_REFLECT)
-#define FILTER_MASK (POINT_FILTER|LINEAR_FILTER)
-#define RENDER_STYLE_MASK (GLVERTEX|DRAW_ARRAYS|ARRAY_ELT)
-#define COMPILED_MASK (COMPILED|IMMEDIATE|DISPLAYLIST)
-#define MATERIAL_MASK (MATERIALS|NO_MATERIALS)
-#define PRIMITIVE_MASK (TRIANGLES|STRIPS)
-#define CLIP_MASK (USER_CLIP|NO_USER_CLIP)
-#define SHADE_MASK (SHADE_SMOOTH|SHADE_FLAT)
-#define FOG_MASK (FOG|NO_FOG)
-#define STIPPLE_MASK (STIPPLE|NO_STIPPLE)
+#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 RENDER_STYLE_MASK      (GLVERTEX|DRAW_ARRAYS|DRAW_ELTS|ARRAY_ELT)
+#define DLIST_MASK             (IMMEDIATE|DISPLAYLIST)
+#define LOCK_MASK              (LOCKED|UNLOCKED)
+#define MATERIAL_MASK          (MATERIALS|NO_MATERIALS)
+#define PRIMITIVE_MASK         (TRIANGLES|STRIPS|POINTS)
+#define CLIP_MASK              (USER_CLIP|NO_USER_CLIP)
+#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 GLint maxverts = MAXVERTS;
 static float data[MAXVERTS][6];
 static float compressed_data[MAXVERTS][6];
+static float expanded_data[MAXVERTS*3][6];
 static GLuint indices[MAXVERTS];
 static GLuint tri_indices[MAXVERTS*3];
+static GLuint strip_indices[MAXVERTS];
 static GLfloat col[100][4];
 static GLint numverts, num_tri_verts, numuniq;
 
 static GLfloat xrot;
 static GLfloat yrot;
-static GLfloat dist = -6;
+static GLfloat dist;
 static GLint state, allowed = ~0;
 static GLboolean doubleBuffer = GL_TRUE;
-static GLdouble plane[4] = {1.0, 0.0, -1.0, 0.0};
-static GLuint surf1;
+static GLdouble plane[4];
+static GLuint surf1, dlist_state;
 
 static GLboolean PrintInfo = GL_FALSE;
 
@@ -139,9 +118,6 @@ static GLubyte halftone[] = {
    0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55};
 
-/* forward decl */
-int BuildList( int mode );
-
 
 static void read_surface( char *filename )
 {
@@ -154,10 +130,12 @@ static void read_surface( char *filename )
    }
 
    numverts = 0;
-   while (!feof(f) && numverts<MAXVERTS) {
-      fscanf( f, "%f %f %f  %f %f %f",
-             &data[numverts][0], &data[numverts][1], &data[numverts][2],
-             &data[numverts][3], &data[numverts][4], &data[numverts][5] );
+   while (!feof(f) && numverts<maxverts) {
+      int result;
+      result = fscanf( f, "%f %f %f  %f %f %f",
+                      &data[numverts][0], &data[numverts][1], &data[numverts][2],
+                      &data[numverts][3], &data[numverts][4], &data[numverts][5] );
+      (void) result;
       numverts++;
    }
    numverts--;
@@ -168,6 +146,33 @@ 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%s%s\n",
+          msg, flags,
+          (flags & GLVERTEX) ? "glVertex, " : "",
+          (flags & DRAW_ARRAYS) ? "glDrawArrays, " : "",
+          (flags & DRAW_ELTS) ? "glDrawElements, " : "",
+          (flags & ARRAY_ELT) ? "glArrayElement, " : "",
+          (flags & LOCKED) ? "locked arrays, " : "",
+          (flags & TRIANGLES) ? "GL_TRIANGLES, " : "",
+          (flags & STRIPS) ? "GL_TRIANGLE_STRIP, " : "",
+          (flags & POINTS) ? "GL_POINTS, " : "",
+          (flags & DISPLAYLIST) ? "as a displaylist, " : "",
+          (flags & LIT) ? "lit, " : "",
+          (flags & UNLIT) ? "unlit, " : "",
+          (flags & REFLECT) ? "reflect, " : "",
+          (flags & SHADE_FLAT) ? "flat-shaded, " : "",
+          (flags & USER_CLIP) ? "user_clip, " : "",
+          (flags & MATERIALS) ? "materials, " : "",
+          (flags & FOG) ? "fog, " : "",
+          (flags & STIPPLE) ? "stipple, " : "",
+          (flags & POLYGON_LINE) ? "polygon mode line, " : "",
+          (flags & POLYGON_POINT) ? "polygon mode point, " : "");
+}
+
+
 
 struct data_idx {
    float *data;
@@ -209,7 +214,7 @@ int (*(compare[7]))( const void *a, const void *b ) =
 
 #define VEC_ELT(f, s, i)  (float *)(((char *)f) + s * i)
 
-static int sort_axis( int axis, 
+static int sort_axis( int axis,
                      int vec_size,
                      int vec_stride,
                      struct data_idx *indices,
@@ -221,27 +226,27 @@ static int sort_axis( int axis,
 {
    int i;
 
-   if (finish-start > 2) 
+   if (finish-start > 2)
    {
       qsort( indices+start, finish-start, sizeof(*indices), compare[axis] );
-   } 
-   else if (indices[start].data[axis] > indices[start+1].data[axis]) 
+   }
+   else if (indices[start].data[axis] > indices[start+1].data[axis])
    {
       struct data_idx tmp = indices[start];
       indices[start] = indices[start+1];
       indices[start+1] = tmp;
    }
-        
+
    if (axis == vec_size-1) {
       for (i = start ; i < finish ; ) {
         float max = indices[i].data[axis] + fudge;
         float *dest = VEC_ELT(out, vec_stride, uniq);
         int j;
-       
+
         for (j = 0 ; j < vec_size ; j++)
            dest[j] = indices[i].data[j];
 
-        for ( ; i < finish && max >= indices[i].data[axis]; i++) 
+        for ( ; i < finish && max >= indices[i].data[axis]; i++)
            indices[i].uniq_idx = uniq;
 
         uniq++;
@@ -256,7 +261,7 @@ static int sort_axis( int axis,
            int k;
 
            indices[i].uniq_idx = uniq;
-       
+
            for (k = 0 ; k < vec_size ; k++)
               dest[k] = indices[i].data[k];
 
@@ -273,7 +278,7 @@ static int sort_axis( int axis,
 }
 
 
-static void extract_indices1( const struct data_idx *in, unsigned int *out, 
+static void extract_indices1( const struct data_idx *in, unsigned int *out,
                              int n )
 {
    int i;
@@ -295,13 +300,13 @@ static void compactify_arrays(void)
       ind[i].data = data[i];
    }
 
-   numuniq = sort_axis(0, 
-                      sizeof(compressed_data[0])/sizeof(float), 
+   numuniq = sort_axis(0,
+                      sizeof(compressed_data[0])/sizeof(float),
                       sizeof(compressed_data[0]),
-                      ind, 
-                      0, 
-                      numverts, 
-                      (float *)compressed_data, 
+                      ind,
+                      0,
+                      numverts,
+                      (float *)compressed_data,
                       0,
                       1e-6);
 
@@ -311,6 +316,20 @@ static void compactify_arrays(void)
    free( ind );
 }
 
+static void expand_arrays(void) 
+{
+   int i;
+   int parity = 0;
+   for (i = 2 ; i < numverts ; i++, parity ^= 1) {
+      int v0 = i-2+parity;
+      int v1 = i-1-parity;
+      int v2 = i;
+      memcpy( expanded_data[(i-2)*3+0], data[v0], sizeof(data[0]) );
+      memcpy( expanded_data[(i-2)*3+1], data[v1], sizeof(data[0]) );
+      memcpy( expanded_data[(i-2)*3+2], data[v2], sizeof(data[0]) );
+   }
+}
+
 static float myrand( float max )
 {
    return max*rand()/(RAND_MAX+1.0);
@@ -321,12 +340,12 @@ 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) {
         *v++ = indices[j-1];
-        *v++ = indices[j-2]; 
+        *v++ = indices[j-2];
         *v++ = indices[j];
       } else {
         *v++ = indices[j-2];
@@ -334,7 +353,7 @@ static void make_tri_indices( void )
         *v++ = indices[j];
       }
    }
-   
+
    num_tri_verts = v - tri_indices;
    printf("num_tri_verts: %d\n", num_tri_verts);
 
@@ -344,31 +363,43 @@ static void make_tri_indices( void )
       col[j][1] = myrand(1);
       col[j][0] = myrand(1);
    }
+
+   for (i = 0; i < numverts ; i++)
+      strip_indices[i] = i;
 }
 
 #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;
-
-   switch (with_state & (COMPILED_MASK|RENDER_STYLE_MASK|PRIMITIVE_MASK)) {
-#ifdef GL_EXT_vertex_array
-
-   case (COMPILED|DRAW_ARRAYS|STRIPS):
-      glDrawElements( GL_TRIANGLE_STRIP, numverts, GL_UNSIGNED_INT, indices );
-      break;
+   GLint i, j;
+   
+   if (with_state & DISPLAYLIST) {
+      if ((with_state & (RENDER_STYLE_MASK|PRIMITIVE_MASK|MATERIAL_MASK)) != 
+         dlist_state) {
+        /* 
+         */
+        fprintf(stderr, "rebuilding displaylist\n");
+
+        if (dlist_state)
+           glDeleteLists( surf1, 1 );
+
+        dlist_state = with_state & (RENDER_STYLE_MASK|PRIMITIVE_MASK|
+                                    MATERIAL_MASK);
+        surf1 = glGenLists(1);
+        glNewList(surf1, GL_COMPILE);
+        draw_surface( dlist_state );
+        glEndList();
+      }
 
+      glCallList( surf1 );
+      return;
+   }
 
-   case (COMPILED|ARRAY_ELT|STRIPS):
-      glBegin( GL_TRIANGLE_STRIP );
-      for (i = 0 ; i < numverts ; i++) 
-        glArrayElement( indices[i] );      
-      glEnd();
-      break;
+   switch (with_state & (RENDER_STYLE_MASK|PRIMITIVE_MASK)) {
+#ifdef GL_EXT_vertex_array
 
-   case (COMPILED|DRAW_ARRAYS|TRIANGLES):
-   case (IMMEDIATE|DRAW_ARRAYS|TRIANGLES):
+   case (DRAW_ELTS|TRIANGLES):
       if (with_state & MATERIALS) {
         for (j = i = 0 ; i < num_tri_verts ; i += 600, j++) {
            GLuint nr = MIN(num_tri_verts-i, 600);
@@ -377,29 +408,16 @@ static void draw_surface( int with_state )
            glDrawElements( GL_TRIANGLES, nr, GL_UNSIGNED_INT, tri_indices+i );
         }
       } else {
-        glDrawElements( GL_TRIANGLES, num_tri_verts, GL_UNSIGNED_INT, 
+        glDrawElements( GL_TRIANGLES, num_tri_verts, GL_UNSIGNED_INT,
                         tri_indices );
       }
-
-      break;
-
-      /* Uses the original arrays (including duplicate elements):
-       */
-   case (IMMEDIATE|DRAW_ARRAYS|STRIPS):
-      glDrawArraysEXT( GL_TRIANGLE_STRIP, 0, numverts );
       break;
 
-      /* Uses the original arrays (including duplicate elements):
-       */
-   case (IMMEDIATE|ARRAY_ELT|STRIPS):
-      glBegin( GL_TRIANGLE_STRIP );
-      for (i = 0 ; i < numverts ; i++)
-        glArrayElement( i );
-      glEnd();
+   case (DRAW_ARRAYS|TRIANGLES):
+      glDrawArraysEXT( GL_TRIANGLES, 0, (numverts-2)*3 );
       break;
 
-   case (IMMEDIATE|ARRAY_ELT|TRIANGLES):
-   case (COMPILED|ARRAY_ELT|TRIANGLES):
+   case (ARRAY_ELT|TRIANGLES):
       if (with_state & MATERIALS) {
         for (j = i = 0 ; i < num_tri_verts ; i += 600, j++) {
            GLuint nr = MIN(num_tri_verts-i, 600);
@@ -415,12 +433,51 @@ static void draw_surface( int with_state )
         glBegin( GL_TRIANGLES );
         for (i = 0 ; i < num_tri_verts ; i++)
            glArrayElement( tri_indices[i] );
-              
+
         glEnd();
-      }         
+      }
+      break;
+
+
+      /* Uses the original arrays (including duplicate elements):
+       */
+   case (DRAW_ARRAYS|STRIPS):
+      glDrawArraysEXT( GL_TRIANGLE_STRIP, 0, numverts );
+      break;
+   case (DRAW_ELTS|STRIPS):
+      glDrawElements( GL_TRIANGLE_STRIP, numverts,
+                     GL_UNSIGNED_INT, strip_indices );
       break;
 
-   case (IMMEDIATE|GLVERTEX|TRIANGLES):
+      /* Uses the original arrays (including duplicate elements):
+       */
+   case (ARRAY_ELT|STRIPS):
+      glBegin( GL_TRIANGLE_STRIP );
+      for (i = 0 ; i < numverts ; i++)
+        glArrayElement( i );
+      glEnd();
+      break;
+
+   case (DRAW_ARRAYS|POINTS):
+      glDrawArraysEXT( GL_POINTS, 0, numuniq );
+      break;
+   case (DRAW_ELTS|POINTS):
+      /* 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 < numuniq ; i++)
+        glArrayElement( i );
+      glEnd();
+      break;
+#endif
+
+   case (GLVERTEX|TRIANGLES):
       if (with_state & MATERIALS) {
         for (j = i = 0 ; i < num_tri_verts ; i += 600, j++) {
            GLuint nr = MIN(num_tri_verts-i, 600);
@@ -441,27 +498,50 @@ static void draw_surface( int with_state )
            glVertex3fv( &compressed_data[tri_indices[i]][0] );
         }
         glEnd();
-      }         
+      }
       break;
 
-   case (DISPLAYLIST|GLVERTEX|STRIPS):
-      if (!surf1)
-        surf1 = BuildList( GL_COMPILE_AND_EXECUTE );
-      else
-        glCallList(surf1);
+   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 < numuniq ; i++ ) {
+         glNormal3fv( &compressed_data[i][3] );
+         glVertex3fv( &compressed_data[i][0] );
+      }
+      glEnd();
       break;
 
-#endif
+   case (GLVERTEX|STRIPS):
+      if (with_state & MATERIALS) {
+         glBegin( GL_TRIANGLE_STRIP );
+         for (i=0;i<numverts;i++) {
+            if (i % 600 == 0 && i != 0) {
+               unsigned j = i / 600;
+               glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, col[j]);
+               glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, col[j]);
+            }
+            glNormal3fv( &data[i][3] );
+            glVertex3fv( &data[i][0] );
+         }
+         glEnd();
+      }
+      else {
+         glBegin( GL_TRIANGLE_STRIP );
+         for (i=0;i<numverts;i++) {
+            glNormal3fv( &data[i][3] );
+            glVertex3fv( &data[i][0] );
+         }
+         glEnd();
+      }
+      break;
 
-      /* Uses the original arrays (including duplicate elements):
-       */
    default:
-      glBegin( GL_TRIANGLE_STRIP );
-      for (i=0;i<numverts;i++) {
-         glNormal3fv( &data[i][3] );
-         glVertex3fv( &data[i][0] );
-      }
-      glEnd();
+      fprintf(stderr, "unimplemented mode %x...\n", 
+             (with_state & (RENDER_STYLE_MASK|PRIMITIVE_MASK)));
+      break;
    }
 }
 
@@ -472,17 +552,9 @@ static void Display(void)
     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
     draw_surface( state );
     glFlush();
-    if (doubleBuffer) glutSwapBuffers();    
+    if (doubleBuffer) glutSwapBuffers();
 }
 
-int BuildList( int mode )
-{
-   int rv = glGenLists(1);
-   glNewList(rv, mode );
-   draw_surface( IMMEDIATE|GLVERTEX|STRIPS );
-   glEndList();
-   return rv;
-}
 
 /* KW: only do this when necessary, so CVA can re-use results.
  */
@@ -544,12 +616,12 @@ static void InitMaterials(void)
     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
     glLightfv(GL_LIGHT0, GL_POSITION, position0);
     glEnable(GL_LIGHT0);
-    
+
     glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
     glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
     glLightfv(GL_LIGHT1, GL_POSITION, position1);
     glEnable(GL_LIGHT1);
-    
+
     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
     glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
 
@@ -563,8 +635,7 @@ static void InitMaterials(void)
 
 
 #define UPDATE(o,n,mask) (o&=~mask, o|=n&mask)
-#define CHANGED(o,n,mask) ((n&mask) && \
-                           (n&mask) != (o&mask) ? UPDATE(o,n,mask) : 0)
+#define CHANGED(o,n,mask) ((n&mask) && (n&mask) != (o&mask) )
 
 static void ModeMenu(int m)
 {
@@ -572,7 +643,7 @@ static void ModeMenu(int m)
 
    if (!m) return;
 
-   if (m==QUIT) 
+   if (m==QUIT)
       exit(0);
 
    if (m==GLINFO) {
@@ -583,6 +654,7 @@ static void ModeMenu(int m)
    }
 
    if (CHANGED(state, m, FILTER_MASK)) {
+      UPDATE(state, m, FILTER_MASK);
       if (m & LINEAR_FILTER) {
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
@@ -593,13 +665,29 @@ static void ModeMenu(int m)
    }
 
    if (CHANGED(state, m, LIGHT_MASK)) {
-      if (m & LIT)
+      UPDATE(state, m, LIGHT_MASK);
+      if (m & LIT) {
         glEnable(GL_LIGHTING);
-      else
+        glDisable(GL_TEXTURE_GEN_S);
+        glDisable(GL_TEXTURE_GEN_T);
+        glDisable(GL_TEXTURE_2D);
+      }
+      else if (m & UNLIT) {
+        glDisable(GL_LIGHTING);
+        glDisable(GL_TEXTURE_GEN_S);
+        glDisable(GL_TEXTURE_GEN_T);
+        glDisable(GL_TEXTURE_2D);
+      }
+      else if (m & REFLECT) {
         glDisable(GL_LIGHTING);
+        glEnable(GL_TEXTURE_GEN_S);
+        glEnable(GL_TEXTURE_GEN_T);
+        glEnable(GL_TEXTURE_2D);
+      }
    }
 
    if (CHANGED(state, m, SHADE_MASK)) {
+      UPDATE(state, m, SHADE_MASK);
       if (m & SHADE_SMOOTH)
         glShadeModel(GL_SMOOTH);
       else
@@ -607,24 +695,8 @@ static void ModeMenu(int m)
    }
 
 
-   if (CHANGED(state, m, TEXTURE_MASK)) {
-      if (m & TEXTURE) 
-        glEnable(GL_TEXTURE_2D);
-      else
-        glDisable(GL_TEXTURE_2D);
-   }
-
-   if (CHANGED(state, m, REFLECT_MASK)) {
-      if (m & REFLECT) {
-        glEnable(GL_TEXTURE_GEN_S);
-        glEnable(GL_TEXTURE_GEN_T);
-      } else {
-        glDisable(GL_TEXTURE_GEN_S);
-        glDisable(GL_TEXTURE_GEN_T);
-      }
-   }
-
    if (CHANGED(state, m, CLIP_MASK)) {
+      UPDATE(state, m, CLIP_MASK);
       if (m & USER_CLIP) {
         glEnable(GL_CLIP_PLANE0);
       } else {
@@ -633,66 +705,130 @@ static void ModeMenu(int m)
    }
 
    if (CHANGED(state, m, FOG_MASK)) {
-      if (m & FOG) 
-      {
+      UPDATE(state, m, FOG_MASK);
+      if (m & FOG) {
         glEnable(GL_FOG);
-        printf("FOG enable\n");
-      } 
-      else 
-      {
+      }
+      else {
         glDisable(GL_FOG);
-        printf("FOG disable\n");
       }
    }
 
    if (CHANGED(state, m, STIPPLE_MASK)) {
-      if (m & STIPPLE) 
-      {
+      UPDATE(state, m, STIPPLE_MASK);
+      if (m & STIPPLE) {
         glEnable(GL_POLYGON_STIPPLE);
-        printf("STIPPLE enable\n");
-      } 
-      else 
-      {
+      }
+      else {
         glDisable(GL_POLYGON_STIPPLE);
-        printf("STIPPLE disable\n");
+      }
+   }
+
+   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, (COMPILED_MASK|RENDER_STYLE_MASK|PRIMITIVE_MASK))) 
+   if (CHANGED(state, m, (LOCK_MASK|RENDER_STYLE_MASK|PRIMITIVE_MASK)))
    {
-      if ((m & (COMPILED_MASK|PRIMITIVE_MASK)) == (IMMEDIATE|STRIPS))
-      {
-        glVertexPointerEXT( 3, GL_FLOAT, sizeof(data[0]), numverts, data );
-        glNormalPointerEXT( GL_FLOAT, sizeof(data[0]), numverts, &data[0][3]);
+      if (m & (PRIMITIVE_MASK)) {
+        UPDATE(state, m, (PRIMITIVE_MASK));
+      }
+
+      if (m & (RENDER_STYLE_MASK)) {
+        UPDATE(state, m, (RENDER_STYLE_MASK));
+      }
+
+      if (m & LOCK_MASK) {
+        UPDATE(state, m, (LOCK_MASK));
       }
-      else 
+
+
+      print_flags("primitive", state & PRIMITIVE_MASK);
+      print_flags("render style", state & RENDER_STYLE_MASK);
+
+      if ((state & PRIMITIVE_MASK) != STRIPS &&
+         ((state & RENDER_STYLE_MASK) == DRAW_ELTS ||
+          (state & RENDER_STYLE_MASK) == ARRAY_ELT || 
+          (state & PRIMITIVE_MASK) == POINTS))
       {
-        glVertexPointerEXT( 3, GL_FLOAT, sizeof(data[0]), numuniq, 
+        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, 
+        glNormalPointerEXT( GL_FLOAT, sizeof(data[0]), numuniq,
                             &compressed_data[0][3]);
+#ifdef GL_EXT_compiled_vertex_array
+        if (allowed & LOCKED) {
+           if (state & LOCKED) {
+              glLockArraysEXT( 0, numuniq );
+           } else {
+              glUnlockArraysEXT();
+           }
+        }
+#endif
       }
+      else if ((state & PRIMITIVE_MASK) == TRIANGLES &&
+              (state & RENDER_STYLE_MASK) == DRAW_ARRAYS) {
+        fprintf(stderr, "enabling big arrays\n");
+        /* Only get here for TRIANGLES and drawarrays
+         */
+        glVertexPointerEXT( 3, GL_FLOAT, sizeof(data[0]), (numverts-2) * 3,
+                            expanded_data );
+        glNormalPointerEXT( GL_FLOAT, sizeof(data[0]), (numverts-2) * 3,
+                            &expanded_data[0][3]);
+
 #ifdef GL_EXT_compiled_vertex_array
-      if (allowed & COMPILED) {
-        if (m & COMPILED) {
-           glLockArraysEXT( 0, numuniq );
-        } else {
-           glUnlockArraysEXT();
+        if (allowed & LOCKED) {
+           if (state & LOCKED) {
+              glLockArraysEXT( 0, (numverts-2)*3 );
+           } else {
+              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
+      }
+
    }
 #endif
 
-   if (m & (RENDER_STYLE_MASK|PRIMITIVE_MASK)) {
-      UPDATE(state, m, (RENDER_STYLE_MASK|PRIMITIVE_MASK));
+
+   if (m & DLIST_MASK) {
+      UPDATE(state, m, DLIST_MASK);
    }
-   
+
    if (m & MATERIAL_MASK) {
       UPDATE(state, m, MATERIAL_MASK);
    }
 
+   print_flags("new flags", state);
+
    glutPostRedisplay();
 }
 
@@ -702,12 +838,18 @@ static void Init(int argc, char *argv[])
 {
    GLfloat fogColor[4] = {0.5,1.0,0.5,1.0};
 
+   xrot = 0;
+   yrot = 0;
+   dist = -6;
+   plane[0] = 1.0;
+   plane[1] = 0.0;
+   plane[2] = -1.0;
+   plane[3] = 0.0;
+
    glClearColor(0.0, 0.0, 1.0, 0.0);
    glEnable( GL_DEPTH_TEST );
-   glEnable( GL_VERTEX_ARRAY_EXT );
-   glEnable( GL_NORMAL_ARRAY_EXT );
-
-   InitMaterials();
+   glEnableClientState( GL_VERTEX_ARRAY );
+   glEnableClientState( GL_NORMAL_ARRAY );
 
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
@@ -715,7 +857,9 @@ static void Init(int argc, char *argv[])
 
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
-   glClipPlane(GL_CLIP_PLANE0, plane); 
+   glClipPlane(GL_CLIP_PLANE0, plane);
+
+   InitMaterials();
 
    set_matrix();
 
@@ -725,10 +869,6 @@ static void Init(int argc, char *argv[])
    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
 
-   if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
-      printf("Error: couldn't load texture image\n");
-      exit(1);
-   }
 
    /* Green fog is easy to see */
    glFogi(GL_FOG_MODE,GL_EXP2);
@@ -736,22 +876,31 @@ static void Init(int argc, char *argv[])
    glFogf(GL_FOG_DENSITY,0.15);
    glHint(GL_FOG_HINT,GL_DONT_CARE);
 
-
-   compactify_arrays();
-   make_tri_indices();
-
-   surf1 = BuildList( GL_COMPILE );
+   {
+      static int firsttime = 1;
+      if (firsttime) {
+        firsttime = 0;
+        compactify_arrays();
+        expand_arrays();
+        make_tri_indices();
+
+        if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
+           printf("Error: couldn't load texture image\n");
+           exit(1);
+        }
+      }
+   }
 
    ModeMenu(SHADE_SMOOTH|
            LIT|
-           NO_TEXTURE|
-           NO_REFLECT|
            POINT_FILTER|
-           IMMEDIATE|
            NO_USER_CLIP|
            NO_MATERIALS|
            NO_FOG|
            NO_STIPPLE|
+           IMMEDIATE|
+           STRIPS|
+           UNLOCKED|
            GLVERTEX);
 
    if (PrintInfo) {
@@ -788,7 +937,7 @@ static void Key( unsigned char key, int x, int y )
       ModeMenu((state ^ STIPPLE_MASK) & STIPPLE_MASK);
       break;
    case 'l':
-      ModeMenu((state ^ LIGHT_MASK) & LIGHT_MASK);
+      ModeMenu((state ^ LIGHT_MASK) & (LIT|UNLIT));
       break;
    case 'm':
       ModeMenu((state ^ MATERIAL_MASK) & MATERIAL_MASK);
@@ -797,11 +946,10 @@ static void Key( unsigned char key, int x, int y )
       ModeMenu((state ^ CLIP_MASK) & CLIP_MASK);
       break;
    case 'v':
-      if (allowed&COMPILED)
-        ModeMenu(COMPILED|DRAW_ARRAYS|TRIANGLES);
+      ModeMenu((LOCKED|IMMEDIATE|DRAW_ELTS|TRIANGLES) & allowed);
       break;
    case 'V':
-      ModeMenu(IMMEDIATE|GLVERTEX|STRIPS);
+      ModeMenu(UNLOCKED|IMMEDIATE|GLVERTEX|STRIPS);
       break;
    case 'b':
       Benchmark(5.0, 0);
@@ -837,7 +985,9 @@ static void Key( unsigned char key, int x, int y )
       set_matrix();
       glutPostRedisplay();
       break;
-
+   case ' ':
+      Init(0,0);
+      break;
    }
 }
 
@@ -883,6 +1033,15 @@ static GLint Args(int argc, char **argv)
       else if (strcmp(argv[i], "-info") == 0) {
          PrintInfo = GL_TRUE;
       }
+      else if (strcmp(argv[i], "-10") == 0) {
+         maxverts = 10;
+      }
+      else if (strcmp(argv[i], "-100") == 0) {
+        maxverts = 100;
+      }
+      else if (strcmp(argv[i], "-1000") == 0) {
+        maxverts = 1000;
+      }
       else {
          printf("%s (Bad option).\n", argv[i]);
         return QUIT;
@@ -895,7 +1054,6 @@ static GLint Args(int argc, char **argv)
 int main(int argc, char **argv)
 {
    GLenum type;
-   char *extensions;
 
    GLuint arg_mode = Args(argc, argv);
 
@@ -904,9 +1062,9 @@ int main(int argc, char **argv)
 
    read_surface( "isosurf.dat" );
 
-   glutInitWindowPosition(0, 0);
    glutInitWindowSize(400, 400);
-   
+   glutInit( &argc, argv);
+
    type = GLUT_DEPTH;
    type |= GLUT_RGB;
    type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
@@ -916,73 +1074,66 @@ 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 &= ~(COMPILED|DRAW_ARRAYS|ARRAY_ELT);
+      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 &= ~COMPILED;
+      allowed &= ~LOCKED;
    }
 
    Init(argc, argv);
    ModeMenu(arg_mode);
-   
+
    glutCreateMenu(ModeMenu);
-   glutAddMenuEntry("GL info",               GLINFO);   
-   glutAddMenuEntry("", 0);   
-   glutAddMenuEntry("Lit",                   LIT|NO_TEXTURE|NO_REFLECT);
-   glutAddMenuEntry("Unlit",                 UNLIT|NO_TEXTURE|NO_REFLECT);
-/*    glutAddMenuEntry("Textured", TEXTURE); */
-   glutAddMenuEntry("Reflect",               TEXTURE|REFLECT);
-   glutAddMenuEntry("", 0);   
+   glutAddMenuEntry("GL info",               GLINFO);
+   glutAddMenuEntry("", 0);
+   glutAddMenuEntry("Lit",                   LIT);
+   glutAddMenuEntry("Unlit",                 UNLIT);
+   glutAddMenuEntry("Reflect",               REFLECT);
+   glutAddMenuEntry("", 0);
    glutAddMenuEntry("Smooth",                SHADE_SMOOTH);
    glutAddMenuEntry("Flat",                  SHADE_FLAT);
-   glutAddMenuEntry("", 0);   
+   glutAddMenuEntry("", 0);
    glutAddMenuEntry("Fog",                   FOG);
    glutAddMenuEntry("No Fog",                NO_FOG);
-   glutAddMenuEntry("", 0);   
+   glutAddMenuEntry("", 0);
    glutAddMenuEntry("Stipple",               STIPPLE);
    glutAddMenuEntry("No Stipple",            NO_STIPPLE);
-   glutAddMenuEntry("", 0);   
+   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);   
-   glutAddMenuEntry("glVertex (STRIPS)",    IMMEDIATE|GLVERTEX|STRIPS);
-   glutAddMenuEntry("glVertex (TRIANGLES)", IMMEDIATE|GLVERTEX|TRIANGLES);
-   glutAddMenuEntry("", 0);   
-   glutAddMenuEntry("glVertex display list (STRIPS)", 
-                   DISPLAYLIST|GLVERTEX|STRIPS);
-   glutAddMenuEntry("", 0);   
+   glutAddMenuEntry("", 0);
+   glutAddMenuEntry("GL_TRIANGLES",          TRIANGLES);
+   glutAddMenuEntry("GL_TRIANGLE_STRIPS",    STRIPS);
+   glutAddMenuEntry("GL_POINTS",             POINTS);
+   glutAddMenuEntry("", 0);
+   glutAddMenuEntry("Displaylist",           DISPLAYLIST);
+   glutAddMenuEntry("Immediate",             IMMEDIATE);
+   glutAddMenuEntry("", 0);
+   if (allowed & LOCKED) {
+      glutAddMenuEntry("Locked Arrays (CVA)", LOCKED);
+      glutAddMenuEntry("Unlocked Arrays",     UNLOCKED);
+      glutAddMenuEntry("", 0);
+   }
+   glutAddMenuEntry("glVertex",               GLVERTEX);
    if (allowed & DRAW_ARRAYS) {
-      glutAddMenuEntry("DrawArrays (STRIPS)",
-                      IMMEDIATE|DRAW_ARRAYS|STRIPS);
-      glutAddMenuEntry("ArrayElement (STRIPS)",  
-                      IMMEDIATE|ARRAY_ELT|STRIPS);
-      glutAddMenuEntry("DrawElements (TRIANGLES)", 
-                      IMMEDIATE|DRAW_ARRAYS|TRIANGLES);
-      glutAddMenuEntry("ArrayElement (TRIANGLES)", 
-                      IMMEDIATE|ARRAY_ELT|TRIANGLES);
-      glutAddMenuEntry("", 0);   
-
-   }
-   if (allowed & COMPILED) {
-      glutAddMenuEntry("Compiled DrawElements (TRIANGLES)", 
-                      COMPILED|DRAW_ARRAYS|TRIANGLES);
-      glutAddMenuEntry("Compiled DrawElements (STRIPS)", 
-                      COMPILED|DRAW_ARRAYS|STRIPS);
-      glutAddMenuEntry("Compiled ArrayElement (TRIANGLES)", 
-                      COMPILED|ARRAY_ELT|TRIANGLES);   
-      glutAddMenuEntry("Compiled ArrayElement (STRIPS)", 
-                      COMPILED|ARRAY_ELT|STRIPS);   
-      glutAddMenuEntry("", 0);   
-   }
-   glutAddMenuEntry("Quit",                  QUIT);
+      glutAddMenuEntry("glDrawElements",      DRAW_ELTS);
+      glutAddMenuEntry("glDrawArrays",       DRAW_ARRAYS);
+      glutAddMenuEntry("glArrayElement",      ARRAY_ELT);
+   }
+   glutAddMenuEntry("", 0);
+   glutAddMenuEntry("Quit",                   QUIT);
    glutAttachMenu(GLUT_RIGHT_BUTTON);
 
    glutReshapeFunc(Reshape);