2 /* Copyright (c) Mark J. Kilgard, 1994. */
5 * (c) Copyright 1993, Silicon Graphics, Inc.
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.
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.
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.
38 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
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.
57 glClearColor(0.0, 0.0, 0.0, 0.0);
59 glEnable(GL_DEPTH_TEST
);
60 glShadeModel(GL_FLAT
);
61 glDepthRange(0.0, 1.0); /* The default z mapping */
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.
69 drawRects(GLenum mode
)
71 if (mode
== GL_SELECT
)
74 glColor3f(1.0, 1.0, 0.0);
80 if (mode
== GL_SELECT
)
83 glColor3f(0.0, 1.0, 1.0);
89 if (mode
== GL_SELECT
)
92 glColor3f(1.0, 0.0, 1.0);
100 /* processHits() prints out the contents of the
104 processHits(GLint hits
, GLuint buffer
[])
107 GLuint j
, names
, *ptr
;
109 printf("hits = %d\n", hits
);
110 ptr
= (GLuint
*) buffer
;
111 for (i
= 0; i
< hits
; i
++) { /* for each hit */
113 printf(" number of names for hit = %d\n", names
);
115 printf(" z1 is %g;", (float) *ptr
/0xffffffff);
117 printf(" z2 is %g\n", (float) *ptr
/0xffffffff);
119 printf(" the name is ");
120 for (j
= 0; j
< names
; j
++) { /* for each name */
128 /* pickRects() sets up selection mode, name stack,
129 * and projection matrix for picking. Then the objects
135 pickRects(int button
, int state
, int x
, int y
)
137 GLuint selectBuf
[BUFSIZE
];
141 if (button
!= GLUT_LEFT_BUTTON
|| state
!= GLUT_DOWN
)
144 glGetIntegerv(GL_VIEWPORT
, viewport
);
146 glSelectBuffer(BUFSIZE
, selectBuf
);
147 (void) glRenderMode(GL_SELECT
);
152 glMatrixMode(GL_PROJECTION
);
155 /* create 5x5 pixel picking region near cursor location */
156 gluPickMatrix((GLdouble
) x
, (GLdouble
) (viewport
[3] - y
),
158 glOrtho(0.0, 8.0, 0.0, 8.0, -0.5, 2.5);
159 drawRects(GL_SELECT
);
163 hits
= glRenderMode(GL_RENDER
);
164 processHits(hits
, selectBuf
);
170 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
171 drawRects(GL_RENDER
);
176 myReshape(int w
, int h
)
178 glViewport(0, 0, w
, h
);
179 glMatrixMode(GL_PROJECTION
);
181 glOrtho(0.0, 8.0, 0.0, 8.0, -0.5, 2.5);
182 glMatrixMode(GL_MODELVIEW
);
187 key(unsigned char k
, int x
, int y
)
190 case 27: /* Escape */
200 * Open window with initial window size, title bar,
201 * RGBA display mode, depth buffer, and handle input events.
204 main(int argc
, char **argv
)
206 glutInitWindowSize(200, 200);
207 glutInitDisplayMode(GLUT_DOUBLE
| GLUT_RGB
| GLUT_DEPTH
);
208 glutInit(&argc
, argv
);
209 glutCreateWindow(argv
[0]);
211 glutMouseFunc(pickRects
);
212 glutReshapeFunc(myReshape
);
213 glutDisplayFunc(display
);
214 glutKeyboardFunc(key
);
216 return 0; /* ANSI C requires main to return int. */