/* * Copyright (c) 1993-1997, Silicon Graphics, Inc. * ALL RIGHTS RESERVED * Permission to use, copy, modify, and distribute this software for * any purpose and without fee is hereby granted, provided that the above * copyright notice appear in all copies and that both the copyright notice * and this permission notice appear in supporting documentation, and that * the name of Silicon Graphics, Inc. not be used in advertising * or publicity pertaining to distribution of the software without specific, * written prior permission. * * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT, * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION, * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE. * * US Government Users Restricted Rights * Use, duplication, or disclosure by the Government is subject to * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph * (c)(1)(ii) of the Rights in Technical Data and Computer Software * clause at DFARS 252.227-7013 and/or in similar or successor * clauses in the FAR or the DOD or NASA FAR Supplement. * Unpublished-- rights reserved under the copyright laws of the * United States. Contractor/manufacturer is Silicon Graphics, * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311. * * OpenGL(R) is a registered trademark of Silicon Graphics, Inc. */ /* * select.c * This is an illustration of the selection mode and * name stack, which detects whether objects collide * with the cursor. First, 12 spheres are drawn * (drawScene routine). When the mouse is clicked, the * selection mode is entered (selectObjects routine). * Drawing to the screen ceases. To see if any collisions * occur, the 12 spheres are called. */ #include #include #include int xchanged = 0; int ychanged = 0; int zchanged = 0; int xrotate = 0; int yrotate = 0; int zrotate = 0; float xtranslate = 0; float ytranslate = 0; float ztranslate = 0; GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 }; GLfloat mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 }; GLfloat mat_ambient_color[] = { 0.8, 0.8, 0.2, 1.0 }; GLfloat mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 }; GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat no_shininess[] = { 0.0 }; GLfloat low_shininess[] = { 5.0 }; GLfloat high_shininess[] = { 100.0 }; GLfloat mat_emission[] = {0.3, 0.2, 0.2, 0.0}; void drawSpheres(GLenum mode); void drawScene (GLenum mode) { glPushMatrix(); gluLookAt(0,0,15,0,0,0,0,1,0); glTranslated(xtranslate, ytranslate, ztranslate); glRotated(yrotate,0,1,0); glRotated(zrotate,0,0,1); glRotated(xrotate,1,0,0); drawSpheres(mode); glFlush(); glPopMatrix(); } /* processHits prints out the contents of the selection array */ void processHits (GLint hits, GLuint buffer[]) { int i, j, names; GLuint *ptr; printf ("hits = %d\n", hits); ptr = (GLuint *) buffer; for (i = 0; i < hits; i++) { /* for each hit */ names = (int) *ptr; printf (" number of names for hit = %d\n", names); ptr++; printf(" z1 is %g;", (float) *ptr/0x7fffffff); ptr++; printf(" z2 is %g\n", (float) *ptr/0x7fffffff); ptr++; for (j = 0; j < names; j++, ptr++) { /* for each name */ printf (" hit %d's name is: %d\n", i, *ptr); } } } /* selectObjects "draws" the spheres in selection mode, * assigning names for the spheres. */ #define BUFSIZE 512 void selectObjects(int button, int state, int x, int y) { GLuint selectBuf[BUFSIZE]; GLint hits; GLint viewport[4]; if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN) return; glGetIntegerv (GL_VIEWPORT, viewport); glSelectBuffer (BUFSIZE, selectBuf); (void) glRenderMode (GL_SELECT); glInitNames(); glPushName(0); glMatrixMode (GL_PROJECTION); glPushMatrix (); glLoadIdentity (); /* create 5x5 pixel picking region near cursor location */ gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y), 5.0, 5.0, viewport); gluPerspective(60,1,1,1000); drawScene(GL_SELECT); glMatrixMode (GL_PROJECTION); glPopMatrix (); glFlush (); glPopName(); hits = glRenderMode (GL_RENDER); processHits (hits, selectBuf); } void display(void) { GLint mode = GL_RENDER; glGetIntegerv(GL_RENDER_MODE, &mode); glMatrixMode (GL_MODELVIEW); glLoadIdentity (); glClearColor (0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); drawScene (GL_RENDER); glFlush(); glutSwapBuffers(); } void reshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluPerspective(60,1,1,1000); } void init (void) { GLfloat ambient[] = { 0.0, 0.0, 0.0, 1.0 }; GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat position[] = { 0.0, 3.0, 2.0, 0.0 }; GLfloat lmodel_ambient[] = { 0.4, 0.4, 0.4, 1.0 }; GLfloat local_view[] = { 0.0 }; glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); glLightfv(GL_LIGHT0, GL_POSITION, position); glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); } /*************************************************************************/ /* template for handling key events for special (non-ascii) keys. */ void specialKey( int key, int x, int y ) { switch (key) { case GLUT_KEY_UP: xrotate -= 1; xrotate %= 360; //printf("x: xrotate = %d\n", xrotate); xchanged = 1; ychanged = 0; zchanged = 0; break; case GLUT_KEY_DOWN: xrotate += 1; xrotate %= 360; //printf("x: xrotate = %d\n", xrotate); xchanged = 1; ychanged = 0; zchanged = 0; break; case GLUT_KEY_LEFT: yrotate -= 1; yrotate %= 360; //printf("y: yrotate = %d\n", yrotate); xchanged = 0; ychanged = 1; zchanged = 0; break; case GLUT_KEY_RIGHT: yrotate += 1; yrotate %= 360; //printf("y: yrotate = %d\n", yrotate); xchanged = 0; ychanged = 1; zchanged = 0; break; } glutPostRedisplay(); } /* ARGSUSED1 */ void keyboard(unsigned char key, int x, int y) { switch (key) { case 27: /* escape key*/ exit(0); break; case 'r': // reset xtranslate = 0; ytranslate = 0; ztranslate = 0; xrotate = 0; yrotate = 0; zrotate = 0; break; case '-': zrotate += 1; zrotate %= 360; //printf("z: zrotate = %d\n", zrotate); xchanged = 0; ychanged = 0; zchanged = 1; break; case '=': zrotate -= 1; zrotate %= 360; //printf("z: zrotate = %d\n", zrotate); xchanged = 0; ychanged = 0; zchanged = 1; break; case 'x': xtranslate -= 1.0; //printf("x: xtranslate = %f\n", xtranslate); break; case 'y': ytranslate -= 1.0; //printf("y: ytranslate = %f\n", ytranslate); break; case 'z': ztranslate -= 1.0; //printf("z: ztranslate = %f\n", ztranslate); break; case 'X': xtranslate += 1.0; //printf("X: xtranslate = %f\n", xtranslate); break; case 'Y': ytranslate += 1.0; //printf("Y: ytranslate = %f\n", ytranslate); break; case 'Z': ztranslate += 1.0; //printf("Z: ztranslate = %f\n", ztranslate); break; } glutPostRedisplay(); } /* Main Loop */ int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize (200, 200); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); glutMouseFunc(selectObjects); glutReshapeFunc(reshape); init(); glutDisplayFunc(display); glutKeyboardFunc(keyboard); glutSpecialFunc(specialKey); glutMainLoop(); return 0; } void drawSpheres(GLenum mode) { /* draw sphere in first row, first column * diffuse reflection only; no ambient or specular */ if(mode == GL_SELECT) glLoadName(1); glPushMatrix(); glTranslatef (-3.75, 3.0, 0.0); glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat); glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess); glMaterialfv(GL_FRONT, GL_EMISSION, no_mat); glutSolidSphere(1.0, 16, 16); glPopMatrix(); /* draw sphere in first row, second column * diffuse and specular reflection; low shininess; no ambient */ if(mode == GL_SELECT) glLoadName(2); glPushMatrix(); glTranslatef (-1.25, 3.0, 0.0); glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess); glMaterialfv(GL_FRONT, GL_EMISSION, no_mat); glutSolidSphere(1.0, 16, 16); glPopMatrix(); /* draw sphere in first row, third column * diffuse and specular reflection; high shininess; no ambient */ if(mode == GL_SELECT) glLoadName(3); glPushMatrix(); glTranslatef (1.25, 3.0, 0.0); glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess); glMaterialfv(GL_FRONT, GL_EMISSION, no_mat); glutSolidSphere(1.0, 16, 16); glPopMatrix(); /* draw sphere in first row, fourth column * diffuse reflection; emission; no ambient or specular reflection */ if(mode == GL_SELECT) glLoadName(4); glPushMatrix(); glTranslatef (3.75, 3.0, 0.0); glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat); glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess); glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission); glutSolidSphere(1.0, 16, 16); glPopMatrix(); /* draw sphere in second row, first column * ambient and diffuse reflection; no specular */ if(mode == GL_SELECT) glLoadName(5); glPushMatrix(); glTranslatef (-3.75, 0.0, 0.0); glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat); glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess); glMaterialfv(GL_FRONT, GL_EMISSION, no_mat); glutSolidSphere(1.0, 16, 16); glPopMatrix(); /* draw sphere in second row, second column * ambient, diffuse and specular reflection; low shininess */ if(mode == GL_SELECT) glLoadName(6); glPushMatrix(); glTranslatef (-1.25, 0.0, 0.0); glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess); glMaterialfv(GL_FRONT, GL_EMISSION, no_mat); glutSolidSphere(1.0, 16, 16); glPopMatrix(); /* draw sphere in second row, third column * ambient, diffuse and specular reflection; high shininess */ if(mode == GL_SELECT) glLoadName(7); glPushMatrix(); glTranslatef (1.25, 0.0, 0.0); glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess); glMaterialfv(GL_FRONT, GL_EMISSION, no_mat); glutSolidSphere(1.0, 16, 16); glPopMatrix(); /* draw sphere in second row, fourth column * ambient and diffuse reflection; emission; no specular */ if(mode == GL_SELECT) glLoadName(8); glPushMatrix(); glTranslatef (3.75, 0.0, 0.0); glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat); glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess); glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission); glutSolidSphere(1.0, 16, 16); glPopMatrix(); /* draw sphere in third row, first column * colored ambient and diffuse reflection; no specular */ if(mode == GL_SELECT) glLoadName(9); glPushMatrix(); glTranslatef (-3.75, -3.0, 0.0); glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat); glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess); glMaterialfv(GL_FRONT, GL_EMISSION, no_mat); glutSolidSphere(1.0, 16, 16); glPopMatrix(); /* draw sphere in third row, second column * colored ambient, diffuse and specular reflection; low shininess */ if(mode == GL_SELECT) glLoadName(10); glPushMatrix(); glTranslatef (-1.25, -3.0, 0.0); glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess); glMaterialfv(GL_FRONT, GL_EMISSION, no_mat); glutSolidSphere(1.0, 16, 16); glPopMatrix(); /* draw sphere in third row, third column * colored ambient, diffuse and specular reflection; high shininess */ if(mode == GL_SELECT) glLoadName(11); glPushMatrix(); glTranslatef (1.25, -3.0, 0.0); glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess); glMaterialfv(GL_FRONT, GL_EMISSION, no_mat); glutSolidSphere(1.0, 16, 16); glPopMatrix(); /* draw sphere in third row, fourth column * colored ambient and diffuse reflection; emission; no specular */ if(mode == GL_SELECT) glLoadName(12); glPushMatrix(); glTranslatef (3.75, -3.0, 0.0); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient_color); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, no_mat); glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, no_shininess); glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_emission); glutSolidSphere(1.0, 16, 16); glPopMatrix(); }