now supports > 2 texture units
authorBrian Paul <brian.paul@tungstengraphics.com>
Wed, 13 Feb 2002 02:23:33 +0000 (02:23 +0000)
committerBrian Paul <brian.paul@tungstengraphics.com>
Wed, 13 Feb 2002 02:23:33 +0000 (02:23 +0000)
progs/demos/multiarb.c

index 211ae17..c4671b5 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: multiarb.c,v 1.10 2001/06/20 19:12:30 brianp Exp $ */
+/* $Id: multiarb.c,v 1.11 2002/02/13 02:23:33 brianp Exp $ */
 
 /*
  * GL_ARB_multitexture demo
@@ -8,72 +8,7 @@
  *
  *
  * Brian Paul  November 1998  This program is in the public domain.
- */
-
-/*
- * $Log: multiarb.c,v $
- * Revision 1.10  2001/06/20 19:12:30  brianp
- * also print GL_MAX_TEXTURE_SIZE
- *
- * Revision 1.9  2001/06/13 14:33:16  brianp
- * moved glTexEnvi calls to better logical locations
- *
- * Revision 1.8  2000/12/24 22:53:54  pesco
- * * demos/Makefile.am (INCLUDES): Added -I$(top_srcdir)/util.
- * * demos/Makefile.X11, demos/Makefile.BeOS-R4, demos/Makefile.cygnus:
- * Essentially the same.
- * Program files updated to include "readtex.c", not "../util/readtex.c".
- * * demos/reflect.c: Likewise for "showbuffer.c".
- *
- *
- * * Makefile.am (EXTRA_DIST): Added top-level regular files.
- *
- * * include/GL/Makefile.am (INC_X11): Added glxext.h.
- *
- *
- * * src/GGI/include/ggi/mesa/Makefile.am (EXTRA_HEADERS): Include
- * Mesa GGI headers in dist even if HAVE_GGI is not given.
- *
- * * configure.in: Look for GLUT and demo source dirs in $srcdir.
- *
- * * src/swrast/Makefile.am (libMesaSwrast_la_SOURCES): Set to *.[ch].
- * More source list updates in various Makefile.am's.
- *
- * * Makefile.am (dist-hook): Remove CVS directory from distribution.
- * (DIST_SUBDIRS): List all possible subdirs here.
- * (SUBDIRS): Only list subdirs selected for build again.
- * The above two applied to all subdir Makefile.am's also.
- *
- * Revision 1.7  2000/11/01 16:02:01  brianp
- * print number of texture units
- *
- * Revision 1.6  2000/05/23 23:21:00  brianp
- * set default window pos
- *
- * Revision 1.5  2000/02/02 17:31:45  brianp
- * changed > to >=
- *
- * Revision 1.4  2000/02/02 01:07:21  brianp
- * limit Drift to [0, 1]
- *
- * Revision 1.3  1999/10/21 16:40:32  brianp
- * added -info command line option
- *
- * Revision 1.2  1999/10/13 12:02:13  brianp
- * use texture objects now
- *
- * Revision 1.1.1.1  1999/08/19 00:55:40  jtg
- * Imported sources
- *
- * Revision 1.3  1999/03/28 18:20:49  brianp
- * minor clean-up
- *
- * Revision 1.2  1998/11/05 04:34:04  brianp
- * moved image files to ../images/ directory
- *
- * Revision 1.1  1998/11/03 01:36:33  brianp
- * Initial revision
- *
+ * Modified on 12 Feb 2002 for > 2 texture units.
  */
 
 
 #define TEXTURE_2_FILE "../images/reflect.rgb"
 
 #define TEX0 1
-#define TEX1 2
-#define TEXBOTH 3
+#define TEX7 8
 #define ANIMATE 10
 #define QUIT 100
 
 static GLboolean Animate = GL_TRUE;
+static GLint NumUnits = 1;
+static GLboolean TexEnabled[8];
 
 static GLfloat Drift = 0.0;
 static GLfloat Xrot = 20.0, Yrot = 30.0, Zrot = 0.0;
