home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / adv_texture / copy_texture.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  6.3 KB  |  255 lines

  1. /*
  2.  * Copyright 1993, 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* copy_texure.c
  19.  * Simple example of how to copy pixels from the frame buffer into 
  20.  * a portion of a bound texture.
  21.  *
  22.  *    Left Mouse Button    - copy pixels under cursor
  23.  *    <o> key            - cycle through texture objects
  24.  *    Escape key        - exit the program
  25.  */
  26. #include <GL/gl.h>
  27. #include <GL/glu.h>
  28. #include <GL/glut.h>
  29.  
  30. #include <math.h>
  31. #include <stdio.h>
  32.  
  33. /*  Function Prototypes  */
  34.  
  35. GLvoid  initgfx( GLvoid );
  36. GLvoid  drawScene( GLvoid );
  37. GLvoid  reshape( GLsizei, GLsizei );
  38. GLvoid  keyboard( GLubyte, GLint, GLint );
  39. GLvoid  mouse( GLint, GLint, GLint, GLint );
  40.  
  41. GLvoid  initTexture();
  42.  
  43. void  printHelp( char *progname );
  44.  
  45. /* Global Definitions */
  46.  
  47. #define KEY_ESC    27    /* ascii value for the escape key */
  48.  
  49. /* Global Variables */
  50.  
  51. static unsigned int     curname = 0;
  52. static GLuint         texnames[4];
  53. static GLclampf     priorities[4] = { 0.0, 1.0, 1.0, 1.0 };
  54.  
  55. static GLuint         redtex[64*64], greentex[64*64], bluetex[64*64]; 
  56. static GLuint        gridtex[256][256];
  57.  
  58. static GLsizei        winheight;
  59.  
  60. static GLboolean    copy_texture_supported = GL_TRUE;
  61.  
  62. void
  63. main( int argc, char *argv[] )
  64. {
  65.     GLsizei        width;
  66.  
  67.     glutInit( &argc, argv );
  68.  
  69.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  70.     winheight = glutGet( GLUT_SCREEN_HEIGHT );
  71.     glutInitWindowPosition( width / 4, winheight / 4 );
  72.     glutInitWindowSize( width / 2, winheight / 2 );
  73.     glutInitDisplayMode( GLUT_RGBA );
  74.     glutCreateWindow( argv[0] );
  75.  
  76.     initgfx();
  77.  
  78.     initTexture();
  79.  
  80.     glutMouseFunc( mouse );
  81.     glutKeyboardFunc( keyboard );
  82.     glutReshapeFunc( reshape );
  83.     glutDisplayFunc( drawScene ); 
  84.  
  85.     printHelp( argv[0] );
  86.  
  87.     glutMainLoop();
  88. }
  89.  
  90. GLvoid
  91. printHelp( char *progname )
  92. {
  93.     fprintf(stdout, "\n%s - demonstrates the texture_object, copy_texture "
  94.         "and subtexture extensions\n"
  95.         "Left Mouse Button    - copy pixels under cursor\n"
  96.         "<o> key        - cycle through texture objects\n"
  97.         "Escape key        - exit the program\n\n",
  98.         progname );
  99.  
  100.     printf("Press a mouse button to copy the 32x32 square of pixels "
  101.          "under the mouse\nto the upper right corner of the currently "
  102.         "bound texture object.\n");
  103. }
  104.  
  105. GLvoid
  106. initgfx( void )
  107. {
  108.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  109.  
  110.     if (!glutExtensionSupported( "GL_EXT_texture_object" )) {
  111.         fprintf(stderr, "GL_EXT_texture_object not supported\n");
  112.     }
  113.     if (!glutExtensionSupported( "GL_EXT_copy_texture" )) {
  114.         fprintf(stderr, "GL_EXT_copy_texture not supported\n");
  115.         copy_texture_supported = GL_FALSE;
  116.     }
  117.     if (!glutExtensionSupported( "GL_EXT_subtexture" )) {
  118.         fprintf(stderr, 
  119.             "GL_EXT_subtexture not advertized.. trying anyway\n");
  120.     }
  121. }
  122.  
  123. static void
  124. makeTextures(void) 
  125. {
  126.     int i, j;
  127.     unsigned int *p;
  128.  
  129.     /* fill textures */
  130.     for (i=0; i < 64*64; i++) {
  131.         redtex[i] = 0xff000000;
  132.         greentex[i] = 0x00ff0000;
  133.         bluetex[i] = 0x0000ff00;
  134.     }
  135.  
  136.     /* texture is a yellow grid */
  137.     p = &gridtex[0][0];
  138.     for (i=0; i<256; i++) {
  139.         for (j=0; j<256; j++) {
  140.             if (i%8 == 0 || j%8 == 0) {
  141.                 *p++ = 0xffff00ff;
  142.             } else {
  143.                 *p++ = 0x808080ff;
  144.             }
  145.         }
  146.     }
  147. }
  148.  
  149. GLvoid 
  150. initTexture( GLubyte *image, GLsizei imageWidth, GLsizei imageHeight )
  151. {
  152.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  153.  
  154.     makeTextures();
  155.  
  156.     /* get some unused texture names */
  157.     glGenTexturesEXT(4, texnames);
  158.  
  159.     /* bind, then define, each texture */
  160.     glBindTextureEXT(GL_TEXTURE_2D, texnames[0]);
  161.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  162.     glTexImage2D(GL_TEXTURE_2D, 0, 4, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE,
  163.             redtex);
  164.     glBindTextureEXT(GL_TEXTURE_2D, texnames[1]);
  165.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  166.     glTexImage2D(GL_TEXTURE_2D, 0, 4, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE,
  167.             greentex);
  168.     glBindTextureEXT(GL_TEXTURE_2D, texnames[2]);
  169.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  170.     glTexImage2D(GL_TEXTURE_2D, 0, 4, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE,
  171.             bluetex);
  172.  
  173.     glBindTextureEXT(GL_TEXTURE_2D, texnames[3]);
  174.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  175.     glTexImage2D(GL_TEXTURE_2D, 0, 4, 256, 256, 0, GL_RGBA, 
  176.             GL_UNSIGNED_BYTE, gridtex);
  177.  
  178.     /* optional; define a working set by giving some textures a higher */
  179.     /* priority than others */
  180.     glPrioritizeTexturesEXT(3, texnames, priorities);
  181.  
  182.     /* use default texture environment, modulation */
  183.     glEnable( GL_TEXTURE_2D );
  184. }
  185.  
  186. GLvoid
  187. mouse( GLint button, GLint state, GLint x, GLint y )
  188. {
  189.     if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
  190.         glBindTextureEXT(GL_TEXTURE_2D, texnames[curname]);
  191.         if (copy_texture_supported) {
  192.             glCopyTexSubImage2DEXT(GL_TEXTURE_2D, 0, 32, 32, 
  193.                 x-16, winheight-y-16, 
  194.                 32, 32);
  195.         } else {
  196.             fprintf(stderr, "GL_EXT_copy_texture not supported\n");
  197.         }
  198.             
  199.         glutPostRedisplay();
  200.     }
  201. }
  202.  
  203. GLvoid 
  204. keyboard( GLubyte key, GLint x, GLint y )
  205. {
  206.     switch (key) {
  207.     case 'o':
  208.         curname = (curname+1) % 3;
  209.         glutPostRedisplay();
  210.         break;
  211.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  212.         exit(0);
  213.     }
  214. }
  215.  
  216. GLvoid
  217. reshape( GLsizei width, GLsizei height )
  218. {
  219.     glViewport( 0, 0, width, height );
  220.  
  221.     winheight = height;
  222.  
  223.     glMatrixMode( GL_PROJECTION );
  224.     glLoadIdentity();
  225.     glOrtho( -3.0, 3.0, -3.0, 3.0, -1.0, 1.0 );
  226.     glMatrixMode( GL_MODELVIEW );
  227.     glLoadIdentity();
  228. }
  229.  
  230.  
  231. GLvoid
  232. drawScene(void)
  233. {
  234.     glClear( GL_COLOR_BUFFER_BIT );
  235.  
  236.     glColor4f( 1.0, 1.0, 1.0, 1.0 );
  237.     glBindTextureEXT(GL_TEXTURE_2D, texnames[3]);
  238.     glBegin(GL_TRIANGLE_STRIP);
  239.         glTexCoord2f(0,0); glVertex2f(-2,-2); 
  240.         glTexCoord2f(0,1); glVertex2f(-2, 2); 
  241.         glTexCoord2f(1,0); glVertex2f( 2,-2); 
  242.         glTexCoord2f(1,1); glVertex2f( 2, 2); 
  243.     glEnd();
  244.  
  245.     glBindTextureEXT(GL_TEXTURE_2D, texnames[curname]);
  246.     glBegin(GL_TRIANGLE_STRIP);
  247.         glTexCoord2f(0,0); glVertex2f(-1,-1); 
  248.         glTexCoord2f(0,1); glVertex2f(-1, 1); 
  249.         glTexCoord2f(1,0); glVertex2f( 1,-1); 
  250.         glTexCoord2f(1,1); glVertex2f( 1, 1); 
  251.     glEnd();
  252.  
  253.     glFlush();
  254. }
  255.