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 / texture3D.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  5.5 KB  |  246 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. /* texture3D.c
  19.  * This program creates a simple 3D texture, and displays it.
  20.  *
  21.  *    Escape Key    - exit program
  22.  */
  23. #include <GL/gl.h>
  24. #include <GL/glu.h>
  25. #include <GL/glut.h>
  26.  
  27. #include <math.h>
  28. #include <stdio.h>
  29.  
  30. /*  Function Prototypes  */
  31.  
  32. GLvoid  initgfx( GLvoid );
  33. GLvoid  animate( GLvoid );
  34. GLvoid  visibility( GLint );
  35. GLvoid  drawScene( GLvoid );
  36. GLvoid  reshape( GLsizei, GLsizei );
  37. GLvoid  keyboard( GLubyte, GLint, GLint );
  38.  
  39. GLvoid  initTexture( GLvoid );
  40.  
  41. void  printHelp( char *progname );
  42.  
  43. /* Global Definitions */
  44.  
  45. #define KEY_ESC    27    /* ascii value for the escape key */
  46.  
  47. #define NUMSLICES 256
  48.  
  49. /* Global Variables */
  50.  
  51. static GLfloat spin = 1.0;
  52.  
  53. static unsigned int tex[64][64][64];
  54.  
  55. void
  56. main( int argc, char *argv[] )
  57. {
  58.     GLsizei        width, height;
  59.  
  60.     glutInit( &argc, argv );
  61.  
  62.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  63.     height = glutGet( GLUT_SCREEN_HEIGHT );
  64.     glutInitWindowPosition( width / 4, height / 4 );
  65.     glutInitWindowSize( width / 2, height / 2 );
  66.     glutInitDisplayMode( GLUT_RGBA );
  67.     glutCreateWindow( argv[0] );
  68.  
  69.     initgfx();
  70.     initTexture();
  71.  
  72.     glutKeyboardFunc( keyboard );
  73.     glutIdleFunc( animate );
  74.     glutVisibilityFunc( visibility );
  75.     glutReshapeFunc( reshape );
  76.     glutDisplayFunc( drawScene ); 
  77.  
  78.     printHelp( argv[0] );
  79.  
  80.     glutMainLoop();
  81. }
  82.  
  83. GLvoid
  84. printHelp( char *progname )
  85. {
  86.     fprintf(stdout, "\n%s - demonstrates basic 3D texture mapping\n"
  87.         "Escape key     - exit the program\n\n",
  88.         progname );
  89. }
  90.  
  91. GLvoid
  92. initgfx( void )
  93. {
  94.     glClearColor(0.2,0.2,0.5,1.0);
  95.  
  96.     if (!glutExtensionSupported( "GL_EXT_texture3D" )) {
  97.         fprintf(stderr, "GL_EXT_texture3D not supported\n");
  98.     }
  99.  
  100.     glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  101.     glEnable(GL_BLEND);
  102. }
  103.  
  104.  
  105. /* generate a simple 3D texture */
  106. static void
  107. make3Dtexture(void) 
  108. {
  109.     int i, j, k;
  110.     unsigned int *p = &tex[0][0][0];
  111.  
  112.     for (i=0; i<64; i++) {
  113.         for (j=0; j<64; j++) {
  114.             for (k=0; k<64; k++) {
  115.                 if (i < 10 || i > 48 ||
  116.                     j < 10 || j > 48 ||
  117.                     k < 10 || k > 48) {
  118.                     if (i < 2 || i > 62 ||
  119.                         j < 2 || j > 62 ||
  120.                         k < 2 || k > 62) {
  121.                         *p++ = 0x00000000;
  122.                     } else {
  123.                         *p++ = 0xff80ffff;
  124.                     }
  125.                 } else {
  126.                     *p++ = 0x000000ff;
  127.                 }
  128.             }
  129.         }
  130.     }
  131. }
  132.  
  133. GLvoid 
  134. initTexture( GLvoid )
  135. {
  136.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  137.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  138.  
  139.     make3Dtexture();
  140.  
  141.     /* Similar to defining a 2D texture, but note the setting of the
  142.      * wrap parameter for the R coordinate.  Also, for 3D textures 
  143.      * you probably won't need mipmaps, hence the linear min filter.
  144.      */
  145.     glTexParameteri(GL_TEXTURE_3D_EXT, GL_TEXTURE_MIN_FILTER, 
  146.                 GL_LINEAR);
  147.     glTexParameteri(GL_TEXTURE_3D_EXT, GL_TEXTURE_WRAP_S, GL_CLAMP);
  148.     glTexParameteri(GL_TEXTURE_3D_EXT, GL_TEXTURE_WRAP_T, GL_CLAMP);
  149.     glTexParameteri(GL_TEXTURE_3D_EXT, GL_TEXTURE_WRAP_R_EXT, 
  150.                 GL_CLAMP);
  151.     glTexImage3DEXT(GL_TEXTURE_3D_EXT, 0, 4, 64, 64, 64, 0,
  152.                 GL_RGBA, GL_UNSIGNED_BYTE, tex);
  153.  
  154.     glEnable(GL_TEXTURE_3D_EXT);
  155.     checkError("initTexture");
  156. }
  157.  
  158. GLvoid 
  159. keyboard( GLubyte key, GLint x, GLint y )
  160. {
  161.     switch (key) {
  162.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  163.         exit(0);
  164.     }
  165. }
  166.  
  167. GLvoid 
  168. animate( GLvoid )
  169. {
  170.     spin = fmodf( spin + 5.0, 360.0 );
  171.  
  172.     /* Tell GLUT to redraw the scene */
  173.     glutPostRedisplay();
  174. }
  175.  
  176. GLvoid
  177. visibility( int state ) 
  178. {
  179.     if (state == GLUT_VISIBLE) {
  180.         glutIdleFunc( animate );
  181.     } else {
  182.         glutIdleFunc( NULL );
  183.     }
  184. }
  185.  
  186. GLvoid
  187. reshape( GLsizei width, GLsizei height )
  188. {
  189.     GLdouble      aspect;
  190.  
  191.     glViewport( 0, 0, width, height );
  192.  
  193.     aspect = (GLdouble) width / (GLdouble) height;
  194.  
  195.     glMatrixMode(GL_PROJECTION);
  196.     glLoadIdentity();
  197.     gluPerspective(60.0, aspect, 1.0, 100.0 );
  198.     glMatrixMode(GL_MODELVIEW);
  199.     glLoadIdentity();
  200.     glTranslatef(0.,0.,-3.0);
  201.     glMatrixMode(GL_TEXTURE);
  202. }
  203.  
  204. GLvoid
  205. drawScene(void)
  206. {
  207.     int i;
  208.     float r, dr, z, dz;
  209.  
  210.     glColor4f(1, 1, 1, 1.4/NUMSLICES);
  211.     glClear(GL_COLOR_BUFFER_BIT);
  212.     
  213.     /* Display the entire 3D texture by drawing a series of quads that  
  214.      * slice through the texture coordinate space.  Note that the      
  215.      * transformations below are applied to the texture matrix, not the 
  216.      * modelview matrix. 
  217.      */
  218.     glPushMatrix();
  219.  
  220.     /* center the texture coords around the [0,1] cube */
  221.     glTranslatef(.5,.5,.5);
  222.  
  223.     /* Add a rotation to make the picture more interesting */
  224.     glRotatef(spin,1.,1.,.5);
  225.  
  226.     /* to make sure that the texture coords, after arbitrary rotations, 
  227.      * still fully contain the [0,1] cube, make them span a range    
  228.      * sqrt(3)=1.74 wide 
  229.      */
  230.     r = -0.87; dr = 1.74/NUMSLICES;
  231.     z = -1.00; dz = 2.00/NUMSLICES;
  232.     for (i=0; i < NUMSLICES; i++) {
  233.         glBegin(GL_TRIANGLE_STRIP);
  234.             glTexCoord3f(-.87,-.87,r); glVertex3f(-1,-1,z); 
  235.             glTexCoord3f(-.87, .87,r); glVertex3f(-1, 1,z); 
  236.             glTexCoord3f( .87,-.87,r); glVertex3f( 1,-1,z); 
  237.             glTexCoord3f( .87, .87,r); glVertex3f( 1, 1,z); 
  238.         glEnd();
  239.         r += dr;
  240.         z += dz;
  241.     }
  242.     glPopMatrix();
  243.     checkError("drawScene");
  244.     glFlush();
  245. }
  246.