home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1995, 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.
- */
-
- /* windowSnap.c
- * Type "windowSnap <imageFileName>". When the leftmouse
- * button is pressed the scene in the window is captured,
- * and the pixel data is written into a file.
- */
- #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 keyboard( GLubyte, GLint, GLint );
- GLvoid mouse( GLint, GLint, GLint, GLint );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
-
- GLvoid grabImage( void );
-
- void printHelp( char * );
-
- /* Global Variables */
-
- static char *imageFileName;
- static GLsizei winWidth, winHeight;
-
- GLvoid
- main ( int argc, char *argv[] )
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- if (argc < 2) {
- fprintf(stderr, "usage: windowSnap <imageFileName>\n");
- imageFileName = "windowSnap.rgb";
- } else
- imageFileName = argv[1];
- fprintf(stdout, "\n\nImage will be saved in %s\n",
- imageFileName);
-
- width = glutGet(GLUT_SCREEN_WIDTH);
- height = glutGet(GLUT_SCREEN_HEIGHT);
- winWidth = width / 2;
- winHeight = height / 2;
- glutInitWindowPosition( width/4, height/4 );
- glutInitWindowSize( winWidth, winHeight);
- glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutReshapeFunc( reshape );
- glutMouseFunc( mouse );
- glutDisplayFunc(drawScene);
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- GLvoid
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s, reads pixels from window and saves \n"
- "them in a file when the Left Mousebutton is released\n\n"
- "Left Mousebutton, up - snapshot of window\n\n",
- progname);
- }
-
- GLvoid
- initgfx( void )
- {
- GLfloat mat_specular[] = { 0.8, 0.8, 0.8, 1.0 };
- GLfloat mat_shininess[] = { 10.0 };
-
- GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
-
- glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
- glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
-
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- glEnable(GL_LIGHT0);
-
- glEnable(GL_LIGHTING);
-
- glClearColor( 0, 0, 0, 1 );
-
- glEnable( GL_DEPTH_TEST );
- }
-
- GLvoid
- mouse( GLint button, GLint state, GLint x, GLint y )
- {
- switch (button) {
- case GLUT_LEFT_BUTTON:
- if (state == GLUT_DOWN) {
- grabImage();
- }
- break;
- }
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
-
- glViewport( 0, 0, width, height );
-
- winWidth = width;
- winHeight = height;
-
- aspect = (GLdouble) width / (GLdouble) height;
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( 45.0, aspect, 3.0, 13.0 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- glTranslatef( 0.0, 0.0, -8.0 );
- }
-
- GLvoid
- grabImage( void )
- {
- GLuint *image;
-
- /* allocate space for the pixel data we're going to read
- * each pixel consists of four 1-byte components (R, G, B and A) */
- image = (GLuint *) malloc( winWidth * winHeight * 4 );
-
- glReadPixels( 0, 0, winWidth, winHeight, GL_RGBA, GL_UNSIGNED_BYTE,
- image );
-
- rgbWriteImageFile( imageFileName, winWidth, winHeight, image );
-
- exit(0);
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- int i, slices = 8;
-
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
-
- glColorMaterial( GL_FRONT, GL_AMBIENT_AND_DIFFUSE );
- glEnable( GL_COLOR_MATERIAL );
- for ( i = 0; i < slices; i++ )
- {
- glColor3f( i/10.0, i/10.0, 1.0 - i/10.0 );
- glPushMatrix();
- glRotatef( i * 360.0/slices, 0, 0, 1 );
- glTranslatef( 1.5, 0.0, 0.0 );
- glRotatef( i * 360.0/slices, 0, 1, 0 );
- glutSolidTorus( 0.25, 0.75, 8, 16 );
- glPopMatrix();
- }
- glDisable( GL_COLOR_MATERIAL );
- glFlush();
- }
-
-