home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / newopg.zip / PICKBOX.C < prev    next >
Text File  |  1995-03-04  |  6KB  |  184 lines

  1. /*
  2.  * (c) Copyright 1993, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. /*
  38.  *  picksquare.c
  39.  *  Use of multiple names and picking are demonstrated.  
  40.  *  A 3x3 grid of squares is drawn.  When the left mouse 
  41.  *  button is pressed, all squares under the cursor position 
  42.  *  have their color changed.
  43.  */
  44. #include <GL/gl.h>
  45. #include <GL/glu.h>
  46. #include "aux.h"
  47.  
  48. int board[3][3];    /*  amount of color for each square    */
  49.  
  50. /*    Clear color value for every square on the board        */
  51. void myinit(void)
  52. {
  53.     int i, j;
  54.     for (i = 0; i < 3; i++) 
  55.     for (j = 0; j < 3; j ++)
  56.         board[i][j] = 0;
  57.     glClearColor (0.0, 0.0, 0.0, 0.0);
  58. }
  59.  
  60. /*  The nine squares are drawn.  In selection mode, each 
  61.  *  square is given two names:  one for the row and the 
  62.  *  other for the column on the grid.  The color of each 
  63.  *  square is determined by its position on the grid, and 
  64.  *  the value in the board[][] array.
  65.  */
  66. void drawSquares(GLenum mode)
  67. {
  68.     GLuint i, j;
  69.     for (i = 0; i < 3; i++) {
  70.     if (mode == GL_SELECT)
  71.         glLoadName (i);
  72.     for (j = 0; j < 3; j ++) {
  73.         if (mode == GL_SELECT)
  74.         glPushName (j);
  75.         glColor3f ((GLfloat) i/3.0, (GLfloat) j/3.0, 
  76.             (GLfloat) board[i][j]/3.0);
  77.         glRecti (i, j, i+1, j+1);
  78.         if (mode == GL_SELECT)
  79.         glPopName ();
  80.     }
  81.     }
  82. }
  83.  
  84. /*  processHits() prints out the contents of the 
  85.  *  selection array.
  86.  */
  87. void processHits (GLint hits, GLuint buffer[])
  88. {
  89.     unsigned int i, j;
  90.     GLuint ii, jj, names, *ptr;
  91.  
  92.     printf ("hits = %d\n", hits);
  93.     ptr = (GLuint *) buffer;
  94.     for (i = 0; i < hits; i++) {    /*  for each hit  */
  95.     names = *ptr;
  96.     printf (" number of names for this hit = %d\n", names);    ptr++;
  97.     printf ("  z1 is %u;", *ptr); ptr++;
  98.     printf (" z2 is %u\n", *ptr); ptr++;
  99.     printf ("   names are ");
  100.     for (j = 0; j < names; j++) {    /*  for each name */
  101.         printf ("%d ", *ptr);
  102.         if (j == 0)    /*  set row and column  */
  103.         ii = *ptr;
  104.         else if (j == 1)
  105.         jj = *ptr;
  106.         ptr++;
  107.     }
  108.     printf ("\n");
  109.     board[ii][jj] = (board[ii][jj] + 1) % 3;
  110.     }
  111. }
  112.  
  113. /*  pickSquares() sets up selection mode, name stack, 
  114.  *  and projection matrix for picking.  Then the 
  115.  *  objects are drawn.
  116.  */
  117. #define BUFSIZE 512
  118.  
  119. void pickSquares(AUX_EVENTREC *event)
  120. {
  121.     GLuint selectBuf[BUFSIZE];
  122.     GLint hits;
  123.     GLint viewport[4];
  124.     int x, y;
  125.  
  126.     x = event->data[AUX_MOUSEX];
  127.     y = event->data[AUX_MOUSEY];
  128.     glGetIntegerv (GL_VIEWPORT, viewport);
  129.  
  130.     glSelectBuffer (BUFSIZE, selectBuf);
  131.     (void) glRenderMode (GL_SELECT);
  132.  
  133.     glInitNames();
  134.     glPushName(-1);
  135.  
  136.     glMatrixMode (GL_PROJECTION);
  137.     glPushMatrix ();
  138.     glLoadIdentity ();
  139. /*  create 5x5 pixel picking region near cursor location    */
  140.     gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y), 
  141.     5.0, 5.0, viewport);
  142.     gluOrtho2D (0.0, 3.0, 0.0, 3.0);
  143.     drawSquares (GL_SELECT);
  144.  
  145.     glMatrixMode (GL_PROJECTION);
  146.     glPopMatrix ();
  147.     glFlush ();
  148.  
  149.     hits = glRenderMode (GL_RENDER);
  150.     processHits (hits, selectBuf);
  151.  
  152. void display(void)
  153. {
  154.     glClear(GL_COLOR_BUFFER_BIT);
  155.     drawSquares (GL_RENDER);
  156.     glFlush();
  157. }
  158.  
  159. void myReshape(GLint w, GLint h)
  160. {
  161.     glViewport(0, 0, w, h);
  162.     glMatrixMode(GL_PROJECTION);
  163.     glLoadIdentity();
  164.     gluOrtho2D (0.0, 3.0, 0.0, 3.0);
  165.     glMatrixMode(GL_MODELVIEW);
  166.     glLoadIdentity();
  167. }
  168.  
  169. /*  Main Loop
  170.  *  Open window with initial window size, title bar, 
  171.  *  RGBA display mode, and handle input events.
  172.  */
  173. int main(int argc, char** argv)
  174. {
  175.     auxInitDisplayMode (AUX_SINGLE | AUX_RGB);
  176.     auxInitPosition (0, 0, 100, 100);
  177.     auxInitWindow (argv[0]);
  178.     myinit ();
  179.     auxMouseFunc (AUX_LEFTBUTTON, AUX_MOUSEDOWN, pickSquares);
  180.     auxReshapeFunc (myReshape);
  181.     auxMainLoop(display);
  182. }
  183.