home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / System / Mesa-3.1 / book / select.c < prev    next >
C/C++ Source or Header  |  1998-02-14  |  7KB  |  223 lines

  1. /*
  2.  * Copyright (c) 1993-1997, 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(R) is a registered trademark of Silicon Graphics, Inc.
  36.  */
  37.  
  38. /*
  39.  * select.c
  40.  * This is an illustration of the selection mode and 
  41.  * name stack, which detects whether objects which collide 
  42.  * with a viewing volume.  First, four triangles and a 
  43.  * rectangular box representing a viewing volume are drawn 
  44.  * (drawScene routine).  The green triangle and yellow 
  45.  * triangles appear to lie within the viewing volume, but 
  46.  * the red triangle appears to lie outside it.  Then the 
  47.  * selection mode is entered (selectObjects routine).  
  48.  * Drawing to the screen ceases.  To see if any collisions 
  49.  * occur, the four triangles are called.  In this example, 
  50.  * the green triangle causes one hit with the name 1, and 
  51.  * the yellow triangles cause one hit with the name 3.
  52.  */
  53. #include <GL/glut.h>
  54. #include <stdlib.h>
  55. #include <stdio.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, 1.0, 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 %g;", (float) *ptr/0x7fffffff); ptr++;
  136.       printf(" z2 is %g\n", (float) *ptr/0x7fffffff); 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;
  157.  
  158.    glSelectBuffer (BUFSIZE, selectBuf);
  159.    (void) glRenderMode (GL_SELECT);
  160.  
  161.    glInitNames();
  162.    glPushName(0);
  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 init (void) 
  184. {
  185.    glEnable(GL_DEPTH_TEST);
  186.    glShadeModel(GL_FLAT);
  187. }
  188.  
  189. void display(void)
  190. {
  191.    glClearColor (0.0, 0.0, 0.0, 0.0);
  192.    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  193.    drawScene ();
  194.    selectObjects ();
  195.    glFlush();
  196. }
  197.  
  198. /* ARGSUSED1 */
  199. void keyboard(unsigned char key, int x, int y)
  200. {
  201.    switch (key) {
  202.       case 27:
  203.          exit(0);
  204.          break;
  205.    }
  206. }
  207.  
  208. /*  Main Loop  */
  209. int main(int argc, char** argv)
  210. {
  211.    glutInit(&argc, argv);
  212.    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  213.    glutInitWindowSize (200, 200);
  214.    glutInitWindowPosition (100, 100);
  215.    glutCreateWindow (argv[0]);
  216.    init();
  217.    glutDisplayFunc(display);
  218.    glutKeyboardFunc(keyboard);
  219.    glutMainLoop();
  220.    return 0; 
  221. }
  222.