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 * Use of multiple names and picking are demonstrated.
41 * A 3x3 grid of squares is drawn. When the left mouse
42 * button is pressed, all squares under the cursor position
43 * have their color changed.
49 int board
[3][3]; /* amount of color for each square */
51 /* Clear color value for every square on the board */
52 static void init(void)
55 for (i
= 0; i
< 3; i
++)
56 for (j
= 0; j
< 3; j
++)
58 glClearColor (0.0, 0.0, 0.0, 0.0);
61 /* The nine squares are drawn. In selection mode, each
62 * square is given two names: one for the row and the
63 * other for the column on the grid. The color of each
64 * square is determined by its position on the grid, and
65 * the value in the board[][] array.
67 static void drawSquares(GLenum mode
)
70 for (i
= 0; i
< 3; i
++) {
71 if (mode
== GL_SELECT
)
73 for (j
= 0; j
< 3; j
++) {
74 if (mode
== GL_SELECT
)
76 glColor3f ((GLfloat
) i
/3.0, (GLfloat
) j
/3.0,
77 (GLfloat
) board
[i
][j
]/3.0);
78 glRecti (i
, j
, i
+1, j
+1);
79 if (mode
== GL_SELECT
)
85 /* processHits prints out the contents of the
88 static void processHits (GLint hits
, GLuint buffer
[])
91 GLuint j
, ii
= 0, jj
= 0, names
, *ptr
;
93 printf ("hits = %d\n", hits
);
94 ptr
= (GLuint
*) buffer
;
95 for (i
= 0; i
< hits
; i
++) { /* for each hit */
97 printf (" number of names for this hit = %d\n", names
); ptr
++;
98 printf(" z1 is %g;", (float) *ptr
/0x7fffffff); ptr
++;
99 printf(" z2 is %g\n", (float) *ptr
/0x7fffffff); ptr
++;
100 printf (" names are ");
101 for (j
= 0; j
< names
; j
++) { /* for each name */
102 printf ("%d ", *ptr
);
103 if (j
== 0) /* set row and column */
110 board
[ii
][jj
] = (board
[ii
][jj
] + 1) % 3;
114 /* pickSquares() sets up selection mode, name stack,
115 * and projection matrix for picking. Then the
120 static void pickSquares(int button
, int state
, int x
, int y
)
122 GLuint selectBuf
[BUFSIZE
];
126 if (button
!= GLUT_LEFT_BUTTON
|| state
!= GLUT_DOWN
)
129 glGetIntegerv (GL_VIEWPORT
, viewport
);
131 glSelectBuffer (BUFSIZE
, selectBuf
);
132 (void) glRenderMode (GL_SELECT
);
137 glMatrixMode (GL_PROJECTION
);
140 /* create 5x5 pixel picking region near cursor location */
141 gluPickMatrix ((GLdouble
) x
, (GLdouble
) (viewport
[3] - y
),
143 gluOrtho2D (0.0, 3.0, 0.0, 3.0);
144 drawSquares (GL_SELECT
);
146 glMatrixMode (GL_PROJECTION
);
150 hits
= glRenderMode (GL_RENDER
);
151 processHits (hits
, selectBuf
);
155 static void display(void)
157 glClear(GL_COLOR_BUFFER_BIT
);
158 drawSquares (GL_RENDER
);
162 static void reshape(int w
, int h
)
164 glViewport(0, 0, w
, h
);
165 glMatrixMode(GL_PROJECTION
);
167 gluOrtho2D (0.0, 3.0, 0.0, 3.0);
168 glMatrixMode(GL_MODELVIEW
);
173 static void keyboard(unsigned char key
, int x
, int y
)
183 int main(int argc
, char** argv
)
185 glutInit(&argc
, argv
);
186 glutInitDisplayMode (GLUT_SINGLE
| GLUT_RGB
);
187 glutInitWindowSize (100, 100);
188 glutInitWindowPosition (100, 100);
189 glutCreateWindow (argv
[0]);
191 glutReshapeFunc (reshape
);
192 glutDisplayFunc(display
);
193 glutMouseFunc (pickSquares
);
194 glutKeyboardFunc (keyboard
);