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

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