home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / System / Mesa-3.1 / Mesa-aux / book.aux / picksquare.c < prev    next >
C/C++ Source or Header  |  1999-06-25  |  6KB  |  210 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 <stdio.h>
  47. #include <stdlib.h>
  48. #include <GL/glaux.h>
  49.  
  50. int board[3][3];                                   /*
  51.  
  52.                                             * amount of color for each square      
  53.                                             */
  54.  
  55. /*
  56.  * Clear color value for every square on the board          
  57.  */
  58. void myinit(void)
  59. {
  60.   int i, j;
  61.  
  62.   for (i = 0; i < 3; i++)
  63.     for (j = 0; j < 3; j++)
  64.       board[i][j] = 0;
  65.   glClearColor(0.0, 0.0, 0.0, 0.0);
  66. }
  67.  
  68. /*
  69.  * The nine squares are drawn.  In selection mode, each 
  70.  * *  square is given two names:  one for the row and the 
  71.  * *  other for the column on the grid.  The color of each 
  72.  * *  square is determined by its position on the grid, and 
  73.  * *  the value in the board[][] array.
  74.  */
  75. void drawSquares(GLenum mode)
  76. {
  77.   GLuint i, j;
  78.  
  79.   for (i = 0; i < 3; i++) {
  80.     if (mode == GL_SELECT)
  81.       glLoadName(i);
  82.     for (j = 0; j < 3; j++) {
  83.       if (mode == GL_SELECT)
  84.     glPushName(j);
  85.       glColor3f((GLfloat) i / 3.0, (GLfloat) j / 3.0,
  86.         (GLfloat) board[i][j] / 3.0);
  87.       glRecti(i, j, i + 1, j + 1);
  88.       if (mode == GL_SELECT)
  89.     glPopName();
  90.     }
  91.   }
  92. }
  93.  
  94. /*
  95.  * processHits() prints out the contents of the 
  96.  * *  selection array.
  97.  */
  98. void processHits(GLint hits, GLuint buffer[])
  99. {
  100.   unsigned int i, j;
  101.   GLuint ii, jj, names, *ptr;
  102.  
  103.   printf("hits = %d\n", hits);
  104.   ptr = (GLuint *) buffer;
  105.   for (i = 0; i < hits; i++) {                               /*
  106.                                             * for each hit  
  107.                                             */
  108.     names = *ptr;
  109.     printf(" number of names for this hit = %d\n", names);
  110.     ptr++;
  111.     printf("  z1 is %u;", *ptr);
  112.     ptr++;
  113.     printf(" z2 is %u\n", *ptr);
  114.     ptr++;
  115.     printf("   names are ");
  116.     for (j = 0; j < names; j++) {                           /*
  117.                                             * for each name 
  118.                                             */
  119.       printf("%d ", *ptr);
  120.       if (j == 0)                                   /*
  121.                                             * set row and column  
  122.                                             */
  123.     ii = *ptr;
  124.       else if (j == 1)
  125.     jj = *ptr;
  126.       ptr++;
  127.     }
  128.     printf("\n");
  129.     board[ii][jj] = (board[ii][jj] + 1) % 3;
  130.   }
  131. }
  132.  
  133. /*
  134.  * pickSquares() sets up selection mode, name stack, 
  135.  * *  and projection matrix for picking.  Then the 
  136.  * *  objects are drawn.
  137.  */
  138. #define BUFSIZE 512
  139.  
  140. void pickSquares(AUX_EVENTREC * event)
  141. {
  142.   GLuint selectBuf[BUFSIZE];
  143.   GLint hits;
  144.   GLint viewport[4];
  145.   int x, y;
  146.  
  147.   x = event->data[AUX_MOUSEX];
  148.   y = event->data[AUX_MOUSEY];
  149.   glGetIntegerv(GL_VIEWPORT, viewport);
  150.  
  151.   glSelectBuffer(BUFSIZE, selectBuf);
  152.   (void)glRenderMode(GL_SELECT);
  153.  
  154.   glInitNames();
  155.   glPushName(-1);
  156.  
  157.   glMatrixMode(GL_PROJECTION);
  158.   glPushMatrix();
  159.   glLoadIdentity();
  160. /*
  161.  * create 5x5 pixel picking region near cursor location 
  162.  */
  163.   gluPickMatrix((GLdouble) x, (GLdouble) (viewport[3] - y),
  164.         5.0, 5.0, viewport);
  165.   gluOrtho2D(0.0, 3.0, 0.0, 3.0);
  166.   drawSquares(GL_SELECT);
  167.  
  168.   glMatrixMode(GL_PROJECTION);
  169.   glPopMatrix();
  170.   glFlush();
  171.  
  172.   hits = glRenderMode(GL_RENDER);
  173.   processHits(hits, selectBuf);
  174. }
  175.  
  176. void display(void)
  177. {
  178.   glClear(GL_COLOR_BUFFER_BIT);
  179.   drawSquares(GL_RENDER);
  180.   glFlush();
  181. }
  182.  
  183. void myReshape(int w, int h)
  184. {
  185.   glViewport(0, 0, w, h);
  186.   glMatrixMode(GL_PROJECTION);
  187.   glLoadIdentity();
  188.   gluOrtho2D(0.0, 3.0, 0.0, 3.0);
  189.   glMatrixMode(GL_MODELVIEW);
  190.   glLoadIdentity();
  191. }
  192.  
  193. /*
  194.  * Main Loop
  195.  * *  Open window with initial window size, title bar, 
  196.  * *  RGBA display mode, and handle input events.
  197.  */
  198. int main(int argc, char **argv)
  199. {
  200.   auxInitDisplayMode(AUX_SINGLE | AUX_RGB);
  201.   auxInitPosition(0, 0, 100, 100);
  202.   if (!auxInitWindow(argv[0]))
  203.     auxQuit();
  204.   myinit();
  205.   auxMouseFunc(AUX_LEFTBUTTON, AUX_MOUSEDOWN, pickSquares);
  206.   auxReshapeFunc(myReshape);
  207.   auxMainLoop(display);
  208.   return 0;
  209. }
  210.