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