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