2 * Copyright (c) 1993-1997, Silicon Graphics, Inc.
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.
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.
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.
35 * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
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.
53 #include "glut_wrap.h"
57 /* draw a triangle with vertices at (x1, y1), (x2, y2)
58 * and (x3, y3) at z units away from the origin.
60 static void drawTriangle (GLfloat x1
, GLfloat y1
, GLfloat x2
,
61 GLfloat y2
, GLfloat x3
, GLfloat y3
, GLfloat z
)
63 glBegin (GL_TRIANGLES
);
64 glVertex3f (x1
, y1
, z
);
65 glVertex3f (x2
, y2
, z
);
66 glVertex3f (x3
, y3
, z
);
70 /* draw a rectangular box with these outer x, y, and z values */
71 static void drawViewVolume (GLfloat x1
, GLfloat x2
, GLfloat y1
,
72 GLfloat y2
, GLfloat z1
, GLfloat z2
)
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
);
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
);
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
);
101 /* drawScene draws 4 triangles and a wire frame
102 * which represents the viewing volume.
104 static void drawScene (void)
106 glMatrixMode (GL_PROJECTION
);
108 gluPerspective (40.0, 4.0/3.0, 1.0, 100.0);
110 glMatrixMode (GL_MODELVIEW
);
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);
123 /* processHits prints out the contents of the selection array
125 static void processHits (GLint hits
, GLuint buffer
[])
128 GLuint j
, names
, *ptr
;
130 printf ("hits = %d\n", hits
);
131 ptr
= (GLuint
*) buffer
;
132 for (i
= 0; i
< hits
; i
++) { /* for each hit */
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
++;
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.
153 static void selectObjects(void)
155 GLuint selectBuf
[BUFSIZE
];
158 glSelectBuffer (BUFSIZE
, selectBuf
);
159 (void) glRenderMode (GL_SELECT
);
165 glMatrixMode (GL_PROJECTION
);
167 glOrtho (0.0, 5.0, 0.0, 5.0, 0.0, 10.0);
168 glMatrixMode (GL_MODELVIEW
);
171 drawTriangle (2.0, 2.0, 3.0, 2.0, 2.5, 3.0, -5.0);
173 drawTriangle (2.0, 7.0, 3.0, 7.0, 2.5, 8.0, -5.0);
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);
180 hits
= glRenderMode (GL_RENDER
);
181 processHits (hits
, selectBuf
);
184 static void init (void)
186 glEnable(GL_DEPTH_TEST
);
187 glShadeModel(GL_FLAT
);
190 static void display(void)
192 glClearColor (0.0, 0.0, 0.0, 0.0);
193 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
200 static void keyboard(unsigned char key
, int x
, int y
)
210 int main(int argc
, char** argv
)
212 glutInit(&argc
, argv
);
213 glutInitDisplayMode (GLUT_SINGLE
| GLUT_RGB
| GLUT_DEPTH
);
214 glutInitWindowSize (200, 200);
215 glutInitWindowPosition (100, 100);
216 glutCreateWindow (argv
[0]);
218 glutDisplayFunc(display
);
219 glutKeyboardFunc(keyboard
);