Merge branch 'mesa_7_5_branch'
[mesa.git] / progs / demos / multiarb.c
index 831842a7382015c0b560d90b464067bb2a4a26f7..85c4e3a266c7f9cd2d9152060d3fa0da580ac05f 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id: multiarb.c,v 1.3 1999/10/21 16:40:32 brianp Exp $ */
-
 /*
  * GL_ARB_multitexture demo
  *
@@ -8,28 +6,7 @@
  *
  *
  * Brian Paul  November 1998  This program is in the public domain.
- */
-
-/*
- * $Log: multiarb.c,v $
- * 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.
  */
 
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <GL/glew.h>
 #include <GL/glut.h>
 
-#include "../util/readtex.c"   /* I know, this is a hack. */
+#include "readtex.h"
 
 #define TEXTURE_1_FILE "../images/girl.rgb"
 #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;
 
 
-
 static void Idle( void )
 {
    if (Animate) {
-      Drift += 0.05;
-
-#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);
+      GLint i;
+
+      Drift = glutGet(GLUT_ELAPSED_TIME) * 0.001;
+
+      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, 1);
+         }
+         else if (i == 1) {
+            glTranslatef(0.0, Drift, 0.0);
+         }
+         else {
+            float tx = 0.5, ty = 0.5;
+            glTranslatef(tx, ty, 0.0);
+            glRotatef(180.0 * Drift, 0, 0, 1);
+            glScalef(1.0/i, 1.0/i, 1.0/i);
+            glTranslatef(-tx, -ty + i * 0.1, 0.0);
+         }
+      }
       glMatrixMode(GL_MODELVIEW);
 
       glutPostRedisplay();
@@ -85,43 +70,28 @@ 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);
+   static const GLfloat tex_coords[] = {  0.0,  0.0,  1.0,  1.0,  0.0 };
+   static const GLfloat vtx_coords[] = { -1.0, -1.0,  1.0,  1.0, -1.0 };
+   GLint i, j;
 
-   glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 2.0, 0.0);
-   glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 0.0);
-   glVertex2f(1.0, -1.0);
-
-   glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 2.0, 2.0);
-   glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 1.0);
-   glVertex2f(1.0, 1.0);
-
-   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);
-   glVertex2f(-1.0, -1.0);
-
-   glTexCoord2f(1.0, 0.0);
-   glVertex2f(1.0, -1.0);
-
-   glTexCoord2f(1.0, 1.0);
-   glVertex2f(1.0, 1.0);
-
-   glTexCoord2f(0.0, 1.0);
-   glVertex2f(-1.0, 1.0);
-#endif
+   if (!TexEnabled[0] && !TexEnabled[1])
+      glColor3f(0.1, 0.1, 0.1);  /* add onto this */
+   else
+      glColor3f(1, 1, 1);  /* modulate this */
 
+   glBegin(GL_QUADS);
+   for (j = 0; j < 4; j++ ) {
+      for (i = 0; i < NumUnits; i++) {
+         if (TexEnabled[i])
+            glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i, 
+                                 tex_coords[j], tex_coords[j+1]);
+      }
+      glVertex2f( vtx_coords[j], vtx_coords[j+1] );
+   }
    glEnd();
 }
 
 
-
 static void Display( void )
 {
    glClear( GL_COLOR_BUFFER_BIT );
@@ -151,46 +121,39 @@ static void Reshape( int width, int height )
 }
 
 
+static void ToggleUnit(int unit)
+{
+   TexEnabled[unit] = !TexEnabled[unit];
+   glActiveTextureARB(GL_TEXTURE0_ARB + unit);
+   if (TexEnabled[unit])
+      glEnable(GL_TEXTURE_2D);
+   else
+      glDisable(GL_TEXTURE_2D);
+   printf("Enabled: ");
+   for (unit = 0; unit < NumUnits; unit++)
+      printf("%d ", (int) TexEnabled[unit]);
+   printf("\n");
+}
+
+
 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;
+      ToggleUnit(i);
    }
    else if (entry==ANIMATE) {
       Animate = !Animate;
+      if (Animate)
+         glutIdleFunc(Idle);
+      else
+         glutIdleFunc(NULL);
    }
    else if (entry==QUIT) {
       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();
 }
 
@@ -200,9 +163,36 @@ static void Key( unsigned char key, int x, int y )
    (void) x;
    (void) y;
    switch (key) {
-      case 27:
-         exit(0);
-         break;
+   case 'a':
+      Animate = !Animate;
+      break;
+   case '0':
+      ToggleUnit(0);
+      break;
+   case '1':
+      ToggleUnit(1);
+      break;
+   case '2':
+      ToggleUnit(2);
+      break;
+   case '3':
+      ToggleUnit(3);
+      break;
+   case '4':
+      ToggleUnit(4);
+      break;
+   case '5':
+      ToggleUnit(5);
+      break;
+   case '6':
+      ToggleUnit(6);
+      break;
+   case '7':
+      ToggleUnit(7);
+      break;
+   case 27:
+      exit(0);
+      break;
    }
    glutPostRedisplay();
 }
@@ -234,7 +224,8 @@ static void SpecialKey( int key, int x, int y )
 
 static void Init( int argc, char *argv[] )
 {
-   GLuint texObj[2];
+   GLuint texObj[8];
+   GLint size, i;
 
    const char *exten = (const char *) glGetString(GL_EXTENSIONS);
    if (!strstr(exten, "GL_ARB_multitexture")) {
@@ -242,62 +233,82 @@ static void Init( int argc, char *argv[] )
       exit(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
+   glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &NumUnits);
+   printf("%d texture units supported\n", NumUnits);
+   if (NumUnits > 8)
+      NumUnits = 8;
 
-   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
+   glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
+   printf("%d x %d max texture size\n", size, size);
 
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
 
-   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
+   /* setup the texture objects */
+   for (i = 0; i < NumUnits; i++) {
 
-   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
+      glActiveTextureARB(GL_TEXTURE0_ARB + i);
+      glBindTexture(GL_TEXTURE_2D, texObj[i]);
 
-   if (!LoadRGBMipmaps(TEXTURE_2_FILE, GL_RGB)) {
-      printf("Error: couldn't load texture image\n");
-      exit(1);
-   }
+      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);
+      }
 
-   /* now bind the texture objects to the respective texture units */
-#ifdef GL_ARB_multitexture
-   glActiveTextureARB(GL_TEXTURE0_ARB);
-   glBindTexture(GL_TEXTURE_2D, texObj[0]);
-   glActiveTextureARB(GL_TEXTURE1_ARB);
-   glBindTexture(GL_TEXTURE_2D, texObj[1]);
-#endif
+      /* 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));
@@ -309,10 +320,14 @@ static void Init( int argc, char *argv[] )
 
 int main( int argc, char *argv[] )
 {
+   GLint i;
+
    glutInit( &argc, argv );
    glutInitWindowSize( 300, 300 );
+   glutInitWindowPosition( 0, 0 );
    glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
    glutCreateWindow(argv[0] );
+   glewInit();
 
    Init( argc, argv );
 
@@ -320,12 +335,16 @@ int main( int argc, char *argv[] )
    glutKeyboardFunc( Key );
    glutSpecialFunc( SpecialKey );
    glutDisplayFunc( Display );
-   glutIdleFunc( Idle );
+   if (Animate)
+      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);