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 / rasterops / copyImage.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  7.1 KB  |  303 lines

  1. /*
  2.  * Copyright 1995, 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. /* copyImage.c
  19.  *    A program to demonstrate the glCopyPixels() routine.
  20.  *    It copies the pixels under the cursor when the Left Mouse 
  21.  *    Button is pressed
  22.  * 
  23.  *    Escape key            - exit the program
  24.  *    Left Mousebutton, down        - set origin of copy
  25.  *    Middle Mousebutton, down    - expand copy area
  26.  *    Right Mousebutton, down        - shrink copy area
  27.  */
  28. #include <GL/gl.h>
  29. #include <GL/glu.h>
  30. #include <GL/glut.h>
  31.  
  32. #include <math.h>
  33. #include <stdio.h>
  34.  
  35. /*  Function Prototypes  */
  36.  
  37. GLvoid  initgfx( GLvoid );
  38. GLvoid  drawScene( GLvoid );
  39. GLvoid  reshape( GLsizei, GLsizei );
  40. GLvoid  mouse( GLint, GLint, GLint, GLint );
  41. GLvoid  keyboard( GLubyte, GLint, GLint );
  42.  
  43. GLvoid    copyImage( GLvoid );
  44. GLvoid     outlineCopyArea( GLvoid );
  45.  
  46. void  printHelp( char * );
  47.  
  48. /* Global Definitions */
  49.  
  50. #define KEY_ESC    27    /* ascii value for the escape key */
  51.  
  52. /* Global Variables */
  53.  
  54. static GLsizei    winWidth, winHeight;
  55. static GLfloat    viewFactor = 3.0;
  56. static GLint    xPos, yPos, midX;
  57. static GLint    xToCopy,  yToCopy;
  58. static GLsizei    widthToCopy, heightToCopy;
  59.  
  60. GLvoid
  61. main( int argc, char *argv[] )
  62. {
  63.     GLsizei width, height, winSize;
  64.  
  65.     glutInit( &argc, argv );
  66.  
  67.     width = glutGet(GLUT_SCREEN_WIDTH); 
  68.     height = glutGet(GLUT_SCREEN_HEIGHT);
  69.     winSize = ( width < height ) ? width : height;
  70.     winWidth = (winSize & 0x1 ? winSize : winSize - 1);
  71.     midX = winWidth/2;
  72.     winHeight = winSize/2;
  73.     xPos = yPos = winSize/4;
  74.  
  75.     glutInitWindowPosition( xPos, yPos ); 
  76.     glutInitWindowSize( winWidth, winHeight );
  77.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  78.     glutCreateWindow( argv[0] );
  79.     
  80.     initgfx();
  81.  
  82.     glutMouseFunc( mouse );
  83.     glutKeyboardFunc( keyboard );
  84.     glutReshapeFunc( reshape );
  85.     glutDisplayFunc( drawScene ); 
  86.  
  87.     printHelp( argv[0] );
  88.         
  89.     glutMainLoop();
  90. }
  91.  
  92. GLvoid
  93. printHelp( char *progname )
  94. {
  95.     fprintf(stdout, "\n%s - copies the pixels under\n"\
  96.         "the cursor when Left Mousebutton is pressed\n\n"\
  97.         "Escape key                - exit the program\n"\
  98.         "Left Mousebutton, down            - set copy origin\n"\
  99.         "Middle Mousebutton, down        - expand copy area\n"\
  100.         "Right Mousebutton, down        - shrink copy area\n\n",
  101.         progname);
  102. }
  103.  
  104. GLvoid
  105. initgfx( void )
  106. {
  107.  
  108.     GLfloat mat_specular[] = { 0.8, 0.8, 0.8, 1.0 };
  109.     GLfloat mat_shininess[] = { 20.0 };
  110.  
  111.     GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  112.  
  113.     glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
  114.  
  115.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  116.     glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  117.  
  118.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  119.  
  120.     glEnable(GL_LIGHTING);
  121.     glEnable(GL_LIGHT0);
  122.  
  123.     glClearColor( 0, 0, 0, 1 );
  124. }
  125.  
  126. GLvoid 
  127. mouse( GLint button, GLint state, GLint x, GLint y )
  128. {
  129.     if (state == GLUT_DOWN) {
  130.         switch (button) {
  131.         case GLUT_LEFT_BUTTON:
  132.             /* set origin of copy area */
  133.             xPos = x;
  134.             yPos = winHeight - y;
  135.             break;
  136.         case GLUT_MIDDLE_BUTTON:
  137.             /* expand the copy area */
  138.             viewFactor -= 0.50;
  139.             if (viewFactor < 1.0 )
  140.                 viewFactor = 1.0;
  141.             break;
  142.         case GLUT_RIGHT_BUTTON:
  143.             /* shrink the copy area */
  144.             viewFactor += 0.50;
  145.             break;
  146.         }
  147.  
  148.         glutPostRedisplay();
  149.     }
  150. }
  151.  
  152. GLvoid 
  153. keyboard( GLubyte key, GLint x, GLint y )
  154. {
  155.     switch (key) {
  156.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  157.         exit(0);
  158.     }
  159. }
  160.  
  161. GLvoid
  162. reshape( GLsizei width, GLsizei height )
  163. {
  164.     GLfloat    aspect;
  165.  
  166.     /* make sure window size is odd */
  167.     winWidth = (width & 0x1 ? width : width - 1);
  168.     midX = winWidth/2;
  169.     winHeight = height;
  170.  
  171.     glViewport( 0, 0, midX + 1, height );
  172.  
  173.     aspect = (GLfloat) (width/2.0) / height;
  174.  
  175.     glMatrixMode( GL_PROJECTION );
  176.     glLoadIdentity();
  177.     gluPerspective( 45.0, aspect, 3.0, 13.0 );
  178.     glMatrixMode( GL_MODELVIEW );
  179.     glLoadIdentity();
  180.     glTranslatef( 0.0, 0.0, -8.0 ); 
  181. }
  182.  
  183. /* outlineCopyArea() calculates size of area to be copied and 
  184.  *    outlines it in red.
  185.  */
  186. GLvoid
  187. outlineCopyArea( GLvoid )
  188. {
  189.     /* calculate size of area to copy to the right half of the
  190.      * window
  191.      */
  192.     xToCopy = xPos - (midX-2)/(2.0*viewFactor); 
  193.     yToCopy = yPos - (winHeight-1)/(2.0*viewFactor); 
  194.     widthToCopy = (midX-2)/viewFactor; 
  195.     heightToCopy = (winHeight-1)/viewFactor; 
  196.  
  197.     glColor3f( 1.0,  0.0,  0.0 );
  198.         glBegin( GL_LINE_LOOP );
  199.             glVertex2i( xToCopy, yToCopy );
  200.             glVertex2i( xToCopy + widthToCopy, yToCopy );
  201.             glVertex2i( xToCopy + widthToCopy, 
  202.                 yToCopy + heightToCopy );
  203.             glVertex2i( xToCopy, yToCopy + heightToCopy );
  204.         glEnd();
  205. }
  206.  
  207. /* copyImage() copies pixels from viewport on left half of window
  208.  */
  209. GLvoid
  210. copyImage( GLvoid )
  211. {
  212.     /* Create the second viewport - the right half of the window */
  213.     glViewport( midX + 1, 0, midX - 1, winHeight );
  214.  
  215.     /* adjust for values out of the viewport */
  216.     if (xToCopy < 0) {
  217.         widthToCopy += xToCopy;
  218.         xToCopy = 0; 
  219.     }
  220.     if (yToCopy < 0) {
  221.         heightToCopy += yToCopy;
  222.         yToCopy = 0; 
  223.     }
  224.  
  225.     /* copy pixels */
  226.     glRasterPos2f(xToCopy, yToCopy);
  227.     glCopyPixels (xToCopy, yToCopy, 
  228.             widthToCopy + 1, heightToCopy + 1, GL_COLOR );
  229. }
  230.     
  231. GLvoid
  232. drawScene( GLvoid )
  233. {
  234.     int i,  slices = 8;
  235.     static GLfloat spin = 0.0;
  236.  
  237.     /* Create the first viewport - the left half of the window */
  238.     glViewport( 0, 0, midX + 1, winHeight );
  239.  
  240.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  241.     
  242.     glEnable( GL_LIGHTING );
  243.     glEnable( GL_DEPTH_TEST );
  244.     glEnable( GL_COLOR_MATERIAL );
  245.  
  246.     glPushMatrix();
  247.     glRotatef( spin, 0, 1, 0 );
  248.  
  249.     for ( i = 0; i < slices; i++ )
  250.     {
  251.         glColor3f( i/10.0, i/10.0, 1.0 - i/10.0 ); 
  252.         glPushMatrix(); 
  253.             glRotatef( i * 360.0/slices, 0, 0, 1 );
  254.             glTranslatef( 1.5, 0.0, 0.0 );
  255.             glRotatef( i * 360.0/slices, 0, 1, 0 );
  256.             glutSolidTorus( 0.25, 0.75, 16, 31 );
  257.         glPopMatrix();
  258.     }
  259.     glPopMatrix();
  260.     glDisable( GL_COLOR_MATERIAL );
  261.     glDisable( GL_DEPTH_TEST );
  262.     glDisable( GL_LIGHTING );
  263.  
  264.     glMatrixMode( GL_PROJECTION );
  265.     glPushMatrix();
  266.         /* map world coords directly to window coords */
  267.         glLoadIdentity();
  268.         gluOrtho2D( 0.0, midX + 1.0, 0.0, winHeight );
  269.  
  270.         glMatrixMode( GL_MODELVIEW );
  271.         glPushMatrix();
  272.             glLoadIdentity();
  273.             glTranslatef( 0.375, 0.375, 0.0 );
  274.  
  275.             /* draw red box around sub image to copy */
  276.             outlineCopyArea();
  277.  
  278.             /* draw white divider line between view halves */
  279.             glColor3f( 1.0,  1.0,  1.0 );
  280.             glBegin( GL_LINES );
  281.                 glVertex2i( midX, 0 );
  282.                 glVertex2i( midX, winHeight);
  283.             glEnd();
  284.  
  285.             /* use glFinish() to ensure that image is 
  286.              * rendered before copyImage() is called
  287.              */
  288.             glFinish();
  289.  
  290.             /* copy the image in the box to right half */
  291.             copyImage();
  292.  
  293.         /* pop modelview matrix */
  294.         glPopMatrix();
  295.  
  296.     glMatrixMode( GL_PROJECTION );
  297.     glPopMatrix();
  298.     glMatrixMode( GL_MODELVIEW );
  299.     
  300.     glutSwapBuffers();
  301.     spin = fmodf( spin + 2.0, 360.0 );
  302. }
  303.