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 / texture / borders.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  5.8 KB  |  235 lines

  1. /*
  2.  * Copyright 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. /* borders.c
  19.  * This program reads in a single image from a .rgb file, and 
  20.  * creates and load mipmaps of the image into texture memory. It
  21.  * sets the texture up to clamp in the s dimension and repeat in the 
  22.  * t dimension, and then applies the texture to a rectangular
  23.  * polygon using explicit texture coordinates outside the range
  24.  * [0,1].
  25.  *
  26.  * It specifies a border color of red which is averaged with the
  27.  * texture edge when the texture is clamped, and the filtering
  28.  * is not GL_NEAREST.
  29.  *
  30.  *    <a> key        - toggle animation on/off
  31.  *    Escape Key    - exit program
  32.  */
  33. #include <GL/gl.h>
  34. #include <GL/glu.h>
  35. #include <GL/glut.h>
  36.  
  37. #include <math.h>
  38. #include <stdio.h>
  39.  
  40. #include "rgbImageFile.h"    /* should be in ../../include */
  41.  
  42. /*  Function Prototypes  */
  43.  
  44. GLvoid  initgfx( GLvoid );
  45. GLvoid  animate( GLvoid );
  46. GLvoid  visibility( GLint );
  47. GLvoid  drawScene( GLvoid );
  48. GLvoid  reshape( GLsizei, GLsizei );
  49. GLvoid  keyboard( GLubyte, GLint, GLint );
  50.  
  51. GLvoid  initTexture( unsigned int *, GLsizei, GLsizei );
  52.  
  53. void  printHelp( char *progname );
  54.  
  55. /* Global Definitions */
  56.  
  57. #define KEY_ESC    27    /* ascii value for the escape key */
  58.  
  59. /* Global Variables */
  60.  
  61. static GLfloat swim = 1.0;
  62.  
  63. static GLboolean animateFlag = GL_TRUE;
  64.  
  65. void
  66. main ( int argc, char *argv[])
  67. {
  68.     char        *imageFileName = "maxHeadroom.rgb";
  69.     unsigned int     *image;
  70.     GLsizei        width, height;
  71.     GLsizei        imageWidth, imageHeight;
  72.     
  73.     glutInit( &argc, argv );
  74.  
  75.     if (argc < 2) {
  76.         fprintf (stderr, "usage: %s <imageFileName>\n", argv[0] );
  77.     } else
  78.         imageFileName = argv[1];
  79.  
  80.     fprintf(stdout, "using image %s\n\n", imageFileName );
  81.  
  82.     image = rgbReadImageFile(imageFileName, &imageWidth,
  83.              &imageHeight);
  84.  
  85.     /* create a window that is 1/4 the size of the screen */
  86.  
  87.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  88.     height = glutGet( GLUT_SCREEN_HEIGHT );
  89.     glutInitWindowPosition( (width / 2) + 4, height / 4 );
  90.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  91.     glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
  92.     glutCreateWindow( argv[0] );
  93.  
  94.     initTexture( image, imageWidth, imageHeight );
  95.     initgfx();
  96.  
  97.     glutKeyboardFunc( keyboard );
  98.     glutIdleFunc( animate );
  99.     glutVisibilityFunc( visibility );
  100.     glutReshapeFunc( reshape );
  101.     glutDisplayFunc( drawScene ); 
  102.  
  103.     printHelp( argv[0] );
  104.  
  105.     glutMainLoop();
  106. }
  107.  
  108. GLvoid
  109. printHelp( char *progname )
  110. {
  111.     fprintf(stdout, "\n%s - demonstrates texture wrapping\n"
  112.         "<a> key    - toggle animation on/off\n"
  113.         "Escape key     - exit the program\n\n",
  114.         progname );
  115. }
  116.  
  117. GLvoid initgfx( GLvoid )
  118. {
  119.     glClearColor( 0, 0, 0, 1 );
  120. }
  121.  
  122. GLvoid 
  123. initTexture( unsigned int *image, 
  124.     GLsizei imageWidth, GLsizei imageHeight )
  125. {
  126.     GLfloat red[4] = { 1.0, 0.0, 0.0, 1.0 };
  127.  
  128.     /* use gluBuild2DMipmaps to create and load mipmaps (it will 
  129.      * also scale the original image to be a power of two, if 
  130.      * necessary)
  131.      *
  132.      *      gluBuild2DMipmaps( target, components, width, height,
  133.      *              format,  type, imageArray ) 
  134.      */
  135.     gluBuild2DMipmaps( GL_TEXTURE_2D, 4, imageWidth, imageHeight,
  136.              GL_RGBA, GL_UNSIGNED_BYTE, image );
  137.  
  138.     /* Set the texture up to clamp in the s dimension and 
  139.      * repeat in the t dimension when the texture 
  140.      * coordinates are outside the range [0,1]
  141.      */
  142.     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
  143.     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  144.  
  145.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  146.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  147.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, red);
  148.  
  149.     /* enable 2D texture mapping */
  150.     glEnable( GL_TEXTURE_2D );
  151. }
  152.  
  153. GLvoid 
  154. keyboard( GLubyte key, GLint x, GLint y )
  155. {
  156.     switch (key) {
  157.     case 'a':    /* toggle animation */
  158.         animateFlag = !animateFlag;
  159.         if (animateFlag)
  160.             glutIdleFunc( animate );
  161.         else
  162.             glutIdleFunc( NULL );
  163.         break;
  164.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  165.         exit(0);
  166.     }
  167. }
  168.  
  169. GLvoid 
  170. animate( GLvoid )
  171. {
  172.     swim = fmodf( swim + 0.1, 35.0 );
  173.  
  174.     /* Tell GLUT to redraw the scene */
  175.     glutPostRedisplay();
  176. }
  177.  
  178. GLvoid
  179. visibility( int state ) 
  180. {
  181.     if (state == GLUT_VISIBLE && animateFlag) {
  182.         glutIdleFunc( animate );
  183.     } else {
  184.         glutIdleFunc( NULL );
  185.     }
  186. }
  187.  
  188. GLvoid
  189. reshape( GLsizei width, GLsizei height )
  190. {
  191.     GLdouble      aspect;
  192.  
  193.     glViewport( 0, 0, width, height );
  194.  
  195.     aspect = (GLdouble) width / (GLdouble) height;
  196.  
  197.     glMatrixMode( GL_PROJECTION );
  198.     glLoadIdentity();
  199.     gluPerspective( 45.0, aspect, 1.0, 50.0 );
  200.     glMatrixMode( GL_MODELVIEW );
  201.     glLoadIdentity();
  202.     glTranslatef( 0.0, 0.0, -12.0 ); 
  203. }
  204.  
  205. GLvoid
  206. drawScene(void)
  207. {
  208.     static float v0[3] = { -1.5, -1.0, 0.0 };
  209.     static float v1[3] = {  1.5, -1.0, 0.0 };
  210.     static float v2[3] = {  1.5,  1.0, 0.0 };
  211.     static float v3[3] = { -1.5,  1.0, 0.0 };
  212.  
  213.     static float t0[2] = { 0.0, 0.0 };
  214.     static float t1[2] = { 2.0, 0.0 };
  215.     static float t2[2] = { 2.0, 2.0 };
  216.     static float t3[2] = { 0.0, 2.0 };
  217.  
  218.     glClear( GL_COLOR_BUFFER_BIT );
  219.  
  220.     glColor4f( 1.0, 1.0, 1.0, 1.0 );
  221.     glPushMatrix ();
  222.         glTranslatef( -6.0 + swim, 0.2*sinf(swim * 3.0), -swim );
  223.     
  224.         glBegin( GL_QUADS );
  225.             glTexCoord2fv( t0 ); glVertex3fv( v0 );
  226.             glTexCoord2fv( t1 ); glVertex3fv( v1 );
  227.             glTexCoord2fv( t2 ); glVertex3fv( v2 );
  228.             glTexCoord2fv( t3 ); glVertex3fv( v3 );
  229.         glEnd();
  230.  
  231.     glPopMatrix ();
  232.  
  233.     glutSwapBuffers();
  234. }
  235.