Merge branch 'mesa_7_5_branch'
[mesa.git] / progs / demos / texenv.c
index bae3117544abf9c16dd037f55a412d61d525120d..c5a8b13f35fbcc692988daadaf8f52b4f55f4bb0 100644 (file)
@@ -65,7 +65,7 @@ GLfloat *labelInfoColor = labelColor0;
 GLfloat labelLevelColor0[4] = { 0.8, 0.8, 0.1, 1.0 };
 GLfloat labelLevelColor1[4] = { 0.0, 0.0, 0.0, 1.0 };
 
-GLboolean doubleBuffered = GL_FALSE;
+GLboolean doubleBuffered = GL_TRUE;
 GLboolean drawBackground = GL_FALSE;
 GLboolean drawBlended = GL_TRUE;
 GLboolean drawSmooth = GL_FALSE;
@@ -76,6 +76,8 @@ int textureWidth = 64;
 int textureHeight = 64;
 
 int winWidth = 580, winHeight = 720;
+static int Win;
+
 
 struct formatInfo {
    GLenum      baseFormat;
@@ -141,7 +143,7 @@ struct formatInfo rgbFormats[] =
 #define NUM_RGBA_FORMATS       (sizeof(rgbaFormats) / sizeof(rgbaFormats[0]))
 struct formatInfo rgbaFormats[] =
 {
-   { GL_RGBA, 4, "RGBA" },
+   { GL_RGBA, GL_RGBA, "RGBA" },
    { GL_RGBA, GL_RGBA2, "RGBA2" },
    { GL_RGBA, GL_RGBA4, "RGBA4" },
    { GL_RGBA, GL_RGB5_A1, "RGB5_A1" },
@@ -169,7 +171,7 @@ struct baseFormatInfo baseFormats[] =
 };
 
 #define NUM_ENV_COLORS         (sizeof(envColors) / sizeof(envColors[0]))
-int envColor;
+int envColor = 0;
 GLfloat envColors[][4] =
 {
    { 0.0, 0.0, 0.0, 1.0 },
@@ -201,14 +203,13 @@ static void checkErrors( void )
 {
    GLenum error;
 
-   return;
-
    while ( (error = glGetError()) != GL_NO_ERROR ) {
       fprintf( stderr, "Error: %s\n", (char *) gluErrorString( error ) );
    }
 }
 
-static void drawString( char *string, GLfloat x, GLfloat y, GLfloat color[4] )
+static void drawString( const char *string, GLfloat x, GLfloat y,
+                        const GLfloat color[4] )
 {
    glColor4fv( color );
    glRasterPos2f( x, y );
@@ -219,8 +220,9 @@ static void drawString( char *string, GLfloat x, GLfloat y, GLfloat color[4] )
    }
 }
 
-static void drawStringOutline( char *string, GLfloat x, GLfloat y,
-                              GLfloat color[4], GLfloat outline[4] )
+static void drawStringOutline( const char *string, GLfloat x, GLfloat y,
+                              const GLfloat color[4],
+                               const GLfloat outline[4] )
 {
    drawString( string, x - 1, y, outline );
    drawString( string, x + 1, y, outline );
@@ -269,7 +271,8 @@ static void keyboard( unsigned char c, int x, int y )
 {
    switch ( c ) {
    case 'c':
-      envColor = ++envColor % (int) NUM_ENV_COLORS;
+      envColor++;
+      envColor = envColor % (int) NUM_ENV_COLORS;
       break;
    case 'g':
       drawBackground = !drawBackground;
@@ -287,6 +290,7 @@ static void keyboard( unsigned char c, int x, int y )
       displayLevelInfo = !displayLevelInfo;
       break;
    case 27:             /* Escape key should force exit. */
+      glutDestroyWindow(Win);
       exit(0);
       break;
    default:
@@ -335,7 +339,8 @@ reshape( int w, int h )
    /* No need to call glViewPort here since "draw" calls it! */
 }
 
-static void loadTexture( int width, int height, struct formatInfo *format )
+static void loadTexture( int width, int height,
+                         const struct formatInfo *format )
 {
    int         luminanceSize = 0;
    int         alphaSize = 0;
@@ -352,7 +357,8 @@ static void loadTexture( int width, int height, struct formatInfo *format )
       break;
    case GL_INTENSITY:
       luminanceSize = 1;
-      textureFormat = GL_INTENSITY;
+      /* Note: format=GL_INTENSITY for glTexImage is not legal */
+      textureFormat = GL_LUMINANCE;
       break;
    case GL_ALPHA:
       alphaSize = 1;
@@ -497,7 +503,8 @@ static void loadTexture( int width, int height, struct formatInfo *format )
    free( texImage );
 }
 
-static void drawCheck( int w, int h, GLfloat lightCheck[4], GLfloat darkCheck[4] )
+static void drawCheck( int w, int h, const GLfloat lightCheck[4],
+                       const GLfloat darkCheck[4] )
 {
    float       dw = 2.0 / w;
    float       dh = 2.0 / h;
@@ -526,8 +533,35 @@ static void drawCheck( int w, int h, GLfloat lightCheck[4], GLfloat darkCheck[4]
    }
 }
 
+static const char *lookupFormat( GLint format )
+{
+   switch ( format ) {
+   case GL_RGBA:
+      return "GL_RGBA";
+   case GL_RGB:
+      return "GL_RGB";
+   case GL_ALPHA:
+      return "GL_ALPHA";
+   case GL_LUMINANCE:
+      return "GL_LUMINANCE";
+   case GL_LUMINANCE_ALPHA:
+      return "GL_LUMINANCE_ALPHA";
+   case GL_INTENSITY:
+      return "GL_INTENSITY";
+   case GL_COLOR_INDEX:
+      return "GL_COLOR_INDEX";
+   case GL_BGRA:
+      return "GL_BGRA";
+   case GL_BGR:
+      return "GL_BGR";
+   default:
+      return "unknown format";
+   }
+}
+
 static void drawSample( int x, int y, int w, int h,
-                       struct formatInfo *format, struct envModeInfo *envMode )
+                       const struct formatInfo *format,
+                        const struct envModeInfo *envMode )
 {
    glViewport( x, y, w, h );
    glScissor( x, y, w, h );
@@ -543,8 +577,8 @@ static void drawSample( int x, int y, int w, int h,
    glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, envMode->mode );
    glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, envColors[envColor] );
 
-   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
-   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
+   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
+   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
 
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
@@ -569,6 +603,10 @@ static void drawSample( int x, int y, int w, int h,
       glEnable( GL_TEXTURE_2D );
    }
 
+   /*
+    * if (drawSmooth) then draw quad which goes from purple at the
+    * bottom (100% alpha) to green at the top (50% alpha).
+    */
    glBegin( GL_QUADS );
       if ( drawSmooth )  glColor4f( 1.0, 0.0, 1.0, 1.0 );
       glTexCoord2f( 0.0, 0.0 );
@@ -578,11 +616,11 @@ static void drawSample( int x, int y, int w, int h,
       glTexCoord2f( 1.0, 0.0 );
       glVertex2f( 0.8, -0.8 );
 
-      if ( drawSmooth )  glColor4f( 0.0, 1.0, 0.0, 1.0 );
+      if ( drawSmooth )  glColor4f( 0.0, 1.0, 0.0, 0.5 );
       glTexCoord2f( 1.0, 1.0 );
       glVertex2f( 0.8, 0.8 );
 
-      if ( drawSmooth )  glColor4f( 0.0, 1.0, 0.0, 1.0 );
+      if ( drawSmooth )  glColor4f( 0.0, 1.0, 0.0, 0.5 );
       glTexCoord2f( 0.0, 1.0 );
       glVertex2f( -0.8, 0.8 );
    glEnd();
@@ -603,7 +641,7 @@ static void drawSample( int x, int y, int w, int h,
       end2D();
    }
    else if ( displayLevelInfo ) {
-      GLint width, height, border, components;
+      GLint width, height, border, format;
       GLint redSize, greenSize, blueSize, alphaSize;
       GLint luminanceSize, intensitySize;
       char buf[255];
@@ -611,7 +649,7 @@ static void drawSample( int x, int y, int w, int h,
       glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width );
       glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height );
       glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER, &border );
-      glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_COMPONENTS, &components );
+      glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &format );
       glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_RED_SIZE, &redSize );
       glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_GREEN_SIZE, &greenSize );
       glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_BLUE_SIZE, &blueSize );
