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

  1. /* texobj.c */
  2.  
  3. /*
  4.  * Example of using the 1.1 texture object functions.
  5.  * Also, this demo utilizes Mesa's fast texture map path.
  6.  *
  7.  * Brian Paul   June 1996
  8.  */
  9.  
  10.  
  11.  
  12.  
  13. #include <math.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "GL/glut.h"
  17.  
  18. static GLuint Window = 0;
  19.  
  20. static GLuint TexObj[2];
  21. static GLfloat Angle = 0.0f;
  22. static GLboolean HaveTexObj = GL_FALSE;
  23.  
  24.  
  25. #if defined(GL_VERSION_1_1)
  26. #  define TEXTURE_OBJECT 1
  27. #elif defined(GL_EXT_texture_object)
  28. #  define TEXTURE_OBJECT 1
  29. #  define glBindTexture(A,B)     glBindTextureEXT(A,B)
  30. #  define glGenTextures(A,B)     glGenTexturesEXT(A,B)
  31. #  define glDeleteTextures(A,B)  glDeleteTexturesEXT(A,B)
  32. #endif
  33.  
  34.  
  35.  
  36.  
  37. static void draw( void )
  38. {
  39.    glDepthFunc(GL_EQUAL);
  40.    /*   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );*/
  41.    glClear( GL_COLOR_BUFFER_BIT );
  42.  
  43.    glColor3f( 1.0, 1.0, 1.0 );
  44.  
  45.    /* draw first polygon */
  46.    glPushMatrix();
  47.    glTranslatef( -1.0, 0.0, 0.0 );
  48.    glRotatef( Angle, 0.0, 0.0, 1.0 );
  49.    if (HaveTexObj) {
  50. #ifdef TEXTURE_OBJECT
  51.       glBindTexture( GL_TEXTURE_2D, TexObj[0] );
  52. #endif
  53.    }
  54.    else {
  55.       glCallList( TexObj[0] );
  56.    }
  57.    glBegin( GL_POLYGON );
  58.    glTexCoord2f( 0.0, 0.0 );   glVertex2f( -1.0, -1.0 );
  59.    glTexCoord2f( 1.0, 0.0 );   glVertex2f(  1.0, -1.0 );
  60.    glTexCoord2f( 1.0, 1.0 );   glVertex2f(  1.0,  1.0 );
  61.    glTexCoord2f( 0.0, 1.0 );   glVertex2f( -1.0,  1.0 );
  62.    glEnd();
  63.    glPopMatrix();
  64.  
  65.    /* draw second polygon */
  66.    glPushMatrix();
  67.    glTranslatef( 1.0, 0.0, 0.0 );
  68.    glRotatef( Angle-90.0, 0.0, 1.0, 0.0 );
  69.    if (HaveTexObj) {
  70. #ifdef TEXTURE_OBJECT
  71.       glBindTexture( GL_TEXTURE_2D, TexObj[1] );
  72. #endif
  73.    }
  74.    else {
  75.       glCallList( TexObj[0] );
  76.    }
  77.    glBegin( GL_POLYGON );
  78.    glTexCoord2f( 0.0, 0.0 );   glVertex2f( -1.0, -1.0 );
  79.    glTexCoord2f( 1.0, 0.0 );   glVertex2f(  1.0, -1.0 );
  80.    glTexCoord2f( 1.0, 1.0 );   glVertex2f(  1.0,  1.0 );
  81.    glTexCoord2f( 0.0, 1.0 );   glVertex2f( -1.0,  1.0 );
  82.    glEnd();
  83.    glPopMatrix();
  84.  
  85.    glutSwapBuffers();
  86. }
  87.  
  88.  
  89.  
  90. static void idle( void )
  91. {
  92.    Angle += 2.0;
  93.    glutPostRedisplay();
  94. }
  95.  
  96.  
  97.  
  98. /* change view Angle, exit upon ESC */
  99. static void key(unsigned char k, int x, int y)
  100. {
  101.    switch (k) {
  102.       case 27:
  103. #ifdef TEXTURE_OBJECT
  104.          glDeleteTextures( 2, TexObj );
  105. #endif
  106.          glutDestroyWindow(Window);
  107.          exit(0);
  108.    }
  109. }
  110.  
  111.  
  112.  
  113. /* new window size or exposure */
  114. static void reshape( int width, int height )
  115. {
  116.    glViewport(0, 0, (GLint)width, (GLint)height);
  117.    glMatrixMode(GL_PROJECTION);
  118.    glLoadIdentity();
  119.    /*   glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 );*/
  120.    glFrustum( -2.0, 2.0, -2.0, 2.0, 6.0, 20.0 );
  121.    glMatrixMode(GL_MODELVIEW);
  122.    glLoadIdentity();
  123.    glTranslatef( 0.0, 0.0, -8.0 );
  124. }
  125.  
  126.  
  127. static void init( void )
  128. {
  129.    static int width=8, height=8;
  130.    static GLubyte tex1[] = {
  131.      0, 0, 0, 0, 0, 0, 0, 0,
  132.      0, 0, 0, 0, 1, 0, 0, 0,
  133.      0, 0, 0, 1, 1, 0, 0, 0,
  134.      0, 0, 0, 0, 1, 0, 0, 0,
  135.      0, 0, 0, 0, 1, 0, 0, 0,
  136.      0, 0, 0, 0, 1, 0, 0, 0,
  137.      0, 0, 0, 1, 1, 1, 0, 0,
  138.      0, 0, 0, 0, 0, 0, 0, 0 };
  139.  
  140.    static GLubyte tex2[] = {
  141.      0, 0, 0, 0, 0, 0, 0, 0,
  142.      0, 0, 0, 2, 2, 0, 0, 0,
  143.      0, 0, 2, 0, 0, 2, 0, 0,
  144.      0, 0, 0, 0, 0, 2, 0, 0,
  145.      0, 0, 0, 0, 2, 0, 0, 0,
  146.      0, 0, 0, 2, 0, 0, 0, 0,
  147.      0, 0, 2, 2, 2, 2, 0, 0,
  148.      0, 0, 0, 0, 0, 0, 0, 0 };
  149.  
  150.    GLubyte tex[64][3];
  151.    GLint i, j;
  152.  
  153.  
  154.    glDisable( GL_DITHER );
  155.  
  156.    /* Setup texturing */
  157.    glEnable( GL_TEXTURE_2D );
  158.    glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
  159.    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
  160.  
  161.  
  162.    /* generate texture object IDs */
  163.    if (HaveTexObj) {
  164. #ifdef TEXTURE_OBJECT
  165.       glGenTextures( 2, TexObj );
  166. #endif
  167.    }
  168.    else {
  169.       TexObj[0] = glGenLists(2);
  170.       TexObj[1] = TexObj[0]+1;
  171.    }
  172.  
  173.    /* setup first texture object */
  174.    if (HaveTexObj) {
  175. #ifdef TEXTURE_OBJECT
  176.       glBindTexture( GL_TEXTURE_2D, TexObj[0] );
  177. #endif
  178.    }
  179.    else {
  180.       glNewList( TexObj[0], GL_COMPILE );
  181.    }
  182.    /* red on white */
  183.    for (i=0;i<height;i++) {
  184.       for (j=0;j<width;j++) {
  185.          int p = i*width+j;
  186.          if (tex1[(height-i-1)*width+j]) {
  187.             tex[p][0] = 255;   tex[p][1] = 0;     tex[p][2] = 0;
  188.          }
  189.          else {
  190.             tex[p][0] = 255;   tex[p][1] = 255;   tex[p][2] = 255;
  191.          }
  192.       }
  193.    }
  194.  
  195.    glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0,
  196.                  GL_RGB, GL_UNSIGNED_BYTE, tex );
  197.    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
  198.    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
  199.    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
  200.    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
  201.    if (!HaveTexObj) {
  202.       glEndList();
  203.    }
  204.    /* end of texture object */
  205.  
  206.    /* setup second texture object */
  207.    if (HaveTexObj) {
  208. #ifdef TEXTURE_OBJECT
  209.       glBindTexture( GL_TEXTURE_2D, TexObj[1] );
  210. #endif
  211.    }
  212.    else {
  213.       glNewList( TexObj[1], GL_COMPILE );
  214.    }
  215.    /* green on blue */
  216.    for (i=0;i<height;i++) {
  217.       for (j=0;j<width;j++) {
  218.          int p = i*width+j;
  219.          if (tex2[(height-i-1)*width+j]) {
  220.             tex[p][0] = 0;     tex[p][1] = 255;   tex[p][2] = 0;
  221.          }
  222.          else {
  223.             tex[p][0] = 0;     tex[p][1] = 0;     tex[p][2] = 255;
  224.          }
  225.       }
  226.    }
  227.    glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0,
  228.                  GL_RGB, GL_UNSIGNED_BYTE, tex );
  229.    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
  230.    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
  231.    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
  232.    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
  233.    if (!HaveTexObj) {
  234.       glEndList();
  235.    }
  236.    /* end texture object */
  237.  
  238. }
  239.  
  240.  
  241.  
  242. int main( int argc, char *argv[] )
  243. {
  244.    glutInitWindowPosition(0, 0);
  245.    glutInitWindowSize(300, 300);
  246.    glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
  247.  
  248.    Window = glutCreateWindow("Texture Objects");
  249.    if (!Window) {
  250.       exit(1);
  251.    }
  252.  
  253.    /* check that renderer has the GL_EXT_texture_object extension
  254.     * or supports OpenGL 1.1
  255.     */
  256. #ifdef TEXTURE_OBJECT
  257.    {
  258.       char *exten = (char *) glGetString( GL_EXTENSIONS );
  259.       char *version = (char *) glGetString( GL_VERSION );
  260.       if (   strstr( exten, "GL_EXT_texture_object" )
  261.           || strncmp( version, "1.1", 3 )==0 ) {
  262.          HaveTexObj = GL_TRUE;
  263.       }
  264.    }
  265. #endif
  266.  
  267.    init();
  268.  
  269.    glutReshapeFunc( reshape );
  270.    glutKeyboardFunc( key );
  271.    glutIdleFunc( idle );
  272.    glutDisplayFunc( draw );
  273.    glutMainLoop();
  274.    return 0;
  275. }
  276.