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