home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / opengl / auxdemo / select.c < prev    next >
C/C++ Source or Header  |  1999-05-11  |  7KB  |  212 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.  *  select.c
  39.  *  This is an illustration of the selection mode and 
  40.  *  name stack, which detects whether objects which collide 
  41.  *  with a viewing volume.  First, four triangles and a 
  42.  *  rectangular box representing a viewing volume are drawn 
  43.  *  (drawScene routine).  The green triangle and yellow 
  44.  *  triangles appear to lie within the viewing volume, but 
  45.  *  the red triangle appears to lie outside it.  Then the 
  46.  *  selection mode is entered (selectObjects routine).  
  47.  *  Drawing to the screen ceases.  To see if any collisions 
  48.  *  occur, the four triangles are called.  In this example, 
  49.  *  the green triangle causes one hit with the name 1, and 
  50.  *  the yellow triangles cause one hit with the name 3.
  51.  */
  52. #include <GL/gl.h>
  53. #include <GL/glu.h>
  54. #include <stdio.h>
  55. #include "aux.h"
  56.  
  57. /*    draw a triangle with vertices at (x1, y1), (x2, y2) 
  58.  *    and (x3, y3) at z units away from the origin.
  59.  */
  60. void drawTriangle (GLfloat x1, GLfloat y1, GLfloat x2, 
  61.     GLfloat y2, GLfloat x3, GLfloat y3, GLfloat z)
  62. {
  63.     glBegin (GL_TRIANGLES);
  64.     glVertex3f (x1, y1, z);
  65.     glVertex3f (x2, y2, z);
  66.     glVertex3f (x3, y3, z);
  67.     glEnd ();
  68. }
  69.  
  70. /*  draw a rectangular box with these outer x, y, and z values    */
  71. void drawViewVolume (GLfloat x1, GLfloat x2, GLfloat y1, 
  72.     GLfloat y2, GLfloat z1, GLfloat z2)
  73. {
  74.     glColor3f (1.0, 1.0, 1.0);
  75.     glBegin (GL_LINE_LOOP);
  76.     glVertex3f (x1, y1, -z1);
  77.     glVertex3f (x2, y1, -z1);
  78.     glVertex3f (x2, y2, -z1);
  79.     glVertex3f (x1, y2, -z1);
  80.     glEnd ();
  81.  
  82.     glBegin (GL_LINE_LOOP);
  83.     glVertex3f (x1, y1, -z2);
  84.     glVertex3f (x2, y1, -z2);
  85.     glVertex3f (x2, y2, -z2);
  86.     glVertex3f (x1, y2, -z2);
  87.     glEnd ();
  88.  
  89.     glBegin (GL_LINES);    /*  4 lines    */
  90.     glVertex3f (x1, y1, -z1);
  91.     glVertex3f (x1, y1, -z2);
  92.     glVertex3f (x1, y2, -z1);
  93.     glVertex3f (x1, y2, -z2);
  94.     glVertex3f (x2, y1, -z1);
  95.     glVertex3f (x2, y1, -z2);
  96.     glVertex3f (x2, y2, -z1);
  97.     glVertex3f (x2, y2, -z2);
  98.     glEnd ();
  99. }
  100.  
  101. /*  drawScene() draws 4 triangles and a wire frame
  102.  *  which represents the viewing volume.
  103.  */
  104. void drawScene (void)
  105. {
  106.     glMatrixMode (GL_PROJECTION);
  107.     glLoadIdentity ();
  108.     gluPerspective (40.0, 4.0/3.0, 0.01, 100.0);
  109.  
  110.     glMatrixMode (GL_MODELVIEW);
  111.     glLoadIdentity ();
  112.     gluLookAt (7.5, 7.5, 12.5, 2.5, 2.5, -5.0, 0.0, 1.0, 0.0);
  113.     glColor3f (0.0, 1.0, 0.0);    /*  green triangle    */
  114.     drawTriangle (2.0, 2.0, 3.0, 2.0, 2.5, 3.0, -5.0);
  115.     glColor3f (1.0, 0.0, 0.0);    /*  red triangle    */
  116.     drawTriangle (2.0, 7.0, 3.0, 7.0, 2.5, 8.0, -5.0);
  117.     glColor3f (1.0, 1.0, 0.0);    /*  yellow triangles    */
  118.     drawTriangle (2.0, 2.0, 3.0, 2.0, 2.5, 3.0, 0.0);
  119.     drawTriangle (2.0, 2.0, 3.0, 2.0, 2.5, 3.0, -10.0);
  120.     drawViewVolume (0.0, 5.0, 0.0, 5.0, 0.0, 10.0);
  121. }
  122.  
  123. /*  processHits() prints out the contents of the selection array.
  124.  */
  125. void processHits (GLint hits, GLuint buffer[])
  126. {
  127.     unsigned int i, j;
  128.     GLuint names, *ptr;
  129.  
  130.     printf ("hits = %d\n", hits);
  131.     ptr = (GLuint *) buffer;
  132.     for (i = 0; i < hits; i++) {    /*  for each hit  */
  133.     names = *ptr;
  134.     printf (" number of names for hit = %d\n", names); ptr++;
  135.     printf ("  z1 is %u;", *ptr); ptr++;
  136.     printf (" z2 is %u\n", *ptr); ptr++;
  137.     printf ("   the name is ");
  138.     for (j = 0; j < names; j++) {    /*  for each name */
  139.         printf ("%d ", *ptr); ptr++;
  140.     }
  141.     printf ("\n");
  142.     }
  143. }
  144.  
  145. /*  selectObjects() "draws" the triangles in selection mode, 
  146.  *  assigning names for the triangles.  Note that the third
  147.  *  and fourth triangles share one name, so that if either 
  148.  *  or both triangles intersects the viewing/clipping volume, 
  149.  *  only one hit will be registered.
  150.  */
  151. #define BUFSIZE 512
  152.  
  153. void selectObjects(void)
  154. {
  155.     GLuint selectBuf[BUFSIZE];
  156.     GLint hits, viewport[4];
  157.  
  158.     glSelectBuffer (BUFSIZE, selectBuf);
  159.     (void) glRenderMode (GL_SELECT);
  160.  
  161.     glInitNames();
  162.     glPushName(-1);
  163.  
  164.     glPushMatrix ();
  165.     glMatrixMode (GL_PROJECTION);
  166.     glLoadIdentity ();
  167.     glOrtho (0.0, 5.0, 0.0, 5.0, 0.0, 10.0);
  168.     glMatrixMode (GL_MODELVIEW);
  169.     glLoadIdentity ();
  170.     glLoadName(1);
  171.     drawTriangle (2.0, 2.0, 3.0, 2.0, 2.5, 3.0, -5.0);
  172.     glLoadName(2);
  173.     drawTriangle (2.0, 7.0, 3.0, 7.0, 2.5, 8.0, -5.0);
  174.     glLoadName(3);
  175.     drawTriangle (2.0, 2.0, 3.0, 2.0, 2.5, 3.0, 0.0);
  176.     drawTriangle (2.0, 2.0, 3.0, 2.0, 2.5, 3.0, -10.0);
  177.     glPopMatrix ();
  178.     glFlush ();
  179.  
  180.     hits = glRenderMode (GL_RENDER);
  181.     processHits (hits, selectBuf);
  182.  
  183. void myinit (void) 
  184. {
  185.     glDepthFunc(GL_LESS);
  186.     glEnable(GL_DEPTH_TEST);
  187.     glShadeModel(GL_FLAT);
  188. }
  189.  
  190. void display(void)
  191. {
  192.     glClearColor (0.0, 0.0, 0.0, 0.0);
  193.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  194.     drawScene ();
  195.     selectObjects ();
  196.     glFlush();
  197. }
  198.  
  199. /*  Main Loop
  200.  *  Open window with initial window size, title bar, 
  201.  *  RGBA display mode, depth buffer, and handle input events.
  202.  */
  203. int main(int argc, char** argv)
  204. {
  205.     auxInitDisplayMode (AUX_SINGLE | AUX_RGB | AUX_DEPTH);
  206.     auxInitPosition (0, 0, 200, 200);
  207.     auxInitWindow (argv[0]);
  208.     myinit ();
  209.     auxMainLoop(display);
  210. }
  211.