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.
- */
-
- /* copyImage.c
- * A program to demonstrate the glCopyPixels() routine.
- * It copies the pixels under the cursor when the Left Mouse
- * Button is pressed
- *
- * Escape key - exit the program
- * Left Mousebutton, down - set origin of copy
- * Middle Mousebutton, down - expand copy area
- * Right Mousebutton, down - shrink copy area
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <math.h>
- #include <stdio.h>
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid mouse( GLint, GLint, GLint, GLint );
- GLvoid keyboard( GLubyte, GLint, GLint );
-
- GLvoid copyImage( GLvoid );
- GLvoid outlineCopyArea( GLvoid );
-
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- static GLsizei winWidth, winHeight;
- static GLfloat viewFactor = 3.0;
- static GLint xPos, yPos, midX;
- static GLint xToCopy, yToCopy;
- static GLsizei widthToCopy, heightToCopy;
-
- GLvoid
- main( int argc, char *argv[] )
- {
- GLsizei width, height, winSize;
-
- glutInit( &argc, argv );
-
- width = glutGet(GLUT_SCREEN_WIDTH);
- height = glutGet(GLUT_SCREEN_HEIGHT);
- winSize = ( width < height ) ? width : height;
- winWidth = (winSize & 0x1 ? winSize : winSize - 1);
- midX = winWidth/2;
- winHeight = winSize/2;
- xPos = yPos = winSize/4;
-
- glutInitWindowPosition( xPos, yPos );
- glutInitWindowSize( winWidth, winHeight );
- glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutMouseFunc( mouse );
- glutKeyboardFunc( keyboard );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- GLvoid
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - copies the pixels under\n"\
- "the cursor when Left Mousebutton is pressed\n\n"\
- "Escape key - exit the program\n"\
- "Left Mousebutton, down - set copy origin\n"\
- "Middle Mousebutton, down - expand copy area\n"\
- "Right Mousebutton, down - shrink copy area\n\n",
- progname);
- }
-
- GLvoid
- initgfx( void )
- {
-
- GLfloat mat_specular[] = { 0.8, 0.8, 0.8, 1.0 };
- GLfloat mat_shininess[] = { 20.0 };
-
- GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
-
- glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
-
- glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
- glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
-
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
-
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
-
- glClearColor( 0, 0, 0, 1 );
- }
-
- GLvoid
- mouse( GLint button, GLint state, GLint x, GLint y )
- {
- if (state == GLUT_DOWN) {
- switch (button) {
- case GLUT_LEFT_BUTTON:
- /* set origin of copy area */
- xPos = x;
- yPos = winHeight - y;
- break;
- case GLUT_MIDDLE_BUTTON:
- /* expand the copy area */
- viewFactor -= 0.50;
- if (viewFactor < 1.0 )
- viewFactor = 1.0;
- break;
- case GLUT_RIGHT_BUTTON:
- /* shrink the copy area */
- viewFactor += 0.50;
- break;
- }
-
- glutPostRedisplay();
- }
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case KEY_ESC: /* Exit whenever the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLfloat aspect;
-
- /* make sure window size is odd */
- winWidth = (width & 0x1 ? width : width - 1);
- midX = winWidth/2;
- winHeight = height;
-
- glViewport( 0, 0, midX + 1, height );
-
- aspect = (GLfloat) (width/2.0) / height;
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( 45.0, aspect, 3.0, 13.0 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- glTranslatef( 0.0, 0.0, -8.0 );
- }
-
- /* outlineCopyArea() calculates size of area to be copied and
- * outlines it in red.
- */
- GLvoid
- outlineCopyArea( GLvoid )
- {
- /* calculate size of area to copy to the right half of the
- * window
- */
- xToCopy = xPos - (midX-2)/(2.0*viewFactor);
- yToCopy = yPos - (winHeight-1)/(2.0*viewFactor);
- widthToCopy = (midX-2)/viewFactor;
- heightToCopy = (winHeight-1)/viewFactor;
-
- glColor3f( 1.0, 0.0, 0.0 );
- glBegin( GL_LINE_LOOP );
- glVertex2i( xToCopy, yToCopy );
- glVertex2i( xToCopy + widthToCopy, yToCopy );
- glVertex2i( xToCopy + widthToCopy,
- yToCopy + heightToCopy );
- glVertex2i( xToCopy, yToCopy + heightToCopy );
- glEnd();
- }
-
- /* copyImage() copies pixels from viewport on left half of window
- */
- GLvoid
- copyImage( GLvoid )
- {
- /* Create the second viewport - the right half of the window */
- glViewport( midX + 1, 0, midX - 1, winHeight );
-
- /* adjust for values out of the viewport */
- if (xToCopy < 0) {
- widthToCopy += xToCopy;
- xToCopy = 0;
- }
- if (yToCopy < 0) {
- heightToCopy += yToCopy;
- yToCopy = 0;
- }
-
- /* copy pixels */
- glRasterPos2f(xToCopy, yToCopy);
- glCopyPixels (xToCopy, yToCopy,
- widthToCopy + 1, heightToCopy + 1, GL_COLOR );
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- int i, slices = 8;
- static GLfloat spin = 0.0;
-
- /* Create the first viewport - the left half of the window */
- glViewport( 0, 0, midX + 1, winHeight );
-
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
-
- glEnable( GL_LIGHTING );
- glEnable( GL_DEPTH_TEST );
- glEnable( GL_COLOR_MATERIAL );
-
- glPushMatrix();
- glRotatef( spin, 0, 1, 0 );
-
- 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, 16, 31 );
- glPopMatrix();
- }
- glPopMatrix();
- glDisable( GL_COLOR_MATERIAL );
- glDisable( GL_DEPTH_TEST );
- glDisable( GL_LIGHTING );
-
- glMatrixMode( GL_PROJECTION );
- glPushMatrix();
- /* map world coords directly to window coords */
- glLoadIdentity();
- gluOrtho2D( 0.0, midX + 1.0, 0.0, winHeight );
-
- glMatrixMode( GL_MODELVIEW );
- glPushMatrix();
- glLoadIdentity();
- glTranslatef( 0.375, 0.375, 0.0 );
-
- /* draw red box around sub image to copy */
- outlineCopyArea();
-
- /* draw white divider line between view halves */
- glColor3f( 1.0, 1.0, 1.0 );
- glBegin( GL_LINES );
- glVertex2i( midX, 0 );
- glVertex2i( midX, winHeight);
- glEnd();
-
- /* use glFinish() to ensure that image is
- * rendered before copyImage() is called
- */
- glFinish();
-
- /* copy the image in the box to right half */
- copyImage();
-
- /* pop modelview matrix */
- glPopMatrix();
-
- glMatrixMode( GL_PROJECTION );
- glPopMatrix();
- glMatrixMode( GL_MODELVIEW );
-
- glutSwapBuffers();
- spin = fmodf( spin + 2.0, 360.0 );
- }
-