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