void SelectObject(int x, int y) { static unsigned int aSelectBuffer[SELECT_BUF_SIZE]; static unsigned int uiHits; static int aViewport[4]; glGetIntegerv(GL_VIEWPORT, aViewport); glSelectBuffer(SELECT_BUF_SIZE, aSelectBuffer); glRenderMode(GL_SELECT); glInitNames(); glPushName(0); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); // 5x5 Region gluPickMatrix((double)x, (double)(aViewport[3] - y), 5.0, 5.0, aViewport); // Same Clipping Window as in Reshape() GLfloat fAspect = (GLfloat)width / (GLfloat)height; gluPerspective(60.0f, fAspect, 1.0f, nRange * 2); gluLookAt(0.f, 0.f, -nRange, 0.f, 0.f, 0.f, 0, 1, 0); glMatrixMode(GL_MODELVIEW); Draw(GL_SELECT); glMatrixMode(GL_PROJECTION); glPopMatrix(); uiHits = glRenderMode(GL_RENDER); ProcessHits(uiHits, aSelectBuffer); glMatrixMode(GL_MODELVIEW); glutPostRedisplay(); }
/*----------------------------------------------------------------------------- * Mouse * Handles Mouse Movement and Selection *-----------------------------------------------------------------------------*/ void Mouse(int button, int state, int x, int y) { GLuint nameBuffer[BUF_SIZ]; GLint hits; GLint viewport[4]; if(levelComplete == TRUE) return; if((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN)) { /* Get the Viewport and set to Projection */ glGetIntegerv( GL_VIEWPORT, viewport ); glMatrixMode( GL_PROJECTION ); /* Save the Projection matrix */ glPushMatrix( ); glLoadIdentity( ); gluPickMatrix( (GLdouble)x, (GLdouble)(viewport[3] - y), N, N, viewport ); gluOrtho2D( xmin, xmax, ymin, ymax ); /* Set the Render Mode to the Selection mode */ glMatrixMode( GL_MODELVIEW ); glSelectBuffer(BUF_SIZ, nameBuffer); glRenderMode( GL_SELECT ); /* Initialize the Object Naming */ glInitNames( ); glPushName( 0 ); /* Draw the Shapes with Select enabled */ Draw( ); /* Reset the Projection Matrix */ glMatrixMode( GL_PROJECTION ); glPopMatrix( ); /* Return drawing to MODELVIEW */ glMatrixMode( GL_MODELVIEW ); /* Get the number of hits */ hits = glRenderMode( GL_RENDER ); ProcessHits( hits, nameBuffer ); /* Call a movement */ MouseMove( x, y ); /* Draw the display */ glutPostRedisplay( ); } else if((button == GLUT_LEFT_BUTTON) && (state == GLUT_UP)) { if(selectedNode != NULL) { TransformList* from = NULL; TransformList* to = NULL; /* Check if its in the dimension */ if(((selectedIndex / SELECTED) == 1) && (lastX > 100.0) && (lastY > -100)) { from = &tlSelectedTransforms; to = &tlAvailableTransforms; selectedList = &tlAvailableTransforms; } else if(((selectedIndex / AVAILABLE) == 1) && (lastY < -100) && (lastX < 100.0)) { from = &tlAvailableTransforms; to = &tlSelectedTransforms; selectedList = &tlSelectedTransforms; } if((from != NULL) && (to != NULL)) { /* Swap the nodes */ RemoveNode(from, selectedNode); AppendNode(to, selectedNode); } /* Reset the mouse display */ selectedIndex = 0; /* Set the new position */ glutPostRedisplay( ); } } }