http://files.cnblogs.com/dragon2012/Opengl%E6%8B%BE%E5%8F%96.rar
贴上关键拾取函数代码
void WALL::pick(GLfloat xpos,GLfloat ypos){xpos,ypos;GLint viewport[4];GLdouble mvmatrix[16],projmatrix[16];float realy;GLdouble wx,wy,wz;glGetIntegerv(GL_VIEWPORT,viewport);glGetDoublev(GL_MODELVIEW_MATRIX,mvmatrix);glGetDoublev(GL_PROJECTION_MATRIX,projmatrix);realy =ypos;printf("%f %f \n",xpos,realy);gluUnProject((GLdouble)xpos,(GLdouble)realy,0.0,mvmatrix,projmatrix,viewport,&wx,&wy,&wz);float g_nearxyz[3]; g_nearxyz[0] = (GLfloat)wx;g_nearxyz[1] = (GLfloat)wy;g_nearxyz[2] = (GLfloat)wz;gluUnProject((GLdouble)xpos,(GLdouble)realy,1.0,mvmatrix,projmatrix,viewport,&wx,&wy,&wz);float g_farxyz[3]; g_farxyz[0] = (GLfloat)wx;g_farxyz[1] = (GLfloat)wy;g_farxyz[2] = (GLfloat)wz;float m_fangxiang[3];for(int i=0 ;i<3 ;i++){ m_fangxiang[i] =g_farxyz[i] -g_nearxyz[i];}float noml =1/sqrtf(m_fangxiang[0]*m_fangxiang[0]+m_fangxiang[1]*m_fangxiang[1]+m_fangxiang[2]*m_fangxiang[2]);for(int i=0 ;i<3 ;i++){ m_fangxiang[i]*=noml;} float distens =-g_nearxyz[1]/m_fangxiang[1];float onefangxiang[3];for(int i=0; i<3 ;i++){ onefangxiang[i] =distens*m_fangxiang[i]+g_nearxyz[i];}for(int i=0 ;i<400 ;i++){ pic[i].select =false; if(abs(onefangxiang[0]-pic[i].x)<4&&abs(onefangxiang[2]-pic[i].y)<4) { pic[i].select =true; }}//printf("%f %f %f \n",onefangxiang[0],onefangxiang[1],onefangxiang[2]);//printf("line:%f,%f ,%f --->%f ,%f,%f\n",g_nearxyz[0],g_nearxyz[1],g_nearxyz[2],g_farxyz[0],g_farxyz[1],g_farxyz[2]);}