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