1/*
2 * Copyright (c) 1993-1997, Silicon Graphics, Inc.
3 * ALL RIGHTS RESERVED
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.
11 *
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.
24 *
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.
34 *
35 * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
36 */
37
38/*
39 * picksquare.c
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.
44 */
45#include <stdlib.h>
46#include <stdio.h>
47#include "glut_wrap.h"
48
49int board[3][3];   /*  amount of color for each square	*/
50
51/*  Clear color value for every square on the board   */
52static void init(void)
53{
54   int i, j;
55   for (i = 0; i < 3; i++)
56      for (j = 0; j < 3; j ++)
57         board[i][j] = 0;
58   glClearColor (0.0, 0.0, 0.0, 0.0);
59}
60
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.
66 */
67static void drawSquares(GLenum mode)
68{
69   GLuint i, j;
70   for (i = 0; i < 3; i++) {
71      if (mode == GL_SELECT)
72         glLoadName (i);
73      for (j = 0; j < 3; j ++) {
74         if (mode == GL_SELECT)
75            glPushName (j);
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)
80            glPopName ();
81      }
82   }
83}
84
85/*  processHits prints out the contents of the
86 *  selection array.
87 */
88static void processHits (GLint hits, GLuint buffer[])
89{
90   GLint i;
91   GLuint j, ii = 0, jj = 0, names, *ptr;
92
93   printf ("hits = %d\n", hits);
94   ptr = (GLuint *) buffer;
95   for (i = 0; i < hits; i++) {	/*  for each hit  */
96      names = *ptr;
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  */
104            ii = *ptr;
105         else if (j == 1)
106            jj = *ptr;
107         ptr++;
108      }
109      printf ("\n");
110      board[ii][jj] = (board[ii][jj] + 1) % 3;
111   }
112}
113
114/*  pickSquares() sets up selection mode, name stack,
115 *  and projection matrix for picking.  Then the
116 *  objects are drawn.
117 */
118#define BUFSIZE 512
119
120static void pickSquares(int button, int state, int x, int y)
121{
122   GLuint selectBuf[BUFSIZE];
123   GLint hits;
124   GLint viewport[4];
125
126   if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN)
127      return;
128
129   glGetIntegerv (GL_VIEWPORT, viewport);
130
131   glSelectBuffer (BUFSIZE, selectBuf);
132   (void) glRenderMode (GL_SELECT);
133
134   glInitNames();
135   glPushName(0);
136
137   glMatrixMode (GL_PROJECTION);
138   glPushMatrix ();
139   glLoadIdentity ();
140/*  create 5x5 pixel picking region near cursor location	*/
141   gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y),
142                  5.0, 5.0, viewport);
143   gluOrtho2D (0.0, 3.0, 0.0, 3.0);
144   drawSquares (GL_SELECT);
145
146   glMatrixMode (GL_PROJECTION);
147   glPopMatrix ();
148   glFlush ();
149
150   hits = glRenderMode (GL_RENDER);
151   processHits (hits, selectBuf);
152   glutPostRedisplay();
153}
154
155static void display(void)
156{
157   glClear(GL_COLOR_BUFFER_BIT);
158   drawSquares (GL_RENDER);
159   glFlush();
160}
161
162static void reshape(int w, int h)
163{
164   glViewport(0, 0, w, h);
165   glMatrixMode(GL_PROJECTION);
166   glLoadIdentity();
167   gluOrtho2D (0.0, 3.0, 0.0, 3.0);
168   glMatrixMode(GL_MODELVIEW);
169   glLoadIdentity();
170}
171
172/* ARGSUSED1 */
173static void keyboard(unsigned char key, int x, int y)
174{
175   switch (key) {
176      case 27:
177         exit(0);
178         break;
179   }
180}
181
182/* Main Loop */
183int main(int argc, char** argv)
184{
185   glutInit(&argc, argv);
186   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
187   glutInitWindowSize (100, 100);
188   glutInitWindowPosition (100, 100);
189   glutCreateWindow (argv[0]);
190   init ();
191   glutReshapeFunc (reshape);
192   glutDisplayFunc(display);
193   glutMouseFunc (pickSquares);
194   glutKeyboardFunc (keyboard);
195   glutMainLoop();
196   return 0;
197}
198