home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / newopg.zip / PICKDPTH.C < prev    next >
Text File  |  1995-03-04  |  6KB  |  179 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.  *  pickdepth.c
  39.  *  Picking is demonstrated in this program.  In 
  40.  *  rendering mode, three overlapping rectangles are 
  41.  *  drawn.  When the left mouse button is pressed, 
  42.  *  selection mode is entered with the picking matrix.  
  43.  *  Rectangles which are drawn under the cursor position
  44.  *  are "picked."  Pay special attention to the depth 
  45.  *  value range, which is returned.
  46.  */
  47. #include <GL/gl.h>
  48. #include <GL/glu.h>
  49. #include "aux.h"
  50.  
  51. void myinit(void)
  52. {
  53.     glClearColor (0.0, 0.0, 0.0, 0.0);
  54.     glDepthFunc(GL_LESS);
  55.     glEnable(GL_DEPTH_TEST);
  56.     glShadeModel(GL_FLAT);
  57.     glDepthRange (0.0, 1.0);    /*  The default z mapping    */
  58. }
  59.  
  60. /*  The three rectangles are drawn.  In selection mode, 
  61.  *  each rectangle is given the same name.  Note that 
  62.  *  each rectangle is drawn with a different z value.
  63.  */
  64. void drawRects(GLenum mode)
  65. {
  66.     if (mode == GL_SELECT)
  67.     glLoadName (1);
  68.     glBegin (GL_QUADS);
  69.     glColor3f (1.0, 1.0, 0.0);
  70.     glVertex3i (2, 0, 0);
  71.     glVertex3i (2, 6, 0);
  72.     glVertex3i (6, 6, 0);
  73.     glVertex3i (6, 0, 0);
  74.     glColor3f (0.0, 1.0, 1.0);
  75.     glVertex3i (3, 2, -1);
  76.     glVertex3i (3, 8, -1);
  77.     glVertex3i (8, 8, -1);
  78.     glVertex3i (8, 2, -1);
  79.     glColor3f (1.0, 0.0, 1.0);
  80.     glVertex3i (0, 2, -2);
  81.     glVertex3i (0, 7, -2);
  82.     glVertex3i (5, 7, -2);
  83.     glVertex3i (5, 2, -2);
  84.     glEnd ();
  85. }
  86.  
  87. /*  processHits() prints out the contents of the 
  88.  *  selection array.
  89.  */
  90. void processHits (GLint hits, GLuint buffer[])
  91. {
  92.     unsigned int i, j;
  93.     GLuint names, *ptr;
  94.  
  95.     printf ("hits = %d\n", hits);
  96.     ptr = (GLuint *) buffer;
  97.     for (i = 0; i < hits; i++) {    /*  for each hit  */
  98.     names = *ptr;
  99.     printf (" number of names for hit = %d\n", names); ptr++;
  100.     printf ("  z1 is %u;", *ptr); ptr++;
  101.     printf (" z2 is %u\n", *ptr); ptr++;
  102.     printf ("   the name is ");
  103.     for (j = 0; j < names; j++) {    /*  for each name */
  104.         printf ("%d ", *ptr); ptr++;
  105.     }
  106.     printf ("\n");
  107.     }
  108. }
  109.  
  110. /*  pickRects() sets up selection mode, name stack, 
  111.  *  and projection matrix for picking.  Then the objects 
  112.  *  are drawn.
  113.  */
  114. #define BUFSIZE 512
  115.  
  116. void pickRects(AUX_EVENTREC *event)
  117. {
  118.     GLuint selectBuf[BUFSIZE];
  119.     GLint hits;
  120.     GLint viewport[4];
  121.     int x, y;
  122.  
  123.     x = event->data[AUX_MOUSEX];
  124.     y = event->data[AUX_MOUSEY];
  125.     glGetIntegerv (GL_VIEWPORT, viewport);
  126.  
  127.     glSelectBuffer (BUFSIZE, selectBuf);
  128.     (void) glRenderMode (GL_SELECT);
  129.  
  130.     glInitNames();
  131.     glPushName(-1);
  132.  
  133.     glMatrixMode (GL_PROJECTION);
  134.     glPushMatrix ();
  135.     glLoadIdentity ();
  136. /*  create 5x5 pixel picking region near cursor location    */
  137.     gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y), 
  138.     5.0, 5.0, viewport);
  139.     glOrtho (0.0, 8.0, 0.0, 8.0, -0.5, 2.5);
  140.     drawRects (GL_SELECT);
  141.     glPopMatrix ();
  142.     glFlush ();
  143.  
  144.     hits = glRenderMode (GL_RENDER);
  145.     processHits (hits, selectBuf);
  146.  
  147. void display(void)
  148. {
  149.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  150.     drawRects (GL_RENDER);
  151.     glFlush();
  152. }
  153.  
  154. void myReshape(GLint w, GLint h)
  155. {
  156.     glViewport(0, 0, w, h);
  157.     glMatrixMode(GL_PROJECTION);
  158.     glLoadIdentity();
  159.     glOrtho (0.0, 8.0, 0.0, 8.0, 0.0, 2.0);
  160.     glMatrixMode(GL_MODELVIEW);
  161.     glLoadIdentity();
  162. }
  163.  
  164. /*  Main Loop
  165.  *  Open window with initial window size, title bar, 
  166.  *  RGBA display mode, depth buffer, and handle input events.
  167.  */
  168. int main(int argc, char** argv)
  169. {
  170.     auxInitDisplayMode (AUX_SINGLE | AUX_RGB | AUX_DEPTH);
  171.     auxInitPosition (0, 0, 100, 100);
  172.     auxInitWindow (argv[0]);
  173.     myinit ();
  174.     auxMouseFunc (AUX_LEFTBUTTON, AUX_MOUSEDOWN, pickRects);
  175.     auxReshapeFunc (myReshape);
  176.     auxMainLoop(display);
  177. }
  178.