@@ -104,24 +40,30 @@ static GLfloat Xrot = 20.0, Yrot = 30.0, Zrot = 0.0;
 static void Idle( void )
 {
    if (Animate) {
+      GLint i;
+
       Drift += 0.05;
       if (Drift >= 1.0)
          Drift = 0.0;
 
-#ifdef GL_ARB_multitexture
-      glActiveTextureARB(GL_TEXTURE0_ARB);
-#endif
-      glMatrixMode(GL_TEXTURE);
-      glLoadIdentity();
-      glTranslatef(Drift, 0.0, 0.0);
-      glMatrixMode(GL_MODELVIEW);
-
-#ifdef GL_ARB_multitexture
-      glActiveTextureARB(GL_TEXTURE1_ARB);
-#endif
-      glMatrixMode(GL_TEXTURE);
-      glLoadIdentity();
-      glTranslatef(0.0, Drift, 0.0);
+      for (i = 0; i < NumUnits; i++) {
+         glActiveTextureARB(GL_TEXTURE0_ARB + i);
+         glMatrixMode(GL_TEXTURE);
+         glLoadIdentity();
+         if (i == 0) {
+            glTranslatef(Drift, 0.0, 0.0);
+            glScalef(2, 2, 2);
+         }
+         else if (i == 1) {
+            glTranslatef(0.0, Drift, 0.0);
+         }
+         else {
+            glTranslatef(0.5, 0.5, 0.0);
+            glRotatef(180.0 * Drift, 0, 0, 1);
+            glScalef(1.0/i, 1.0/i, 1.0/i);
+            glTranslatef(-0.5, -0.5, 0.0);
+         }
+      }
       glMatrixMode(GL_MODELVIEW);
 
       glutPostRedisplay();
@@ -131,37 +73,30 @@ static void Idle( void )
 
 static void DrawObject(void)
 {
-   glBegin(GL_QUADS);
-
-#ifdef GL_ARB_multitexture
-   glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 0.0);
-   glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 0.0);
-   glVertex2f(-1.0, -1.0);
+   GLint i;
 
-   glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 2.0, 0.0);
-   glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 0.0);
-   glVertex2f(1.0, -1.0);
+   if (!TexEnabled[0] && !TexEnabled[1])
+      glColor3f(0.1, 0.1, 0.1);  /* add onto this */
+   else
+      glColor3f(1, 1, 1);  /* modulate this */
 
-   glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 2.0, 2.0);
-   glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 1.0);
-   glVertex2f(1.0, 1.0);
+   glBegin(GL_QUADS);
 
-   glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 2.0);
-   glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 1.0);
-   glVertex2f(-1.0, 1.0);
-#else
-   glTexCoord2f(0.0, 0.0);
+   for (i = 0; i < NumUnits; i++)
+      glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i, 0.0, 0.0);
    glVertex2f(-1.0, -1.0);
 
-   glTexCoord2f(1.0, 0.0);
+   for (i = 0; i < NumUnits; i++)
+      glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i, 1.0, 0.0);
    glVertex2f(1.0, -1.0);
 
-   glTexCoord2f(1.0, 1.0);
+   for (i = 0; i < NumUnits; i++)
+      glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i, 1.0, 1.0);
    glVertex2f(1.0, 1.0);
 
-   glTexCoord2f(0.0, 1.0);
+   for (i = 0; i < NumUnits; i++)
+      glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i, 0.0, 1.0);
    glVertex2f(-1.0, 1.0);
-#endif
 
    glEnd();
 }
@@ -199,16 +134,19 @@ static void Reshape( int width, int height )
 
 static void ModeMenu(int entry)
 {
-   GLboolean enable0 = GL_FALSE, enable1 = GL_FALSE;
-   if (entry==TEX0) {
-      enable0 = GL_TRUE;
-   }
-   else if (entry==TEX1) {
-      enable1 = GL_TRUE;
-   }
-   else if (entry==TEXBOTH) {
-      enable0 = GL_TRUE;
-      enable1 = GL_TRUE;
+   if (entry >= TEX0 && entry <= TEX7) {
+      /* toggle */
+      GLint i = entry - TEX0;
+      TexEnabled[i] = !TexEnabled[i];
+      glActiveTextureARB(GL_TEXTURE0_ARB + i);
+      if (TexEnabled[i])
+         glEnable(GL_TEXTURE_2D);
+      else
+         glDisable(GL_TEXTURE_2D);
+      printf("Enabled: ");
+      for (i = 0; i < NumUnits; i++)
+         printf("%d ", (int) TexEnabled[i]);
+      printf("\n");
    }
    else if (entry==ANIMATE) {
       Animate = !Animate;
@@ -217,26 +155,6 @@ static void ModeMenu(int entry)
       exit(0);
    }
 
-   if (entry != ANIMATE) {
-#ifdef GL_ARB_multitexture
-      glActiveTextureARB(GL_TEXTURE0_ARB);
-#endif
-      if (enable0) {
-         glEnable(GL_TEXTURE_2D);
-      }
-      else
-         glDisable(GL_TEXTURE_2D);
-
-#ifdef GL_ARB_multitexture
-      glActiveTextureARB(GL_TEXTURE1_ARB);
-#endif
-      if (enable1) {
-         glEnable(GL_TEXTURE_2D);
-      }
-      else
-         glDisable(GL_TEXTURE_2D);
-   }
-
    glutPostRedisplay();
 }
 
@@ -280,8 +198,8 @@ static void SpecialKey( int key, int x, int y )
 
 static void Init( int argc, char *argv[] )
 {
-   GLuint texObj[2];
-   GLint units, size;
+   GLuint texObj[8];
+   GLint size, i;
 
    const char *exten = (const char *) glGetString(GL_EXTENSIONS);
    if (!strstr(exten, "GL_ARB_multitexture")) {
@@ -289,64 +207,82 @@ static void Init( int argc, char *argv[] )
       exit(1);
    }
 
-   glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &units);
-   printf("%d texture units supported\n", units);
+   glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &NumUnits);
+   printf("%d texture units supported\n", NumUnits);
+   if (NumUnits > 8)
+      NumUnits = 8;
 
    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
    printf("%d x %d max texture size\n", size, size);
 
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
 
