Merge commit 'origin/gallium-0.1' into gallium-0.2
[mesa.git] / progs / tests / manytex.c
index be776249e10f41415c3a2956c0d9ac947f054d7a..83c86766571013bd0129cc278b885820e544dfbc 100644 (file)
@@ -1,4 +1,3 @@
-/* $Id: manytex.c,v 1.1 2000/08/02 17:57:56 brianp Exp $ */
 
 /*
  * test handling of many texture maps
 #include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <math.h>
 #include <GL/glut.h>
 
 
 static GLint NumTextures = 20;
 static GLuint *TextureID = NULL;
+static GLint *TextureWidth = NULL, *TextureHeight = NULL;
 static GLboolean *TextureResidency = NULL;
 static GLint TexWidth = 128, TexHeight = 128;
 static GLfloat Zrot = 0;
@@ -27,8 +28,8 @@ static GLboolean MipMap = GL_FALSE;
 static GLboolean LinearFilter = GL_FALSE;
 static GLboolean RandomSize = GL_FALSE;
 static GLint Rows, Columns;
-static GLuint LowPriorityCount = 0;
-
+static GLint LowPriorityCount = 0;
+static GLint Win;
 
 
 static void Idle( void )
@@ -45,6 +46,7 @@ static void Display( void )
    GLint i;
 
    /* test residency */
+   if (0)
    {
       GLboolean b;
       GLint i, resident;
@@ -71,6 +73,11 @@ static void Display( void )
       GLfloat x = col * spacing + spacing * 0.5;
       GLfloat y = row * spacing + spacing * 0.5;
 
+      GLfloat maxDim = (TextureWidth[i] > TextureHeight[i])
+         ? TextureWidth[i] : TextureHeight[i];
+      GLfloat w = TextureWidth[i] / maxDim;
+      GLfloat h = TextureHeight[i] / maxDim;
+
       glPushMatrix();
          glTranslatef(x, y, 0.0);
          glRotatef(Zrot, 0, 0, 1);
@@ -78,10 +85,17 @@ static void Display( void )
 
          glBindTexture(GL_TEXTURE_2D, TextureID[i]);
          glBegin(GL_POLYGON);
+#if 0
          glTexCoord2f(0, 0);  glVertex2f(-1, -1);
          glTexCoord2f(1, 0);  glVertex2f( 1, -1);
          glTexCoord2f(1, 1);  glVertex2f( 1,  1);
          glTexCoord2f(0, 1);  glVertex2f(-1,  1);
+#else
+         glTexCoord2f(0, 0);  glVertex2f(-w, -h);
+         glTexCoord2f(1, 0);  glVertex2f( w, -h);
+         glTexCoord2f(1, 1);  glVertex2f( w,  h);
+         glTexCoord2f(0, 1);  glVertex2f(-w,  h);
+#endif
          glEnd();
       glPopMatrix();
    }
@@ -103,6 +117,25 @@ static void Reshape( int width, int height )
 }
 
 
+/*
+ * Return a random int in [min, max].
+ */
+static int RandomInt(int min, int max)
+{
+   int i = rand();
+   int j = i % (max - min + 1);
+   return min + j;
+}
+
+
+static void DeleteTextures(void)
+{
+   glDeleteTextures(NumTextures, TextureID);
+   free(TextureID);
+   TextureID = NULL;
+}
+
+
 
 static void Init( void )
 {
@@ -148,6 +181,15 @@ static void Init( void )
       assert(TextureResidency);
    }
 
+   if (!TextureWidth) {
+      TextureWidth = (GLint *) malloc(sizeof(GLint) * NumTextures);
+      assert(TextureWidth);
+   }
+   if (!TextureHeight) {
+      TextureHeight = (GLint *) malloc(sizeof(GLint) * NumTextures);
+      assert(TextureHeight);
+   }
+
    for (i = 0; i < NumTextures; i++) {
       GLubyte color[4];
       GLubyte *texImage;
@@ -162,11 +204,20 @@ static void Init( void )
          glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, 0.5F);
 
       if (RandomSize) {
+#if 0
          int k = (glutGet(GLUT_ELAPSED_TIME) % 7) + 2;
          TexWidth  = 1 << k;
          TexHeight = 1 << k;
+#else
+         TexWidth = 1 << RandomInt(2, 7);
+         TexHeight = 1 << RandomInt(2, 7);
+         printf("Random size of %3d: %d x %d\n", i, TexWidth, TexHeight);
+#endif
       }
 
+      TextureWidth[i] = TexWidth;
+      TextureHeight[i] = TexHeight;
+
       texImage = (GLubyte*) malloc(4 * TexWidth * TexHeight * sizeof(GLubyte));
       assert(texImage);
 
@@ -188,7 +239,7 @@ static void Init( void )
          GLint level = 0;
          GLint w = TexWidth, h = TexHeight;
          while (1) {
-            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0,
+            glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, w, h, 0,
                          GL_RGBA, GL_UNSIGNED_BYTE, texImage);
             if (w == 1 && h == 1)
                break;
@@ -211,6 +262,16 @@ static void Init( void )
          }
       }
       else {
+         /* Set corners to white */
+         int k = 0;
+         texImage[k+0] = texImage[k+1] = texImage[k+2] = texImage[k+3] = 255;
+         k = (TexWidth - 1) * 4;
+         texImage[k+0] = texImage[k+1] = texImage[k+2] = texImage[k+3] = 255;
+         k = (TexWidth * TexHeight - TexWidth) * 4;
+         texImage[k+0] = texImage[k+1] = texImage[k+2] = texImage[k+3] = 255;
+         k = (TexWidth * TexHeight - 1) * 4;
+         texImage[k+0] = texImage[k+1] = texImage[k+2] = texImage[k+3] = 255;
+
          glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TexWidth, TexHeight, 0,
                       GL_RGBA, GL_UNSIGNED_BYTE, texImage);
          if (LinearFilter) {
@@ -243,6 +304,9 @@ static void Key( unsigned char key, int x, int y )
          else
             glutIdleFunc(NULL);
          break;
+      case 's':
+         Idle();
+         break;
       case 'z':
          Zrot -= step;
          break;
@@ -250,9 +314,12 @@ static void Key( unsigned char key, int x, int y )
          Zrot += step;
          break;
       case ' ':
+         DeleteTextures();
          Init();
          break;
       case 27:
+         DeleteTextures();
+         glutDestroyWindow(Win);
          exit(0);
          break;
    }
@@ -268,7 +335,7 @@ int main( int argc, char *argv[] )
    glutInitWindowPosition( 0, 0 );
    glutInitWindowSize( WinWidth, WinHeight );
    glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
-   glutCreateWindow(argv[0]);
+   Win = glutCreateWindow(argv[0]);
    glutReshapeFunc( Reshape );
    glutKeyboardFunc( Key );
    glutDisplayFunc( Display );