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.
- */
-
- /* texgen.c
- * This program reads in a texture from a .rgb file, sets up the
- * texture and the texture environment, then applies the texture
- * to a polygon using automatically generated texture coordinates.
- *
- * <g> key - toggles between eye and object linear modes
- * 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 */
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid animate( GLvoid );
- GLvoid visibility( GLint );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
- GLvoid initialize( int, char ** );
-
- GLvoid initTexture( unsigned int *, GLsizei, GLsizei );
- GLvoid toggleTexGenMode( GLvoid );
-
- GLvoid printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- static GLfloat spin = 1.0;
-
- static GLuint mode = GL_OBJECT_LINEAR;
- static GLenum plane = GL_OBJECT_PLANE;
-
- void
- main ( int argc, char *argv[])
- {
- char *imageFileName = "bricks.rgb";
- unsigned int *image;
- GLsizei width, height, imageWidth, imageHeight;
-
- glutInit( &argc, argv );
-
- if (argc < 2) {
- fprintf (stderr, "usage: %s <imageFileName>\n", argv[0] );
- } else
- 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_DEPTH | 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 automatic texture coordinate "
- "generation\n"
- "<g> key - toggle texture coordinate generation mode\n"
- "Escape key - exit the program\n\n",
- progname );
- fprintf(stdout, "Using %s linear coordinate generation\n",
- (mode == GL_OBJECT_LINEAR ? "OBJECT":"EYE"));
- }
-
-
- GLvoid initgfx( GLvoid )
- {
- glEnable( GL_DEPTH_TEST );
-
- glClearColor( 0, 0, 0, 1 );
- }
-
- GLvoid initTexture( unsigned int *image,
- GLsizei imageWidth, GLsizei imageHeight )
- {
- /* 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 );
-
- /* Set texture coordinate generation function for both
- * the s and t planes to object linear mode. Texture
- * will remain attached to the object.
- */
- glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, mode );
- glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, mode );
-
- /* Enable automatic texture coordinate generation for
- * both the s and t planes
- */
- glEnable( GL_TEXTURE_GEN_S );
- glEnable( GL_TEXTURE_GEN_T );
-
- glEnable( GL_TEXTURE_2D );
- }
-
- GLvoid
- toggleTexGenMode( GLvoid )
- {
- if ( mode == GL_OBJECT_LINEAR ) {
- mode = GL_EYE_LINEAR;
- plane = GL_EYE_PLANE;
- } else {
- mode = GL_OBJECT_LINEAR;
- plane = GL_OBJECT_PLANE;
- }
-
- fprintf(stdout, "Using %s linear coordinate generation\n",
- (mode == GL_OBJECT_LINEAR ? "OBJECT":"EYE"));
-
- glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, mode );
- glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, mode );
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case 'g': /* toggle coordinate generation mode */
- toggleTexGenMode();
- glutPostRedisplay();
- break;
- case KEY_ESC: /* Exit when the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- animate( GLvoid )
- {
- spin = fmodf( spin + 5.0, 360.0 );
-
- /* Tell GLUT to redraw the scene */
- glutPostRedisplay();
- }
-
- GLvoid
- visibility( int state )
- {
- if (state == GLUT_VISIBLE) {
- glutIdleFunc( animate );
- } else {
- glutIdleFunc( NULL );
- }
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
-
- glViewport( 0, 0, width, height );
-
- aspect = (GLdouble) width / (GLdouble) height;
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( 45.0, aspect, 3.0, 15.0 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- glTranslatef( 0.0, 0.0, -6.0 );
- }
-
- GLvoid
- drawScene(void)
- {
- /* Define s and t planes */
- static GLfloat sgenparams[] = { 2, 0, 0, 0 };
- static GLfloat tgenparams[] = { 0, 1, 0, 0 };
-
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
-
- /* Specify texture coordinate generation planes */
- glTexGenfv( GL_S, plane, sgenparams );
- glTexGenfv( GL_T, plane, tgenparams );
-
- glColor4f( 1.0, 1.0, 1.0, 1.0 );
- glPushMatrix();
- glRotatef( spin, 0.0, 0.0, 1.0 );
- glRotatef( spin, 0.0, 1.0, 0.0 );
- glutSolidTorus( 0.25, 0.75, 32, 32 );
-
- glRotatef( 90.0, 1.0, 0.0, 0.0 );
- glutSolidTorus( 0.25, 0.75, 32, 32 );
- glPopMatrix();
-
- glutSwapBuffers();
- }
-
-