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

  1. /* texcyl.c */
  2.  
  3. /*
  4.  * Textured cylinder demo: lighting, texturing, reflection mapping.
  5.  * Brian Paul  May 1997
  6.  * This program is in the public domain.
  7.  */
  8.  
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <math.h>
  13. #include <GL/glut.h>
  14.  
  15. #include "../util/readtex.c"   /* I know, this is a hack. */
  16.  
  17.  
  18. #define LIT 1
  19. #define TEXTURED 2
  20. #define REFLECT 3
  21. #define ANIMATE 10
  22. #define QUIT 100
  23.  
  24. static GLuint CylinderObj = 0;
  25. static GLboolean Animate = GL_TRUE;
  26.  
  27. static GLfloat Xrot = 0.0, Yrot = 0.0, Zrot = 0.0;
  28. static GLfloat DXrot = 1.0, DYrot = 2.5, DZrot = -1.5;
  29.  
  30.  
  31. static void Idle( void )
  32. {
  33.    if (Animate) {
  34.       Xrot += DXrot;
  35.       Yrot += DYrot;
  36.       glutPostRedisplay();
  37.    }
  38. }
  39.  
  40.  
  41. static void Display( void )
  42. {
  43.    glClear( GL_COLOR_BUFFER_BIT );
  44.  
  45.    glPushMatrix();
  46.    glRotatef(Xrot, 1.0, 0.0, 0.0);
  47.    glRotatef(Yrot, 0.0, 1.0, 0.0);
  48.    glRotatef(Zrot, 0.0, 0.0, 1.0);
  49.    glScalef(5.0, 5.0, 5.0);
  50.    glCallList(CylinderObj);
  51.  
  52.    glPopMatrix();
  53.  
  54.    glutSwapBuffers();
  55. }
  56.  
  57.  
  58. static void Reshape( int width, int height )
  59. {
  60.    glViewport( 0, 0, width, height );
  61.    glMatrixMode( GL_PROJECTION );
  62.    glLoadIdentity();
  63.    glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
  64.    glMatrixMode( GL_MODELVIEW );
  65.    glLoadIdentity();
  66.    glTranslatef( 0.0, 0.0, -70.0 );
  67. }
  68.  
  69.  
  70. static void SetMode(GLuint m)
  71. {
  72.    /* disable everything */
  73.    glDisable(GL_LIGHTING);
  74.    glDisable(GL_TEXTURE_2D);
  75.    glDisable(GL_TEXTURE_GEN_S);
  76.    glDisable(GL_TEXTURE_GEN_T);
  77.  
  78.    /* enable what's needed */
  79.    if (m==LIT) {
  80.       glEnable(GL_LIGHTING);
  81.    }
  82.    else if (m==TEXTURED) {
  83.       glEnable(GL_TEXTURE_2D);
  84.    }
  85.    else if (m==REFLECT) {
  86.       glEnable(GL_TEXTURE_2D);
  87.       glEnable(GL_TEXTURE_GEN_S);
  88.       glEnable(GL_TEXTURE_GEN_T);
  89.    }
  90. }
  91.  
  92.  
  93. static void ModeMenu(int entry)
  94. {
  95.    if (entry==ANIMATE) {
  96.       Animate = !Animate;
  97.    }
  98.    else if (entry==QUIT) {
  99.       exit(0);
  100.    }
  101.    else {
  102.       SetMode(entry);
  103.    }
  104.    glutPostRedisplay();
  105. }
  106.  
  107.  
  108. static void Key( unsigned char key, int x, int y )
  109. {
  110.    switch (key) {
  111.       case 27:
  112.          exit(0);
  113.          break;
  114.    }
  115.    glutPostRedisplay();
  116. }
  117.  
  118.  
  119. static void SpecialKey( int key, int x, int y )
  120. {
  121.    float step = 3.0;
  122.  
  123.    switch (key) {
  124.       case GLUT_KEY_UP:
  125.          Xrot += step;
  126.          break;
  127.       case GLUT_KEY_DOWN:
  128.          Xrot -= step;
  129.          break;
  130.       case GLUT_KEY_LEFT:
  131.          Yrot += step;
  132.          break;
  133.       case GLUT_KEY_RIGHT:
  134.          Yrot -= step;
  135.          break;
  136.    }
  137.    glutPostRedisplay();
  138. }
  139.  
  140.  
  141. static void Init( void )
  142. {
  143.    GLUquadricObj *q = gluNewQuadric();
  144.    CylinderObj = glGenLists(1);
  145.    glNewList(CylinderObj, GL_COMPILE);
  146.  
  147.    glTranslatef(0.0, 0.0, -1.0);
  148.  
  149.    /* cylinder */
  150.    gluQuadricNormals(q, GL_SMOOTH);
  151.    gluQuadricTexture(q, GL_TRUE);
  152.    gluCylinder(q, 0.6, 0.6, 2.0, 24, 1);
  153.  
  154.    /* end cap */
  155.    glTranslatef(0.0, 0.0, 2.0);
  156.    gluDisk(q, 0.0, 0.6, 24, 1);
  157.  
  158.    /* other end cap */
  159.    glTranslatef(0.0, 0.0, -2.0);
  160.    gluQuadricOrientation(q, GLU_INSIDE);
  161.    gluDisk(q, 0.0, 0.6, 24, 1);
  162.  
  163.    glEndList();
  164.    gluDeleteQuadric(q);
  165.  
  166.    /* lighting */
  167.    glEnable(GL_LIGHTING);
  168.    {
  169.       GLfloat gray[4] = {0.2, 0.2, 0.2, 1.0};
  170.       GLfloat white[4] = {1.0, 1.0, 1.0, 1.0};
  171.       GLfloat teal[4] = { 0.0, 1.0, 0.8, 1.0 };
  172.       glMaterialfv(GL_FRONT, GL_DIFFUSE, teal);
  173.       glLightfv(GL_LIGHT0, GL_AMBIENT, gray);
  174.       glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
  175.       glEnable(GL_LIGHT0);
  176.    }
  177.  
  178.    /* setup texturing */
  179. #ifdef LINEAR_FILTER
  180.    /* linear filtering looks much nicer but is much slower for Mesa */
  181.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  182.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  183. #else
  184.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  185.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  186. #endif
  187.  
  188.    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  189.    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
  190.  
  191.    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  192.    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  193.  
  194.    if (!LoadRGBMipmaps("../samples/2.rgb", GL_RGB)) {
  195.       printf("Error: couldn't load texture image\n");
  196.       exit(1);
  197.    }
  198.  
  199.    glEnable(GL_CULL_FACE);  /* don't need Z testing for convex objects */
  200.  
  201.    SetMode(LIT);
  202. }
  203.  
  204.  
  205. int main( int argc, char *argv[] )
  206. {
  207.    glutInit( &argc, argv );
  208.    glutInitWindowSize( 400, 400 );
  209.  
  210.    glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  211.  
  212.    glutCreateWindow(argv[0] );
  213.  
  214.    Init();
  215.  
  216.    glutReshapeFunc( Reshape );
  217.    glutKeyboardFunc( Key );
  218.    glutSpecialFunc( SpecialKey );
  219.    glutDisplayFunc( Display );
  220.    glutIdleFunc( Idle );
  221.  
  222.    glutCreateMenu(ModeMenu);
  223.    glutAddMenuEntry("Lit", LIT);
  224.    glutAddMenuEntry("Textured", TEXTURED);
  225.    glutAddMenuEntry("Reflect", REFLECT);
  226.    glutAddMenuEntry("Toggle Animation", ANIMATE);
  227.    glutAddMenuEntry("Quit", QUIT);
  228.    glutAttachMenu(GLUT_RIGHT_BUTTON);
  229.  
  230.    glutMainLoop();
  231. }
  232.