home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1993, 1996, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* mipmap.c
- * This program reads in a texture from a .rgb file, uses the utility
- * routine gluBulid2dMipmaps() to scale and load mipmaps for a single
- * texture image, then applies the texture to a rectangular polygon
- * using explicit texture coordinates. If no file is given, the
- * program creates its own solid color mipmaps. The user can
- * switch between the various minification and magnification filters.
- *
- * <a> key - pause/restart movement
- * <m> key - cycle minification filter
- * <M> key - cycle Magnification filter
- * <R> key - reset image to starting position
- * Escape Key - exit program
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <math.h>
- #include <stdio.h>
-
- #include "rgbImageFile.h" /* should be in ../../include directory */
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid animate( GLvoid );
- GLvoid visibility( GLint );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
-
- GLvoid initTexture( unsigned int *, GLsizei, GLsizei );
- GLvoid cycleMinFilter( GLvoid );
- GLvoid cycleMagFilter( GLvoid );
- GLvoid toggleMoving( GLvoid );
- GLvoid resetSwim( GLvoid );
-
- void printFilters( GLvoid );
- void printHelp( char *progname );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- #define MAXDIM 128 /* Maximum texture size */
-
- /* Global Variables */
-
- static GLboolean moving = GL_TRUE;
- static GLfloat swim = 1.0;
-
- typedef struct {
- GLint type;
- char *name;
- } FilterInfo;
-
- static FilterInfo filters[6] = {
- { GL_NEAREST, "GL_NEAREST" },
- { GL_LINEAR, "GL_LINEAR" },
- { GL_NEAREST_MIPMAP_NEAREST, "GL_NEAREST_MIPMAP_NEAREST" },
- { GL_LINEAR_MIPMAP_NEAREST, "GL_LINEAR_MIPMAP_NEAREST" },
- { GL_NEAREST_MIPMAP_LINEAR, "GL_NEAREST_MIPMAP_LINEAR" },
- { GL_LINEAR_MIPMAP_LINEAR, "GL_LINEAR_MIPMAP_LINEAR" }
- };
-
- static int curMagFilter = 0;
- static int curMinFilter = 0;
-
- void
- main( int argc, char *argv[] )
- {
- GLsizei width, height;
- char *imageFileName = "fish.rgba";
- unsigned int *image;
- GLsizei imageWidth = 0, imageHeight = 0;
-
- glutInit( &argc, argv );
-
- if (argc > 2) {
- fprintf (stderr, "usage: %s [filename]\n", argv[0]);
- exit(-1);
- } else if (argc == 2) {
- imageFileName = argv[1];
- fprintf(stdout, "using image %s\n", imageFileName );
- image = rgbReadImageFile(imageFileName, &imageWidth,
- &imageHeight);
- }
-
- /* create a window that is 1/4 the size of the screen */
-
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowPosition( (width / 2) + 4, height / 4 );
- glutInitWindowSize( (width / 2) - 4, height / 2 );
- glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
- glutCreateWindow( argv[0] );
-
- initTexture( image, imageWidth, imageHeight );
- initgfx();
-
- glutKeyboardFunc( keyboard );
- glutIdleFunc( animate );
- glutVisibilityFunc( visibility );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- GLvoid
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - demonstrates texture map filtering \n"
- "<a> key - pause/restart movement\n"
- "<m> key - cycle minification filter\n"
- "<M> key - cycle Magnification filter\n"
- "<R> key - reset image to starting position\n"
- "Escape key - exit the program\n\n",
- progname );
- }
-
- GLvoid
- initgfx( void )
- {
- glClearColor( 0.0, 0.0, 0.0, 1.0 );
- }
-
- GLvoid
- initTexture( unsigned int *image, GLsizei imageWidth, GLsizei imageHeight )
- {
- GLint r, g, b;
- int i, dim;
- GLint level;
- GLubyte *mipmap;
-
- if (imageWidth != 0 && imageHeight != 0 ) {
- /* scale texture image, make mipmaps and load texture
- * gluBuild2DMipmaps( target, components, width, height,
- * format, type, imageArray )
- */
- gluBuild2DMipmaps(GL_TEXTURE_2D, 4, imageWidth, imageHeight,
- GL_RGBA, GL_UNSIGNED_BYTE, image);
- } else {
- /* build artificial colored mipmaps to show the changes */
-
- fprintf (stdout, "creating artificial mipmaps\n");
-
- dim = MAXDIM;
- for (level = 0; dim >= 1; level++, dim >>= 1 ) {
-
- mipmap = (GLubyte *) malloc(dim*dim*4*sizeof(GLubyte));
-
- b = level & 0x1;
- g = (level>>1) & 0x1;
- r = (level>>2) & 0x1;
- r=!r; g=!g; b=!b;
- for ( i = 0; i < dim * dim * 4; ) {
- mipmap[i++] = r * 0xff;
- mipmap[i++] = g * 0xff;
- mipmap[i++] = b * 0xff;
- mipmap[i++] = 0;
- }
-
- printf("mipmap %d size is %3d X %3d, "
- "color is (%d, %d, %d, %d)\n",
- level, dim, dim, r, g, b, 0);
-
- /* use the image as a texture */
-
- glTexImage2D(GL_TEXTURE_2D, level, 4, dim, dim,
- 0, GL_RGBA, GL_UNSIGNED_BYTE, mipmap);
- }
- }
-
- glEnable(GL_TEXTURE_2D);
-
- glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
- filters[curMinFilter].type );
- glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
- filters[curMagFilter].type );
- printFilters();
- }
-
-
- GLvoid
- cycleMinFilter( GLvoid )
- {
- curMinFilter = (curMinFilter +1) % 6;
- glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
- filters[curMinFilter].type );
- printFilters();
- }
-
-
- GLvoid
- cycleMagFilter( GLvoid )
- {
- curMagFilter = (curMagFilter +1) % 2;
- glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
- filters[curMagFilter].type );
- printFilters();
- }
-
-
- GLvoid
- printFilters( GLvoid )
- {
- printf("mag filter: %10s, min filter: %26s\n",
- filters[curMagFilter].name, filters[curMinFilter].name);
- }
-
-
- GLvoid
- resetSwim ( GLvoid )
- {
- swim = 1.0;
- }
-
-
- GLvoid
- toggleMoving ( GLvoid )
- {
- moving = ! moving;
- printf ("%s\n", moving?"...MOVING":"PAUSED...");
-
- if (moving) {
- glutIdleFunc( animate );
- } else {
- glutIdleFunc( NULL );
- }
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case 'a': /* toggle animation */
- toggleMoving();
- break;
- case 'm': /* cycle minification filter */
- cycleMinFilter();
- break;
- case 'M': /* cycle Magnification filter */
- cycleMagFilter();
- break;
- case 'R': /* reset image to starting position */
- resetSwim();
- break;
- case KEY_ESC: /* Exit when the Escape key is pressed */
- exit(0);
- }
- glutPostRedisplay();
- }
-
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLfloat aspect;
- glViewport( 0, 0, width, height );
-
- aspect = (GLfloat) (width) / height;
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( 30.0, aspect, 1.0, 1500.0 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- glTranslatef( 0.0, 0.0, -3.0 );
- }
-
- GLvoid
- animate( GLvoid )
- {
- swim = fmodf( swim + 0.1, 35.0 );
-
- /* Tell GLUT to redraw the scene */
- glutPostRedisplay();
- }
-
- GLvoid
- visibility( int state )
- {
- if (state == GLUT_VISIBLE && moving) {
- glutIdleFunc( animate );
- } else {
- glutIdleFunc( NULL );
- }
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- static float v0[3] = { -1.5, -1.0, 0.0 };
- static float v1[3] = { 1.5, -1.0, 0.0 };
- static float v2[3] = { 1.5, 1.0, 0.0 };
- static float v3[3] = { -1.5, 1.0, 0.0 };
-
- static float t0[2] = { 0.0, 0.0 };
- static float t1[2] = { 1.0, 0.0 };
- static float t2[2] = { 1.0, 1.0 };
- static float t3[2] = { 0.0, 1.0 };
-
- glClear( GL_COLOR_BUFFER_BIT );
-
- glColor4f( 1.0, 1.0, 1.0, 1.0 );
- glPushMatrix();
- glTranslatef( -3+swim, 0.2*sinf(swim * 3.0), -swim*5.0 );
-
- glRotatef( 45.0, 0.0, 1.0, 0.0 );
- glBegin( GL_QUADS );
- glTexCoord2fv( t0 ); glVertex3fv( v0 );
- glTexCoord2fv( t1 ); glVertex3fv( v1 );
- glTexCoord2fv( t2 ); glVertex3fv( v2 );
- glTexCoord2fv( t3 ); glVertex3fv( v3 );
- glEnd();
-
- glPopMatrix();
-
- glutSwapBuffers();
- }
-