select.c revision 32001f49
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 "glut_wrap.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 */ 60static 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 */ 71static 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 */ 104static 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 */ 125static void processHits (GLint hits, GLuint buffer[]) 126{ 127 GLint i; 128 GLuint j, 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 153static 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 184static void init (void) 185{ 186 glEnable(GL_DEPTH_TEST); 187 glShadeModel(GL_FLAT); 188} 189 190static 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/* ARGSUSED1 */ 200static void keyboard(unsigned char key, int x, int y) 201{ 202 switch (key) { 203 case 27: 204 exit(0); 205 break; 206 } 207} 208 209/* Main Loop */ 210int main(int argc, char** argv) 211{ 212 glutInit(&argc, argv); 213 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); 214 glutInitWindowSize (200, 200); 215 glutInitWindowPosition (100, 100); 216 glutCreateWindow (argv[0]); 217 init(); 218 glutDisplayFunc(display); 219 glutKeyboardFunc(keyboard); 220 glutMainLoop(); 221 return 0; 222} 223