void mousePressEvent(QMouseEvent *e)
{
	int xpos = e->x(), ypos = e->y(), bestIndex;
	float x, y, z, dist, bestDist;  // Projected point coordinates
	GLint vp[4];  // Viewport
	GLdouble mvmatrix[16], projmatrix[16];  // GL matrices
	gmVector3 cp; // The control point
	
	if (e->Button() == LeftButton) {
		// Get the viewport information,
		// returned as [x, y, width, height]
		glGetIntegerv(GL_VIEWPORT, vp);
		
		// Transform Qt window coordinates to GL viewport coordinates
		ypos = vp[3] - ypos - 1;
		
		// Get the modelview and projection matrices
		glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix);
		glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
		
		// Check the distance from each control point to the mouse click
		for (int i=0; i < controlPoints.size(); i++) {
			p = controlPoints[i];
			
			// Use the GL matrices to find where the control point is projected to
			// in the viewport (not exactly window coordinates)
			gluProject(p[0], p[1], p[2], mvmatrix, projmatrix, vp, &x, &y, &z);
			
			// Compute the distance from the control point to the mouse event
			// and see if it is the closest thus far
			dist = (x-xpos)*(x-xpos) + (y-ypos)*(y-ypos);
			if (i == 0 || dist < bestDist) {
				bestIndex = i;
				bestDist = dist;
			}
		}
		
		selectedPointIndex = bestIndex;
	}
}