-   /* allocate two texture objects */
-   glGenTextures(2, texObj);
-
-   /* setup texture obj 0 */
-   glBindTexture(GL_TEXTURE_2D, texObj[0]);
-#ifdef LINEAR_FILTER
-   /* linear filtering looks much nicer but is much slower for Mesa */
-   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
-   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-#else
-   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
-   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-#endif
-   if (!LoadRGBMipmaps(TEXTURE_1_FILE, GL_RGB)) {
-      printf("Error: couldn't load texture image\n");
-      exit(1);
+   for (i = 0; i < NumUnits; i++) {
+      if (i < 2)
+         TexEnabled[i] = GL_TRUE;
+      else
+         TexEnabled[i] = GL_FALSE;
    }
 
+   /* allocate two texture objects */
+   glGenTextures(NumUnits, texObj);
 
-   /* setup texture obj 1 */
-   glBindTexture(GL_TEXTURE_2D, texObj[1]);
-#ifdef LINEAR_FILTER
-   /* linear filtering looks much nicer but is much slower for Mesa */
-   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
-   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-#else
-   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
-   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-#endif
-   if (!LoadRGBMipmaps(TEXTURE_2_FILE, GL_RGB)) {
-      printf("Error: couldn't load texture image\n");
-      exit(1);
-   }
+   /* setup the texture objects */
+   for (i = 0; i < NumUnits; i++) {
 
+      glActiveTextureARB(GL_TEXTURE0_ARB + i);
+      glBindTexture(GL_TEXTURE_2D, texObj[i]);
 
-   /* now bind the texture objects to the respective texture units */
-#ifdef GL_ARB_multitexture
-   glActiveTextureARB(GL_TEXTURE0_ARB);
-   glBindTexture(GL_TEXTURE_2D, texObj[0]);
-   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
-   glActiveTextureARB(GL_TEXTURE1_ARB);
-   glBindTexture(GL_TEXTURE_2D, texObj[1]);
-   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
-#endif
+      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+
+      if (i == 0) {
+         if (!LoadRGBMipmaps(TEXTURE_1_FILE, GL_RGB)) {
+            printf("Error: couldn't load texture image\n");
+            exit(1);
+         }
+      }
+      else if (i == 1) {
+         if (!LoadRGBMipmaps(TEXTURE_2_FILE, GL_RGB)) {
+            printf("Error: couldn't load texture image\n");
+            exit(1);
+         }
+      }
+      else {
+         /* checker */
+         GLubyte image[8][8][3];
+         GLint i, j;
+         for (i = 0; i < 8; i++) {
+            for (j = 0; j < 8; j++) {
+               if ((i + j) & 1) {
+                  image[i][j][0] = 50;
+                  image[i][j][1] = 50;
+                  image[i][j][2] = 50;
+               }
+               else {
+                  image[i][j][0] = 25;
+                  image[i][j][1] = 25;
+                  image[i][j][2] = 25;
+               }
+            }
+         }
+         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0,
+                      GL_RGB, GL_UNSIGNED_BYTE, (GLvoid *) image);
+      }
+
+      /* Bind texObj[i] to ith texture unit */
+      if (i < 2)
+         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
+      else
+         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
+
+      if (TexEnabled[i])
+         glEnable(GL_TEXTURE_2D);
+   }
 
    glShadeModel(GL_FLAT);
    glClearColor(0.3, 0.3, 0.4, 1.0);
 
-   ModeMenu(TEXBOTH);
-
    if (argc > 1 && strcmp(argv[1], "-info")==0) {
       printf("GL_RENDERER   = %s\n", (char *) glGetString(GL_RENDERER));
       printf("GL_VERSION    = %s\n", (char *) glGetString(GL_VERSION));
@@ -358,6 +294,8 @@ static void Init( int argc, char *argv[] )
 
 int main( int argc, char *argv[] )
 {
+   GLint i;
+
    glutInit( &argc, argv );
    glutInitWindowSize( 300, 300 );
    glutInitWindowPosition( 0, 0 );
@@ -373,9 +311,12 @@ int main( int argc, char *argv[] )
    glutIdleFunc( Idle );
 
    glutCreateMenu(ModeMenu);
-   glutAddMenuEntry("Texture 0", TEX0);
-   glutAddMenuEntry("Texture 1", TEX1);
-   glutAddMenuEntry("Multi-texture", TEXBOTH);
+
+   for (i = 0; i < NumUnits; i++) {
+      char s[100];
+      sprintf(s, "Toggle Texture %d", i);
+      glutAddMenuEntry(s, TEX0 + i);
+   }
    glutAddMenuEntry("Toggle Animation", ANIMATE);
    glutAddMenuEntry("Quit", QUIT);
    glutAttachMenu(GLUT_RIGHT_BUTTON);