132001f49Smrg/* 232001f49Smrg * Copyright (c) 1993-1997, Silicon Graphics, Inc. 332001f49Smrg * ALL RIGHTS RESERVED 432001f49Smrg * Permission to use, copy, modify, and distribute this software for 532001f49Smrg * any purpose and without fee is hereby granted, provided that the above 632001f49Smrg * copyright notice appear in all copies and that both the copyright notice 732001f49Smrg * and this permission notice appear in supporting documentation, and that 832001f49Smrg * the name of Silicon Graphics, Inc. not be used in advertising 932001f49Smrg * or publicity pertaining to distribution of the software without specific, 1032001f49Smrg * written prior permission. 1132001f49Smrg * 1232001f49Smrg * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" 1332001f49Smrg * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, 1432001f49Smrg * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR 1532001f49Smrg * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON 1632001f49Smrg * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT, 1732001f49Smrg * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY 1832001f49Smrg * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION, 1932001f49Smrg * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF 2032001f49Smrg * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN 2132001f49Smrg * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON 2232001f49Smrg * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE 2332001f49Smrg * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE. 2432001f49Smrg * 2532001f49Smrg * US Government Users Restricted Rights 2632001f49Smrg * Use, duplication, or disclosure by the Government is subject to 2732001f49Smrg * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph 2832001f49Smrg * (c)(1)(ii) of the Rights in Technical Data and Computer Software 2932001f49Smrg * clause at DFARS 252.227-7013 and/or in similar or successor 3032001f49Smrg * clauses in the FAR or the DOD or NASA FAR Supplement. 3132001f49Smrg * Unpublished-- rights reserved under the copyright laws of the 3232001f49Smrg * United States. Contractor/manufacturer is Silicon Graphics, 3332001f49Smrg * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311. 3432001f49Smrg * 3532001f49Smrg * OpenGL(R) is a registered trademark of Silicon Graphics, Inc. 3632001f49Smrg */ 3732001f49Smrg 3832001f49Smrg/* 3932001f49Smrg * picksquare.c 4032001f49Smrg * Use of multiple names and picking are demonstrated. 4132001f49Smrg * A 3x3 grid of squares is drawn. When the left mouse 4232001f49Smrg * button is pressed, all squares under the cursor position 4332001f49Smrg * have their color changed. 4432001f49Smrg */ 4532001f49Smrg#include <stdlib.h> 4632001f49Smrg#include <stdio.h> 4732001f49Smrg#include "glut_wrap.h" 4832001f49Smrg 4932001f49Smrgint board[3][3]; /* amount of color for each square */ 5032001f49Smrg 5132001f49Smrg/* Clear color value for every square on the board */ 5232001f49Smrgstatic void init(void) 5332001f49Smrg{ 5432001f49Smrg int i, j; 5532001f49Smrg for (i = 0; i < 3; i++) 5632001f49Smrg for (j = 0; j < 3; j ++) 5732001f49Smrg board[i][j] = 0; 5832001f49Smrg glClearColor (0.0, 0.0, 0.0, 0.0); 5932001f49Smrg} 6032001f49Smrg 6132001f49Smrg/* The nine squares are drawn. In selection mode, each 6232001f49Smrg * square is given two names: one for the row and the 6332001f49Smrg * other for the column on the grid. The color of each 6432001f49Smrg * square is determined by its position on the grid, and 6532001f49Smrg * the value in the board[][] array. 6632001f49Smrg */ 6732001f49Smrgstatic void drawSquares(GLenum mode) 6832001f49Smrg{ 6932001f49Smrg GLuint i, j; 7032001f49Smrg for (i = 0; i < 3; i++) { 7132001f49Smrg if (mode == GL_SELECT) 7232001f49Smrg glLoadName (i); 7332001f49Smrg for (j = 0; j < 3; j ++) { 7432001f49Smrg if (mode == GL_SELECT) 7532001f49Smrg glPushName (j); 7632001f49Smrg glColor3f ((GLfloat) i/3.0, (GLfloat) j/3.0, 7732001f49Smrg (GLfloat) board[i][j]/3.0); 7832001f49Smrg glRecti (i, j, i+1, j+1); 7932001f49Smrg if (mode == GL_SELECT) 8032001f49Smrg glPopName (); 8132001f49Smrg } 8232001f49Smrg } 8332001f49Smrg} 8432001f49Smrg 8532001f49Smrg/* processHits prints out the contents of the 8632001f49Smrg * selection array. 8732001f49Smrg */ 8832001f49Smrgstatic void processHits (GLint hits, GLuint buffer[]) 8932001f49Smrg{ 9032001f49Smrg GLint i; 9132001f49Smrg GLuint j, ii = 0, jj = 0, names, *ptr; 9232001f49Smrg 9332001f49Smrg printf ("hits = %d\n", hits); 9432001f49Smrg ptr = (GLuint *) buffer; 9532001f49Smrg for (i = 0; i < hits; i++) { /* for each hit */ 9632001f49Smrg names = *ptr; 9732001f49Smrg printf (" number of names for this hit = %d\n", names); ptr++; 9832001f49Smrg printf(" z1 is %g;", (float) *ptr/0x7fffffff); ptr++; 9932001f49Smrg printf(" z2 is %g\n", (float) *ptr/0x7fffffff); ptr++; 10032001f49Smrg printf (" names are "); 10132001f49Smrg for (j = 0; j < names; j++) { /* for each name */ 10232001f49Smrg printf ("%d ", *ptr); 10332001f49Smrg if (j == 0) /* set row and column */ 10432001f49Smrg ii = *ptr; 10532001f49Smrg else if (j == 1) 10632001f49Smrg jj = *ptr; 10732001f49Smrg ptr++; 10832001f49Smrg } 10932001f49Smrg printf ("\n"); 11032001f49Smrg board[ii][jj] = (board[ii][jj] + 1) % 3; 11132001f49Smrg } 11232001f49Smrg} 11332001f49Smrg 11432001f49Smrg/* pickSquares() sets up selection mode, name stack, 11532001f49Smrg * and projection matrix for picking. Then the 11632001f49Smrg * objects are drawn. 11732001f49Smrg */ 11832001f49Smrg#define BUFSIZE 512 11932001f49Smrg 12032001f49Smrgstatic void pickSquares(int button, int state, int x, int y) 12132001f49Smrg{ 12232001f49Smrg GLuint selectBuf[BUFSIZE]; 12332001f49Smrg GLint hits; 12432001f49Smrg GLint viewport[4]; 12532001f49Smrg 12632001f49Smrg if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN) 12732001f49Smrg return; 12832001f49Smrg 12932001f49Smrg glGetIntegerv (GL_VIEWPORT, viewport); 13032001f49Smrg 13132001f49Smrg glSelectBuffer (BUFSIZE, selectBuf); 13232001f49Smrg (void) glRenderMode (GL_SELECT); 13332001f49Smrg 13432001f49Smrg glInitNames(); 13532001f49Smrg glPushName(0); 13632001f49Smrg 13732001f49Smrg glMatrixMode (GL_PROJECTION); 13832001f49Smrg glPushMatrix (); 13932001f49Smrg glLoadIdentity (); 14032001f49Smrg/* create 5x5 pixel picking region near cursor location */ 14132001f49Smrg gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y), 14232001f49Smrg 5.0, 5.0, viewport); 14332001f49Smrg gluOrtho2D (0.0, 3.0, 0.0, 3.0); 14432001f49Smrg drawSquares (GL_SELECT); 14532001f49Smrg 14632001f49Smrg glMatrixMode (GL_PROJECTION); 14732001f49Smrg glPopMatrix (); 14832001f49Smrg glFlush (); 14932001f49Smrg 15032001f49Smrg hits = glRenderMode (GL_RENDER); 15132001f49Smrg processHits (hits, selectBuf); 15232001f49Smrg glutPostRedisplay(); 15332001f49Smrg} 15432001f49Smrg 15532001f49Smrgstatic void display(void) 15632001f49Smrg{ 15732001f49Smrg glClear(GL_COLOR_BUFFER_BIT); 15832001f49Smrg drawSquares (GL_RENDER); 15932001f49Smrg glFlush(); 16032001f49Smrg} 16132001f49Smrg 16232001f49Smrgstatic void reshape(int w, int h) 16332001f49Smrg{ 16432001f49Smrg glViewport(0, 0, w, h); 16532001f49Smrg glMatrixMode(GL_PROJECTION); 16632001f49Smrg glLoadIdentity(); 16732001f49Smrg gluOrtho2D (0.0, 3.0, 0.0, 3.0); 16832001f49Smrg glMatrixMode(GL_MODELVIEW); 16932001f49Smrg glLoadIdentity(); 17032001f49Smrg} 17132001f49Smrg 17232001f49Smrg/* ARGSUSED1 */ 17332001f49Smrgstatic void keyboard(unsigned char key, int x, int y) 17432001f49Smrg{ 17532001f49Smrg switch (key) { 17632001f49Smrg case 27: 17732001f49Smrg exit(0); 17832001f49Smrg break; 17932001f49Smrg } 18032001f49Smrg} 18132001f49Smrg 18232001f49Smrg/* Main Loop */ 18332001f49Smrgint main(int argc, char** argv) 18432001f49Smrg{ 18532001f49Smrg glutInit(&argc, argv); 18632001f49Smrg glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); 18732001f49Smrg glutInitWindowSize (100, 100); 18832001f49Smrg glutInitWindowPosition (100, 100); 18932001f49Smrg glutCreateWindow (argv[0]); 19032001f49Smrg init (); 19132001f49Smrg glutReshapeFunc (reshape); 19232001f49Smrg glutDisplayFunc(display); 19332001f49Smrg glutMouseFunc (pickSquares); 19432001f49Smrg glutKeyboardFunc (keyboard); 19532001f49Smrg glutMainLoop(); 19632001f49Smrg return 0; 19732001f49Smrg} 198