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