@@ -626,16 +664,19 @@ static void drawSample( int x, int y, int w, int h,
       sprintf( buf, "border: %d", border );
       drawStringOutline( buf, 15, h / 2 + 10, labelLevelColor0, labelLevelColor1 );
 
-      sprintf( buf, "components: 0x%04X", components );
+      sprintf( buf, "internal format:" );
       drawStringOutline( buf, 15, h / 2, labelLevelColor0, labelLevelColor1 );
 
-      sprintf( buf, "sizes:" );
+      sprintf( buf, "  %s",  lookupFormat( format ) );
       drawStringOutline( buf, 15, h / 2 - 10, labelLevelColor0, labelLevelColor1 );
 
+      sprintf( buf, "sizes:" );
+      drawStringOutline( buf, 15, h / 2 - 20, labelLevelColor0, labelLevelColor1 );
+
       sprintf( buf, "  %d / %d / %d / %d / %d / %d",
               redSize, greenSize, blueSize, alphaSize,
               luminanceSize, intensitySize );
-      drawStringOutline( buf, 15, h / 2 - 20, labelLevelColor0, labelLevelColor1 );
+      drawStringOutline( buf, 15, h / 2 - 30, labelLevelColor0, labelLevelColor1 );
 
       end2D();
    }
@@ -715,6 +756,8 @@ static void instructions( void )
    fprintf( stderr, "  [s] - toggle smooth shading\n" );
    fprintf( stderr, "  [t] - toggle texturing\n" );
    fprintf( stderr, "  [i] - toggle information display\n" );
+   fprintf( stderr, "  up/down - select row\n" );
+   fprintf( stderr, "  left/right - change row's internal format\n" );
 }
 
 int main( int argc, char *argv[] )
@@ -744,7 +787,8 @@ int main( int argc, char *argv[] )
    }
 
    glutInitWindowSize( winWidth, winHeight );
-   glutCreateWindow( "Texture Environment Test" );
+   glutInitWindowPosition( 0, 0 );
+   Win = glutCreateWindow( "Texture Environment Test" );
 
    initialize();
    instructions();