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