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