home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / System / Mesa-3.1 / demos / multitex.c < prev    next >
C/C++ Source or Header  |  2000-01-07  |  7KB  |  303 lines

  1. /* $Id: multitex.c,v 3.3 1998/11/05 04:34:04 brianp Exp $ */
  2.  
  3. /*
  4.  * Multitexture demo
  5.  * Brian Paul  February 1998  This program is in the public domain.
  6.  */
  7.  
  8. /*
  9.  * $Log: multitex.c,v $
  10.  * Revision 3.3  1998/11/05 04:34:04  brianp
  11.  * moved image files to ../images/ directory
  12.  *
  13.  * Revision 3.2  1998/04/01 02:57:49  brianp
  14.  * added extension test in Init()
  15.  *
  16.  * Revision 3.1  1998/02/21 00:59:17  brianp
  17.  * changed background color
  18.  *
  19.  * Revision 3.0  1998/02/20 04:42:06  brianp
  20.  * initial rev
  21.  *
  22.  */
  23.  
  24.  
  25. #include <math.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <GL/glut.h>
  30.  
  31. #include "../util/readtex.c"   /* I know, this is a hack. */
  32.  
  33. #define TEXTURE_1_FILE "../images/girl.rgb"
  34. #define TEXTURE_2_FILE "../images/reflect.rgb"
  35.  
  36. #define TEX0 1
  37. #define TEX1 2
  38. #define TEXBOTH 3
  39. #define ANIMATE 10
  40. #define QUIT 100
  41.  
  42. static GLboolean Animate = GL_TRUE;
  43.  
  44. static GLfloat Drift = 0.0;
  45. static GLfloat Xrot = 20.0, Yrot = 30.0, Zrot = 0.0;
  46.  
  47.  
  48. static void Idle( void )
  49. {
  50.    if (Animate) {
  51.       Drift += 0.05;
  52.  
  53. #ifdef GL_SGIS_multitexture
  54.       glSelectTextureSGIS(GL_TEXTURE0_SGIS);
  55. #endif
  56.       glMatrixMode(GL_TEXTURE);
  57.       glLoadIdentity();
  58.       glTranslatef(Drift, 0.0, 0.0);
  59.       glMatrixMode(GL_MODELVIEW);
  60.  
  61. #ifdef GL_SGIS_multitexture
  62.       glSelectTextureSGIS(GL_TEXTURE1_SGIS);
  63. #endif
  64.       glMatrixMode(GL_TEXTURE);
  65.       glLoadIdentity();
  66.       glTranslatef(0.0, Drift, 0.0);
  67.       glMatrixMode(GL_MODELVIEW);
  68.  
  69.       glutPostRedisplay();
  70.    }
  71. }
  72.  
  73.  
  74. static void DrawObject(void)
  75. {
  76.    glBegin(GL_QUADS);
  77.  
  78. #ifdef GL_SGIS_multitexture
  79.    glMultiTexCoord2fSGIS(GL_TEXTURE0_SGIS, 0.0, 0.0);
  80.    glMultiTexCoord2fSGIS(GL_TEXTURE1_SGIS, 0.0, 0.0);
  81.    glVertex2f(-1.0, -1.0);
  82.  
  83.    glMultiTexCoord2fSGIS(GL_TEXTURE0_SGIS, 2.0, 0.0);
  84.    glMultiTexCoord2fSGIS(GL_TEXTURE1_SGIS, 1.0, 0.0);
  85.    glVertex2f(1.0, -1.0);
  86.  
  87.    glMultiTexCoord2fSGIS(GL_TEXTURE0_SGIS, 2.0, 2.0);
  88.    glMultiTexCoord2fSGIS(GL_TEXTURE1_SGIS, 1.0, 1.0);
  89.    glVertex2f(1.0, 1.0);
  90.  
  91.    glMultiTexCoord2fSGIS(GL_TEXTURE0_SGIS, 0.0, 2.0);
  92.    glMultiTexCoord2fSGIS(GL_TEXTURE1_SGIS, 0.0, 1.0);
  93.    glVertex2f(-1.0, 1.0);
  94. #else
  95.    glTexCoord2f(0.0, 0.0);
  96.    glVertex2f(-1.0, -1.0);
  97.  
  98.    glTexCoord2f(1.0, 0.0);
  99.    glVertex2f(1.0, -1.0);
  100.  
  101.    glTexCoord2f(1.0, 1.0);
  102.    glVertex2f(1.0, 1.0);
  103.  
  104.    glTexCoord2f(0.0, 1.0);
  105.    glVertex2f(-1.0, 1.0);
  106. #endif
  107.  
  108.    glEnd();
  109. }
  110.  
  111.  
  112.  
  113. static void Display( void )
  114. {
  115.    glClear( GL_COLOR_BUFFER_BIT );
  116.  
  117.    glPushMatrix();
  118.       glRotatef(Xrot, 1.0, 0.0, 0.0);
  119.       glRotatef(Yrot, 0.0, 1.0, 0.0);
  120.       glRotatef(Zrot, 0.0, 0.0, 1.0);
  121.       glScalef(5.0, 5.0, 5.0);
  122.       DrawObject();
  123.    glPopMatrix();
  124.  
  125.    glutSwapBuffers();
  126. }
  127.  
  128.  
  129. static void Reshape( int width, int height )
  130. {
  131.    glViewport( 0, 0, width, height );
  132.    glMatrixMode( GL_PROJECTION );
  133.    glLoadIdentity();
  134.    glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
  135.    /*glOrtho( -6.0, 6.0, -6.0, 6.0, 10.0, 100.0 );*/
  136.    glMatrixMode( GL_MODELVIEW );
  137.    glLoadIdentity();
  138.    glTranslatef( 0.0, 0.0, -70.0 );
  139. }
  140.  
  141.  
  142. static void ModeMenu(int entry)
  143. {
  144.    GLboolean enable0 = GL_FALSE, enable1 = GL_FALSE;
  145.    if (entry==TEX0) {
  146.       enable0 = GL_TRUE;
  147.    }
  148.    else if (entry==TEX1) {
  149.       enable1 = GL_TRUE;
  150.    }
  151.    else if (entry==TEXBOTH) {
  152.       enable0 = GL_TRUE;
  153.       enable1 = GL_TRUE;
  154.    }
  155.    else if (entry==ANIMATE) {
  156.       Animate = !Animate;
  157.    }
  158.    else if (entry==QUIT) {
  159.       exit(0);
  160.    }
  161.  
  162.    if (entry != ANIMATE) {
  163. #ifdef GL_SGIS_multitexture
  164.       glSelectTextureSGIS(GL_TEXTURE0_SGIS);
  165. #endif
  166.       if (enable0) {
  167.          glEnable(GL_TEXTURE_2D);
  168.       }
  169.       else
  170.          glDisable(GL_TEXTURE_2D);
  171.  
  172. #ifdef GL_SGIS_multitexture
  173.       glSelectTextureSGIS(GL_TEXTURE1_SGIS);
  174. #endif
  175.       if (enable1) {
  176.          glEnable(GL_TEXTURE_2D);
  177.       }
  178.       else
  179.          glDisable(GL_TEXTURE_2D);
  180.    }
  181.  
  182.    glutPostRedisplay();
  183. }
  184.  
  185.  
  186. static void Key( unsigned char key, int x, int y )
  187. {
  188.    switch (key) {
  189.       case 27:
  190.          exit(0);
  191.          break;
  192.    }
  193.    glutPostRedisplay();
  194. }
  195.  
  196.  
  197. static void SpecialKey( int key, int x, int y )
  198. {
  199.    float step = 3.0;
  200.  
  201.    switch (key) {
  202.       case GLUT_KEY_UP:
  203.          Xrot += step;
  204.          break;
  205.       case GLUT_KEY_DOWN:
  206.          Xrot -= step;
  207.          break;
  208.       case GLUT_KEY_LEFT:
  209.          Yrot += step;
  210.          break;
  211.       case GLUT_KEY_RIGHT:
  212.          Yrot -= step;
  213.          break;
  214.    }
  215.    glutPostRedisplay();
  216. }
  217.  
  218.  
  219. static void Init( void )
  220. {
  221.    const char *exten = (const char *) glGetString(GL_EXTENSIONS);
  222.    if (!strstr(exten, "GL_SGIS_multitexture")) {
  223.       printf("Sorry, GL_SGIS_multitexture not supported by this renderer.\n");
  224.       exit(1);
  225.    }
  226.  
  227.    /* setup textur env 0 */
  228. #ifdef GL_SGIS_multitexture
  229.    glSelectTextureSGIS(GL_TEXTURE0_SGIS);
  230. #endif
  231. #ifdef LINEAR_FILTER
  232.    /* linear filtering looks much nicer but is much slower for Mesa */
  233.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  234.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  235. #else
  236.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  237.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  238. #endif
  239.  
  240.    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  241.  
  242.    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  243.  
  244.    if (!LoadRGBMipmaps(TEXTURE_1_FILE, GL_RGB)) {
  245.       printf("Error: couldn't load texture image\n");
  246.       exit(1);
  247.    }
  248.  
  249.  
  250.    /* setup textur env 1 */
  251. #ifdef GL_SGIS_multitexture
  252.    glSelectTextureSGIS(GL_TEXTURE1_SGIS);
  253. #endif
  254. #ifdef LINEAR_FILTER
  255.    /* linear filtering looks much nicer but is much slower for Mesa */
  256.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  257.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  258. #else
  259.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  260.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  261. #endif
  262.  
  263.    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  264.  
  265.    if (!LoadRGBMipmaps(TEXTURE_2_FILE, GL_RGB)) {
  266.       printf("Error: couldn't load texture image\n");
  267.       exit(1);
  268.    }
  269.  
  270.    glShadeModel(GL_FLAT);
  271.    glClearColor(0.3, 0.3, 0.4, 1.0);
  272.  
  273.    ModeMenu(TEXBOTH);
  274. }
  275.  
  276.  
  277. int main( int argc, char *argv[] )
  278. {
  279.    glutInit( &argc, argv );
  280.    glutInitWindowSize( 300, 300 );
  281.    glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  282.    glutCreateWindow(argv[0] );
  283.  
  284.    Init();
  285.  
  286.    glutReshapeFunc( Reshape );
  287.    glutKeyboardFunc( Key );
  288.    glutSpecialFunc( SpecialKey );
  289.    glutDisplayFunc( Display );
  290.    glutIdleFunc( Idle );
  291.  
  292.    glutCreateMenu(ModeMenu);
  293.    glutAddMenuEntry("Texture 0", TEX0);
  294.    glutAddMenuEntry("Texture 1", TEX1);
  295.    glutAddMenuEntry("Multi-texture", TEXBOTH);
  296.    glutAddMenuEntry("Toggle Animation", ANIMATE);
  297.    glutAddMenuEntry("Quit", QUIT);
  298.    glutAttachMenu(GLUT_RIGHT_BUTTON);
  299.  
  300.    glutMainLoop();
  301.    return 0;
  302. }
  303.