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 / pan.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  5.0 KB  |  222 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. /* pan.c
  19.  *    This program takes the name of an image file in SGI .rgb 
  20.  *    format. It opens the file and creates a window displaying 
  21.  *    a subimage taken from the middle of the image.
  22.  *
  23.  *    The image can be panned by dragging the mouse with the
  24.  *    Left Mouse button pressed.
  25.  */
  26. #include <GL/gl.h>
  27. #include <GL/glu.h>
  28. #include <GL/glut.h>
  29.  
  30. #include <math.h>
  31. #include <stdio.h>
  32. #include "rgbImageFile.h"    /* should be in ../../include */
  33.  
  34. /*  Function Prototypes  */
  35.  
  36. GLvoid  initgfx( GLvoid );
  37. GLvoid  drawScene( GLvoid );
  38. GLvoid  reshape( GLsizei, GLsizei );
  39. GLvoid  keyboard( GLubyte, GLint, GLint );
  40. GLvoid  mouse( GLint, GLint, GLint, GLint );
  41. GLvoid  motion( GLint, GLint );
  42.  
  43. GLvoid    adjustSkipParams( GLvoid );
  44.  
  45. void printHelp( char * );
  46.  
  47. /* Global Definitions */
  48.  
  49. #define KEY_ESC    27    /* ascii value for the escape key */
  50.  
  51. /* Global Variables */
  52.  
  53. GLuint        *image;
  54. GLint        skipRows, skipPixels;
  55. GLint        xStart, yStart;
  56.  
  57. GLint        imageWidth, imageHeight;
  58. GLint        subimageWidth, subimageHeight;
  59.  
  60. GLboolean    panning = GL_FALSE;
  61.  
  62. GLvoid
  63. main ( int argc, char *argv[])
  64. {
  65.     GLsizei width, height;
  66.  
  67.     glutInit( &argc, argv );
  68.  
  69.     if (argc < 2) {
  70.         fprintf (stderr, "usage: %s <imageFileName>\n", 
  71.             argv[0] );
  72.         exit (1);
  73.     }
  74.     
  75.     image = rgbReadImageFile(argv[1], &imageWidth, &imageHeight);
  76.  
  77.     /* set up the window size and the sub-image size 
  78.      * to be 1/4 of the original image size.
  79.      */
  80.     subimageWidth = imageWidth/2;
  81.     subimageHeight = imageHeight/2;
  82.  
  83.     glutInitWindowPosition( 100, 100 );
  84.     glutInitWindowSize( subimageWidth, subimageHeight );
  85.     glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
  86.     glutCreateWindow( argv[0] );
  87.  
  88.     initgfx();
  89.  
  90.     glutMouseFunc( mouse );
  91.     glutMotionFunc( motion );
  92.     glutKeyboardFunc( keyboard );
  93.     glutReshapeFunc( reshape );
  94.     glutDisplayFunc( drawScene ); 
  95.  
  96.     printHelp( argv[0] );
  97.  
  98.     glutMainLoop();
  99. }
  100.  
  101. GLvoid
  102. printHelp( char *progname )
  103. {
  104.     fprintf(stdout, 
  105.         "\n%s, a program to pan an image\n\n"\
  106.         "Escape key            - exit the program\n"\
  107.         "Left Mousebutton, down        - pan image\n\n", 
  108.         progname);
  109. }
  110.  
  111. GLvoid
  112. initgfx( void )
  113. {
  114.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  115. }
  116.  
  117. GLvoid 
  118. keyboard( GLubyte key, GLint x, GLint y )
  119. {
  120.     switch (key) {
  121.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  122.         exit(0);
  123.     }
  124. }
  125.  
  126. GLvoid
  127. reshape( GLsizei width, GLsizei height )
  128. {
  129.     glViewport( 0, 0, width, height);
  130.  
  131.     glMatrixMode( GL_PROJECTION );
  132.     glLoadIdentity();
  133.     gluOrtho2D( 0.0, (GLdouble) width, 0.0, (GLdouble) height );
  134.     glMatrixMode( GL_MODELVIEW );
  135.     glLoadIdentity();
  136.     glTranslatef( 0.375, 0.375, 0.0 );
  137.  
  138.     subimageWidth = width;
  139.     subimageHeight = height;
  140.     if (subimageWidth > imageWidth)
  141.         subimageWidth = imageWidth;    
  142.     if (subimageHeight > imageHeight)
  143.         subimageHeight = imageHeight;    
  144. }
  145.  
  146. GLvoid 
  147. mouse( GLint button, GLint state, GLint x, GLint y )
  148. {
  149.     switch (button) {
  150.     case GLUT_LEFT_BUTTON:
  151.         if (state == GLUT_DOWN) {
  152.             xStart = x; /* save current mouse position */
  153.             yStart = y;
  154.             panning = GL_TRUE;
  155.         } else {
  156.             panning = GL_FALSE;
  157.         }
  158.         break;
  159.     }
  160. }
  161.  
  162. GLvoid
  163. motion( int x, int y )
  164. {
  165.     if (panning) {
  166.         /* adjust skip info based on how far the mouse moved */
  167.         skipPixels -= (x - xStart);
  168.         skipRows += (y - yStart);
  169.  
  170.         
  171.         xStart = x;    /* Update the stored mouse position */
  172.         yStart = y;
  173.  
  174.         glutPostRedisplay();
  175.     }
  176. }
  177.  
  178. /* make sure the selected rectangle is always
  179.  * completely inside the original image    
  180.  */
  181. GLvoid
  182. adjustSkipParams( GLvoid )
  183. {            
  184.     if (skipPixels < 0)
  185.         skipPixels = 0;
  186.     if (skipPixels > imageWidth-subimageWidth)
  187.         skipPixels = imageWidth-subimageWidth;
  188.  
  189.     if (skipRows < 0)
  190.         skipRows = 0;
  191.     if (skipRows >= imageHeight-subimageHeight)
  192.         skipRows = imageHeight-subimageHeight;
  193.  
  194. GLvoid
  195. drawScene( GLvoid )
  196. {
  197.     glClear( GL_COLOR_BUFFER_BIT );
  198.  
  199.     adjustSkipParams ();
  200.  
  201.     /* Set up to grab a portion of the original stored image. */
  202.  
  203.     /* Set the pixel width of the original image. */
  204.     glPixelStorei( GL_UNPACK_ROW_LENGTH, imageWidth );
  205.  
  206.     /* Indicate how many pixels to skip in the x and y directions
  207.      * (from the lower left corner of the original image) */
  208.     glPixelStorei( GL_UNPACK_SKIP_PIXELS, skipPixels );
  209.     glPixelStorei( GL_UNPACK_SKIP_ROWS, skipRows );
  210.  
  211.     /* In this example, the xy coordinates to map directly 
  212.      * to window coordinates.  This would not be the case if any
  213.      * projection or modeling transforms had been applied.
  214.      */
  215.     glRasterPos2i( 0,  0 );
  216.     glDrawPixels (subimageWidth, subimageHeight, GL_RGBA,
  217.              GL_UNSIGNED_BYTE, image);
  218.  
  219.     glutSwapBuffers();
  220. }
  221.