pickdepth.c revision 32001f49
1 2/* Copyright (c) Mark J. Kilgard, 1994. */ 3 4/* 5 * (c) Copyright 1993, Silicon Graphics, Inc. 6 * ALL RIGHTS RESERVED 7 * Permission to use, copy, modify, and distribute this software for 8 * any purpose and without fee is hereby granted, provided that the above 9 * copyright notice appear in all copies and that both the copyright notice 10 * and this permission notice appear in supporting documentation, and that 11 * the name of Silicon Graphics, Inc. not be used in advertising 12 * or publicity pertaining to distribution of the software without specific, 13 * written prior permission. 14 * 15 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" 16 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, 17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR 18 * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON 19 * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT, 20 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY 21 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION, 22 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF 23 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN 24 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON 25 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE 26 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE. 27 * 28 * US Government Users Restricted Rights 29 * Use, duplication, or disclosure by the Government is subject to 30 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph 31 * (c)(1)(ii) of the Rights in Technical Data and Computer Software 32 * clause at DFARS 252.227-7013 and/or in similar or successor 33 * clauses in the FAR or the DOD or NASA FAR Supplement. 34 * Unpublished-- rights reserved under the copyright laws of the 35 * United States. Contractor/manufacturer is Silicon Graphics, 36 * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311. 37 * 38 * OpenGL(TM) is a trademark of Silicon Graphics, Inc. 39 */ 40/* 41 * pickdepth.c 42 * Picking is demonstrated in this program. In 43 * rendering mode, three overlapping rectangles are 44 * drawn. When the left mouse button is pressed, 45 * selection mode is entered with the picking matrix. 46 * Rectangles which are drawn under the cursor position 47 * are "picked." Pay special attention to the depth 48 * value range, which is returned. 49 */ 50#include <stdlib.h> 51#include <stdio.h> 52#include "glut_wrap.h" 53 54static void 55myinit(void) 56{ 57 glClearColor(0.0, 0.0, 0.0, 0.0); 58 glDepthFunc(GL_LESS); 59 glEnable(GL_DEPTH_TEST); 60 glShadeModel(GL_FLAT); 61 glDepthRange(0.0, 1.0); /* The default z mapping */ 62} 63 64/* The three rectangles are drawn. In selection mode, 65 * each rectangle is given the same name. Note that 66 * each rectangle is drawn with a different z value. 67 */ 68static void 69drawRects(GLenum mode) 70{ 71 if (mode == GL_SELECT) 72 glLoadName(1); 73 glBegin(GL_QUADS); 74 glColor3f(1.0, 1.0, 0.0); 75 glVertex3i(2, 0, 0); 76 glVertex3i(2, 6, 0); 77 glVertex3i(6, 6, 0); 78 glVertex3i(6, 0, 0); 79 glEnd(); 80 if (mode == GL_SELECT) 81 glLoadName(2); 82 glBegin(GL_QUADS); 83 glColor3f(0.0, 1.0, 1.0); 84 glVertex3i(3, 2, -1); 85 glVertex3i(3, 8, -1); 86 glVertex3i(8, 8, -1); 87 glVertex3i(8, 2, -1); 88 glEnd(); 89 if (mode == GL_SELECT) 90 glLoadName(3); 91 glBegin(GL_QUADS); 92 glColor3f(1.0, 0.0, 1.0); 93 glVertex3i(0, 2, -2); 94 glVertex3i(0, 7, -2); 95 glVertex3i(5, 7, -2); 96 glVertex3i(5, 2, -2); 97 glEnd(); 98} 99 100/* processHits() prints out the contents of the 101 * selection array. 102 */ 103static void 104processHits(GLint hits, GLuint buffer[]) 105{ 106 GLint i; 107 GLuint j, names, *ptr; 108 109 printf("hits = %d\n", hits); 110 ptr = (GLuint *) buffer; 111 for (i = 0; i < hits; i++) { /* for each hit */ 112 names = *ptr; 113 printf(" number of names for hit = %d\n", names); 114 ptr++; 115 printf(" z1 is %g;", (float) *ptr/0xffffffff); 116 ptr++; 117 printf(" z2 is %g\n", (float) *ptr/0xffffffff); 118 ptr++; 119 printf(" the name is "); 120 for (j = 0; j < names; j++) { /* for each name */ 121 printf("%d ", *ptr); 122 ptr++; 123 } 124 printf("\n"); 125 } 126} 127 128/* pickRects() sets up selection mode, name stack, 129 * and projection matrix for picking. Then the objects 130 * are drawn. 131 */ 132#define BUFSIZE 512 133 134static void 135pickRects(int button, int state, int x, int y) 136{ 137 GLuint selectBuf[BUFSIZE]; 138 GLint hits; 139 GLint viewport[4]; 140 141 if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN) 142 return; 143 144 glGetIntegerv(GL_VIEWPORT, viewport); 145 146 glSelectBuffer(BUFSIZE, selectBuf); 147 (void) glRenderMode(GL_SELECT); 148 149 glInitNames(); 150 glPushName(-1); 151 152 glMatrixMode(GL_PROJECTION); 153 glPushMatrix(); 154 glLoadIdentity(); 155/* create 5x5 pixel picking region near cursor location */ 156 gluPickMatrix((GLdouble) x, (GLdouble) (viewport[3] - y), 157 5.0, 5.0, viewport); 158 glOrtho(0.0, 8.0, 0.0, 8.0, -0.5, 2.5); 159 drawRects(GL_SELECT); 160 glPopMatrix(); 161 glFlush(); 162 163 hits = glRenderMode(GL_RENDER); 164 processHits(hits, selectBuf); 165} 166 167static void 168display(void) 169{ 170 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 171 drawRects(GL_RENDER); 172 glutSwapBuffers(); 173} 174 175static void 176myReshape(int w, int h) 177{ 178 glViewport(0, 0, w, h); 179 glMatrixMode(GL_PROJECTION); 180 glLoadIdentity(); 181 glOrtho(0.0, 8.0, 0.0, 8.0, -0.5, 2.5); 182 glMatrixMode(GL_MODELVIEW); 183 glLoadIdentity(); 184} 185 186static void 187key(unsigned char k, int x, int y) 188{ 189 switch (k) { 190 case 27: /* Escape */ 191 exit(0); 192 break; 193 default: 194 return; 195 } 196 glutPostRedisplay(); 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 */ 203int 204main(int argc, char **argv) 205{ 206 glutInitWindowSize(200, 200); 207 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); 208 glutInit(&argc, argv); 209 glutCreateWindow(argv[0]); 210 myinit(); 211 glutMouseFunc(pickRects); 212 glutReshapeFunc(myReshape); 213 glutDisplayFunc(display); 214 glutKeyboardFunc(key); 215 glutMainLoop(); 216 return 0; /* ANSI C requires main to return int. */ 217} 218