132001f49Smrg 232001f49Smrg/* Copyright (c) Mark J. Kilgard, 1994. */ 332001f49Smrg 432001f49Smrg/* 532001f49Smrg * (c) Copyright 1993, Silicon Graphics, Inc. 632001f49Smrg * ALL RIGHTS RESERVED 732001f49Smrg * Permission to use, copy, modify, and distribute this software for 832001f49Smrg * any purpose and without fee is hereby granted, provided that the above 932001f49Smrg * copyright notice appear in all copies and that both the copyright notice 1032001f49Smrg * and this permission notice appear in supporting documentation, and that 1132001f49Smrg * the name of Silicon Graphics, Inc. not be used in advertising 1232001f49Smrg * or publicity pertaining to distribution of the software without specific, 1332001f49Smrg * written prior permission. 1432001f49Smrg * 1532001f49Smrg * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" 1632001f49Smrg * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, 1732001f49Smrg * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR 1832001f49Smrg * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON 1932001f49Smrg * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT, 2032001f49Smrg * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY 2132001f49Smrg * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION, 2232001f49Smrg * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF 2332001f49Smrg * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN 2432001f49Smrg * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON 2532001f49Smrg * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE 2632001f49Smrg * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE. 2732001f49Smrg * 2832001f49Smrg * US Government Users Restricted Rights 2932001f49Smrg * Use, duplication, or disclosure by the Government is subject to 3032001f49Smrg * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph 3132001f49Smrg * (c)(1)(ii) of the Rights in Technical Data and Computer Software 3232001f49Smrg * clause at DFARS 252.227-7013 and/or in similar or successor 3332001f49Smrg * clauses in the FAR or the DOD or NASA FAR Supplement. 3432001f49Smrg * Unpublished-- rights reserved under the copyright laws of the 3532001f49Smrg * United States. Contractor/manufacturer is Silicon Graphics, 3632001f49Smrg * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311. 3732001f49Smrg * 3832001f49Smrg * OpenGL(TM) is a trademark of Silicon Graphics, Inc. 3932001f49Smrg */ 4032001f49Smrg/* 4132001f49Smrg * pickdepth.c 4232001f49Smrg * Picking is demonstrated in this program. In 4332001f49Smrg * rendering mode, three overlapping rectangles are 4432001f49Smrg * drawn. When the left mouse button is pressed, 4532001f49Smrg * selection mode is entered with the picking matrix. 4632001f49Smrg * Rectangles which are drawn under the cursor position 4732001f49Smrg * are "picked." Pay special attention to the depth 4832001f49Smrg * value range, which is returned. 4932001f49Smrg */ 5032001f49Smrg#include <stdlib.h> 5132001f49Smrg#include <stdio.h> 5232001f49Smrg#include "glut_wrap.h" 5332001f49Smrg 5432001f49Smrgstatic void 5532001f49Smrgmyinit(void) 5632001f49Smrg{ 5732001f49Smrg glClearColor(0.0, 0.0, 0.0, 0.0); 5832001f49Smrg glDepthFunc(GL_LESS); 5932001f49Smrg glEnable(GL_DEPTH_TEST); 6032001f49Smrg glShadeModel(GL_FLAT); 6132001f49Smrg glDepthRange(0.0, 1.0); /* The default z mapping */ 6232001f49Smrg} 6332001f49Smrg 6432001f49Smrg/* The three rectangles are drawn. In selection mode, 6532001f49Smrg * each rectangle is given the same name. Note that 6632001f49Smrg * each rectangle is drawn with a different z value. 6732001f49Smrg */ 6832001f49Smrgstatic void 6932001f49SmrgdrawRects(GLenum mode) 7032001f49Smrg{ 7132001f49Smrg if (mode == GL_SELECT) 7232001f49Smrg glLoadName(1); 7332001f49Smrg glBegin(GL_QUADS); 7432001f49Smrg glColor3f(1.0, 1.0, 0.0); 7532001f49Smrg glVertex3i(2, 0, 0); 7632001f49Smrg glVertex3i(2, 6, 0); 7732001f49Smrg glVertex3i(6, 6, 0); 7832001f49Smrg glVertex3i(6, 0, 0); 7932001f49Smrg glEnd(); 8032001f49Smrg if (mode == GL_SELECT) 8132001f49Smrg glLoadName(2); 8232001f49Smrg glBegin(GL_QUADS); 8332001f49Smrg glColor3f(0.0, 1.0, 1.0); 8432001f49Smrg glVertex3i(3, 2, -1); 8532001f49Smrg glVertex3i(3, 8, -1); 8632001f49Smrg glVertex3i(8, 8, -1); 8732001f49Smrg glVertex3i(8, 2, -1); 8832001f49Smrg glEnd(); 8932001f49Smrg if (mode == GL_SELECT) 9032001f49Smrg glLoadName(3); 9132001f49Smrg glBegin(GL_QUADS); 9232001f49Smrg glColor3f(1.0, 0.0, 1.0); 9332001f49Smrg glVertex3i(0, 2, -2); 9432001f49Smrg glVertex3i(0, 7, -2); 9532001f49Smrg glVertex3i(5, 7, -2); 9632001f49Smrg glVertex3i(5, 2, -2); 9732001f49Smrg glEnd(); 9832001f49Smrg} 9932001f49Smrg 10032001f49Smrg/* processHits() prints out the contents of the 10132001f49Smrg * selection array. 10232001f49Smrg */ 10332001f49Smrgstatic void 10432001f49SmrgprocessHits(GLint hits, GLuint buffer[]) 10532001f49Smrg{ 10632001f49Smrg GLint i; 10732001f49Smrg GLuint j, names, *ptr; 10832001f49Smrg 10932001f49Smrg printf("hits = %d\n", hits); 11032001f49Smrg ptr = (GLuint *) buffer; 11132001f49Smrg for (i = 0; i < hits; i++) { /* for each hit */ 11232001f49Smrg names = *ptr; 11332001f49Smrg printf(" number of names for hit = %d\n", names); 11432001f49Smrg ptr++; 11532001f49Smrg printf(" z1 is %g;", (float) *ptr/0xffffffff); 11632001f49Smrg ptr++; 11732001f49Smrg printf(" z2 is %g\n", (float) *ptr/0xffffffff); 11832001f49Smrg ptr++; 11932001f49Smrg printf(" the name is "); 12032001f49Smrg for (j = 0; j < names; j++) { /* for each name */ 12132001f49Smrg printf("%d ", *ptr); 12232001f49Smrg ptr++; 12332001f49Smrg } 12432001f49Smrg printf("\n"); 12532001f49Smrg } 12632001f49Smrg} 12732001f49Smrg 12832001f49Smrg/* pickRects() sets up selection mode, name stack, 12932001f49Smrg * and projection matrix for picking. Then the objects 13032001f49Smrg * are drawn. 13132001f49Smrg */ 13232001f49Smrg#define BUFSIZE 512 13332001f49Smrg 13432001f49Smrgstatic void 13532001f49SmrgpickRects(int button, int state, int x, int y) 13632001f49Smrg{ 13732001f49Smrg GLuint selectBuf[BUFSIZE]; 13832001f49Smrg GLint hits; 13932001f49Smrg GLint viewport[4]; 14032001f49Smrg 14132001f49Smrg if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN) 14232001f49Smrg return; 14332001f49Smrg 14432001f49Smrg glGetIntegerv(GL_VIEWPORT, viewport); 14532001f49Smrg 14632001f49Smrg glSelectBuffer(BUFSIZE, selectBuf); 14732001f49Smrg (void) glRenderMode(GL_SELECT); 14832001f49Smrg 14932001f49Smrg glInitNames(); 15032001f49Smrg glPushName(-1); 15132001f49Smrg 15232001f49Smrg glMatrixMode(GL_PROJECTION); 15332001f49Smrg glPushMatrix(); 15432001f49Smrg glLoadIdentity(); 15532001f49Smrg/* create 5x5 pixel picking region near cursor location */ 15632001f49Smrg gluPickMatrix((GLdouble) x, (GLdouble) (viewport[3] - y), 15732001f49Smrg 5.0, 5.0, viewport); 15832001f49Smrg glOrtho(0.0, 8.0, 0.0, 8.0, -0.5, 2.5); 15932001f49Smrg drawRects(GL_SELECT); 16032001f49Smrg glPopMatrix(); 16132001f49Smrg glFlush(); 16232001f49Smrg 16332001f49Smrg hits = glRenderMode(GL_RENDER); 16432001f49Smrg processHits(hits, selectBuf); 16532001f49Smrg} 16632001f49Smrg 16732001f49Smrgstatic void 16832001f49Smrgdisplay(void) 16932001f49Smrg{ 17032001f49Smrg glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 17132001f49Smrg drawRects(GL_RENDER); 17232001f49Smrg glutSwapBuffers(); 17332001f49Smrg} 17432001f49Smrg 17532001f49Smrgstatic void 17632001f49SmrgmyReshape(int w, int h) 17732001f49Smrg{ 17832001f49Smrg glViewport(0, 0, w, h); 17932001f49Smrg glMatrixMode(GL_PROJECTION); 18032001f49Smrg glLoadIdentity(); 18132001f49Smrg glOrtho(0.0, 8.0, 0.0, 8.0, -0.5, 2.5); 18232001f49Smrg glMatrixMode(GL_MODELVIEW); 18332001f49Smrg glLoadIdentity(); 18432001f49Smrg} 18532001f49Smrg 18632001f49Smrgstatic void 18732001f49Smrgkey(unsigned char k, int x, int y) 18832001f49Smrg{ 18932001f49Smrg switch (k) { 19032001f49Smrg case 27: /* Escape */ 19132001f49Smrg exit(0); 19232001f49Smrg break; 19332001f49Smrg default: 19432001f49Smrg return; 19532001f49Smrg } 19632001f49Smrg glutPostRedisplay(); 19732001f49Smrg} 19832001f49Smrg 19932001f49Smrg/* Main Loop 20032001f49Smrg * Open window with initial window size, title bar, 20132001f49Smrg * RGBA display mode, depth buffer, and handle input events. 20232001f49Smrg */ 20332001f49Smrgint 20432001f49Smrgmain(int argc, char **argv) 20532001f49Smrg{ 20632001f49Smrg glutInitWindowSize(200, 200); 20732001f49Smrg glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); 20832001f49Smrg glutInit(&argc, argv); 20932001f49Smrg glutCreateWindow(argv[0]); 21032001f49Smrg myinit(); 21132001f49Smrg glutMouseFunc(pickRects); 21232001f49Smrg glutReshapeFunc(myReshape); 21332001f49Smrg glutDisplayFunc(display); 21432001f49Smrg glutKeyboardFunc(key); 21532001f49Smrg glutMainLoop(); 21632001f49Smrg return 0; /* ANSI C requires main to return int. */ 21732001f49Smrg} 218