home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 5 / MA_Cover_5.iso / ppc / mesa / gdemos / glutfx.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-31  |  3.9 KB  |  187 lines

  1. /* glutfx.c */
  2.  
  3. /*
  4.  * Example of how one might use GLUT with the 3Dfx driver in full-screen mode.
  5.  * Note: this only works with X since we're using Mesa's GLX "hack" for
  6.  * using Glide.
  7.  *
  8.  * Goals:
  9.  *   easy setup and input event handling with GLUT
  10.  *   use 3Dfx hardware
  11.  *   automatically set MESA environment variables
  12.  *   don't lose mouse input focus
  13.  *
  14.  * Brian Paul
  15.  */
  16.  
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <math.h>
  21. #include <GL/glut.h>
  22.  
  23.  
  24. #define WIDTH 640
  25. #define HEIGHT 480
  26.  
  27.  
  28. static int Window = 0;
  29. static int ScreenWidth, ScreenHeight;
  30. static GLuint Torus = 0;
  31. static GLfloat Xrot = 0.0, Yrot = 0.0;
  32.  
  33.  
  34.  
  35. static void Display( void )
  36. {
  37.    static GLfloat blue[4] = {0.2, 0.2, 1.0, 1.0};
  38.    static GLfloat red[4] = {1.0, 0.2, 0.2, 1.0};
  39.    static GLfloat green[4] = {0.2, 1.0, 0.2, 1.0};
  40.  
  41.    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  42.  
  43.    glPushMatrix();
  44.    glRotatef(Xrot, 1, 0, 0);
  45.    glRotatef(Yrot, 0, 1, 0);
  46.  
  47.    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue);
  48.    glCallList(Torus);
  49.  
  50.    glRotatef(90.0, 1, 0, 0);
  51.    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
  52.    glCallList(Torus);
  53.  
  54.    glRotatef(90.0, 0, 1, 0);
  55.    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, green);
  56.    glCallList(Torus);
  57.  
  58.    glPopMatrix();
  59.  
  60.    glutSwapBuffers();
  61. }
  62.  
  63.  
  64. static void Reshape( int width, int height )
  65. {
  66.    float ratio = (float) width / (float) height;
  67.  
  68.    ScreenWidth = width;
  69.    ScreenHeight = height;
  70.  
  71.    /*
  72.     * The 3Dfx driver is limited to 640 x 480 but the X window may be larger.
  73.     * Enforce that here.
  74.     */
  75.    if (width > WIDTH)
  76.       width = WIDTH;
  77.    if (height > HEIGHT)
  78.       height = HEIGHT;
  79.  
  80.    glViewport( 0, 0, width, height );
  81.    glMatrixMode( GL_PROJECTION );
  82.    glLoadIdentity();
  83.    glFrustum( -ratio, ratio, -1.0, 1.0, 5.0, 30.0 );
  84.    glMatrixMode( GL_MODELVIEW );
  85.    glLoadIdentity();
  86.    glTranslatef( 0.0, 0.0, -20.0 );
  87. }
  88.  
  89.  
  90. static void Key( unsigned char key, int x, int y )
  91. {
  92.    switch (key) {
  93.       case 27:
  94.          glutDestroyWindow(Window);
  95.          exit(0);
  96.          break;
  97.    }
  98.    glutPostRedisplay();
  99. }
  100.  
  101.  
  102. static void SpecialKey( int key, int x, int y )
  103. {
  104.    switch (key) {
  105.       case GLUT_KEY_UP:
  106.          break;
  107.       case GLUT_KEY_DOWN:
  108.          break;
  109.       case GLUT_KEY_LEFT:
  110.          break;
  111.       case GLUT_KEY_RIGHT:
  112.          break;
  113.    }
  114.    glutPostRedisplay();
  115. }
  116.  
  117.  
  118. static void MouseMove( int x, int y )
  119. {
  120.    Xrot = y - ScreenWidth / 2;
  121.    Yrot = x - ScreenHeight / 2;
  122.    glutPostRedisplay();
  123. }
  124.  
  125.  
  126. static void Init( void )
  127. {
  128.    Torus = glGenLists(1);
  129.    glNewList(Torus, GL_COMPILE);
  130.    glutSolidTorus(0.5, 2.0, 10, 20);
  131.    glEndList();
  132.  
  133.    glEnable(GL_LIGHTING);
  134.    glEnable(GL_LIGHT0);
  135.  
  136.    glEnable(GL_DEPTH_TEST);
  137.    glEnable(GL_CULL_FACE);
  138. }
  139.  
  140.  
  141. int main( int argc, char *argv[] )
  142. {
  143.    printf("NOTE: if you've got 3Dfx VooDoo hardware you must run this");
  144.    printf(" program as root.\n\n");
  145.    printf("Move the mouse.  Press ESC to exit.\n\n");
  146.    sleep(2);
  147.  
  148.  
  149.    /* Tell Mesa GLX to use 3Dfx driver in fullscreen mode. */
  150.    putenv("MESA_GLX_FX=fullscreen");
  151.  
  152.    /* Disable 3Dfx Glide splash screen */
  153.    putenv("FX_GLIDE_NO_SPLASH=");
  154.  
  155.    /* Give an initial size and position so user doesn't have to place window */
  156.    glutInitWindowPosition(0, 0);
  157.    glutInitWindowSize(WIDTH, HEIGHT);
  158.    glutInit( &argc, argv );
  159.  
  160.    glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
  161.  
  162.    Window = glutCreateWindow(argv[0]);
  163.    if (!Window) {
  164.       printf("Error, couldn't open window\n");
  165.       exit(1);
  166.    }
  167.  
  168.    /*
  169.     * Want the X window to fill the screen so that we don't have to
  170.     * worry about losing the mouse input focus.
  171.     * Note that we won't actually see the X window since we never draw
  172.     * to it, hence, the original X screen's contents aren't disturbed.
  173.     */
  174.    glutFullScreen();
  175.  
  176.    Init();
  177.  
  178.    glutReshapeFunc( Reshape );
  179.    glutKeyboardFunc( Key );
  180.    glutSpecialFunc( SpecialKey );
  181.    glutDisplayFunc( Display );
  182.    glutPassiveMotionFunc( MouseMove );
  183.  
  184.    glutMainLoop();
  185.    return 0;
  186. }
  187.